00001 // Filename: urlSpec.I 00002 // Created by: drose (24Sep02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: URLSpec::Constructor 00018 // Access: Published 00019 // Description: 00020 //////////////////////////////////////////////////////////////////// 00021 INLINE URLSpec:: 00022 URLSpec(const string &url, bool server_name_expected) { 00023 set_url(url, server_name_expected); 00024 } 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: URLSpec::Copy Constructor 00028 // Access: Published 00029 // Description: 00030 //////////////////////////////////////////////////////////////////// 00031 INLINE URLSpec:: 00032 URLSpec(const URLSpec ©) { 00033 (*this) = copy; 00034 } 00035 00036 //////////////////////////////////////////////////////////////////// 00037 // Function: URLSpec::Assignment Operator 00038 // Access: Published 00039 // Description: 00040 //////////////////////////////////////////////////////////////////// 00041 INLINE void URLSpec:: 00042 operator = (const string &url) { 00043 set_url(url); 00044 } 00045 00046 //////////////////////////////////////////////////////////////////// 00047 // Function: URLSpec::Operator == 00048 // Access: Published 00049 // Description: 00050 //////////////////////////////////////////////////////////////////// 00051 INLINE bool URLSpec:: 00052 operator == (const URLSpec &other) const { 00053 return _url == other._url; 00054 } 00055 00056 //////////////////////////////////////////////////////////////////// 00057 // Function: URLSpec::Operator != 00058 // Access: Published 00059 // Description: 00060 //////////////////////////////////////////////////////////////////// 00061 INLINE bool URLSpec:: 00062 operator != (const URLSpec &other) const { 00063 return !operator == (other); 00064 } 00065 00066 //////////////////////////////////////////////////////////////////// 00067 // Function: URLSpec::Operator < 00068 // Access: Published 00069 // Description: 00070 //////////////////////////////////////////////////////////////////// 00071 INLINE bool URLSpec:: 00072 operator < (const URLSpec &other) const { 00073 return _url < other._url; 00074 } 00075 00076 //////////////////////////////////////////////////////////////////// 00077 // Function: URLSpec::compare_to 00078 // Access: Published 00079 // Description: Returns a number less than zero if this URLSpec 00080 // sorts before the other one, greater than zero if it 00081 // sorts after, or zero if they are equivalent. 00082 //////////////////////////////////////////////////////////////////// 00083 INLINE int URLSpec:: 00084 compare_to(const URLSpec &other) const { 00085 return strcmp(_url.c_str(), other._url.c_str()); 00086 } 00087 00088 //////////////////////////////////////////////////////////////////// 00089 // Function: URLSpec::has_scheme 00090 // Access: Published 00091 // Description: Returns true if the URL specifies a scheme 00092 // (e.g. "http:"), false otherwise. 00093 //////////////////////////////////////////////////////////////////// 00094 INLINE bool URLSpec:: 00095 has_scheme() const { 00096 return (_flags & F_has_scheme) != 0; 00097 } 00098 00099 //////////////////////////////////////////////////////////////////// 00100 // Function: URLSpec::has_authority 00101 // Access: Published 00102 // Description: Returns true if the URL specifies an authority 00103 // (this includes username, server, and/or port), false 00104 // otherwise. 00105 //////////////////////////////////////////////////////////////////// 00106 INLINE bool URLSpec:: 00107 has_authority() const { 00108 return (_flags & F_has_authority) != 0; 00109 } 00110 00111 //////////////////////////////////////////////////////////////////// 00112 // Function: URLSpec::has_username 00113 // Access: Published 00114 // Description: Returns true if the URL specifies a username 00115 // (and/or password), false otherwise. 00116 //////////////////////////////////////////////////////////////////// 00117 INLINE bool URLSpec:: 00118 has_username() const { 00119 return (_flags & F_has_username) != 0; 00120 } 00121 00122 //////////////////////////////////////////////////////////////////// 00123 // Function: URLSpec::has_server 00124 // Access: Published 00125 // Description: Returns true if the URL specifies a server name, 00126 // false otherwise. 00127 //////////////////////////////////////////////////////////////////// 00128 INLINE bool URLSpec:: 00129 has_server() const { 00130 return (_flags & F_has_server) != 0; 00131 } 00132 00133 //////////////////////////////////////////////////////////////////// 00134 // Function: URLSpec::has_port 00135 // Access: Published 00136 // Description: Returns true if the URL specifies a port number, 00137 // false otherwise. 00138 //////////////////////////////////////////////////////////////////// 00139 INLINE bool URLSpec:: 00140 has_port() const { 00141 return (_flags & F_has_port) != 0; 00142 } 00143 00144 //////////////////////////////////////////////////////////////////// 00145 // Function: URLSpec::has_path 00146 // Access: Published 00147 // Description: Returns true if the URL includes a path specification 00148 // (that is, the particular filename on the server to 00149 // retrieve), false otherwise. 00150 //////////////////////////////////////////////////////////////////// 00151 INLINE bool URLSpec:: 00152 has_path() const { 00153 return (_flags & F_has_path) != 0; 00154 } 00155 00156 //////////////////////////////////////////////////////////////////// 00157 // Function: URLSpec::has_query 00158 // Access: Published 00159 // Description: Returns true if the URL includes a query 00160 // specification, false otherwise. 00161 //////////////////////////////////////////////////////////////////// 00162 INLINE bool URLSpec:: 00163 has_query() const { 00164 return (_flags & F_has_query) != 0; 00165 } 00166 00167 //////////////////////////////////////////////////////////////////// 00168 // Function: URLSpec::get_authority 00169 // Access: Published 00170 // Description: Returns the authority specified by the URL (this 00171 // includes username, server, and/or port), or empty 00172 // string if no authority is specified. 00173 //////////////////////////////////////////////////////////////////// 00174 INLINE string URLSpec:: 00175 get_authority() const { 00176 return _url.substr(_username_start, _port_end - _username_start); 00177 } 00178 00179 //////////////////////////////////////////////////////////////////// 00180 // Function: URLSpec::get_username 00181 // Access: Published 00182 // Description: Returns the username specified by the URL, if any. 00183 // This might also include a password, 00184 // e.g. "username:password", although putting a password 00185 // on the URL is probably a bad idea. 00186 //////////////////////////////////////////////////////////////////// 00187 INLINE string URLSpec:: 00188 get_username() const { 00189 return _url.substr(_username_start, _username_end - _username_start); 00190 } 00191 00192 //////////////////////////////////////////////////////////////////// 00193 // Function: URLSpec::get_server 00194 // Access: Published 00195 // Description: Returns the server name specified by the URL, if any. 00196 //////////////////////////////////////////////////////////////////// 00197 INLINE string URLSpec:: 00198 get_server() const { 00199 return _url.substr(_server_start, _server_end - _server_start); 00200 } 00201 00202 //////////////////////////////////////////////////////////////////// 00203 // Function: URLSpec::get_port_str 00204 // Access: Published 00205 // Description: Returns the port specified by the URL as a string, or 00206 // the empty string if no port is specified. Compare 00207 // this with get_port(), which returns a default port 00208 // number if no port is specified. 00209 //////////////////////////////////////////////////////////////////// 00210 INLINE string URLSpec:: 00211 get_port_str() const { 00212 return _url.substr(_port_start, _port_end - _port_start); 00213 } 00214 00215 //////////////////////////////////////////////////////////////////// 00216 // Function: URLSpec::get_query 00217 // Access: Published 00218 // Description: Returns the query specified by the URL, or empty 00219 // string if no query is specified. 00220 //////////////////////////////////////////////////////////////////// 00221 INLINE string URLSpec:: 00222 get_query() const { 00223 return _url.substr(_query_start); 00224 } 00225 00226 //////////////////////////////////////////////////////////////////// 00227 // Function: URLSpec::is_ssl 00228 // Access: Published 00229 // Description: Returns true if the URL's scheme specifies an 00230 // SSL-secured protocol such as https, or false 00231 // otherwise. 00232 //////////////////////////////////////////////////////////////////// 00233 INLINE bool URLSpec:: 00234 is_ssl() const { 00235 if (has_scheme() && _scheme_end > 0) { 00236 // If we have a scheme specification, assume it is SSL-secured if 00237 // it ends in "s", except for the special case of "socks". 00238 if (_url.substr(0, _scheme_end) == "socks") { 00239 return false; 00240 } 00241 return (_url[_scheme_end - 1] == 's'); 00242 } 00243 00244 // If we have no scheme specification, it's not SSL-secured. 00245 return false; 00246 } 00247 00248 //////////////////////////////////////////////////////////////////// 00249 // Function: URLSpec::get_url 00250 // Access: Published 00251 // Description: Returns the complete URL specification. 00252 //////////////////////////////////////////////////////////////////// 00253 INLINE const string &URLSpec:: 00254 get_url() const { 00255 return _url; 00256 } 00257 00258 //////////////////////////////////////////////////////////////////// 00259 // Function: URLSpec::string typecast operator 00260 // Access: Public 00261 // Description: 00262 //////////////////////////////////////////////////////////////////// 00263 INLINE URLSpec:: 00264 operator const string & () const { 00265 return _url; 00266 } 00267 00268 //////////////////////////////////////////////////////////////////// 00269 // Function: URLSpec::c_str 00270 // Access: Public 00271 // Description: 00272 //////////////////////////////////////////////////////////////////// 00273 INLINE const char *URLSpec:: 00274 c_str() const { 00275 return _url.c_str(); 00276 } 00277 00278 //////////////////////////////////////////////////////////////////// 00279 // Function: URLSpec::empty 00280 // Access: Public 00281 // Description: 00282 //////////////////////////////////////////////////////////////////// 00283 INLINE bool URLSpec:: 00284 empty() const { 00285 return _url.empty(); 00286 } 00287 00288 //////////////////////////////////////////////////////////////////// 00289 // Function: URLSpec::length 00290 // Access: Public 00291 // Description: 00292 //////////////////////////////////////////////////////////////////// 00293 INLINE size_t URLSpec:: 00294 length() const { 00295 return _url.length(); 00296 } 00297 00298 //////////////////////////////////////////////////////////////////// 00299 // Function: URLSpec::Indexing operator 00300 // Access: Public 00301 // Description: 00302 //////////////////////////////////////////////////////////////////// 00303 INLINE char URLSpec:: 00304 operator [] (int n) const { 00305 nassertr(n >= 0 && n < (int)_url.length(), '\0'); 00306 return _url[n]; 00307 } 00308 00309 INLINE istream & 00310 operator >> (istream &in, URLSpec &url) { 00311 if (!url.input(in)) { 00312 in.clear(ios::failbit | in.rdstate()); 00313 } 00314 return in; 00315 } 00316 00317 INLINE ostream & 00318 operator << (ostream &out, const URLSpec &url) { 00319 url.output(out); 00320 return out; 00321 } 00322 00323