1 | // |
---|
2 | // URI.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/URI.h#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: URI |
---|
8 | // Module: URI |
---|
9 | // |
---|
10 | // Definition of the URI class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_URI_INCLUDED |
---|
40 | #define Foundation_URI_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include <vector> |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | |
---|
49 | |
---|
50 | class Foundation_API URI |
---|
51 | /// A Uniform Resource Identifier, as specified in RFC 3986. |
---|
52 | /// |
---|
53 | /// The URI class provides methods for building URIs from their |
---|
54 | /// parts, as well as for splitting URIs into their parts. |
---|
55 | /// Furthermore, the class provides methods for resolving |
---|
56 | /// relative URIs against base URIs. |
---|
57 | /// |
---|
58 | /// The class automatically performs a few normalizations on |
---|
59 | /// all URIs and URI parts passed to it: |
---|
60 | /// * scheme identifiers are converted to lower case. |
---|
61 | /// * percent-encoded characters are decoded |
---|
62 | /// * optionally, dot segments are removed from paths (see normalize()) |
---|
63 | { |
---|
64 | public: |
---|
65 | URI(); |
---|
66 | /// Creates an empty URI. |
---|
67 | |
---|
68 | explicit URI(const std::string& uri); |
---|
69 | /// Parses an URI from the given string. Throws a |
---|
70 | /// SyntaxException if the uri is not valid. |
---|
71 | |
---|
72 | explicit URI(const char* uri); |
---|
73 | /// Parses an URI from the given string. Throws a |
---|
74 | /// SyntaxException if the uri is not valid. |
---|
75 | |
---|
76 | URI(const std::string& scheme, const std::string& pathEtc); |
---|
77 | /// Creates an URI from its parts. |
---|
78 | |
---|
79 | URI(const std::string& scheme, const std::string& authority, const std::string& pathEtc); |
---|
80 | /// Creates an URI from its parts. |
---|
81 | |
---|
82 | URI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query); |
---|
83 | /// Creates an URI from its parts. |
---|
84 | |
---|
85 | URI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment); |
---|
86 | /// Creates an URI from its parts. |
---|
87 | |
---|
88 | URI(const URI& uri); |
---|
89 | /// Copy constructor. Creates an URI from another one. |
---|
90 | |
---|
91 | URI(const URI& baseURI, const std::string& relativeURI); |
---|
92 | /// Creates an URI from a base URI and a relative URI, according to |
---|
93 | /// the algorithm in section 5.2 of RFC 3986. |
---|
94 | |
---|
95 | ~URI(); |
---|
96 | /// Destroys the URI. |
---|
97 | |
---|
98 | URI& operator = (const URI& uri); |
---|
99 | /// Assignment operator. |
---|
100 | |
---|
101 | URI& operator = (const std::string& uri); |
---|
102 | /// Parses and assigns an URI from the given string. Throws a |
---|
103 | /// SyntaxException if the uri is not valid. |
---|
104 | |
---|
105 | URI& operator = (const char* uri); |
---|
106 | /// Parses and assigns an URI from the given string. Throws a |
---|
107 | /// SyntaxException if the uri is not valid. |
---|
108 | |
---|
109 | void swap(URI& uri); |
---|
110 | /// Swaps the URI with another one. |
---|
111 | |
---|
112 | void clear(); |
---|
113 | /// Clears all parts of the URI. |
---|
114 | |
---|
115 | std::string toString() const; |
---|
116 | /// Returns a string representation of the URI. |
---|
117 | /// |
---|
118 | /// Characters in the path, query and fragment parts will be |
---|
119 | /// percent-encoded as necessary. |
---|
120 | |
---|
121 | const std::string& getScheme() const; |
---|
122 | /// Returns the scheme part of the URI. |
---|
123 | |
---|
124 | void setScheme(const std::string& scheme); |
---|
125 | /// Sets the scheme part of the URI. The given scheme |
---|
126 | /// is converted to lower-case. |
---|
127 | /// |
---|
128 | /// A list of registered URI schemes can be found |
---|
129 | /// at <http://www.iana.org/assignments/uri-schemes>. |
---|
130 | |
---|
131 | const std::string& getUserInfo() const; |
---|
132 | /// Returns the user-info part of the URI. |
---|
133 | |
---|
134 | void setUserInfo(const std::string& userInfo); |
---|
135 | /// Sets the user-info part of the URI. |
---|
136 | |
---|
137 | const std::string& getHost() const; |
---|
138 | /// Returns the host part of the URI. |
---|
139 | |
---|
140 | void setHost(const std::string& host); |
---|
141 | /// Sets the host part of the URI. |
---|
142 | |
---|
143 | unsigned short getPort() const; |
---|
144 | /// Returns the port number part of the URI. |
---|
145 | /// |
---|
146 | /// If no port number (0) has been specified, the |
---|
147 | /// well-known port number (e.g., 80 for http) for |
---|
148 | /// the given scheme is returned if it is known. |
---|
149 | /// Otherwise, 0 is returned. |
---|
150 | |
---|
151 | void setPort(unsigned short port); |
---|
152 | /// Sets the port number part of the URI. |
---|
153 | |
---|
154 | std::string getAuthority() const; |
---|
155 | /// Returns the authority part (userInfo, host and port) |
---|
156 | /// of the URI. |
---|
157 | /// |
---|
158 | /// If the port number is a well-known port |
---|
159 | /// number for the given scheme (e.g., 80 for http), it |
---|
160 | /// is not included in the authority. |
---|
161 | |
---|
162 | void setAuthority(const std::string& authority); |
---|
163 | /// Parses the given authority part for the URI and sets |
---|
164 | /// the user-info, host, port components accordingly. |
---|
165 | |
---|
166 | const std::string& getPath() const; |
---|
167 | /// Returns the path part of the URI. |
---|
168 | |
---|
169 | void setPath(const std::string& path); |
---|
170 | /// Sets the path part of the URI. |
---|
171 | |
---|
172 | std::string getQuery() const; |
---|
173 | /// Returns the query part of the URI. |
---|
174 | |
---|
175 | void setQuery(const std::string& query); |
---|
176 | /// Sets the query part of the URI. |
---|
177 | |
---|
178 | const std::string& getRawQuery() const; |
---|
179 | /// Returns the unencoded query part of the URI. |
---|
180 | |
---|
181 | void setRawQuery(const std::string& query); |
---|
182 | /// Sets the query part of the URI. |
---|
183 | |
---|
184 | const std::string& getFragment() const; |
---|
185 | /// Returns the fragment part of the URI. |
---|
186 | |
---|
187 | void setFragment(const std::string& fragment); |
---|
188 | /// Sets the fragment part of the URI. |
---|
189 | |
---|
190 | void setPathEtc(const std::string& pathEtc); |
---|
191 | /// Sets the path, query and fragment parts of the URI. |
---|
192 | |
---|
193 | std::string getPathEtc() const; |
---|
194 | /// Returns the path, query and fragment parts of the URI. |
---|
195 | |
---|
196 | std::string getPathAndQuery() const; |
---|
197 | /// Returns the path and query parts of the URI. |
---|
198 | |
---|
199 | void resolve(const std::string& relativeURI); |
---|
200 | /// Resolves the given relative URI against the base URI. |
---|
201 | /// See section 5.2 of RFC 3986 for the algorithm used. |
---|
202 | |
---|
203 | void resolve(const URI& relativeURI); |
---|
204 | /// Resolves the given relative URI against the base URI. |
---|
205 | /// See section 5.2 of RFC 3986 for the algorithm used. |
---|
206 | |
---|
207 | bool isRelative() const; |
---|
208 | /// Returns true if the URI is a relative reference, false otherwise. |
---|
209 | /// |
---|
210 | /// A relative reference does not contain a scheme identifier. |
---|
211 | /// Relative references are usually resolved against an absolute |
---|
212 | /// base reference. |
---|
213 | |
---|
214 | bool empty() const; |
---|
215 | /// Returns true if the URI is empty, false otherwise. |
---|
216 | |
---|
217 | bool operator == (const URI& uri) const; |
---|
218 | /// Returns true if both URIs are identical, false otherwise. |
---|
219 | /// |
---|
220 | /// Two URIs are identical if their scheme, authority, |
---|
221 | /// path, query and fragment part are identical. |
---|
222 | |
---|
223 | bool operator == (const std::string& uri) const; |
---|
224 | /// Parses the given URI and returns true if both URIs are identical, |
---|
225 | /// false otherwise. |
---|
226 | |
---|
227 | bool operator != (const URI& uri) const; |
---|
228 | /// Returns true if both URIs are identical, false otherwise. |
---|
229 | |
---|
230 | bool operator != (const std::string& uri) const; |
---|
231 | /// Parses the given URI and returns true if both URIs are identical, |
---|
232 | /// false otherwise. |
---|
233 | |
---|
234 | void normalize(); |
---|
235 | /// Normalizes the URI by removing all but leading . and .. segments from the path. |
---|
236 | /// |
---|
237 | /// If the first path segment in a relative path contains a colon (:), |
---|
238 | /// such as in a Windows path containing a drive letter, a dot segment (./) |
---|
239 | /// is prepended in accordance with section 3.3 of RFC 3986. |
---|
240 | |
---|
241 | void getPathSegments(std::vector<std::string>& segments); |
---|
242 | /// Places the single path segments (delimited by slashes) into the |
---|
243 | /// given vector. |
---|
244 | |
---|
245 | static void encode(const std::string& str, const std::string& reserved, std::string& encodedStr); |
---|
246 | /// URI-encodes the given string by escaping reserved and non-ASCII |
---|
247 | /// characters. The encoded string is appended to encodedStr. |
---|
248 | |
---|
249 | static void decode(const std::string& str, std::string& decodedStr); |
---|
250 | /// URI-decodes the given string by replacing percent-encoded |
---|
251 | /// characters with the actual character. The decoded string |
---|
252 | /// is appended to decodedStr. |
---|
253 | |
---|
254 | protected: |
---|
255 | bool equals(const URI& uri) const; |
---|
256 | /// Returns true if both uri's are equivalent. |
---|
257 | |
---|
258 | bool isWellKnownPort() const; |
---|
259 | /// Returns true if the URI's port number is a well-known one |
---|
260 | /// (for example, 80, if the scheme is http). |
---|
261 | |
---|
262 | unsigned short getWellKnownPort() const; |
---|
263 | /// Returns the well-known port number for the URI's scheme, |
---|
264 | /// or 0 if the port number is not known. |
---|
265 | |
---|
266 | void parse(const std::string& uri); |
---|
267 | /// Parses and assigns an URI from the given string. Throws a |
---|
268 | /// SyntaxException if the uri is not valid. |
---|
269 | |
---|
270 | void parseAuthority(std::string::const_iterator& it, const std::string::const_iterator& end); |
---|
271 | /// Parses and sets the user-info, host and port from the given data. |
---|
272 | |
---|
273 | void parseHostAndPort(std::string::const_iterator& it, const std::string::const_iterator& end); |
---|
274 | /// Parses and sets the host and port from the given data. |
---|
275 | |
---|
276 | void parsePath(std::string::const_iterator& it, const std::string::const_iterator& end); |
---|
277 | /// Parses and sets the path from the given data. |
---|
278 | |
---|
279 | void parsePathEtc(std::string::const_iterator& it, const std::string::const_iterator& end); |
---|
280 | /// Parses and sets the path, query and fragment from the given data. |
---|
281 | |
---|
282 | void parseQuery(std::string::const_iterator& it, const std::string::const_iterator& end); |
---|
283 | /// Parses and sets the query from the given data. |
---|
284 | |
---|
285 | void parseFragment(std::string::const_iterator& it, const std::string::const_iterator& end); |
---|
286 | /// Parses and sets the fragment from the given data. |
---|
287 | |
---|
288 | void mergePath(const std::string& path); |
---|
289 | /// Appends a path to the URI's path. |
---|
290 | |
---|
291 | void removeDotSegments(bool removeLeading = true); |
---|
292 | /// Removes all dot segments from the path. |
---|
293 | |
---|
294 | static void getPathSegments(const std::string& path, std::vector<std::string>& segments); |
---|
295 | /// Places the single path segments (delimited by slashes) into the |
---|
296 | /// given vector. |
---|
297 | |
---|
298 | void buildPath(const std::vector<std::string>& segments, bool leadingSlash, bool trailingSlash); |
---|
299 | /// Builds the path from the given segments. |
---|
300 | |
---|
301 | static const std::string RESERVED_PATH; |
---|
302 | static const std::string RESERVED_QUERY; |
---|
303 | static const std::string RESERVED_FRAGMENT; |
---|
304 | static const std::string ILLEGAL; |
---|
305 | |
---|
306 | private: |
---|
307 | std::string _scheme; |
---|
308 | std::string _userInfo; |
---|
309 | std::string _host; |
---|
310 | unsigned short _port; |
---|
311 | std::string _path; |
---|
312 | std::string _query; |
---|
313 | std::string _fragment; |
---|
314 | }; |
---|
315 | |
---|
316 | |
---|
317 | // |
---|
318 | // inlines |
---|
319 | // |
---|
320 | inline const std::string& URI::getScheme() const |
---|
321 | { |
---|
322 | return _scheme; |
---|
323 | } |
---|
324 | |
---|
325 | |
---|
326 | inline const std::string& URI::getUserInfo() const |
---|
327 | { |
---|
328 | return _userInfo; |
---|
329 | } |
---|
330 | |
---|
331 | |
---|
332 | inline const std::string& URI::getHost() const |
---|
333 | { |
---|
334 | return _host; |
---|
335 | } |
---|
336 | |
---|
337 | |
---|
338 | inline const std::string& URI::getPath() const |
---|
339 | { |
---|
340 | return _path; |
---|
341 | } |
---|
342 | |
---|
343 | |
---|
344 | inline const std::string& URI::getRawQuery() const |
---|
345 | { |
---|
346 | return _query; |
---|
347 | } |
---|
348 | |
---|
349 | |
---|
350 | inline const std::string& URI::getFragment() const |
---|
351 | { |
---|
352 | return _fragment; |
---|
353 | } |
---|
354 | |
---|
355 | |
---|
356 | inline void swap(URI& u1, URI& u2) |
---|
357 | { |
---|
358 | u1.swap(u2); |
---|
359 | } |
---|
360 | |
---|
361 | |
---|
362 | } // namespace Poco |
---|
363 | |
---|
364 | |
---|
365 | #endif // Foundation_URI_INCLUDED |
---|