Panda3D
bioPtr.cxx
1 // Filename: bioPtr.cxx
2 // Created by: drose (15Oct02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "bioPtr.h"
16 
17 #ifdef HAVE_OPENSSL
18 
19 #include "urlSpec.h"
20 #include "config_downloader.h"
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: BioPtr::Constructor
24 // Access: Public
25 // Description: This flavor of the constructor automatically creates
26 // a socket BIO and feeds it the server and port name
27 // from the indicated URL. It doesn't call
28 // BIO_do_connect(), though.
29 ////////////////////////////////////////////////////////////////////
30 BioPtr::
31 BioPtr(const URLSpec &url) {
32  if (url.get_scheme() == "file") {
33  // We're just reading a disk file.
34  string filename = URLSpec::unquote(url.get_path());
35 #ifdef _WIN32
36  // On Windows, we have to munge the filename specially, because it's
37  // been URL-munged. It might begin with a leading slash as well as
38  // a drive letter. Clean up that nonsense.
39  if (!filename.empty()) {
40  if (filename[0] == '/' || filename[0] == '\\') {
41  Filename fname = Filename::from_os_specific(filename.substr(1));
42  if (fname.is_local()) {
43  // Put the slash back on.
44  fname = string("/") + fname.get_fullpath();
45  }
46  filename = fname.to_os_specific();
47  }
48  }
49 #endif // _WIN32
50  _server_name = "";
51  _port = 0;
52  _bio = BIO_new_file(filename.c_str(), "rb");
53 
54  } else {
55  // A normal network-based URL.
56  _server_name = url.get_server();
57  _port = url.get_port();
58  _bio = BIO_new_connect((char *)_server_name.c_str());
59  BIO_set_conn_int_port(_bio, &_port);
60  }
61 }
62 
63 ////////////////////////////////////////////////////////////////////
64 // Function: BioPtr::Destructor
65 // Access: Public
66 // Description:
67 ////////////////////////////////////////////////////////////////////
68 BioPtr::
69 ~BioPtr() {
70  if (_bio != (BIO *)NULL) {
71  if (downloader_cat.is_debug() && !_server_name.empty()) {
72  downloader_cat.debug()
73  << "Dropping connection to " << _server_name << ":" << _port << "\n";
74  }
75 
76  BIO_free_all(_bio);
77  _bio = (BIO *)NULL;
78  }
79 }
80 
81 #endif // HAVE_OPENSSL
A container for a URL, e.g.
Definition: urlSpec.h:29
string get_fullpath() const
Returns the entire filename: directory, basename, extension.
Definition: filename.I:398
string get_server() const
Returns the server name specified by the URL, if any.
Definition: urlSpec.I:198
int get_port() const
Returns the port number specified by the URL, or the default port if not specified.
Definition: urlSpec.cxx:84
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
string get_path() const
Returns the path specified by the URL, or "/" if no path is specified.
Definition: urlSpec.cxx:153
static string unquote(const string &source)
Reverses the operation of quote(): converts escaped characters of the form "%xx" to their ascii equiv...
Definition: urlSpec.cxx:743
bool is_local() const
Returns true if the filename is local, e.g.
Definition: filename.I:664
string get_scheme() const
Returns the scheme specified by the URL, or empty string if no scheme is specified.
Definition: urlSpec.cxx:70
string to_os_specific() const
Converts the filename from our generic Unix-like convention (forward slashes starting with the root a...
Definition: filename.cxx:1196
static Filename from_os_specific(const string &os_specific, Type type=T_general)
This named constructor returns a Panda-style filename (that is, using forward slashes, and no drive letter) based on the supplied filename string that describes a filename in the local system conventions (for instance, on Windows, it may use backslashes or begin with a drive letter and a colon).
Definition: filename.cxx:332