aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/include/Poco/URI.h
blob: 658be1403b5eb29b57ec9dfdc2839df35a593a38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// 
// URI.h 
// 
// Library: Foundation 
// Package: URI 
// Module:  URI 
// 
// Definition of the URI class. 
// 
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. 
// and Contributors. 
// 
// SPDX-License-Identifier:	BSL-1.0 
// 
 
 
#ifndef Foundation_URI_INCLUDED 
#define Foundation_URI_INCLUDED 
 
 
#include "Poco/Foundation.h" 
#include <vector> 
#include <utility> 
 
 
namespace Poco { 
 
 
class Path; 
 
 
class Foundation_API URI 
	/// A Uniform Resource Identifier, as specified in RFC 3986. 
	///  
	/// The URI class provides methods for building URIs from their 
	/// parts, as well as for splitting URIs into their parts. 
	/// Furthermore, the class provides methods for resolving 
	/// relative URIs against base URIs. 
	/// 
	/// The class automatically performs a few normalizations on 
	/// all URIs and URI parts passed to it: 
	///   * scheme identifiers are converted to lower case 
	///   * percent-encoded characters are decoded (except for the query string) 
	///   * optionally, dot segments are removed from paths (see normalize()) 
	/// 
	/// Note that dealing with query strings requires some precautions, as, internally, 
	/// query strings are stored in percent-encoded form, while all other parts of the URI 
	/// are stored in decoded form. While parsing query strings from properly encoded URLs 
	/// generally works, explicitly setting query strings with setQuery() or extracting 
	/// query strings with getQuery() may lead to ambiguities. See the descriptions of 
	/// setQuery(), setRawQuery(), getQuery() and getRawQuery() for more information. 
{ 
public: 
	typedef std::vector<std::pair<std::string, std::string> > QueryParameters; 
 
	URI(); 
		/// Creates an empty URI. 
 
	explicit URI(const std::string& uri); 
		/// Parses an URI from the given string. Throws a 
		/// SyntaxException if the uri is not valid. 
		 
	explicit URI(const char* uri); 
		/// Parses an URI from the given string. Throws a 
		/// SyntaxException if the uri is not valid. 
		 
	URI(const std::string& scheme, const std::string& pathEtc); 
		/// Creates an URI from its parts. 
		 
	URI(const std::string& scheme, const std::string& authority, const std::string& pathEtc); 
		/// Creates an URI from its parts. 
 
	URI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query); 
		/// Creates an URI from its parts. 
 
