-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathurl_view.hpp
More file actions
414 lines (336 loc) · 9.86 KB
/
Copy pathurl_view.hpp
File metadata and controls
414 lines (336 loc) · 9.86 KB
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
407
408
409
410
411
412
413
414
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
// Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/url
//
#ifndef BOOST_URL_URL_VIEW_HPP
#define BOOST_URL_URL_VIEW_HPP
#include <boost/url/detail/config.hpp>
#include <boost/url/url_view_base.hpp>
#include <utility>
namespace boost {
namespace urls {
namespace implementation_defined {
struct origin_form_rule_t;
struct uri_rule_t;
struct relative_ref_rule_t;
struct absolute_uri_rule_t;
} // implementation_defined
/** A non-owning reference to a valid URL
Objects of this type represent valid URL
strings constructed from a parsed, external
character buffer whose storage is managed
by the caller. That is, it acts like a
`core::string_view` in terms of ownership.
The caller is responsible for ensuring
that the lifetime of the underlying
character buffer extends until it is no
longer referenced.
@par Example 1
Construction from a string parses the input
as a <em>URI-reference</em> and throws an
exception on error. Upon success, the
constructed object points to the passed
character buffer; ownership is not
transferred.
@code
url_view u( "https://www.example.com/index.htm?text=none#a1" );
@endcode
@par Example 2
Parsing functions like @ref parse_uri_reference
return a `boost::system::result` containing either a valid
@ref url_view upon success, otherwise they
contain an error. The error can be converted to
an exception by the caller if desired:
@code
system::result< url_view > rv = parse_uri_reference( "https://www.example.com/index.htm?text=none#a1" );
@endcode
@par BNF
@code
URI-reference = URI / relative-ref
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
relative-ref = relative-part [ "?" query ] [ "#" fragment ]
@endcode
@par Specification
@li <a href="https://tools.ietf.org/html/rfc3986"
>Uniform Resource Identifier (URI): Generic Syntax (rfc3986)</a>
@see
@ref parse_absolute_uri,
@ref parse_origin_form,
@ref parse_relative_ref,
@ref parse_uri,
@ref parse_uri_reference.
*/
class BOOST_SYMBOL_VISIBLE url_view
: public url_view_base
{
friend std::hash<url_view>;
friend class url_view_base;
friend class params_base;
friend class params_encoded_base;
friend struct implementation_defined::origin_form_rule_t;
friend struct implementation_defined::uri_rule_t;
friend struct implementation_defined::relative_ref_rule_t;
friend struct implementation_defined::absolute_uri_rule_t;
using url_view_base::digest;
BOOST_URL_CXX14_CONSTEXPR
explicit
url_view(
detail::url_impl const& impl) noexcept
: url_view_base(impl)
{
}
public:
//--------------------------------------------
//
// Special Members
//
//--------------------------------------------
/** Destructor
Any params, segments, iterators, or
other views which reference the same
underlying character buffer remain
valid.
*/
~url_view() = default;
/** Constructor
Default constructed views refer to
a string with zero length, which
always remains valid. This matches
the grammar for a relative-ref with
an empty path and no query or
fragment.
@par Example
@code
url_view u;
@endcode
@par Postconditions
@code
this->empty() == true
@endcode
@par Complexity
Constant.
@par Exception Safety
Throws nothing.
@par BNF
@code
relative-ref = relative-part [ "?" query ] [ "#" fragment ]
@endcode
@par Specification
<a href="https://datatracker.ietf.org/doc/html/rfc3986#section-4.2"
>4.2. Relative Reference (rfc3986)</a>
*/
BOOST_URL_CXX14_CONSTEXPR
url_view() noexcept = default;
/** Constructor
This function constructs a URL from
the string `s`, which must contain a
valid <em>URI</em> or <em>relative-ref</em>
or else an exception is thrown. Upon
successful construction, the view
refers to the characters in the
buffer pointed to by `s`.
Ownership is not transferred; The caller
is responsible for ensuring that the
lifetime of the buffer extends until
it is no longer referenced.
@par Example
@code
url_view u( "http://www.example.com/index.htm" );
@endcode
@par Effects
@code
return parse_uri_reference( s ).value();
@endcode
@par Complexity
Linear in `s.size()`.
@par Exception Safety
Exceptions thrown on invalid input.
@throw system_error
The input failed to parse correctly.
@param s The string to parse.
@par BNF
@code
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
relative-ref = relative-part [ "?" query ] [ "#" fragment ]
@endcode
@par Specification
@li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-4.1"
>4.1. URI Reference</a>
@see
@ref parse_uri_reference.
*/
url_view(core::string_view s);
/// @copydoc url_view(core::string_view)
template<
class String
#ifndef BOOST_URL_DOCS
, class = typename std::enable_if<
std::is_convertible<
String,
core::string_view
>::value &&
!std::is_convertible<
String*,
url_view_base*
>::value
>::type
#endif
>
url_view(
String const& s)
: url_view(
detail::to_sv(s))
{
}
/** Constructor
After construction, both views
reference the same underlying character
buffer. Ownership is not transferred.
@par Postconditions
@code
this->buffer().data() == other.buffer().data()
@endcode
@par Complexity
Constant.
@par Exception Safety
Throws nothing.
@param other The other view.
*/
BOOST_URL_CXX14_CONSTEXPR
url_view(
url_view const& other) noexcept = default;
/** Move constructor
*/
BOOST_URL_CXX14_CONSTEXPR
url_view(
url_view&& other) noexcept = default;
/** Constructor
After construction, both views
reference the same underlying character
buffer. Ownership is not transferred.
@par Postconditions
@code
this->buffer().data() == other.buffer().data()
@endcode
@par Complexity
Constant.
@par Exception Safety
Throws nothing.
@param other The other view.
*/
BOOST_URL_CXX14_CONSTEXPR
url_view(
url_view_base const& other) noexcept
: url_view_base(other.impl_)
{
external_impl_ = other.external_impl_;
}
/** Assignment
After assignment, both views
reference the same underlying character
buffer. Ownership is not transferred.
@par Postconditions
@code
this->buffer().data() == other.buffer().data()
@endcode
@par Complexity
Constant.
@par Exception Safety
Throws nothing.
@param other The other view.
@return A reference to this object.
*/
BOOST_URL_CXX14_CONSTEXPR
url_view&
operator=(
url_view const& other) noexcept
{
impl_ = other.impl_;
external_impl_ = other.external_impl_;
return *this;
}
/** Assignment
After assignment, both views
reference the same underlying character
buffer. Ownership is not transferred.
@par Postconditions
@code
this->buffer().data() == other.buffer().data()
@endcode
@par Complexity
Constant.
@par Exception Safety
Throws nothing.
@param other The other view.
@return A reference to this object.
*/
BOOST_URL_CXX14_CONSTEXPR
url_view& operator=(
url_view_base const& other) noexcept
{
impl_ = other.impl_;
external_impl_ = other.external_impl_;
return *this;
}
//--------------------------------------------
//
// Observers
//
//--------------------------------------------
/** Return the maximum number of characters possible
This represents the largest number of
characters that are possible in a url,
not including any null terminator.
@par Complexity
Constant.
@par Exception Safety
Throws nothing.
@return The maximum number of characters possible.
*/
static
constexpr
std::size_t
max_size() noexcept
{
return BOOST_URL_MAX_SIZE;
}
};
} // urls
} // boost
//------------------------------------------------
// std::hash specialization
#ifndef BOOST_URL_DOCS
namespace std {
template<>
struct hash< ::boost::urls::url_view >
{
hash() = default;
hash(hash const&) = default;
hash& operator=(hash const&) = default;
explicit
hash(std::size_t salt) noexcept
: salt_(salt)
{
}
std::size_t
operator()(::boost::urls::url_view const& u) const noexcept
{
return u.digest(salt_);
}
private:
std::size_t salt_ = 0;
};
} // std
#endif
// When parse.hpp is being processed,
// it will include impl/url_view.hpp itself
// after declaring parse_uri_reference.
#if !defined(BOOST_URL_PARSE_HPP)
#include <boost/url/impl/url_view.hpp>
#endif
#endif