Panda3D
urlSpec.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file urlSpec.I
10  * @author drose
11  * @date 2002-09-24
12  */
13 
14 /**
15  *
16  */
17 INLINE URLSpec::
18 URLSpec(const std::string &url, bool server_name_expected) {
19  set_url(url, server_name_expected);
20 }
21 
22 /**
23  *
24  */
25 INLINE void URLSpec::
26 operator = (const std::string &url) {
27  set_url(url);
28 }
29 
30 /**
31  *
32  */
33 INLINE bool URLSpec::
34 operator == (const URLSpec &other) const {
35  return compare_to(other) == 0;
36 }
37 
38 /**
39  *
40  */
41 INLINE bool URLSpec::
42 operator != (const URLSpec &other) const {
43  return compare_to(other) != 0;
44 }
45 
46 /**
47  *
48  */
49 INLINE bool URLSpec::
50 operator < (const URLSpec &other) const {
51  return compare_to(other) < 0;
52 }
53 
54 /**
55  * Returns true if the URL specifies a scheme (e.g. "http:"), false
56  * otherwise.
57  */
58 INLINE bool URLSpec::
59 has_scheme() const {
60  return (_flags & F_has_scheme) != 0;
61 }
62 
63 /**
64  * Returns true if the URL specifies an authority (this includes username,
65  * server, and/or port), false otherwise.
66  */
67 INLINE bool URLSpec::
68 has_authority() const {
69  return (_flags & F_has_authority) != 0;
70 }
71 
72 /**
73  * Returns true if the URL specifies a username (and/or password), false
74  * otherwise.
75  */
76 INLINE bool URLSpec::
77 has_username() const {
78  return (_flags & F_has_username) != 0;
79 }
80 
81 /**
82  * Returns true if the URL specifies a server name, false otherwise.
83  */
84 INLINE bool URLSpec::
85 has_server() const {
86  return (_flags & F_has_server) != 0;
87 }
88 
89 /**
90  * Returns true if the URL specifies a port number, false otherwise.
91  */
92 INLINE bool URLSpec::
93 has_port() const {
94  return (_flags & F_has_port) != 0;
95 }
96 
97 /**
98  * Returns true if the URL includes a path specification (that is, the
99  * particular filename on the server to retrieve), false otherwise.
100  */
101 INLINE bool URLSpec::
102 has_path() const {
103  return (_flags & F_has_path) != 0;
104 }
105 
106 /**
107  * Returns true if the URL includes a query specification, false otherwise.
108  */
109 INLINE bool URLSpec::
110 has_query() const {
111  return (_flags & F_has_query) != 0;
112 }
113 
114 /**
115  * Returns the authority specified by the URL (this includes username, server,
116  * and/or port), or empty string if no authority is specified.
117  */
118 INLINE std::string URLSpec::
119 get_authority() const {
120  return _url.substr(_username_start, _port_end - _username_start);
121 }
122 
123 /**
124  * Returns the username specified by the URL, if any. This might also include
125  * a password, e.g. "username:password", although putting a password on the
126  * URL is probably a bad idea.
127  */
128 INLINE std::string URLSpec::
129 get_username() const {
130  return _url.substr(_username_start, _username_end - _username_start);
131 }
132 
133 /**
134  * Returns the server name specified by the URL, if any. In case of an IPv6
135  * address, does not include the enclosing brackets.
136  */
137 INLINE std::string URLSpec::
138 get_server() const {
139  return _url.substr(_server_start, _server_end - _server_start);
140 }
141 
142 /**
143  * Returns the port specified by the URL as a string, or the empty string if
144  * no port is specified. Compare this with get_port(), which returns a
145  * default port number if no port is specified.
146  */
147 INLINE std::string URLSpec::
148 get_port_str() const {
149  return _url.substr(_port_start, _port_end - _port_start);
150 }
151 
152 /**
153  * Returns the query specified by the URL, or empty string if no query is
154  * specified.
155  */
156 INLINE std::string URLSpec::
157 get_query() const {
158  return _url.substr(_query_start);
159 }
160 
161 /**
162  * Returns true if the URL's scheme specifies an SSL-secured protocol such as
163  * https, or false otherwise.
164  */
165 INLINE bool URLSpec::
166 is_ssl() const {
167  if (has_scheme() && _scheme_end > 0) {
168  // If we have a scheme specification, assume it is SSL-secured if it ends
169  // in "s", except for the special case of "socks".
170  if (_url.substr(0, _scheme_end) == "socks") {
171  return false;
172  }
173  return (_url[_scheme_end - 1] == 's');
174  }
175 
176  // If we have no scheme specification, it's not SSL-secured.
177  return false;
178 }
179 
180 /**
181  * Returns the complete URL specification.
182  */
183 INLINE const std::string &URLSpec::
184 get_url() const {
185  return _url;
186 }
187 
188 /**
189  *
190  */
191 INLINE URLSpec::
192 operator const std::string & () const {
193  return _url;
194 }
195 
196 /**
197  *
198  */
199 INLINE const char *URLSpec::
200 c_str() const {
201  return _url.c_str();
202 }
203 
204 /**
205  * Returns false if the URLSpec is valid (not empty), or true if it is an
206  * empty string.
207  */
208 INLINE bool URLSpec::
209 empty() const {
210  return _url.empty();
211 }
212 
213 /**
214  * Returns true if the URLSpec is valid (not empty), or false if it is an
215  * empty string.
216  */
217 INLINE URLSpec::
218 operator bool() const {
219  return !_url.empty();
220 }
221 
222 /**
223  *
224  */
225 INLINE size_t URLSpec::
226 length() const {
227  return _url.length();
228 }
229 
230 /**
231  *
232  */
233 INLINE size_t URLSpec::
234 size() const {
235  return _url.size();
236 }
237 
238 /**
239  *
240  */
241 INLINE char URLSpec::
242 operator [] (size_t n) const {
243  nassertr(n < _url.length(), '\0');
244  return _url[n];
245 }
246 
247 INLINE std::istream &
248 operator >> (std::istream &in, URLSpec &url) {
249  if (!url.input(in)) {
250  in.clear(std::ios::failbit | in.rdstate());
251  }
252  return in;
253 }
254 
255 INLINE std::ostream &
256 operator << (std::ostream &out, const URLSpec &url) {
257  url.output(out);
258  return out;
259 }
A container for a URL, e.g.
Definition: urlSpec.h:28
bool has_username() const
Returns true if the URL specifies a username (and/or password), false otherwise.
Definition: urlSpec.I:77
bool has_authority() const
Returns true if the URL specifies an authority (this includes username, server, and/or port),...
Definition: urlSpec.I:68
bool has_path() const
Returns true if the URL includes a path specification (that is, the particular filename on the server...
Definition: urlSpec.I:102
bool empty() const
Returns false if the URLSpec is valid (not empty), or true if it is an empty string.
Definition: urlSpec.I:209
std::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:148
bool has_scheme() const
Returns true if the URL specifies a scheme (e.g.
Definition: urlSpec.I:59
bool has_port() const
Returns true if the URL specifies a port number, false otherwise.
Definition: urlSpec.I:93
bool has_server() const
Returns true if the URL specifies a server name, false otherwise.
Definition: urlSpec.I:85
bool has_query() const
Returns true if the URL includes a query specification, false otherwise.
Definition: urlSpec.I:110
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.cxx:77
void set_url(const std::string &url, bool server_name_expected=false)
Completely replaces the URL with the indicated string.
Definition: urlSpec.cxx:554
const std::string & get_url() const
Returns the complete URL specification.
Definition: urlSpec.I:184