	URI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment); 
		/// Creates an URI from its parts. 
 
	URI(const URI& uri); 
		/// Copy constructor. Creates an URI from another one. 
		 
	URI(const URI& baseURI, const std::string& relativeURI); 
		/// Creates an URI from a base URI and a relative URI, according to 
		/// the algorithm in section 5.2 of RFC 3986. 
 
	explicit URI(const Path& path); 
		/// Creates a URI from a path. 
		/// 
		/// The path will be made absolute, and a file:// URI 
		/// will be built from it. 
 
	~URI(); 
		/// Destroys the URI. 
	 
	URI& operator = (const URI& uri); 
		/// Assignment operator. 
		 
	URI& operator = (const std::string& uri); 
		/// Parses and assigns an URI from the given string. Throws a 
		/// SyntaxException if the uri is not valid. 
 
	URI& operator = (const char* uri); 
		/// Parses and assigns an URI from the given string. Throws a 
		/// SyntaxException if the uri is not valid. 
		 
	void swap(URI& uri); 
		/// Swaps the URI with another one.	 
		 
	void clear(); 
		/// Clears all parts of the URI. 
	 
	std::string toString() const; 
		/// Returns a string representation of the URI. 
		/// 
		/// Characters in the path, query and fragment parts will be  
		/// percent-encoded as necessary. 
	 
	const std::string& getScheme() const; 
		/// Returns the scheme part of the URI. 
		 
	void setScheme(const std::string& scheme); 
		/// Sets the scheme part of the URI. The given scheme 
		/// is converted to lower-case. 
		/// 
		/// A list of registered URI schemes can be found 
		/// at <http://www.iana.org/assignments/uri-schemes>. 
		 
	const std::string& getUserInfo() const; 
		/// Returns the user-info part of the URI. 
		 
	void setUserInfo(const std::string& userInfo); 
		/// Sets the user-info part of the URI. 
		 
	const std::string& getHost() const; 
		/// Returns the host part of the URI. 
		 
	void setHost(const std::string& host); 
		/// Sets the host part of the URI. 
		 
	unsigned short getPort() const; 
		/// Returns the port number part of the URI. 
		/// 
		/// If no port number (0) has been specified, the 
		/// well-known port number (e.g., 80 for http) for 
		/// the given scheme is returned if it is known. 
		/// Otherwise, 0 is returned. 
		 
	void setPort(unsigned short port); 
		/// Sets the port number part of the URI. 
		 
	std::string getAuthority() const; 
		/// Returns the authority part (userInfo, host and port) 
		/// of the URI.  
		/// 
		/// If the port number is a well-known port 
		/// number for the given scheme (e.g., 80 for http), it 
		/// is not included in the authority. 
		 
	void setAuthority(const std::string& authority); 
		/// Parses the given authority part for the URI and sets 
		/// the user-info, host, port components accordingly. 
		 
	const std::string& getPath() const; 
		/// Returns the decoded path part of the URI. 
		 
	void setPath(const std::string& path); 
		/// Sets the path part of the URI. 
	 
	std::string getQuery() const; 
		/// Returns the decoded query part of the URI. 
		/// 
		/// Note that encoded ampersand characters ('&', "%26")  
		/// will be decoded, which could cause ambiguities if the query  
		/// string contains multiple parameters and a parameter name 
		/// or value contains an ampersand as well. 
		/// In such a case it's better to use getRawQuery() or 
		/// getQueryParameters(). 
		 
	void setQuery(const std::string& query);	 
		/// Sets the query part of the URI. 
		/// 
		/// The query string will be percent-encoded. If the query 
		/// already contains percent-encoded characters, these 
		/// will be double-encoded, which is probably not what's 
		/// intended by the caller. Furthermore, ampersand ('&') 
		/// characters in the query will not be encoded. This could 
		/// lead to ambiguity issues if the query string contains multiple 
		/// name-value parameters separated by ampersand, and if any 
		/// name or value also contains an ampersand. In such a  
		/// case, it's better to use setRawQuery() with a properly 
		/// percent-encoded query string, or use addQueryParameter() 
		/// or setQueryParameters(), which take care of appropriate  
		/// percent encoding of parameter names and values. 
 
	void addQueryParameter(const std::string& param, const std::string& val = ""); 
		/// Adds "param=val" to the query; "param" may not be empty. 
		/// If val is empty, only '=' is appended to the parameter. 
		///  
		/// In addition to regular encoding, function also encodes '&' and '=', 
		/// if found in param or val. 
 
	const std::string& getRawQuery() const; 
		/// Returns the query string in raw form, which usually 
		/// means percent encoded. 
		 
	void setRawQuery(const std::string& query);	 
		/// Sets the query part of the URI. 
		/// 
		/// The given query string must be properly percent-encoded. 
	 
	QueryParameters getQueryParameters() const; 
		/// Returns the decoded query string parameters as a vector 
		/// of name-value pairs. 
 
	void setQueryParameters(const QueryParameters& params); 
		/// Sets the query part of the URI from a vector 
		/// of query parameters. 
		/// 
		/// Calls addQueryParameter() for each parameter name and value. 
 
	const std::string& getFragment() const; 
		/// Returns the fragment part of the URI. 
		 
	void setFragment(const std::string& fragment); 
		/// Sets the fragment part of the URI. 
		 
	void setPathEtc(const std::string& pathEtc); 
		/// Sets the path, query and fragment parts of the URI. 
		 
	std::string getPathEtc() const; 
		/// Returns the encoded path, query and fragment parts of the URI. 
		 
	std::string getPathAndQuery() const; 
		/// Returns the encoded path and query parts of the URI.	 
		 
	void resolve(const std::string& relativeURI); 
		/// Resolves the given relative URI against the base URI. 
		/// See section 5.2 of RFC 3986 for the algorithm used. 
 
	void resolve(const URI& relativeURI); 
		/// Resolves the given relative URI against the base URI. 
		/// See section 5.2 of RFC 3986 for the algorithm used. 
 
	bool isRelative() const; 
		/// Returns true if the URI is a relative reference, false otherwise. 
		/// 
		/// A relative reference does not contain a scheme identifier. 
		/// Relative references are usually resolved against an absolute 
		/// base reference. 
	 
	bool empty() const; 
		/// Returns true if the URI is empty, false otherwise. 
		 
	bool operator == (const URI& uri) const; 
		/// Returns true if both URIs are identical, false otherwise. 
		/// 
		/// Two URIs are identical if their scheme, authority, 
		/// path, query and fragment part are identical. 
 
	bool operator == (const std::string& uri) const; 
		/// Parses the given URI and returns true if both URIs are identical, 
		/// false otherwise. 
 
	bool operator != (const URI& uri) const; 
		/// Returns true if both URIs are identical, false otherwise. 
 
	bool operator != (const std::string& uri) const; 
		/// Parses the given URI and returns true if both URIs are identical, 
		/// false otherwise. 
		 
	void normalize(); 
		/// Normalizes the URI by removing all but leading . and .. segments from the path. 
		/// 
		/// If the first path segment in a relative path contains a colon (:),  
		/// such as in a Windows path containing a drive letter, a dot segment (./)  
		/// is prepended in accordance with section 3.3 of RFC 3986. 
	 
	void getPathSegments(std::vector<std::string>& segments); 
		/// Places the single path segments (delimited by slashes) into the 
		/// given vector. 
	 
	static void encode(const std::string& str, const std::string& reserved, std::string& encodedStr); 
		/// URI-encodes the given string by escaping reserved and non-ASCII 
		/// characters. The encoded string is appended to encodedStr. 
		 
	static void decode(const std::string& str, std::string& decodedStr, bool plusAsSpace = false); 
		/// URI-decodes the given string by replacing percent-encoded 
		/// characters with the actual character. The decoded string 
		/// is appended to decodedStr. 
		/// 
		/// When plusAsSpace is true, non-encoded plus signs in the query are decoded as spaces. 
		/// (http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1) 
 
