Panda3D
urlSpec.I
1 // Filename: urlSpec.I
2 // Created by: drose (24Sep02)
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 
16 ////////////////////////////////////////////////////////////////////
17 // Function: URLSpec::Constructor
18 // Access: Published
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE URLSpec::
22 URLSpec(const string &url, bool server_name_expected) {
23  set_url(url, server_name_expected);
24 }
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: URLSpec::Copy Constructor
28 // Access: Published
29 // Description:
30 ////////////////////////////////////////////////////////////////////
31 INLINE URLSpec::
32 URLSpec(const URLSpec &copy) {
33  (*this) = copy;
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: URLSpec::Assignment Operator
38 // Access: Published
39 // Description:
40 ////////////////////////////////////////////////////////////////////
41 INLINE void URLSpec::
42 operator = (const string &url) {
43  set_url(url);
44 }
45 
46 ////////////////////////////////////////////////////////////////////
47 // Function: URLSpec::Operator ==
48 // Access: Published
49 // Description:
50 ////////////////////////////////////////////////////////////////////
51 INLINE bool URLSpec::
52 operator == (const URLSpec &other) const {
53  return _url == other._url;
54 }
55 
56 ////////////////////////////////////////////////////////////////////
57 // Function: URLSpec::Operator !=
58 // Access: Published
59 // Description:
60 ////////////////////////////////////////////////////////////////////
61 INLINE bool URLSpec::
62 operator != (const URLSpec &other) const {
63  return !operator == (other);
64 }
65 
66 ////////////////////////////////////////////////////////////////////
67 // Function: URLSpec::Operator <
68 // Access: Published
69 // Description:
70 ////////////////////////////////////////////////////////////////////
71 INLINE bool URLSpec::
72 operator < (const URLSpec &other) const {
73  return _url < other._url;
74 }
75 
76 ////////////////////////////////////////////////////////////////////
77 // Function: URLSpec::compare_to
78 // Access: Published
79 // Description: Returns a number less than zero if this URLSpec
80 // sorts before the other one, greater than zero if it
81 // sorts after, or zero if they are equivalent.
82 ////////////////////////////////////////////////////////////////////
83 INLINE int URLSpec::
84 compare_to(const URLSpec &other) const {
85  return strcmp(_url.c_str(), other._url.c_str());
86 }
87 
88 ////////////////////////////////////////////////////////////////////
89 // Function: URLSpec::has_scheme
90 // Access: Published
91 // Description: Returns true if the URL specifies a scheme
92 // (e.g. "http:"), false otherwise.
93 ////////////////////////////////////////////////////////////////////
94 INLINE bool URLSpec::
95 has_scheme() const {
96  return (_flags & F_has_scheme) != 0;
97 }
98 
99 ////////////////////////////////////////////////////////////////////
100 // Function: URLSpec::has_authority
101 // Access: Published
102 // Description: Returns true if the URL specifies an authority
103 // (this includes username, server, and/or port), false
104 // otherwise.
105 ////////////////////////////////////////////////////////////////////
106 INLINE bool URLSpec::
107 has_authority() const {
108  return (_flags & F_has_authority) != 0;
109 }
110 
111 ////////////////////////////////////////////////////////////////////
112 // Function: URLSpec::has_username
113 // Access: Published
114 // Description: Returns true if the URL specifies a username
115 // (and/or password), false otherwise.
116 ////////////////////////////////////////////////////////////////////
117 INLINE bool URLSpec::
118 has_username() const {
119  return (_flags & F_has_username) != 0;
120 }
121 
122 ////////////////////////////////////////////////////////////////////
123 // Function: URLSpec::has_server
124 // Access: Published
125 // Description: Returns true if the URL specifies a server name,
126 // false otherwise.
127 ////////////////////////////////////////////////////////////////////
128 INLINE bool URLSpec::
129 has_server() const {
130  return (_flags & F_has_server) != 0;
131 }
132 
133 ////////////////////////////////////////////////////////////////////
134 // Function: URLSpec::has_port
135 // Access: Published
136 // Description: Returns true if the URL specifies a port number,
137 // false otherwise.
138 ////////////////////////////////////////////////////////////////////
139 INLINE bool URLSpec::
140 has_port() const {
141  return (_flags & F_has_port) != 0;
142 }
143 
144 ////////////////////////////////////////////////////////////////////
145 // Function: URLSpec::has_path
146 // Access: Published
147 // Description: Returns true if the URL includes a path specification
148 // (that is, the particular filename on the server to
149 // retrieve), false otherwise.
150 ////////////////////////////////////////////////////////////////////
151 INLINE bool URLSpec::
152 has_path() const {
153  return (_flags & F_has_path) != 0;
154 }
155 
156 ////////////////////////////////////////////////////////////////////
157 // Function: URLSpec::has_query
158 // Access: Published
159 // Description: Returns true if the URL includes a query
160 // specification, false otherwise.
161 ////////////////////////////////////////////////////////////////////
162 INLINE bool URLSpec::
163 has_query() const {
164  return (_flags & F_has_query) != 0;
165 }
166 
167 ////////////////////////////////////////////////////////////////////
168 // Function: URLSpec::get_authority
169 // Access: Published
170 // Description: Returns the authority specified by the URL (this
171 // includes username, server, and/or port), or empty
172 // string if no authority is specified.
173 ////////////////////////////////////////////////////////////////////
174 INLINE string URLSpec::
175 get_authority() const {
176  return _url.substr(_username_start, _port_end - _username_start);
177 }
178 
179 ////////////////////////////////////////////////////////////////////
180 // Function: URLSpec::get_username
181 // Access: Published
182 // Description: Returns the username specified by the URL, if any.
183 // This might also include a password,
184 // e.g. "username:password", although putting a password
185 // on the URL is probably a bad idea.
186 ////////////////////////////////////////////////////////////////////
187 INLINE string URLSpec::
188 get_username() const {
189  return _url.substr(_username_start, _username_end - _username_start);
190 }
191 
192 ////////////////////////////////////////////////////////////////////
193 // Function: URLSpec::get_server
194 // Access: Published
195 // Description: Returns the server name specified by the URL, if any.
196 ////////////////////////////////////////////////////////////////////
197 INLINE string URLSpec::
198 get_server() const {
199  return _url.substr(_server_start, _server_end - _server_start);
200 }
201 
202 ////////////////////////////////////////////////////////////////////
203 // Function: URLSpec::get_port_str
204 // Access: Published
205 // Description: Returns the port specified by the URL as a string, or
206 // the empty string if no port is specified. Compare
207 // this with get_port(), which returns a default port
208 // number if no port is specified.
209 ////////////////////////////////////////////////////////////////////
210 INLINE string URLSpec::
211 get_port_str() const {
212  return _url.substr(_port_start, _port_end - _port_start);
213 }
214 
215 ////////////////////////////////////////////////////////////////////
216 // Function: URLSpec::get_query
217 // Access: Published
218 // Description: Returns the query specified by the URL, or empty
219 // string if no query is specified.
220 ////////////////////////////////////////////////////////////////////
221 INLINE string URLSpec::
222 get_query() const {
223  return _url.substr(_query_start);
224 }
225 
226 ////////////////////////////////////////////////////////////////////
227 // Function: URLSpec::is_ssl
228 // Access: Published
229 // Description: Returns true if the URL's scheme specifies an
230 // SSL-secured protocol such as https, or false
231 // otherwise.
232 ////////////////////////////////////////////////////////////////////
233 INLINE bool URLSpec::
234 is_ssl() const {
235  if (has_scheme() && _scheme_end > 0) {
236  // If we have a scheme specification, assume it is SSL-secured if
237  // it ends in "s", except for the special case of "socks".
238  if (_url.substr(0, _scheme_end) == "socks") {
239  return false;
240  }
241  return (_url[_scheme_end - 1] == 's');
242  }
243 
244  // If we have no scheme specification, it's not SSL-secured.
245  return false;
246 }
247 
248 ////////////////////////////////////////////////////////////////////
249 // Function: URLSpec::get_url
250 // Access: Published
251 // Description: Returns the complete URL specification.
252 ////////////////////////////////////////////////////////////////////
253 INLINE const string &URLSpec::
254 get_url() const {
255  return _url;
256 }
257 
258 ////////////////////////////////////////////////////////////////////
259 // Function: URLSpec::string typecast operator
260 // Access: Public
261 // Description:
262 ////////////////////////////////////////////////////////////////////
263 INLINE URLSpec::
264 operator const string & () const {
265  return _url;
266 }
267 
268 ////////////////////////////////////////////////////////////////////
269 // Function: URLSpec::c_str
270 // Access: Public
271 // Description:
272 ////////////////////////////////////////////////////////////////////
273 INLINE const char *URLSpec::
274 c_str() const {
275  return _url.c_str();
276 }
277 
278 ////////////////////////////////////////////////////////////////////
279 // Function: URLSpec::empty
280 // Access: Public
281 // Description:
282 ////////////////////////////////////////////////////////////////////
283 INLINE bool URLSpec::
284 empty() const {
285  return _url.empty();
286 }
287 
288 ////////////////////////////////////////////////////////////////////
289 // Function: URLSpec::length
290 // Access: Public
291 // Description:
292 ////////////////////////////////////////////////////////////////////
293 INLINE size_t URLSpec::
294 length() const {
295  return _url.length();
296 }
297 
298 ////////////////////////////////////////////////////////////////////
299 // Function: URLSpec::Indexing operator
300 // Access: Public
301 // Description:
302 ////////////////////////////////////////////////////////////////////
303 INLINE char URLSpec::
304 operator [] (int n) const {
305  nassertr(n >= 0 && n < (int)_url.length(), '\0');
306  return _url[n];
307 }
308 
309 INLINE istream &
310 operator >> (istream &in, URLSpec &url) {
311  if (!url.input(in)) {
312  in.clear(ios::failbit | in.rdstate());
313  }
314  return in;
315 }
316 
317 INLINE ostream &
318 operator << (ostream &out, const URLSpec &url) {
319  url.output(out);
320  return out;
321 }
322 
323 
A container for a URL, e.g.
Definition: urlSpec.h:29
string get_username() const
Returns the username specified by the URL, if any.
Definition: urlSpec.I:188
string get_server() const
Returns the server name specified by the URL, if any.
Definition: urlSpec.I:198
bool has_username() const
Returns true if the URL specifies a username (and/or password), false otherwise.
Definition: urlSpec.I:118
bool has_authority() const
Returns true if the URL specifies an authority (this includes username, server, and/or port)...
Definition: urlSpec.I:107
bool has_path() const
Returns true if the URL includes a path specification (that is, the particular filename on the server...
Definition: urlSpec.I:152
bool has_scheme() const
Returns true if the URL specifies a scheme (e.g.
Definition: urlSpec.I:95
bool is_ssl() const
Returns true if the URL&#39;s scheme specifies an SSL-secured protocol such as https, or false otherwise...
Definition: urlSpec.I:234
bool has_port() const
Returns true if the URL specifies a port number, false otherwise.
Definition: urlSpec.I:140
string get_authority() const
Returns the authority specified by the URL (this includes username, server, and/or port)...
Definition: urlSpec.I:175
string get_query() const
Returns the query specified by the URL, or empty string if no query is specified. ...
Definition: urlSpec.I:222
bool has_server() const
Returns true if the URL specifies a server name, false otherwise.
Definition: urlSpec.I:129
bool has_query() const
Returns true if the URL includes a query specification, false otherwise.
Definition: urlSpec.I:163
int compare_to(const URLSpec &other) const
Returns a number less than zero if this URLSpec sorts before the other one, greater than zero if it s...
Definition: urlSpec.I:84
const string & get_url() const
Returns the complete URL specification.
Definition: urlSpec.I:254
void set_url(const string &url, bool server_name_expected=false)
Completely replaces the URL with the indicated string.
Definition: urlSpec.cxx:493
string get_port_str() const
Returns the port specified by the URL as a string, or the empty string if no port is specified...
Definition: urlSpec.I:211