-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathuri.hpp
278 lines (240 loc) · 7.8 KB
/
uri.hpp
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
/**
* \file
* URI parsing and handling.
*
* \copyright
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#ifndef COSIM_URI_HPP
#define COSIM_URI_HPP
#include <cosim/fs_portability.hpp>
#include <cstddef>
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
namespace cosim
{
namespace detail
{
struct subrange
{
std::size_t offset = 0;
std::size_t size = 0;
};
} // namespace detail
/**
* A URI reference.
*
* A URI reference is an (absolute) URI if and only if it has a *scheme*
* component, i.e., the segment leading up to the first colon character.
* (For example: the `http` part of `http://example.com`).
*/
class uri
{
public:
/// Constructs an empty URI reference.
uri() noexcept;
/**
* Parses the contents of `string`.
*
* `string` must either contain a valid URI reference or be empty.
* Passing an empty string is equivalent to calling the default
* constructor.
*
* Complies with [RFC 3986](https://tools.ietf.org/html/rfc3986).
* The "authority" component is not validated or decomposed.
*
* \throws std::invalid_argument
* if `string` does not contain a valid URI reference.
*/
/*implicit*/ uri(std::string string);
// Enable indirect implicit conversions from various string types.
/*implicit*/ uri(std::string_view string)
: uri(std::string(string))
{ }
/*implicit*/ uri(const char* string)
: uri(std::string(string))
{ }
/**
* Composes a URI reference from its individual components.
*
* Each component must conform to the rules described in RFC 3986.
* Beyond that, no validation is performed. (That is, no hostname lookup,
* no scheme-specific validation, and so on).
*
* Passing an empty `path` and `std::nullopt` for all other components is
* equivalent to calling the default constructor.
*
* \throws std::invalid_argument
* if any of the components have an invalid value.
*/
uri(
std::optional<std::string_view> scheme,
std::optional<std::string_view> authority,
std::string_view path,
std::optional<std::string_view> query = std::nullopt,
std::optional<std::string_view> fragment = std::nullopt);
/**
* Returns the entire URI reference as a string.
*
* The returned `std::string_view` is only valid as long as the `uri`
* object remains alive and unmodified.
*/
std::string_view view() const noexcept;
/**
* Returns the scheme component, or null if there is none.
*
* The returned `std::string_view` is only valid as long as the `uri`
* object remains alive and unmodified.
*/
std::optional<std::string_view> scheme() const noexcept;
/**
* Returns the authority component, or null if there is none.
*
* The returned `std::string_view` is only valid as long as the `uri`
* object remains alive and unmodified.
*/
std::optional<std::string_view> authority() const noexcept;
/**
* Returns the path component.
*
* The returned `std::string_view` is only valid as long as the `uri`
* object remains alive and unmodified.
*/
std::string_view path() const noexcept;
/**
* Returns the query component, or null if there is none.
*
* The returned `std::string_view` is only valid as long as the `uri`
* object remains alive and unmodified.
*/
std::optional<std::string_view> query() const noexcept;
/**
* Returns the fragment component, or null if there is none.
*
* The returned `std::string_view` is only valid as long as the `uri`
* object remains alive and unmodified.
*/
std::optional<std::string_view> fragment() const noexcept;
/// Returns whether the `uri` object is empty.
bool empty() const noexcept;
private:
std::string data_;
std::optional<detail::subrange> scheme_;
std::optional<detail::subrange> authority_;
detail::subrange path_;
std::optional<detail::subrange> query_;
std::optional<detail::subrange> fragment_;
};
/// Compares two URI references for equality.
inline bool operator==(const uri& a, const uri& b) noexcept
{
return a.view() == b.view();
}
/// Compares two URI references for inequality.
inline bool operator!=(const uri& a, const uri& b) noexcept
{
return a.view() != b.view();
}
/// Writes a URI reference to an output stream.
inline std::ostream& operator<<(std::ostream& stream, const uri& u)
{
return stream << u.view();
}
/**
* Resolves a URI reference relative to a base URI.
*
* Strictly complies with [RFC 3986](https://tools.ietf.org/html/rfc3986).
*
* \param [in] base
* An absolute URI.
* \param [in] reference
* A URI reference, which may be relative.
*
* \throws std::invalid_argument
* if `base` is not absolute.
*/
uri resolve_reference(const uri& base, const uri& reference);
/**
* Percent-encodes a string.
*
* All characters will be encoded, with the exception of those that are
* classified as "unreserved characters" in
* [RFC 3986](https://tools.ietf.org/html/rfc3986) and those in `exceptions`.
*
* \param [in] string
* The string to encode.
* \param [in] exceptions
* A string containing additional characters that are *not* to be encoded,
* e.g. if they have a special meaning in a particular URI scheme.
*
* \returns
* A new, percent-encoded string.
*/
std::string percent_encode(std::string_view string, const char* exceptions = nullptr);
/**
* Decodes a percent-encoded string.
*
* \param [in] encoded
* A percent-encoded string.
*
* \returns
* A new, decoded string.
*/
std::string percent_decode(std::string_view encoded);
/**
* Composes a percent-encoded URI from (unencoded) components.
*
* This will percent-encode each component according to some rules that work
* with many URI schemes, but not necessarily all. The characters that are
* left unencoded (beyond the "unreserved character set") are specified in
* the parameter descriptions below.
*
* \param [in] scheme
* The URI scheme component. Un-encoded characters: `+`
* \param [in] authority
* The authority component. Un-encoded characters: `@:+`
* \param [in] path
* The path component. Un-encoded characters: `/+`
* \param [in] query
* The query component. Un-encoded characters: `=&;/:+`
* \param [in] fragment
* The fragment component. Un-encoded characters: (none)
*
* \returns
* The composed and encoded URI.
*/
uri percent_encode_uri(
std::optional<std::string_view> scheme,
std::optional<std::string_view> authority,
std::string_view path,
std::optional<std::string_view> query = std::nullopt,
std::optional<std::string_view> fragment = std::nullopt);
/**
* Converts a local filesystem path to a `file` URI.
*
* \param [in] path
* A path that either satisfies `path.has_root_directory()` or is empty.
*
* \returns
* The URI that corresponds to `path`. The general format will be
* `file:///<os-dependent path>`, except when `path` is empty, in which
* case the function returns `file:`.
*/
uri path_to_file_uri(const cosim::filesystem::path& path);
/**
* Converts a `file` URI to a local filesystem path.
*
* \param [in] fileUri
* An URI where the scheme component is equal to `file` and the `authority`
* component is either empty or equal to `localhost` (but not undefined).
*
* \returns
* The path that corresponds to `fileUri`.
*/
cosim::filesystem::path file_uri_to_path(const uri& fileUri);
} // namespace cosim
#endif // header guard