protected: 
	bool equals(const URI& uri) const; 
		/// Returns true if both uri's are equivalent. 
	 
	bool isWellKnownPort() const; 
		/// Returns true if the URI's port number is a well-known one 
		/// (for example, 80, if the scheme is http). 
		 
	unsigned short getWellKnownPort() const; 
		/// Returns the well-known port number for the URI's scheme, 
		/// or 0 if the port number is not known. 
		 
	void parse(const std::string& uri); 
		/// Parses and assigns an URI from the given string. Throws a 
		/// SyntaxException if the uri is not valid. 
 
	void parseAuthority(std::string::const_iterator& it, const std::string::const_iterator& end); 
		/// Parses and sets the user-info, host and port from the given data. 
 
	void parseHostAndPort(std::string::const_iterator& it, const std::string::const_iterator& end); 
		/// Parses and sets the host and port from the given data. 
 
	void parsePath(std::string::const_iterator& it, const std::string::const_iterator& end); 
		/// Parses and sets the path from the given data. 
 
	void parsePathEtc(std::string::const_iterator& it, const std::string::const_iterator& end); 
		/// Parses and sets the path, query and fragment from the given data. 
 
	void parseQuery(std::string::const_iterator& it, const std::string::const_iterator& end); 
		/// Parses and sets the query from the given data. 
 
	void parseFragment(std::string::const_iterator& it, const std::string::const_iterator& end); 
		/// Parses and sets the fragment from the given data. 
 
	void mergePath(const std::string& path); 
		/// Appends a path to the URI's path. 
 
	void removeDotSegments(bool removeLeading = true); 
		/// Removes all dot segments from the path. 
 
	static void getPathSegments(const std::string& path, std::vector<std::string>& segments); 
		/// Places the single path segments (delimited by slashes) into the 
		/// given vector. 
 
	void buildPath(const std::vector<std::string>& segments, bool leadingSlash, bool trailingSlash); 
		/// Builds the path from the given segments. 
 
	static const std::string RESERVED_PATH; 
	static const std::string RESERVED_QUERY; 
	static const std::string RESERVED_QUERY_PARAM; 
	static const std::string RESERVED_FRAGMENT; 
	static const std::string ILLEGAL; 
	 
private: 
	std::string    _scheme; 
	std::string    _userInfo; 
	std::string    _host; 
	unsigned short _port; 
	std::string    _path; 
	std::string    _query; 
	std::string    _fragment; 
}; 
 
 
// 
// inlines 
// 
inline const std::string& URI::getScheme() const 
{ 
	return _scheme; 
} 
	 
 
inline const std::string& URI::getUserInfo() const 
{ 
	return _userInfo; 
} 
 
	 
inline const std::string& URI::getHost() const 
{ 
	return _host; 
} 
 
	 
inline const std::string& URI::getPath() const 
{ 
	return _path; 
} 
 
	 
inline const std::string& URI::getRawQuery() const 
{ 
	return _query; 
} 
 
	 
inline const std::string& URI::getFragment() const 
{ 
	return _fragment; 
} 
 
 
inline void swap(URI& u1, URI& u2) 
{ 
	u1.swap(u2); 
} 
 
	 
} // namespace Poco 
 
 
#endif // Foundation_URI_INCLUDED