libs/url/doc/modules/ROOT/examples/unit/snippets.cpp

97.8% Lines (1125/1150) 93.1% Functions (54/58) 95.1% Branches (1360/1430)
libs/url/doc/modules/ROOT/examples/unit/snippets.cpp
Line Branch Hits Source Code
1 //
2 // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/url
8 //
9
10 #include "test_suite.hpp"
11
12 #include <boost/container/string.hpp>
13 #include <boost/filesystem/path.hpp>
14 #include <boost/core/ignore_unused.hpp>
15 #include <boost/core/detail/string_view.hpp>
16 #include <boost/url/string_view.hpp>
17 #include <boost/url/grammar/string_token.hpp>
18
19 // tag::snippet_headers_1[]
20 #include <boost/url.hpp>
21 // end::snippet_headers_1[]
22
23 #include <algorithm>
24 #include <iostream>
25 #include <sstream>
26 #include <vector>
27 #include <cctype>
28 #include <tuple>
29 #include <initializer_list>
30 #include <string>
31 #include <cstddef>
32 #include <type_traits>
33 #include <iterator>
34 #include <utility>
35
36 // tag::snippet_headers_3[]
37 #include <boost/url.hpp>
38 using namespace boost::urls;
39 // end::snippet_headers_3[]
40
41 #ifdef assert
42 #undef assert
43 #endif
44 #define assert BOOST_TEST
45
46 namespace {
47
48 namespace detail {
49
50 template<class It1, class It2, class Pred1, class Pred2>
51 bool
52 18 equal_range_impl(
53 It1 first1,
54 It1 last1,
55 It2 first2,
56 It2 last2,
57 Pred1 const& pred1,
58 Pred2 const& pred2)
59 {
60
5/6
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 44 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 44 times.
✓ Branch 7 taken 18 times.
62 for (; first1 != last1 && first2 != last2; ++first1, ++first2)
61 {
62
10/13
✓ Branch 1 taken 44 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 34 times.
✓ Branch 7 taken 10 times.
✓ Branch 8 taken 9 times.
✗ Branch 10 not taken.
✓ Branch 10 taken 5 times.
✓ Branch 11 taken 25 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 9 times.
✓ Branch 14 taken 5 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 5 times.
44 if (pred1(*first1) != pred2(*first2))
63 return false;
64 }
65
66
2/4
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
18 return first1 == last1 && first2 == last2;
67 }
68
69 } // detail
70
71 template<class R1, class R2, class Pred1, class Pred2>
72 bool
73 equal_range(R1&& r1, R2&& r2, Pred1 pred1, Pred2 pred2)
74 {
75 using std::begin;
76 using std::end;
77
78 return detail::equal_range_impl(
79 begin(r1),
80 end(r1),
81 begin(r2),
82 end(r2),
83 pred1,
84 pred2);
85 }
86
87 template<class R1, class T, class Pred1, class Pred2>
88 bool
89 18 equal_range(
90 R1&& r1,
91 std::initializer_list<T> r2,
92 Pred1 pred1,
93 Pred2 pred2)
94 {
95 using std::begin;
96 using std::end;
97
98 36 return detail::equal_range_impl(
99 begin(r1),
100 end(r1),
101 r2.begin(),
102 r2.end(),
103 pred1,
104 18 pred2);
105 }
106
107 template<class Range>
108 bool
109 11 equal_range(
110 Range const& rng,
111 std::initializer_list<const char*> expected)
112 {
113 typedef typename std::decay<
114 decltype(*std::declval<Range const&>().begin())>::type value_type;
115
116 auto project_range = [](value_type const& value)
117 {
118 return std::string(value.begin(), value.end());
119 };
120
121 auto project_expected = [](const char* value)
122 {
123 return std::string(value);
124 };
125
126
1/1
✓ Branch 1 taken 11 times.
11 return equal_range(
127 rng,
128 expected,
129 project_range,
130 22 project_expected);
131 }
132
133 template<class Range>
134 bool
135 4 equal_range(
136 Range const& rng,
137 std::initializer_list<param_view> expected)
138 {
139 typedef typename std::decay<
140 decltype(*std::declval<Range const&>().begin())>::type param_type;
141 typedef std::tuple<std::string, bool, std::string> param_tuple;
142
143 auto project_range = [](param_type const& param)
144 {
145 param_tuple result(
146 std::string(param.key.begin(), param.key.end()),
147 param.has_value,
148 std::string());
149 if (param.has_value)
150 {
151 std::get<2>(result) =
152 std::string(param.value.begin(), param.value.end());
153 }
154 return result;
155 };
156
157 auto project_expected = [](param_view const& param)
158 {
159 param_tuple result(
160 std::string(param.key.begin(), param.key.end()),
161 param.has_value,
162 std::string());
163 if (param.has_value)
164 {
165 std::get<2>(result) =
166 std::string(param.value.begin(), param.value.end());
167 }
168 return result;
169 };
170
171
1/1
✓ Branch 1 taken 4 times.
4 return equal_range(
172 rng,
173 expected,
174 project_range,
175 8 project_expected);
176 }
177
178 } // namespace
179
180
181 void
182 1 using_url_views()
183 {
184 {
185 // tag::snippet_accessing_1[]
186
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://user:pass@example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe+Jingleheimer%2DSchmidt#page%20anchor" );
187
1/1
✓ Branch 4 taken 1 time.
1 assert(u.scheme() == "https");
188
1/1
✓ Branch 5 taken 1 time.
1 assert(u.authority().buffer() == "user:pass@example.com:443");
189
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.userinfo() == "user:pass");
190
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.user() == "user");
191
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.password() == "pass");
192
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.host() == "example.com");
193
1/1
✓ Branch 4 taken 1 time.
1 assert(u.port() == "443");
194
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "/path/to/my-file.txt");
195
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.query() == "id=42&name=John Doe+Jingleheimer-Schmidt");
196
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.fragment() == "page anchor");
197 // end::snippet_accessing_1[]
198 }
199
200 // tag::code_urls_parsing_1[]
201 1 boost::core::string_view s = "https://user:pass@example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe+Jingleheimer%2DSchmidt#page%20anchor";
202 // end::code_urls_parsing_1[]
203
204 {
205 // tag::code_urls_parsing_2[]
206
1/1
✓ Branch 1 taken 1 time.
1 boost::system::result<url_view> r = parse_uri( s );
207 // end::code_urls_parsing_2[]
208 // tag::snippet_parsing_3[]
209
1/1
✓ Branch 2 taken 1 time.
1 url_view u = r.value();
210 // end::snippet_parsing_3[]
211 boost::ignore_unused(u);
212 }
213
214 {
215
1/1
✓ Branch 1 taken 1 time.
1 boost::system::result<url_view> r = parse_uri( s );
216 // tag::snippet_parsing_4[]
217 1 url_view u = *r;
218 // end::snippet_parsing_4[]
219 boost::ignore_unused(u);
220 }
221
222
1/1
✓ Branch 1 taken 1 time.
1 url_view u( s );
223 // tag::snippet_accessing_1b[]
224
3/3
✓ Branch 4 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 1 time.
4 for (auto seg: u.segments())
225
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 std::cout << seg << "\n";
226
1/1
✓ Branch 1 taken 1 time.
1 std::cout << "\n";
227
228
3/3
✓ Branch 4 taken 2 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 1 time.
3 for (auto param: u.params())
229
4/4
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
2 std::cout << param.key << ": " << param.value << "\n";
230
1/1
✓ Branch 1 taken 1 time.
1 std::cout << "\n";
231 // end::snippet_accessing_1b[]
232
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(equal_range(u.segments(), {"path", "to", "my-file.txt"}));
233 1 auto params = u.params();
234
2/2
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
1 BOOST_TEST(equal_range(
235 params,
236 {
237 param_view{"id", "42", true},
238 param_view{"name", "John Doe Jingleheimer-Schmidt", true}
239 }));
240
241 {
242 // tag::snippet_token_1[]
243
1/1
✓ Branch 2 taken 1 time.
1 std::string h = u.host();
244
1/1
✓ Branch 2 taken 1 time.
1 assert(h == "example.com");
245 // end::snippet_token_1[]
246 boost::ignore_unused(h);
247 1 }
248
249 {
250 // tag::snippet_token_2[]
251
1/1
✓ Branch 1 taken 1 time.
1 std::string h = "host: ";
252
1/1
✓ Branch 2 taken 1 time.
1 u.host(string_token::append_to(h));
253
1/1
✓ Branch 2 taken 1 time.
1 assert(h == "host: example.com");
254 // end::snippet_token_2[]
255 boost::ignore_unused(h);
256 1 }
257
258 {
259 // tag::snippet_accessing_2a[]
260
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url_view u1 = parse_uri( "http://www.example.com" ).value();
261
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u1.fragment().empty());
262
1/1
✓ Branch 2 taken 1 time.
1 assert(!u1.has_fragment());
263 // end::snippet_accessing_2a[]
264 boost::ignore_unused(u1);
265 }
266
267 {
268 // tag::snippet_accessing_2b[]
269
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url_view u2 = parse_uri( "http://www.example.com/#" ).value();
270
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u2.fragment().empty());
271
1/1
✓ Branch 2 taken 1 time.
1 assert(u2.has_fragment());
272 // end::snippet_accessing_2b[]
273 }
274
275 {
276 // tag::snippet_accessing_3a[]
277
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url_view u1 = parse_uri( "http://www.example.com" ).value();
278
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
1 std::cout << "has fragment 1 : " << u1.has_fragment() << "\n";
279
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 std::cout << "fragment 1 : " << u1.fragment() << "\n\n";
280 // end::snippet_accessing_3a[]
281
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u1.has_fragment());
282
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u1.fragment().empty());
283
284 // tag::snippet_accessing_3b[]
285
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url_view u2 = parse_uri( "http://www.example.com/#" ).value();
286
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
1 std::cout << "has fragment 2 : " << u2.has_fragment() << "\n";
287
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 std::cout << "fragment 2 : " << u2.fragment() << "\n\n";
288 // end::snippet_accessing_3b[]
289
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u2.has_fragment());
290
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u2.fragment().empty());
291 }
292
293 {
294 // tag::snippet_accessing_4[]
295 std::cout <<
296
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 "url : " << u << "\n"
297 "scheme : " << u.scheme() << "\n"
298
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 12 taken 1 time.
1 "authority : " << u.encoded_authority() << "\n"
299
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "userinfo : " << u.encoded_userinfo() << "\n"
300
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "user : " << u.encoded_user() << "\n"
301
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "password : " << u.encoded_password() << "\n"
302
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "host : " << u.encoded_host() << "\n"
303 "port : " << u.port() << "\n"
304
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 12 taken 1 time.
1 "path : " << u.encoded_path() << "\n"
305
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "query : " << u.encoded_query() << "\n"
306
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
1 "fragment : " << u.encoded_fragment() << "\n";
307 // end::snippet_accessing_4[]
308
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.scheme() == "https");
309
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_authority() == "user:pass@example.com:443");
310
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_userinfo() == "user:pass");
311
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_user() == "user");
312
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_password() == "pass");
313
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_host() == "example.com");
314
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.port() == "443");
315
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_path() == "/path/to/my%2dfile.txt");
316
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_query() == "id=42&name=John%20Doe+Jingleheimer%2DSchmidt");
317
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_fragment() == "page%20anchor");
318 }
319
320 {
321 // tag::snippet_decoding_1[]
322
1/1
✓ Branch 2 taken 1 time.
1 decode_view dv("id=42&name=John%20Doe%20Jingleheimer%2DSchmidt");
323
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::cout << dv << "\n";
324 // end::snippet_decoding_1[]
325
2/3
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 time.
1 BOOST_TEST(dv == "id=42&name=John Doe Jingleheimer-Schmidt");
326 }
327 {
328
1/1
✓ Branch 1 taken 1 time.
1 url u1 = u;
329
1/1
✓ Branch 1 taken 1 time.
1 url u2 = u;
330 // tag::snippet_decoding_2[]
331
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 u1.set_host(u2.host());
332 // end::snippet_decoding_2[]
333
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::cout << u1 << "\n";
334
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u1.buffer() == u.buffer());
335 1 }
336 {
337 // tag::snippet_decoding_3[]
338 1 boost::filesystem::path p;
339
3/3
✓ Branch 4 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 1 time.
4 for (auto seg: u.segments())
340 6 p.append(seg.begin(), seg.end());
341
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
1 std::cout << "path: " << p << "\n";
342 // end::snippet_decoding_3[]
343
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(p.generic_string() == "path/to/my-file.txt");
344 1 }
345 // transparent std::equal_to<> required
346 #if BOOST_CXX_VERSION >= 201402L && !defined(BOOST_CLANG)
347 {
348 auto handle_route = [](
349 std::vector<std::string> const&,
350 url_view)
351 {};
352
353 // tag::snippet_decoding_4a[]
354 auto match = [](
355 std::vector<std::string> const& route,
356 url_view u)
357 {
358 auto segs = u.segments();
359 if (route.size() != segs.size())
360 return false;
361 return std::equal(
362 route.begin(),
363 route.end(),
364 segs.begin());
365 };
366 // end::snippet_decoding_4a[]
367 // tag::snippet_decoding_4b[]
368 std::vector<std::string> route =
369 {"community", "reviews.html"};
370 if (match(route, u))
371 {
372 handle_route(route, u);
373 }
374 // end::snippet_decoding_4b[]
375 }
376 #endif
377 {
378 // tag::snippet_compound_elements_0[]
379 1 segments_encoded_view segs_encoded = u.encoded_segments();
380
381
2/2
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 1 time.
4 for( auto v : segs_encoded )
382 {
383
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 std::cout << v << "\n";
384 }
385 // end::snippet_compound_elements_0[]
386
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 BOOST_TEST(equal_range(
387 segs_encoded,
388 {"path", "to", "my%2dfile.txt"}));
389 }
390
391 {
392 // tag::snippet_compound_elements_0b[]
393 1 segments_encoded_view segs_encoded = u.encoded_segments();
394
395
2/2
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 1 time.
4 for( pct_string_view v : segs_encoded )
396 {
397 3 decode_view dv = *v;
398
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 std::cout << dv << "\n";
399 }
400 // end::snippet_compound_elements_0b[]
401 3 auto decode_segment = [](pct_string_view const& value)
402 {
403 3 decode_view dv = *value;
404
1/1
✓ Branch 3 taken 3 times.
6 return std::string(dv.begin(), dv.end());
405 };
406 3 auto literal = [](const char* value)
407 {
408
1/1
✓ Branch 1 taken 3 times.
6 return std::string(value);
409 };
410
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 BOOST_TEST(equal_range(
411 segs_encoded,
412 {"path", "to", "my-file.txt"},
413 decode_segment,
414 literal));
415 }
416
417 {
418 // tag::snippet_compound_elements_1[]
419 1 segments_encoded_view segs_encoded = u.encoded_segments();
420
2/2
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 1 time.
4 for( auto v : segs_encoded )
421 {
422
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 std::cout << v << "\n";
423 }
424 // end::snippet_compound_elements_1[]
425
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 BOOST_TEST(equal_range(
426 segs_encoded,
427 {"path", "to", "my%2dfile.txt"}));
428 }
429
430 {
431 // tag::snippet_encoded_compound_elements_1[]
432 1 segments_encoded_view segs_encoded = u.encoded_segments();
433
434
2/2
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 1 time.
4 for( pct_string_view v : segs_encoded )
435 {
436 3 decode_view dv = *v;
437
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 std::cout << dv << "\n";
438 }
439 // end::snippet_encoded_compound_elements_1[]
440 3 auto decode_segment = [](pct_string_view const& value)
441 {
442 3 decode_view dv = *value;
443
1/1
✓ Branch 3 taken 3 times.
6 return std::string(dv.begin(), dv.end());
444 };
445 3 auto literal = [](const char* value)
446 {
447
1/1
✓ Branch 1 taken 3 times.
6 return std::string(value);
448 };
449
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 BOOST_TEST(equal_range(
450 segs_encoded,
451 {"path", "to", "my-file.txt"},
452 decode_segment,
453 literal));
454 }
455
456 {
457 // tag::snippet_encoded_compound_elements_2[]
458 1 params_encoded_view params_ref_view = u.encoded_params();
459
460
2/2
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1 time.
3 for( auto v : params_ref_view )
461 {
462 2 decode_view dk(v.key);
463 2 decode_view dv(v.value);
464
465 std::cout <<
466
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 "key = " << dk <<
467
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
2 ", value = " << dv << "\n";
468 }
469 // end::snippet_encoded_compound_elements_2[]
470 2 auto decode_param = [](param_pct_view const& param)
471 {
472 2 decode_view dk(param.key);
473 std::tuple<std::string, bool, std::string> result(
474
1/1
✓ Branch 3 taken 2 times.
4 std::string(dk.begin(), dk.end()),
475 2 param.has_value,
476 6 std::string());
477
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (param.has_value)
478 {
479 2 decode_view dv(param.value);
480 2 std::get<2>(result) =
481
1/1
✓ Branch 3 taken 2 times.
6 std::string(dv.begin(), dv.end());
482 }
483 4 return result;
484 };
485 2 auto expect_param_tuple = [](param_view const& param)
486 {
487 std::tuple<std::string, bool, std::string> result(
488
1/1
✓ Branch 3 taken 2 times.
4 std::string(param.key.begin(), param.key.end()),
489 2 param.has_value,
490 6 std::string());
491
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (param.has_value)
492 {
493 2 std::get<2>(result) =
494
1/1
✓ Branch 3 taken 2 times.
6 std::string(param.value.begin(), param.value.end());
495 }
496 2 return result;
497 };
498
2/2
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
1 BOOST_TEST(equal_range(
499 params_ref_view,
500 {
501 param_view{"id", "42", true},
502 param_view{"name", "John Doe+Jingleheimer-Schmidt", true}
503 },
504 decode_param,
505 expect_param_tuple));
506 }
507 1 }
508
509 void
510 1 using_urls()
511 {
512 1 boost::core::string_view s = "https://user:pass@www.example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe#page%20anchor";
513
514 // tag::snippet_quicklook_modifying_1[]
515
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
1 url u = parse_uri( s ).value();
516 // end::snippet_quicklook_modifying_1[]
517
518 // tag::snippet_quicklook_modifying_1b[]
519
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
1 static_url<1024> su = parse_uri( s ).value();
520 // end::snippet_quicklook_modifying_1b[]
521 (void)su;
522
523 // tag::snippet_quicklook_modifying_2[]
524
1/1
✓ Branch 2 taken 1 time.
1 u.set_scheme( "https" );
525 // end::snippet_quicklook_modifying_2[]
526
527 // tag::snippet_quicklook_modifying_3[]
528
1/1
✓ Branch 1 taken 1 time.
1 u.set_scheme_id( scheme::https ); // equivalent to u.set_scheme( "https" );
529 // end::snippet_quicklook_modifying_3[]
530
531 // tag::snippet_quicklook_modifying_4[]
532
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
1 u.set_host_ipv4( ipv4_address( "192.168.0.1" ) )
533
1/1
✓ Branch 1 taken 1 time.
1 .set_port_number( 8080 )
534 1 .remove_userinfo();
535
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::cout << u << "\n";
536 // end::snippet_quicklook_modifying_4[]
537
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.buffer() == "https://192.168.0.1:8080/path/to/my%2dfile.txt?id=42&name=John%20Doe#page%20anchor");
538
539 // tag::snippet_quicklook_modifying_5[]
540 1 params_ref p = u.params();
541
1/1
✓ Branch 6 taken 1 time.
1 p.replace(p.find("name"), {"name", "John Doe"});
542
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::cout << u << "\n";
543 // end::snippet_quicklook_modifying_5[]
544
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.buffer() == "https://192.168.0.1:8080/path/to/my%2dfile.txt?id=42&name=John+Doe#page%20anchor");
545 1 }
546
547 void
548 1 parsing_urls()
549 {
550 {
551 auto handle_error = [](boost::system::error_code e)
552 {
553 boost::ignore_unused(e);
554 };
555 // tag::snippet_parsing_url_1[]
556
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
1 boost::system::result< url > ru = parse_uri_reference( "https://www.example.com/path/to/file.txt" );
557
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if ( ru )
558 {
559
1/1
✓ Branch 2 taken 1 time.
1 url u = *ru;
560
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 assert(u.encoded_path() == "/path/to/file.txt");
561 1 }
562 else
563 {
564 boost::system::error_code e = ru.error();
565 handle_error(e);
566 }
567 // end::snippet_parsing_url_1[]
568 1 }
569
570 {
571 auto handle_error = [](boost::system::system_error& e)
572 {
573 boost::ignore_unused(e);
574 };
575 // tag::snippet_parsing_url_1b[]
576 try
577 {
578
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 url u = parse_uri_reference( "https://www.example.com/path/to/file.txt" ).value();
579
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 assert(u.encoded_path() == "/path/to/file.txt");
580 1 }
581 catch (boost::system::system_error &e)
582 {
583 handle_error(e);
584 }
585 // end::snippet_parsing_url_1b[]
586 }
587
588 {
589 // tag::snippet_parsing_url_1b2[]
590
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
1 boost::system::result< url > ru = parse_uri_reference( "https://www.example.com/path/to/file.txt" );
591
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if ( ru.has_value() )
592 {
593
1/1
✓ Branch 2 taken 1 time.
1 url u = *ru;
594
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 assert(u.encoded_path() == "/path/to/file.txt");
595 1 }
596 // end::snippet_parsing_url_1b2[]
597 boost::ignore_unused(ru);
598 1 }
599
600 {
601 // tag::snippet_parsing_url_1bb[]
602
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 url u = parse_uri_reference( "https://www.example.com/path/to/file.txt" ).value();
603
604
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 assert(u.encoded_path() == "/path/to/file.txt");
605 // end::snippet_parsing_url_1bb[]
606 boost::ignore_unused(u);
607 1 }
608
609 {
610 // tag::snippet_parsing_url_1bc[]
611
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
1 boost::system::result< url > rv = parse_uri_reference( "https://www.example.com/path/to/file.txt" );
612
613 static_assert( std::is_convertible< boost::system::result< url_view >, boost::system::result< url > >::value, "" );
614 // end::snippet_parsing_url_1bc[]
615 boost::ignore_unused(rv);
616 1 }
617
618 {
619 // tag::snippet_parsing_url_1bd[]
620
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
1 boost::system::result< static_url<1024> > rv = parse_uri_reference( "https://www.example.com/path/to/file.txt" );
621
622 static_assert( std::is_convertible< boost::system::result< static_url<1024> >, boost::system::result< url > >::value, "" );
623 // end::snippet_parsing_url_1bd[]
624 boost::ignore_unused(rv);
625 1 }
626
627 {
628 // tag::snippet_parsing_url_1c[]
629
1/1
✓ Branch 2 taken 1 time.
1 boost::system::result< url_view > r0 = parse_relative_ref( "/path/to/file.txt" );
630
1/1
✓ Branch 2 taken 1 time.
1 assert( r0.has_value() );
631 // end::snippet_parsing_url_1c[]
632 // tag::snippet_parsing_url_1d[]
633
1/1
✓ Branch 2 taken 1 time.
1 boost::system::result< url_view > r1 = parse_uri( "https://www.example.com" );
634
1/1
✓ Branch 2 taken 1 time.
1 assert( r1.has_value() );
635 1 url dest;
636
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 resolve(r1.value(), r0.value(), dest);
637
1/1
✓ Branch 4 taken 1 time.
1 assert(dest.buffer() == "https://www.example.com/path/to/file.txt");
638 // end::snippet_parsing_url_1d[]
639 boost::ignore_unused(dest);
640 1 }
641
642 // tag::snippet_parsing_url_2[]
643 // This will hold our copy
644 1 std::shared_ptr<url_view const> sp;
645 {
646
1/1
✓ Branch 1 taken 1 time.
1 std::string s = "/path/to/file.txt";
647
648 // result::value() will throw an exception if an error occurs
649
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url_view u = parse_relative_ref( s ).value();
650
651 // create a copy with ownership and string lifetime extension
652
1/1
✓ Branch 1 taken 1 time.
1 sp = u.persist();
653
654 // At this point the string goes out of scope
655 1 }
656
657 // but `*sp` remains valid since it has its own copy
658
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
1 std::cout << *sp << "\n";
659 // end::snippet_parsing_url_2[]
660
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(sp);
661
1/1
✓ Branch 5 taken 1 time.
1 BOOST_TEST(sp->buffer() == "/path/to/file.txt");
662
663 {
664 // tag::snippet_parsing_url_3[]
665 // This will hold our mutable copy
666 1 url v;
667 {
668
1/1
✓ Branch 1 taken 1 time.
1 std::string s = "/path/to/file.txt";
669
670 // result::value() will throw an exception if an error occurs
671
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 v = parse_relative_ref(s).value();
672
673 // At this point the string goes out of scope
674 1 }
675
676 // but `v` remains valid since it has its own copy
677
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::cout << v << "\n";
678
679 // and it's mutable
680
1/1
✓ Branch 2 taken 1 time.
1 v.set_fragment("anchor");
681
682 // path/to/file.txt#anchor
683
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::cout << v << "\n";
684 // end::snippet_parsing_url_3[]
685
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(v.buffer() == "/path/to/file.txt#anchor");
686 1 }
687 1 }
688
689 void
690 1 parsing_components()
691 {
692 {
693 // tag::snippet_components_2a[]
694
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://www.ietf.org/rfc/rfc2396.txt" );
695
1/1
✓ Branch 4 taken 1 time.
1 assert(u.scheme() == "https");
696
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.host() == "www.ietf.org");
697
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "/rfc/rfc2396.txt");
698 // end::snippet_components_2a[]
699 }
700 {
701 // tag::snippet_components_2b[]
702
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "ftp://ftp.is.co.za/rfc/rfc1808.txt" );
703
1/1
✓ Branch 4 taken 1 time.
1 assert(u.scheme() == "ftp");
704
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.host() == "ftp.is.co.za");
705
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "/rfc/rfc1808.txt");
706 // end::snippet_components_2b[]
707 }
708 {
709 // tag::snippet_components_2c[]
710
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "mailto:John.Doe@example.com" );
711
1/1
✓ Branch 4 taken 1 time.
1 assert(u.scheme() == "mailto");
712
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "John.Doe@example.com");
713 // end::snippet_components_2c[]
714 }
715 {
716 // tag::snippet_components_2d[]
717
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "urn:isbn:096139210x" );
718
1/1
✓ Branch 4 taken 1 time.
1 assert(u.scheme() == "urn");
719
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "isbn:096139210x");
720 // end::snippet_components_2d[]
721 }
722 {
723 // tag::snippet_components_2e[]
724
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36" );
725
1/1
✓ Branch 4 taken 1 time.
1 assert(u.scheme() == "magnet");
726
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "");
727
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.query() == "xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36");
728 // end::snippet_components_2e[]
729 }
730 1 }
731
732 void
733 1 formatting_components()
734 {
735 // I have spent a lot of time on this and have no
736 // idea how to fix this bug in GCC 4.8 and GCC 5.0
737 // without help from the pros.
738 #if !BOOST_WORKAROUND( BOOST_GCC_VERSION, < 60000 )
739 {
740 // tag::snippet_format_1[]
741
1/1
✓ Branch 2 taken 1 time.
1 url u = format("{}://{}:{}/rfc/{}", "https", "www.ietf.org", 80, "rfc2396.txt");
742
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://www.ietf.org:80/rfc/rfc2396.txt");
743 // end::snippet_format_1[]
744 1 }
745
746 {
747 // tag::snippet_format_2[]
748
1/1
✓ Branch 2 taken 1 time.
1 url u = format("https://{}/{}", "www.boost.org", "Hello world!");
749
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://www.boost.org/Hello%20world!");
750 // end::snippet_format_2[]
751 1 }
752
753 {
754 // tag::snippet_format_3a[]
755
1/1
✓ Branch 2 taken 1 time.
1 url u = format("{}:{}", "mailto", "someone@example.com");
756
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "mailto:someone@example.com");
757
1/1
✓ Branch 4 taken 1 time.
1 assert(u.scheme() == "mailto");
758
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "someone@example.com");
759 // end::snippet_format_3a[]
760 1 }
761 {
762 // tag::snippet_format_3b[]
763
1/1
✓ Branch 2 taken 1 time.
1 url u = format("{}{}", "mailto:", "someone@example.com");
764
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "mailto%3Asomeone@example.com");
765
1/1
✓ Branch 2 taken 1 time.
1 assert(!u.has_scheme());
766
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "mailto:someone@example.com");
767
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 assert(u.encoded_path() == "mailto%3Asomeone@example.com");
768 // end::snippet_format_3b[]
769 1 }
770
771 {
772 // tag::snippet_format_4[]
773 1 static_url<50> u;
774
1/1
✓ Branch 2 taken 1 time.
1 format_to(u, "{}://{}:{}/rfc/{}", "https", "www.ietf.org", 80, "rfc2396.txt");
775
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://www.ietf.org:80/rfc/rfc2396.txt");
776 // end::snippet_format_4[]
777 1 }
778
779 {
780 // tag::snippet_format_5a[]
781
1/1
✓ Branch 2 taken 1 time.
1 url u = format("{0}://{2}:{1}/{3}{4}{3}", "https", 80, "www.ietf.org", "abra", "cad");
782
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://www.ietf.org:80/abracadabra");
783 // end::snippet_format_5a[]
784 1 }
785 {
786 // tag::snippet_format_5b[]
787
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url u = format("https://example.com/~{username}", arg("username", "mark"));
788
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://example.com/~mark");
789 // end::snippet_format_5b[]
790 1 }
791
792 {
793 // tag::snippet_format_5c[]
794 1 boost::core::string_view fmt = "{scheme}://{host}:{port}/{dir}/{file}";
795
6/6
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 14 taken 1 time.
✓ Branch 18 taken 1 time.
✓ Branch 21 taken 1 time.
1 url u = format(fmt, {{"scheme", "https"}, {"port", 80}, {"host", "example.com"}, {"dir", "path/to"}, {"file", "file.txt"}});
796
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://example.com:80/path/to/file.txt");
797 // end::snippet_format_5c[]
798 1 }
799 #endif
800 1 }
801
802 void
803 1 parsing_scheme()
804 {
805 {
806 // tag::snippet_parsing_scheme_1[]
807
1/1
✓ Branch 1 taken 1 time.
1 url_view u("mailto:name@email.com" );
808
1/1
✓ Branch 2 taken 1 time.
1 assert( u.has_scheme() );
809
1/1
✓ Branch 4 taken 1 time.
1 assert( u.scheme() == "mailto" );
810 // end::snippet_parsing_scheme_1[]
811 boost::ignore_unused(u);
812 }
813 {
814 // tag::snippet_parsing_scheme_3[]
815
1/1
✓ Branch 1 taken 1 time.
1 url_view u("file://host/path/to/file" );
816
1/1
✓ Branch 2 taken 1 time.
1 assert( u.scheme_id() == scheme::file );
817 // end::snippet_parsing_scheme_3[]
818 boost::ignore_unused(u);
819 }
820 1 }
821
822 // tag::code_compound_scheme_1[]
823 // Helper function to extract transport scheme from compound schemes
824 boost::core::string_view
825 4 scheme_ex(boost::core::string_view s)
826 {
827 // Find the last '+' in the scheme
828 // Examples: "git+https" -> "https", "svn+ssh" -> "ssh"
829 4 auto pos = s.rfind('+');
830
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
4 if (pos != boost::core::string_view::npos)
831 3 return s.substr(pos + 1);
832 1 return {};
833 }
834 // end::code_compound_scheme_1[]
835
836 void
837 1 parsing_compound_scheme()
838 {
839
840 // Parse a URL with a compound scheme
841
1/1
✓ Branch 1 taken 1 time.
1 url_view u("git+https://github.com/user/repo.git");
842
843 // The library treats the entire string as a single scheme per RFC 3986
844
1/1
✓ Branch 4 taken 1 time.
1 assert(u.scheme() == "git+https");
845
846 // Extract just the transport protocol suffix
847
1/1
✓ Branch 2 taken 1 time.
1 boost::core::string_view transport = scheme_ex(u.scheme());
848
1/1
✓ Branch 3 taken 1 time.
1 assert(transport == "https");
849
850 // Test with other compound schemes
851
1/1
✓ Branch 1 taken 1 time.
1 url_view u2("svn+ssh://example.com/repo");
852
1/1
✓ Branch 4 taken 1 time.
1 assert(u2.scheme() == "svn+ssh");
853
2/2
✓ Branch 3 taken 1 time.
✓ Branch 7 taken 1 time.
1 assert(scheme_ex(u2.scheme()) == "ssh");
854
855 // Regular schemes without '+' return empty
856
1/1
✓ Branch 1 taken 1 time.
1 url_view u3("https://example.com");
857
1/1
✓ Branch 4 taken 1 time.
1 assert(u3.scheme() == "https");
858
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(scheme_ex(u3.scheme()).empty());
859
860 // Multiple '+' characters: rfind returns the last one
861
1/1
✓ Branch 1 taken 1 time.
1 url_view u4("npm+http+custom://example.com");
862
1/1
✓ Branch 4 taken 1 time.
1 assert(u4.scheme() == "npm+http+custom");
863
2/2
✓ Branch 3 taken 1 time.
✓ Branch 7 taken 1 time.
1 assert(scheme_ex(u4.scheme()) == "custom");
864
865 boost::ignore_unused(u, u2, u3, u4, transport);
866 1 }
867
868 void
869 1 parsing_authority()
870 {
871 {
872 // tag::snippet_parsing_authority_1[]
873
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https:///path/to_resource" );
874
1/1
✓ Branch 5 taken 1 time.
1 assert( u.authority().buffer() == "");
875
1/1
✓ Branch 2 taken 1 time.
1 assert( u.has_authority() );
876
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( u.path() == "/path/to_resource" );
877 // end::snippet_parsing_authority_1[]
878 }
879 {
880 // tag::snippet_parsing_authority_2[]
881
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org" );
882 std::cout << "scheme: " << u.scheme() << "\n"
883
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 12 taken 1 time.
1 "has authority: " << u.has_authority() << "\n"
884
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "authority: " << u.authority() << "\n"
885
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 "path: " << u.path() << "\n";
886 // end::snippet_parsing_authority_2[]
887
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.scheme() == "https");
888
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.has_authority());
889
1/1
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.authority().buffer() == "www.boost.org");
890
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.path().empty());
891 }
892 {
893 // tag::snippet_parsing_authority_3a[]
894
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://www.boost.org/users/download/" );
895
1/1
✓ Branch 2 taken 1 time.
1 assert(u.has_authority());
896 1 authority_view a = u.authority();
897 // end::snippet_parsing_authority_3a[]
898 // tag::snippet_parsing_authority_3b[]
899
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(a.host() == "www.boost.org");
900 // end::snippet_parsing_authority_3b[]
901 1 }
902 {
903 // tag::snippet_parsing_authority_4[]
904
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://www.boost.org/" );
905 std::cout << "scheme: " << u.scheme() << "\n"
906
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 12 taken 1 time.
1 "has authority: " << u.has_authority() << "\n"
907
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "authority: " << u.authority() << "\n"
908
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 "path: " << u.path() << "\n";
909 // end::snippet_parsing_authority_4[]
910
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.scheme() == "https");
911
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.has_authority());
912
1/1
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.authority().buffer() == "www.boost.org");
913
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.path() == "/");
914 }
915 {
916 // tag::snippet_parsing_authority_5[]
917
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "mailto:John.Doe@example.com" );
918 std::cout << "scheme: " << u.scheme() << "\n"
919
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 12 taken 1 time.
1 "has authority: " << u.has_authority() << "\n"
920
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "authority: " << u.authority() << "\n"
921
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 "path: " << u.path() << "\n";
922 // end::snippet_parsing_authority_5[]
923
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.scheme() == "mailto");
924
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.has_authority());
925
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.authority().buffer().empty());
926
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.path() == "John.Doe@example.com");
927 }
928 {
929 // tag::snippet_parsing_authority_6[]
930
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "mailto://John.Doe@example.com" );
931
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
932 "scheme: " << u.scheme() << "\n"
933
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 12 taken 1 time.
1 "has authority: " << u.has_authority() << "\n"
934
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "authority: " << u.authority() << "\n"
935
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 "path: " << u.path() << "\n";
936 // end::snippet_parsing_authority_6[]
937
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.scheme() == "mailto");
938
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.has_authority());
939
1/1
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.authority().buffer() == "John.Doe@example.com");
940
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.path().empty());
941 }
942 {
943 // tag::snippet_parsing_authority_7[]
944
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://john.doe@www.example.com:123/forum/questions/" );
945 std::cout << "scheme: " << u.scheme() << "\n"
946
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 12 taken 1 time.
1 "has authority: " << u.has_authority() << "\n"
947
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "authority: " << u.authority() << "\n"
948
1/1
✓ Branch 1 taken 1 time.
2 "host: " << u.host() << "\n"
949
1/1
✓ Branch 1 taken 1 time.
2 "userinfo: " << u.userinfo() << "\n"
950 "port: " << u.port() << "\n"
951
10/10
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 12 taken 1 time.
✓ Branch 15 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 22 taken 1 time.
✓ Branch 26 taken 1 time.
✓ Branch 29 taken 1 time.
✓ Branch 32 taken 1 time.
3 "path: " << u.path() << "\n";
952 // end::snippet_parsing_authority_7[]
953
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.scheme() == "https");
954
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.has_authority());
955
1/1
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.authority().buffer() == "john.doe@www.example.com:123");
956
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.host() == "www.example.com");
957
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.userinfo() == "john.doe");
958
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.port() == "123");
959
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.path() == "/forum/questions/");
960 }
961 {
962 // tag::snippet_parsing_authority_8[]
963
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://john.doe@www.example.com:123/forum/questions/" );
964
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.host() == "www.example.com");
965
1/1
✓ Branch 4 taken 1 time.
1 assert(u.port() == "123");
966 // end::snippet_parsing_authority_8[]
967 }
968 {
969 // tag::snippet_parsing_authority_9[]
970
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://john.doe@192.168.2.1:123/forum/questions/" );
971
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.host() == "192.168.2.1");
972
1/1
✓ Branch 4 taken 1 time.
1 assert(u.port() == "123");
973 // end::snippet_parsing_authority_9[]
974 }
975 {
976 // tag::snippet_parsing_authority_9b[]
977
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://www.example.com" );
978
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.host() == "www.example.com");
979
3/4
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 9 taken 1 time.
1 assert(u.host() == u.encoded_host());
980 // end::snippet_parsing_authority_9b[]
981 }
982 {
983 struct resolve_f {
984 boost::core::string_view
985 1 operator()(boost::core::string_view h)
986 {
987 1 return h;
988 }
989 } resolve;
990 struct write_request_f {
991 1 void operator()(boost::core::string_view) {}
992 void operator()(ipv4_address) {}
993 void operator()(ipv6_address) {}
994 } write_request;
995
996 // tag::snippet_parsing_authority_10[]
997
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://www.boost.org/users/download/" );
998
1/4
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 switch (u.host_type())
999 {
1000 1 case host_type::name:
1001
1/1
✓ Branch 2 taken 1 time.
1 write_request(resolve(u.host()));
1002 1 break;
1003 case host_type::ipv4:
1004 write_request(u.host_ipv4_address());
1005 break;
1006 case host_type::ipv6:
1007 write_request(u.host_ipv6_address());
1008 break;
1009 default:
1010 break;
1011 }
1012 // end::snippet_parsing_authority_10[]
1013 }
1014 {
1015 // tag::snippet_parsing_authority_10a[]
1016
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https:///path/to_resource" );
1017
1/1
✓ Branch 2 taken 1 time.
1 assert( u.has_authority() );
1018
1/1
✓ Branch 4 taken 1 time.
1 assert( u.authority().buffer().empty() );
1019
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( u.path() == "/path/to_resource" );
1020 // end::snippet_parsing_authority_10a[]
1021 }
1022 {
1023 // tag::snippet_parsing_authority_10b[]
1024
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://www.boost.org" );
1025
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( u.host() == "www.boost.org" );
1026
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( u.path().empty() );
1027 // end::snippet_parsing_authority_10b[]
1028 }
1029 {
1030 // tag::snippet_parsing_authority_10c[]
1031
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://www.boost.org/users/download/" );
1032
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( u.host() == "www.boost.org" );
1033
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( u.path() == "/users/download/" );
1034 // end::snippet_parsing_authority_10c[]
1035 }
1036 {
1037 // tag::snippet_parsing_authority_10d[]
1038
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://www.boost.org/" );
1039
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( u.host() == "www.boost.org" );
1040
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( u.path() == "/" );
1041 // end::snippet_parsing_authority_10d[]
1042 }
1043 {
1044 // tag::snippet_parsing_authority_10e[]
1045
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "mailto:John.Doe@example.com" );
1046
1/1
✓ Branch 2 taken 1 time.
1 assert( !u.has_authority() );
1047
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( u.path() == "John.Doe@example.com" );
1048 // end::snippet_parsing_authority_10e[]
1049 }
1050 {
1051 // tag::snippet_parsing_authority_10f[]
1052
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "mailto://John.Doe@example.com" );
1053
1/1
✓ Branch 5 taken 1 time.
1 assert( u.authority().buffer() == "John.Doe@example.com" );
1054
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( u.path().empty() );
1055 // end::snippet_parsing_authority_10f[]
1056 }
1057 {
1058 // tag::snippet_parsing_authority_11a[]
1059
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://john.doe@www.example.com:123/forum/questions/" );
1060
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.userinfo() == "john.doe");
1061
1/1
✓ Branch 4 taken 1 time.
1 assert(u.port() == "123");
1062 // end::snippet_parsing_authority_11a[]
1063 }
1064 {
1065 // tag::snippet_parsing_authority_11b[]
1066
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://john:doe@www.somehost.com/forum/questions/" );
1067
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.userinfo() == "john:doe");
1068
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.user() == "john");
1069
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.password() == "doe");
1070 // end::snippet_parsing_authority_11b[]
1071 }
1072 {
1073 // tag::snippet_parsing_authority_12[]
1074
1/1
✓ Branch 4 taken 1 time.
1 authority_view a = parse_authority( "www.example.com:80" ).value();
1075
1/1
✓ Branch 2 taken 1 time.
1 assert(!a.has_userinfo());
1076
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(a.host() == "www.example.com");
1077
1/1
✓ Branch 4 taken 1 time.
1 assert(a.port() == "80");
1078 // end::snippet_parsing_authority_12[]
1079 1 }
1080 {
1081 // tag::snippet_parsing_authority_13[]
1082
1/1
✓ Branch 4 taken 1 time.
1 authority_view a = parse_authority( "user:pass@www.example.com:443" ).value();
1083
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(a.userinfo() == "user:pass");
1084
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(a.user() == "user");
1085
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(a.password() == "pass");
1086
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(a.host() == "www.example.com");
1087
1/1
✓ Branch 4 taken 1 time.
1 assert(a.port() == "443");
1088 // end::snippet_parsing_authority_13[]
1089 1 }
1090 1 }
1091
1092 void
1093 1 parsing_path()
1094 {
1095 {
1096 // tag::snippet_parsing_path_0[]
1097
1/1
✓ Branch 1 taken 1 time.
1 url_view u("http://www.example.com/path/to/file.txt");
1098
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "/path/to/file.txt");
1099 1 segments_view segs = u.segments();
1100 1 auto it = segs.begin();
1101
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( *it++ == "path" );
1102
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( *it++ == "to" );
1103
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert( *it == "file.txt" );
1104 // end::snippet_parsing_path_0[]
1105 boost::ignore_unused(it);
1106 }
1107 {
1108 // tag::snippet_parsing_path_1[]
1109
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org/doc/the%20libs/");
1110
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "/doc/the libs/");
1111
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 assert(u.encoded_path() == "/doc/the%20libs/");
1112 // end::snippet_parsing_path_1[]
1113
1114 // tag::snippet_parsing_path_1_b[]
1115
2/2
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
1 std::cout << u.encoded_segments().size() << " segments\n";
1116
2/2
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 1 time.
4 for (auto seg: u.encoded_segments())
1117
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
3 std::cout << "segment: " << seg << "\n";
1118 // end::snippet_parsing_path_1_b[]
1119
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(equal_range(
1120 u.encoded_segments(),
1121 {"doc", "the%20libs", ""}));
1122 }
1123 {
1124 // tag::snippet_parsing_path_2[]
1125
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org/doc/libs");
1126
2/2
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
1 std::cout << u.segments().size() << " segments\n";
1127
3/3
✓ Branch 4 taken 2 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 1 time.
3 for (auto seg: u.segments())
1128
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
2 std::cout << "segment: " << seg << "\n";
1129 // end::snippet_parsing_path_2[]
1130 1 auto segs = u.segments();
1131
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 BOOST_TEST(equal_range(segs, {"doc", "libs"}));
1132 }
1133 {
1134 // tag::snippet_parsing_path_3[]
1135
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org");
1136
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path().empty());
1137
1/1
✓ Branch 3 taken 1 time.
1 assert(u.encoded_path().empty());
1138 // end::snippet_parsing_path_3[]
1139 }
1140 {
1141 // tag::snippet_parsing_path_3a[]
1142
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 9 taken 1 time.
1 assert( url_view("urn:isbn:096139210x").path() == "isbn:096139210x" );
1143 // end::snippet_parsing_path_3a[]
1144 }
1145 {
1146 // tag::snippet_parsing_path_4[]
1147 /*
1148 url_view u("https://www.boost.org//doc///libs");
1149 std::cout << u << "\n"
1150 "path: " << u.encoded_path() << "\n"
1151 "encoded segments: " << u.encoded_segments() << "\n"
1152 "segments: " << u.segments() << "\n";
1153 std::cout << u.encoded_segments().size() << " segments\n";
1154 for (auto seg: u.encoded_segments())
1155 std::cout << "segment: " << seg << "\n";
1156 */
1157 // end::snippet_parsing_path_4[]
1158 }
1159
1160 {
1161 {
1162 // tag::snippet_parsing_path_5_a[]
1163 1 boost::core::string_view s = "https://www.boost.org";
1164
1/1
✓ Branch 1 taken 1 time.
1 url_view u(s);
1165
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1166
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 8 taken 1 time.
1 << "path: " << u.encoded_host() << "\n"
1167
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 8 taken 1 time.
1 << "path: " << u.encoded_path() << "\n"
1168
4/4
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 12 taken 1 time.
1 << "segments: " << u.encoded_segments().size() << "\n";
1169 // end::snippet_parsing_path_5_a[]
1170
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_host() == "www.boost.org");
1171
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST(u.encoded_path().empty());
1172
2/2
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(equal_range(
1173 u.encoded_segments(),
1174 std::initializer_list<const char*>{}));
1175 }
1176 {
1177 // tag::snippet_parsing_path_5_b[]
1178 1 boost::core::string_view s = "https://www.boost.org/";
1179
1/1
✓ Branch 1 taken 1 time.
1 url_view u(s);
1180
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1181
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 8 taken 1 time.
1 << "host: " << u.encoded_host() << "\n"
1182
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 8 taken 1 time.
1 << "path: " << u.encoded_path() << "\n"
1183
4/4
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 12 taken 1 time.
1 << "segments: " << u.encoded_segments().size() << "\n";
1184 // end::snippet_parsing_path_5_b[]
1185
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_host() == "www.boost.org");
1186
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_path() == "/");
1187
2/2
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(equal_range(
1188 u.encoded_segments(),
1189 std::initializer_list<const char*>{}));
1190 }
1191 {
1192 // tag::snippet_parsing_path_5_c[]
1193 1 boost::core::string_view s = "https://www.boost.org//";
1194
1/1
✓ Branch 1 taken 1 time.
1 url_view u(s);
1195
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1196
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 8 taken 1 time.
1 << "host: " << u.encoded_host() << "\n"
1197
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 8 taken 1 time.
1 << "path: " << u.encoded_path() << "\n"
1198
4/4
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 12 taken 1 time.
1 << "segments: " << u.encoded_segments().size() << "\n";
1199 // end::snippet_parsing_path_5_c[]
1200
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_host() == "www.boost.org");
1201
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_path() == "//");
1202 1 auto segs = u.encoded_segments();
1203
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 BOOST_TEST(equal_range(segs, {"", ""}));
1204 }
1205 }
1206
1207 {
1208 // tag::snippet_parsing_path_6[]
1209
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org//doc/libs/");
1210
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1211
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "authority: " << u.encoded_authority() << "\n"
1212
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
1 "path: " << u.encoded_path() << "\n";
1213
2/2
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
1 std::cout << u.encoded_segments().size() << " segments\n";
1214
2/2
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 1 time.
5 for (auto seg: u.encoded_segments())
1215
3/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
4 std::cout << "segment: " << seg << "\n";
1216 // end::snippet_parsing_path_6[]
1217
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_authority() == "www.boost.org");
1218
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_path() == "//doc/libs/");
1219 1 auto segs = u.encoded_segments();
1220
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 BOOST_TEST(equal_range(segs, {"", "doc", "libs", ""}));
1221 }
1222
1223 {
1224 // tag::snippet_parsing_path_7[]
1225
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://doc/libs/");
1226
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1227
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "authority: " << u.encoded_authority() << "\n"
1228
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
1 "path: " << u.encoded_path() << "\n";
1229
2/2
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
1 std::cout << u.encoded_segments().size() << " segments\n";
1230
2/2
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 time.
3 for (auto seg: u.encoded_segments())
1231
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
2 std::cout << "segment: " << seg << "\n";
1232 // end::snippet_parsing_path_7[]
1233
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_authority() == "doc");
1234
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_path() == "/libs/");
1235 1 auto segs = u.encoded_segments();
1236
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 BOOST_TEST(equal_range(segs, {"libs", ""}));
1237 }
1238
1239 {
1240 // tag::snippet_parsing_path_8[]
1241
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org/doc@folder/libs:boost");
1242
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1243
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "authority: " << u.encoded_authority() << "\n"
1244
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
1 "path: " << u.encoded_path() << "\n";
1245
2/2
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
1 std::cout << u.encoded_segments().size() << " segments\n";
1246
2/2
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 time.
3 for (auto seg: u.encoded_segments())
1247
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
2 std::cout << "segment: " << seg << "\n";
1248 // end::snippet_parsing_path_8[]
1249
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_authority() == "www.boost.org");
1250
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_path() == "/doc@folder/libs:boost");
1251 1 auto segs = u.encoded_segments();
1252
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 BOOST_TEST(equal_range(segs, {"doc@folder", "libs:boost"}));
1253 }
1254
1255 {
1256 // tag::snippet_parsing_path_9[]
1257 /*
1258 segments_view segs = parse_path("/doc/libs").value();
1259 assert( segs.size() == 2 );
1260 */
1261 // end::snippet_parsing_path_9[]
1262 //boost::ignore_unused(segs);
1263 }
1264
1265 {
1266 // tag::snippet_parsing_path_use_case_1[]
1267
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org/doc/libs/");
1268
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.host() == "www.boost.org");
1269
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "/doc/libs/");
1270 // end::snippet_parsing_path_use_case_1[]
1271 boost::ignore_unused(u);
1272 }
1273 {
1274 // tag::snippet_parsing_path_use_case_2[]
1275
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( parse_uri("https://www.boost.org").has_value() );
1276
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( parse_uri("https://www.boost.org/").has_value() );
1277
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( parse_uri("https://www.boost.org//").has_value() );
1278 // end::snippet_parsing_path_use_case_2[]
1279 }
1280 {
1281 // tag::snippet_parsing_path_use_case_3[]
1282
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 9 taken 1 time.
1 assert( url_view("https://www.boost.org").path().empty() );
1283 // end::snippet_parsing_path_use_case_3[]
1284 }
1285 {
1286 // tag::snippet_parsing_path_use_case_4[]
1287
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org/doc@folder/libs:boost");
1288
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "/doc@folder/libs:boost");
1289 // end::snippet_parsing_path_use_case_4[]
1290 boost::ignore_unused(u);
1291 }
1292 {
1293 // tag::snippet_parsing_path_use_case_5[]
1294
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org/doc/libs/");
1295 1 segments_view segs = u.segments();
1296 1 auto it = segs.begin();
1297
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(*it++ == "doc");
1298
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(*it++ == "libs");
1299
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert(*it == "");
1300 // end::snippet_parsing_path_use_case_5[]
1301 boost::ignore_unused(it);
1302 }
1303 {
1304 // tag::snippet_parsing_path_use_case_6[]
1305
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org//doc///libs");
1306 1 segments_view segs = u.segments();
1307 1 auto it = segs.begin();
1308
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(*it++ == "");
1309
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(*it++ == "doc");
1310
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(*it++ == "");
1311
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(*it++ == "");
1312
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert(*it == "libs");
1313 // end::snippet_parsing_path_use_case_6[]
1314 boost::ignore_unused(it);
1315 }
1316 {
1317 // tag::snippet_parsing_path_use_case_7[]
1318
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org//doc/libs/");
1319 1 segments_view segs = u.segments();
1320 1 auto it = segs.begin();
1321
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(*it++ == "");
1322
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(*it++ == "doc");
1323
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(*it++ == "libs");
1324
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert(*it == "");
1325 // end::snippet_parsing_path_use_case_7[]
1326 boost::ignore_unused(it);
1327 }
1328 {
1329 // tag::snippet_parsing_path_use_case_8[]
1330
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://doc/libs/");
1331
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.host() == "doc");
1332 1 segments_view segs = u.segments();
1333 1 auto it = segs.begin();
1334
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(*it++ == "libs");
1335
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert(*it == "");
1336 // end::snippet_parsing_path_use_case_8[]
1337 boost::ignore_unused(it);
1338 }
1339 1 }
1340
1341 void
1342 1 parsing_query()
1343 {
1344 {
1345 // tag::snippet_parsing_query_0[]
1346
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/get-customer.php?id=409&name=Joe&individual");
1347
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert( u.query() == "id=409&name=Joe&individual" );
1348 // end::snippet_parsing_query_0[]
1349 }
1350 {
1351 // tag::snippet_parsing_query_1[]
1352
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/get-customer.php?id=409&name=Joe&individual");
1353 1 params_view ps = u.params();
1354
1/1
✓ Branch 2 taken 1 time.
1 assert(ps.size() == 3);
1355 // end::snippet_parsing_query_1[]
1356 // tag::snippet_parsing_query_1a[]
1357 1 auto it = ps.begin();
1358
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).key == "id");
1359
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).value == "409");
1360 1 ++it;
1361
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).key == "name");
1362
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).value == "Joe");
1363 1 ++it;
1364
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).key == "individual");
1365
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 assert(!(*it).has_value);
1366 // end::snippet_parsing_query_1a[]
1367 boost::ignore_unused(it);
1368 }
1369 {
1370 // tag::snippet_parsing_query_2[]
1371
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/get-customer.php?key-1=value-1&key-2=&key-3&&=value-2");
1372
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1373
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "has query: " << u.has_query() << "\n"
1374
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "encoded query: " << u.encoded_query() << "\n"
1375
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 "query: " << u.query() << "\n";
1376
2/2
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
1 std::cout << u.encoded_params().size() << " parameters\n";
1377
2/2
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 1 time.
6 for (auto p: u.encoded_params())
1378 {
1379
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (p.has_value)
1380 {
1381 std::cout <<
1382
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 "parameter: <" << p.key <<
1383
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
3 ", " << p.value << ">\n";
1384 } else {
1385
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
2 std::cout << "parameter: " << p.key << "\n";
1386 }
1387 }
1388 // end::snippet_parsing_query_2[]
1389
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.has_query());
1390
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_query() == "key-1=value-1&key-2=&key-3&&=value-2");
1391
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.query() == "key-1=value-1&key-2=&key-3&&=value-2");
1392
2/2
✓ Branch 17 taken 1 time.
✓ Branch 20 taken 1 time.
1 BOOST_TEST(equal_range(
1393 u.encoded_params(),
1394 {
1395 param_view{"key-1", "value-1", true},
1396 param_view{"key-2", "", true},
1397 param_view{"key-3", boost::core::string_view(), false},
1398 param_view{"", boost::core::string_view(), false},
1399 param_view{"", "value-2", true}
1400 }));
1401 }
1402 {
1403 // tag::snippet_parsing_query_3[]
1404
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/get-customer.php?email=joe@email.com&code=a:2@/!");
1405
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1406
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "has query: " << u.has_query() << "\n"
1407
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "encoded query: " << u.encoded_query() << "\n"
1408
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 "query: " << u.query() << "\n";
1409
2/2
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
1 std::cout << u.encoded_params().size() << " parameters\n";
1410
2/2
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 time.
3 for (auto p: u.encoded_params())
1411 {
1412
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (p.has_value)
1413 {
1414 std::cout <<
1415
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 "parameter: <" << p.key <<
1416
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
2 ", " << p.value << ">\n";
1417 } else {
1418 std::cout << "parameter: " << p.key << "\n";
1419 }
1420 }
1421 // end::snippet_parsing_query_3[]
1422
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.has_query());
1423
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_query() == "email=joe@email.com&code=a:2@/!");
1424
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.query() == "email=joe@email.com&code=a:2@/!");
1425
2/2
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 BOOST_TEST(equal_range(
1426 u.encoded_params(),
1427 {
1428 param_view{"email", "joe@email.com", true},
1429 param_view{"code", "a:2@/!", true}
1430 }));
1431 }
1432 {
1433 // tag::snippet_parsing_query_4[]
1434
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/get-customer.php?name=joe");
1435
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1436
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 "query: " << u.query() << "\n";
1437 // end::snippet_parsing_query_4[]
1438
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.query() == "name=joe");
1439 }
1440 {
1441 // tag::snippet_parsing_query_5[]
1442
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert(url_view("https://www.example.com/get-customer.php?").has_query());
1443
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert(!url_view("https://www.example.com/get-customer.php").has_query());
1444 // end::snippet_parsing_query_5[]
1445 }
1446 {
1447 // tag::snippet_parsing_query_6[]
1448
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/get-customer.php?name=John%20Doe");
1449
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1450
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "has query: " << u.has_query() << "\n"
1451
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "encoded query: " << u.encoded_query() << "\n"
1452
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 "query: " << u.query() << "\n";
1453 // end::snippet_parsing_query_6[]
1454
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.has_query());
1455
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_query() == "name=John%20Doe");
1456
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.query() == "name=John Doe");
1457 }
1458 {
1459 // tag::snippet_parsing_query_7[]
1460
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/get-customer.php?name=John%26Doe");
1461
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.query() == "name=John&Doe");
1462 // end::snippet_parsing_query_7[]
1463 }
1464 {
1465 // tag::snippet_parsing_query_8[]
1466
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/get-customer.php?key-1=value-1&key-2=&key-3&&=value-4");
1467 1 params_view ps = u.params();
1468
1/1
✓ Branch 2 taken 1 time.
1 assert(ps.size() == 5);
1469 // end::snippet_parsing_query_8[]
1470 // tag::snippet_parsing_query_8a[]
1471 1 auto it = ps.begin();
1472
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).key == "key-1");
1473
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).value == "value-1");
1474 // end::snippet_parsing_query_8a[]
1475 // tag::snippet_parsing_query_8b[]
1476 1 ++it;
1477
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).key == "key-2");
1478
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).value == "");
1479
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 assert((*it).has_value);
1480 // end::snippet_parsing_query_8b[]
1481 // tag::snippet_parsing_query_8c[]
1482 1 ++it;
1483
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).key == "key-3");
1484
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 assert(!(*it).has_value);
1485 // end::snippet_parsing_query_8c[]
1486 // tag::snippet_parsing_query_8d[]
1487 1 ++it;
1488
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).key == "");
1489
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 assert(!(*it).has_value);
1490 // end::snippet_parsing_query_8d[]
1491 // tag::snippet_parsing_query_8e[]
1492 1 ++it;
1493
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).key == "");
1494
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert((*it).value == "value-4");
1495 // end::snippet_parsing_query_8e[]
1496 }
1497 {
1498 // tag::snippet_parsing_query_9[]
1499
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/get-customer.php?email=joe@email.com&code=a:2@/!");
1500
1/1
✓ Branch 2 taken 1 time.
1 assert(u.has_query());
1501 // end::snippet_parsing_query_9[]
1502 }
1503 1 }
1504
1505 void
1506 1 parsing_fragment()
1507 {
1508 {
1509 // tag::snippet_parsing_fragment_1[]
1510
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/index.html#section%202");
1511
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1512
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "has fragment: " << u.has_fragment() << "\n"
1513
1/1
✓ Branch 1 taken 1 time.
2 "fragment: " << u.fragment() << "\n"
1514
5/5
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 12 taken 1 time.
✓ Branch 15 taken 1 time.
2 "encoded fragment: " << u.encoded_fragment() << "\n";
1515 // end::snippet_parsing_fragment_1[]
1516
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.has_fragment());
1517
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.fragment() == "section 2");
1518
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 BOOST_TEST(u.encoded_fragment() == "section%202");
1519 }
1520 {
1521 // tag::snippet_parsing_fragment_2_a[]
1522
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/index.html#");
1523
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1524
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "has fragment: " << u.has_fragment() << "\n"
1525
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 "fragment: " << u.fragment() << "\n";
1526 // end::snippet_parsing_fragment_2_a[]
1527
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.has_fragment());
1528
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.fragment().empty());
1529 }
1530 {
1531 // tag::snippet_parsing_fragment_2_b[]
1532
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/index.html");
1533
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1534
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "has fragment: " << u.has_fragment() << "\n"
1535
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 "fragment: " << u.fragment() << "\n";
1536 // end::snippet_parsing_fragment_2_b[]
1537
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.has_fragment());
1538
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.fragment().empty());
1539 }
1540 {
1541 // tag::snippet_parsing_fragment_3[]
1542
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/index.html#code%20:a@b?c/d");
1543
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u << "\n"
1544
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "has fragment: " << u.has_fragment() << "\n"
1545
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 "fragment: " << u.fragment() << "\n";
1546 // end::snippet_parsing_fragment_3[]
1547
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.has_fragment());
1548
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 BOOST_TEST(u.fragment() == "code :a@b?c/d");
1549 }
1550 {
1551 // tag::snippet_parsing_fragment_4[]
1552
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/index.html#section2");
1553
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.fragment() == "section2");
1554 // end::snippet_parsing_fragment_4[]
1555 }
1556 {
1557 // tag::snippet_parsing_fragment_5[]
1558
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert(url_view("https://www.example.com/index.html#").has_fragment());
1559
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert(!url_view("https://www.example.com/index.html").has_fragment());
1560 // end::snippet_parsing_fragment_5[]
1561 }
1562 {
1563 // tag::snippet_parsing_fragment_6[]
1564
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com/index.html#code%20:a@b?c/d");
1565
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.fragment() == "code :a@b?c/d");
1566 // end::snippet_parsing_fragment_6[]
1567 }
1568 1 }
1569
1570 void
1571 1 using_modifying()
1572 {
1573 {
1574 // tag::snippet_modifying_1[]
1575
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.example.com");
1576
1/1
✓ Branch 1 taken 1 time.
1 url v(u);
1577 // end::snippet_modifying_1[]
1578
1579 // tag::snippet_modifying_2[]
1580
1/1
✓ Branch 4 taken 1 time.
1 assert(v.scheme() == "https");
1581
1/1
✓ Branch 2 taken 1 time.
1 assert(v.has_authority());
1582
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 assert(v.encoded_authority() == "www.example.com");
1583
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 assert(v.encoded_path() == "");
1584 // end::snippet_modifying_2[]
1585
1586 // tag::snippet_modifying_3[]
1587
1/1
✓ Branch 2 taken 1 time.
1 v.set_host("my website.com");
1588
1/1
✓ Branch 2 taken 1 time.
1 v.set_path("my file.txt");
1589
1/1
✓ Branch 2 taken 1 time.
1 v.set_query("id=42&name=John Doe");
1590
1/1
✓ Branch 4 taken 1 time.
1 assert(v.buffer() == "https://my%20website.com/my%20file.txt?id=42&name=John%20Doe");
1591 // end::snippet_modifying_3[]
1592
1593 // tag::snippet_modifying_4[]
1594
1/1
✓ Branch 2 taken 1 time.
1 v.set_scheme("http");
1595
1/1
✓ Branch 4 taken 1 time.
1 assert(v.buffer() == "http://my%20website.com/my%20file.txt?id=42&name=John%20Doe");
1596
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 v.set_encoded_host("www.my%20example.com");
1597
1/1
✓ Branch 4 taken 1 time.
1 assert(v.buffer() == "http://www.my%20example.com/my%20file.txt?id=42&name=John%20Doe");
1598 // end::snippet_modifying_4[]
1599
1600
1601 1 }
1602 1 }
1603
1604 void
1605 1 grammar_parse()
1606 {
1607 {
1608 // tag::snippet_parse_1[]
1609 // VFALCO we should not show this example
1610 /*
1611 boost::core::string_view s = "http:after_scheme";
1612 const char* it = s.begin();
1613 auto rv = grammar::parse(it, s.end(), scheme_rule() );
1614 if( ! rv )
1615 {
1616 std::cout << "scheme: " << rv->scheme << '\n';
1617 std::cout << "suffix: " << it << '\n';
1618 }
1619 */
1620 // end::snippet_parse_1[]
1621 }
1622
1623 {
1624 // tag::snippet_parse_2[]
1625 // VFALCO This needs refactoring
1626 /*
1627 boost::core::string_view s = "?key=value#anchor";
1628 const char* it = s.begin();
1629 boost::system::error_code ec;
1630 if (grammar::parse(it, s.end(), ec, r1))
1631 {
1632 auto r2 = grammar::parse( it, s.end(), fragment_part_rule );
1633 if( r2 )
1634 {
1635 std::cout << "query: " << r1.query_part << '\n';
1636 std::cout << "fragment: " << std::get<1>(*r2.value()).encoded() << '\n';
1637 }
1638 }
1639 */
1640 // end::snippet_parse_2[]
1641 }
1642
1643 {
1644 // tag::snippet_parse_3[]
1645 // VFALCO This needs refactoring
1646 /*
1647 boost::core::string_view s = "?key=value#anchor";
1648 query_part_rule r1;
1649 const char* it = s.begin();
1650 boost::system::error_code ec;
1651 auto r2 = grammar::parse( it, s.end(), ec, fragment_part_rule );
1652 if( ! ec.failed() )
1653 {
1654 std::cout << "query: " << r1.query_part << '\n';
1655 std::cout << "fragment: " << r2.fragment.encoded() << '\n';
1656 }
1657 */
1658 // end::snippet_parse_3[]
1659 }
1660
1661 {
1662 // tag::snippet_parse_4[]
1663 /* VFALCO This will be removed
1664 boost::core::string_view s = "http://www.boost.org";
1665 uri_rule r;
1666 boost::system::error_code ec;
1667 if (grammar::parse_string(s, ec, r))
1668 {
1669 std::cout << "scheme: " << r.scheme_part.scheme << '\n';
1670 std::cout << "host: " << r.hier_part.authority.host.host_part << '\n';
1671 }
1672 */
1673 // end::snippet_parse_4[]
1674 }
1675 1 }
1676
1677 // tag::snippet_customization_1[]
1678 /* VFALCO This needs rewriting
1679 struct lowercase_rule
1680 {
1681 boost::core::string_view str;
1682
1683 friend
1684 void
1685 tag_invoke(
1686 grammar::parse_tag const&,
1687 char const*& it,
1688 char const* const end,
1689 boost::system::error_code& ec,
1690 lowercase_rule& t) noexcept
1691 {
1692 ec = {};
1693 char const* begin = it;
1694 while (it != end && std::islower(*it))
1695 {
1696 ++it;
1697 }
1698 t.str = boost::core::string_view(begin, it);
1699 }
1700 };
1701 */
1702 // end::snippet_customization_1[]
1703
1704 void
1705 1 grammar_customization()
1706 {
1707 {
1708 // tag::snippet_customization_2[]
1709 // VFALCO THIS NEEDS TO BE PORTED
1710 /*
1711 boost::core::string_view s = "http:somelowercase";
1712 scheme_rule r1;
1713 lowercase_rule r2;
1714 boost::system::error_code ec;
1715 if (grammar::parse_string(s, ec, r1, ':', r2))
1716 {
1717 std::cout << "scheme: " << r1.scheme << '\n';
1718 std::cout << "lower: " << r2.str << '\n';
1719 }
1720 */
1721 // end::snippet_customization_2[]
1722 }
1723 1 }
1724
1725 // tag::code_charset_1[]
1726 struct CharSet
1727 {
1728 bool operator()( char c ) const noexcept;
1729
1730 // These are both optional. If either or both are left
1731 // unspecified, a default implementation will be used.
1732 //
1733 char const* find_if( char const* first, char const* last ) const noexcept;
1734 char const* find_if_not( char const* first, char const* last ) const noexcept;
1735 };
1736 // end::code_charset_1[]
1737
1738 void
1739 1 modifying_path()
1740 {
1741 {
1742 // tag::snippet_modifying_path_1[]
1743
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org");
1744
1745 // end::snippet_modifying_path_1[]
1746
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.is_path_absolute());
1747
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST_EQ(u.encoded_segments().size(), 0u);
1748 }
1749 {
1750 // tag::snippet_modifying_path_2[]
1751
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://www.boost.org/");
1752 // end::snippet_modifying_path_2[]
1753
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.is_path_absolute());
1754
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST_EQ(u.encoded_segments().size(), 0u);
1755 }
1756 {
1757 // tag::snippet_modifying_path_1_2[]
1758
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert( !url_view("https://www.boost.org").is_path_absolute() );
1759
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 assert( url_view("https://www.boost.org/").is_path_absolute() );
1760 // end::snippet_modifying_path_1_2[]
1761 }
1762
1763 {
1764 // tag::snippet_modifying_path_3[]
1765
1/1
✓ Branch 2 taken 1 time.
1 url u("https://www.boost.org/./a/../b");
1766
1/1
✓ Branch 1 taken 1 time.
1 u.normalize();
1767
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://www.boost.org/b");
1768 // end::snippet_modifying_path_3[]
1769
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.is_path_absolute());
1770
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_EQ(u.buffer(), "https://www.boost.org/b");
1771
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST_EQ(u.encoded_segments().size(), 1u);
1772 1 }
1773 {
1774 // tag::snippet_modifying_path_4[]
1775 // scheme and a relative path
1776
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https:path/to/file.txt");
1777 // end::snippet_modifying_path_4[]
1778
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_EQ(u.scheme(), "https");
1779
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.has_authority());
1780
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.is_path_absolute());
1781
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST_EQ(u.encoded_segments().size(), 3u);
1782 }
1783
1784 {
1785 // tag::snippet_modifying_path_5[]
1786 // scheme and an absolute path
1787
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https:/path/to/file.txt");
1788 // end::snippet_modifying_path_5[]
1789
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_EQ(u.scheme(), "https");
1790
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.has_authority());
1791
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.is_path_absolute());
1792
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST_EQ(u.encoded_segments().size(), 3u);
1793 }
1794
1795 {
1796 // tag::snippet_modifying_path_6[]
1797 // "//path" will be considered the authority component
1798
1/1
✓ Branch 1 taken 1 time.
1 url_view u("https://path/to/file.txt");
1799 // end::snippet_modifying_path_6[]
1800
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_EQ(u.scheme(), "https");
1801
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.has_authority());
1802
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.is_path_absolute());
1803
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST_EQ(u.encoded_segments().size(), 2u);
1804 }
1805
1806 {
1807 // tag::snippet_modifying_path_4_5_6[]
1808 // scheme and a relative path
1809
1/1
✓ Branch 1 taken 1 time.
1 url_view u1("https:path/to/file.txt");
1810
1/1
✓ Branch 2 taken 1 time.
1 assert(!u1.has_authority());
1811
1/1
✓ Branch 2 taken 1 time.
1 assert(!u1.is_path_absolute());
1812
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u1.path() == "path/to/file.txt");
1813
1814 // scheme and an absolute path
1815
1/1
✓ Branch 1 taken 1 time.
1 url_view u2("https:/path/to/file.txt");
1816
1/1
✓ Branch 2 taken 1 time.
1 assert(!u2.has_authority());
1817
1/1
✓ Branch 2 taken 1 time.
1 assert(u2.is_path_absolute());
1818
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u2.path() == "/path/to/file.txt");
1819
1820 // "//path" will be considered the authority component
1821
1/1
✓ Branch 1 taken 1 time.
1 url_view u3("https://path/to/file.txt");
1822
1/1
✓ Branch 2 taken 1 time.
1 assert(u3.has_authority());
1823
1/1
✓ Branch 5 taken 1 time.
1 assert(u3.authority().buffer() == "path");
1824
1/1
✓ Branch 2 taken 1 time.
1 assert(u3.is_path_absolute());
1825
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u3.path() == "/to/file.txt");
1826 // end::snippet_modifying_path_4_5_6[]
1827 }
1828
1829 {
1830 // tag::snippet_modifying_path_7[]
1831 // only a relative path
1832
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url_view u = parse_uri_reference("path-to/file.txt").value();
1833 // end::snippet_modifying_path_7[]
1834
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.has_scheme());
1835
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.has_authority());
1836
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.is_path_absolute());
1837
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST_EQ(u.encoded_segments().size(), 2u);
1838 }
1839
1840 {
1841 // tag::snippet_modifying_path_8[]
1842 // "path:" will be considered the scheme component
1843 // instead of a substring of the first segment
1844
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url_view u = parse_uri_reference("path:to/file.txt").value();
1845 // end::snippet_modifying_path_8[]
1846
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.has_scheme());
1847
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.has_authority());
1848
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.is_path_absolute());
1849
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST_EQ(u.encoded_segments().size(), 2u);
1850 }
1851
1852 {
1853 // tag::snippet_modifying_path_7_8[]
1854 // only a relative path
1855
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url_view u1 = parse_uri_reference("path-to/file.txt").value();
1856
1/1
✓ Branch 2 taken 1 time.
1 assert(!u1.has_scheme());
1857
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u1.path() == "path-to/file.txt");
1858
1859 // "path:" will be considered the scheme component
1860 // instead of a substring of the first segment
1861
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url_view u2 = parse_uri_reference("path:to/file.txt").value();
1862
1/1
✓ Branch 2 taken 1 time.
1 assert(u2.has_scheme());
1863
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u2.path() == "to/file.txt");
1864 // end::snippet_modifying_path_7_8[]
1865 }
1866
1867 {
1868 // tag::snippet_modifying_path_9[]
1869 // "path" should not become the authority component
1870
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 url u = parse_uri("https:path/to/file.txt").value();
1871
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 u.set_encoded_path("//path/to/file.txt");
1872 // end::snippet_modifying_path_9[]
1873
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_EQ(u.scheme(), "https");
1874
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.has_authority());
1875
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(u.is_path_absolute());
1876
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST_EQ(u.encoded_segments().size(), 4u);
1877 1 }
1878
1879 {
1880 // tag::snippet_modifying_path_10[]
1881 // "path:to" should not make the scheme become "path:"
1882
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 url u = parse_uri_reference("path-to/file.txt").value();
1883
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 u.set_encoded_path("path:to/file.txt");
1884 // end::snippet_modifying_path_10[]
1885
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.has_scheme());
1886
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.has_authority());
1887
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.is_path_absolute());
1888
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST_EQ(u.encoded_segments().size(), 2u);
1889 1 }
1890 {
1891 // tag::snippet_modifying_path_9_10[]
1892
1/1
✓ Branch 2 taken 1 time.
1 url u1("https:path/to/file.txt" );
1893
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 u1.set_encoded_path("//path/to/file.txt");
1894
1/1
✓ Branch 4 taken 1 time.
1 assert(u1.buffer() == "https:/.//path/to/file.txt");
1895 // end::snippet_modifying_path_9_10[]
1896 1 }
1897 {
1898 // tag::snippet_modifying_path_11[]
1899 // should not insert as "pathto/file.txt"
1900
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 url u = parse_uri_reference("to/file.txt").value();
1901 1 segments_ref segs = u.segments();
1902
1/1
✓ Branch 3 taken 1 time.
1 segs.insert(segs.begin(), "path");
1903
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "path/to/file.txt");
1904 // end::snippet_modifying_path_11[]
1905
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.has_scheme());
1906
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.has_authority());
1907
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST_NOT(u.is_path_absolute());
1908
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST_EQ(u.encoded_segments().size(), 3u);
1909 1 }
1910 1 }
1911
1912 void
1913 1 normalizing()
1914 {
1915 {
1916 // tag::snippet_normalizing_1[]
1917
1/1
✓ Branch 1 taken 1 time.
1 url_view u1("https://www.boost.org/index.html");
1918
1/1
✓ Branch 1 taken 1 time.
1 url_view u2("https://www.boost.org/doc/../index.html");
1919
1/1
✓ Branch 4 taken 1 time.
1 assert(u1.buffer() != u2.buffer());
1920 // end::snippet_normalizing_1[]
1921 }
1922
1923 {
1924 // tag::snippet_normalizing_2[]
1925
1/1
✓ Branch 1 taken 1 time.
1 url_view u1("https://www.boost.org/index.html");
1926
1/1
✓ Branch 1 taken 1 time.
1 url_view u2("https://www.boost.org/doc/../index.html");
1927
1/1
✓ Branch 2 taken 1 time.
1 assert(u1 == u2);
1928 // end::snippet_normalizing_2[]
1929 }
1930
1931 {
1932 // tag::snippet_normalizing_3[]
1933
1/1
✓ Branch 1 taken 1 time.
1 url_view u1("https://www.boost.org/index.html");
1934
1/1
✓ Branch 2 taken 1 time.
1 url u2("https://www.boost.org/doc/../index.html");
1935
1/1
✓ Branch 4 taken 1 time.
1 assert(u1.buffer() != u2.buffer());
1936
1/1
✓ Branch 2 taken 1 time.
1 assert(u1 == u2);
1937
1/1
✓ Branch 1 taken 1 time.
1 u2.normalize();
1938
1/1
✓ Branch 4 taken 1 time.
1 assert(u1.buffer() == u2.buffer());
1939
1/1
✓ Branch 2 taken 1 time.
1 assert(u1 == u2);
1940 // end::snippet_normalizing_3[]
1941 1 }
1942
1943 {
1944 // tag::snippet_normalizing_4[]
1945
1/1
✓ Branch 2 taken 1 time.
1 url u("https://www.boost.org/doc/../%69%6e%64%65%78%20file.html");
1946
1/1
✓ Branch 1 taken 1 time.
1 u.normalize();
1947
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://www.boost.org/index%20file.html");
1948 // end::snippet_normalizing_4[]
1949 1 }
1950
1951 {
1952 // tag::snippet_normalizing_5[]
1953 auto normalize_http_url =
1954 4 [](url& u)
1955 {
1956 4 u.normalize();
1957
3/4
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
7 if (u.port() == "80" ||
1958
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
7 u.port().empty())
1959 4 u.remove_port();
1960
3/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 3 times.
8 if (u.has_authority() &&
1961
2/2
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 3 times.
8 u.encoded_path().empty())
1962 1 u.set_path_absolute(true);
1963 4 };
1964
1965
1/1
✓ Branch 2 taken 1 time.
1 url u1("https://www.boost.org");
1966
1/1
✓ Branch 1 taken 1 time.
1 normalize_http_url(u1);
1967
1/1
✓ Branch 2 taken 1 time.
1 url u2("https://www.boost.org/");
1968
1/1
✓ Branch 1 taken 1 time.
1 normalize_http_url(u2);
1969
1/1
✓ Branch 2 taken 1 time.
1 url u3("https://www.boost.org:/");
1970
1/1
✓ Branch 1 taken 1 time.
1 normalize_http_url(u3);
1971
1/1
✓ Branch 2 taken 1 time.
1 url u4("https://www.boost.org:80/");
1972
1/1
✓ Branch 1 taken 1 time.
1 normalize_http_url(u4);
1973
1974
1/1
✓ Branch 4 taken 1 time.
1 assert(u1.buffer() == "https://www.boost.org/");
1975
1/1
✓ Branch 4 taken 1 time.
1 assert(u2.buffer() == "https://www.boost.org/");
1976
1/1
✓ Branch 4 taken 1 time.
1 assert(u3.buffer() == "https://www.boost.org/");
1977
1/1
✓ Branch 4 taken 1 time.
1 assert(u4.buffer() == "https://www.boost.org/");
1978 // end::snippet_normalizing_5[]
1979 1 }
1980 1 }
1981
1982 void
1983 1 decode_with_token()
1984 {
1985 {
1986 // tag::snippet_string_token_1[]
1987
1/1
✓ Branch 1 taken 1 time.
1 url_view u("http://www.example.com/my%20file.txt");
1988 1 pct_string_view sv = u.encoded_path();
1989
2/3
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 time.
1 assert(sv == "/my%20file.txt");
1990
1/1
✓ Branch 2 taken 1 time.
1 std::string s = u.path();
1991
1/1
✓ Branch 2 taken 1 time.
1 assert(s == "/my file.txt");
1992 // end::snippet_string_token_1[]
1993 1 }
1994
1995 {
1996 // tag::snippet_string_token_2[]
1997
1/1
✓ Branch 1 taken 1 time.
1 url_view u("http://www.example.com/my%20file.txt");
1998
1/1
✓ Branch 1 taken 1 time.
1 std::string s = "existing string";
1999
1/1
✓ Branch 2 taken 1 time.
1 u.path(string_token::assign_to(s));
2000
1/1
✓ Branch 2 taken 1 time.
1 assert(s == "/my file.txt");
2001 // end::snippet_string_token_2[]
2002 1 }
2003
2004 {
2005 // tag::snippet_string_token_3[]
2006
1/1
✓ Branch 1 taken 1 time.
1 url_view u("http://www.example.com/my%20file.txt");
2007
1/1
✓ Branch 1 taken 1 time.
1 std::string s = "existing string";
2008
1/1
✓ Branch 2 taken 1 time.
1 u.path(string_token::append_to(s));
2009
1/1
✓ Branch 2 taken 1 time.
1 assert(s == "existing string/my file.txt");
2010 // end::snippet_string_token_3[]
2011 1 }
2012
2013 {
2014 // tag::snippet_string_token_4[]
2015
1/1
✓ Branch 1 taken 1 time.
1 url_view u("http://www.example.com/my%20file.txt");
2016
1/1
✓ Branch 1 taken 1 time.
1 std::string s = "existing string";
2017
1/1
✓ Branch 2 taken 1 time.
1 boost::core::string_view sv = u.path(string_token::preserve_size(s));
2018
1/1
✓ Branch 3 taken 1 time.
1 assert(sv == "/my file.txt");
2019 // end::snippet_string_token_4[]
2020 1 }
2021
2022 {
2023 #if defined(__cpp_static_assert) && __cpp_static_assert >= 201411L
2024 // tag::snippet_string_token_5[]
2025 static_assert(
2026 string_token::is_token<string_token::return_string>::value);
2027 // end::snippet_string_token_5[]
2028 #endif
2029 }
2030 1 }
2031
2032
2033 void
2034 1 encoding()
2035 {
2036 {
2037 // tag::snippet_encoding_1[]
2038 1 std::string s = encode("hello world!", unreserved_chars);
2039
1/1
✓ Branch 2 taken 1 time.
1 assert(s == "hello%20world%21");
2040 // end::snippet_encoding_1[]
2041 1 }
2042
2043 {
2044 // tag::snippet_encoding_2[]
2045 1 encoding_opts opt;
2046 1 opt.space_as_plus = true;
2047 1 std::string s = encode("msg=hello world", pchars, opt);
2048
1/1
✓ Branch 2 taken 1 time.
1 assert(s == "msg=hello+world");
2049 // end::snippet_encoding_2[]
2050 1 }
2051
2052 {
2053 // tag::snippet_encoding_3[]
2054 1 std::string s;
2055 1 encode("hello ", pchars, {}, string_token::assign_to(s));
2056 1 encode("world", pchars, {}, string_token::append_to(s));
2057
1/1
✓ Branch 2 taken 1 time.
1 assert(s == "hello%20world");
2058 // end::snippet_encoding_3[]
2059 1 }
2060
2061 {
2062 // tag::snippet_encoding_4[]
2063 1 boost::core::string_view e = "hello world";
2064 1 std::string s;
2065
1/1
✓ Branch 3 taken 1 time.
1 s.reserve(encoded_size(e, pchars));
2066 1 encode(e, pchars, {}, string_token::assign_to(s));
2067
1/1
✓ Branch 2 taken 1 time.
1 assert(s == "hello%20world");
2068 // end::snippet_encoding_4[]
2069 1 }
2070
2071 {
2072 // tag::snippet_encoding_5[]
2073 1 boost::core::string_view e = "hello world";
2074 1 std::string s;
2075
1/1
✓ Branch 3 taken 1 time.
1 s.resize(encoded_size(e, pchars));
2076
1/1
✓ Branch 3 taken 1 time.
1 encode(&s[0], s.size(), e, pchars);
2077
1/1
✓ Branch 2 taken 1 time.
1 assert(s == "hello%20world");
2078 // end::snippet_encoding_5[]
2079 1 }
2080
2081 {
2082 // tag::snippet_encoding_6[]
2083
1/1
✓ Branch 1 taken 1 time.
1 pct_string_view sv = "hello%20world";
2084
2/3
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 time.
1 assert(sv == "hello%20world");
2085 // end::snippet_encoding_6[]
2086 }
2087
2088 {
2089 // tag::snippet_encoding_7[]
2090 boost::system::result<pct_string_view> rs =
2091 1 make_pct_string_view("hello%20world");
2092
1/1
✓ Branch 2 taken 1 time.
1 assert(rs.has_value());
2093
1/1
✓ Branch 2 taken 1 time.
1 pct_string_view sv = rs.value();
2094
2/3
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 time.
1 assert(sv == "hello%20world");
2095 // end::snippet_encoding_7[]
2096 }
2097
2098 {
2099 // tag::snippet_encoding_8[]
2100
1/1
✓ Branch 1 taken 1 time.
1 pct_string_view s = "path/to/file";
2101 1 url u;
2102
1/1
✓ Branch 1 taken 1 time.
1 u.set_encoded_path(s);
2103
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "path/to/file");
2104 // end::snippet_encoding_8[]
2105 1 }
2106
2107 {
2108 // tag::snippet_encoding_9[]
2109 1 url u;
2110
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 u.set_encoded_path("path/to/file");
2111
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "path/to/file");
2112 // end::snippet_encoding_9[]
2113 1 }
2114
2115 {
2116 // tag::snippet_encoding_10[]
2117
1/1
✓ Branch 1 taken 1 time.
1 url_view uv("path/to/file");
2118 1 url u;
2119
1/1
✓ Branch 2 taken 1 time.
1 u.set_encoded_path(uv.encoded_path());
2120
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "path/to/file");
2121 // end::snippet_encoding_10[]
2122 1 }
2123
2124 {
2125 // tag::snippet_encoding_11[]
2126
1/1
✓ Branch 1 taken 1 time.
1 pct_string_view es("hello%20world");
2127
2/3
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 time.
1 assert(es == "hello%20world");
2128
2129
1/1
✓ Branch 2 taken 1 time.
1 decode_view dv("hello%20world");
2130
2/3
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 time.
1 assert(dv == "hello world");
2131 // end::snippet_encoding_11[]
2132 }
2133
2134 {
2135 // tag::snippet_encoding_12[]
2136 boost::system::result<pct_string_view> rs =
2137 1 make_pct_string_view("hello%20world");
2138
1/1
✓ Branch 2 taken 1 time.
1 assert(rs.has_value());
2139
1/1
✓ Branch 2 taken 1 time.
1 pct_string_view s = rs.value();
2140 1 decode_view dv = *s;
2141
2/3
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 time.
1 assert(dv == "hello world");
2142 // end::snippet_encoding_12[]
2143 }
2144
2145 {
2146 // tag::snippet_encoding_13[]
2147 url_view u =
2148
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 parse_relative_ref("user/john%20doe/profile%20photo.jpg").value();
2149 std::vector<std::string> route =
2150
1/1
✓ Branch 1 taken 1 time.
2 {"user", "john doe", "profile photo.jpg"};
2151 1 auto segs = u.encoded_segments();
2152 1 auto it0 = segs.begin();
2153 1 auto end0 = segs.end();
2154 1 auto it1 = route.begin();
2155 1 auto end1 = route.end();
2156 1 while (
2157
5/6
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 1 time.
7 it0 != end0 &&
2158 3 it1 != end1)
2159 {
2160 3 pct_string_view seg0 = *it0;
2161 3 decode_view dseg0 = *seg0;
2162 3 boost::core::string_view seg1 = *it1;
2163
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (dseg0 == seg1)
2164 {
2165 3 ++it0;
2166 3 ++it1;
2167 }
2168 else
2169 {
2170 break;
2171 }
2172 }
2173
2/4
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
1 bool route_match = it0 == end0 && it1 == end1;
2174
1/1
✓ Branch 1 taken 1 time.
1 assert(route_match);
2175 // end::snippet_encoding_13[]
2176 1 }
2177
2178 {
2179 // tag::snippet_encoding_14[]
2180
1/1
✓ Branch 1 taken 1 time.
1 pct_string_view s = "user/john%20doe/profile%20photo.jpg";
2181 1 std::string buf;
2182
1/1
✓ Branch 2 taken 1 time.
1 buf.resize(s.decoded_size());
2183
1/1
✓ Branch 3 taken 1 time.
1 s.decode({}, string_token::assign_to(buf));
2184
1/1
✓ Branch 2 taken 1 time.
1 assert(buf == "user/john doe/profile photo.jpg");
2185 // end::snippet_encoding_14[]
2186 1 }
2187 1 }
2188
2189 void
2190 1 decoding_helpers()
2191 {
2192 {
2193 // tag::snippet_decoding_helpers_1[]
2194 1 boost::core::string_view encoded = "name%3Dboost+url";
2195 1 encoding_opts opt;
2196 1 opt.space_as_plus = true;
2197
2198
1/1
✓ Branch 3 taken 1 time.
1 auto const needed = decoded_size(encoded).value();
2199 1 std::string buffer;
2200
1/1
✓ Branch 1 taken 1 time.
1 buffer.resize(needed);
2201
2/2
✓ Branch 2 taken 1 time.
✓ Branch 7 taken 1 time.
1 auto const written = decode(&buffer[0], buffer.size(), encoded, opt).value();
2202
1/1
✓ Branch 1 taken 1 time.
1 buffer.resize(written);
2203
2204
1/1
✓ Branch 2 taken 1 time.
1 assert(buffer == "name=boost url");
2205 // end::snippet_decoding_helpers_1[]
2206 1 }
2207
2208 {
2209 // tag::snippet_decoding_helpers_2[]
2210 1 encoding_opts opt;
2211 1 opt.space_as_plus = true;
2212
2213
1/1
✓ Branch 5 taken 1 time.
1 auto plain = decode(boost::core::string_view("city%3DSan+Jose"), opt).value();
2214
1/1
✓ Branch 2 taken 1 time.
1 assert(plain == "city=San Jose");
2215
2216
1/1
✓ Branch 1 taken 1 time.
1 std::string scratch = "prefix:";
2217
1/1
✓ Branch 6 taken 1 time.
1 decode(boost::core::string_view("value%2F42"), {}, string_token::append_to(scratch)).value();
2218
1/1
✓ Branch 2 taken 1 time.
1 assert(scratch == "prefix:value/42");
2219 // end::snippet_decoding_helpers_2[]
2220 1 }
2221 1 }
2222
2223 void
2224 1 readme_snippets()
2225 {
2226 {
2227 // Parse a URL. This allocates no memory. The view
2228 // references the character buffer without taking ownership.
2229 //
2230
1/1
✓ Branch 1 taken 1 time.
1 url_view uv( "https://www.example.com/path/to/file.txt?id=1001&name=John%20Doe&results=full" );
2231
2232 // Print the query parameters with percent-decoding applied
2233 //
2234
3/3
✓ Branch 4 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 1 time.
4 for( auto v : uv.params() )
2235 {
2236
4/4
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
3 std::cout << v.key << "=" << v.value << " ";
2237 3 }
2238
2239
2/2
✓ Branch 11 taken 1 time.
✓ Branch 14 taken 1 time.
1 BOOST_TEST(equal_range(
2240 uv.params(),
2241 {
2242 param_view{"id", "1001", true},
2243 param_view{"name", "John Doe", true},
2244 param_view{"results", "full", true}
2245 }));
2246
2247 // Prints: id=1001 name=John Doe results=full
2248
2249 // Create a modifiable copy of `uv`, with ownership of the buffer
2250 //
2251
1/1
✓ Branch 1 taken 1 time.
1 url u = uv;
2252
2253 // Change some elements in the URL
2254 //
2255
1/1
✓ Branch 2 taken 1 time.
1 u.set_scheme( "http" )
2256
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 .set_encoded_host( "boost.org" )
2257
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 .set_encoded_path( "/index.htm" )
2258 1 .remove_query()
2259 1 .remove_fragment()
2260
1/1
✓ Branch 4 taken 1 time.
1 .params().append( {"key", "value"} );
2261
2262
1/1
✓ Branch 1 taken 1 time.
1 std::cout << u;
2263
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.buffer() == "http://boost.org/index.htm?key=value");
2264 1 }
2265
2266 {
2267 boost::core::string_view s =
2268 1 "https://user:pass@example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe+Jingleheimer%2DSchmidt#page%20anchor";
2269 {
2270
1/1
✓ Branch 1 taken 1 time.
1 boost::system::result<url_view> r = parse_uri( s );
2271
1/1
✓ Branch 2 taken 1 time.
1 BOOST_TEST(r.has_value());
2272 }
2273
2274 {
2275
1/1
✓ Branch 1 taken 1 time.
1 boost::system::result<url_view> r = parse_uri( s );
2276
1/1
✓ Branch 2 taken 1 time.
1 url_view u = r.value();
2277
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST(!u.buffer().empty());
2278 }
2279
2280 {
2281
1/1
✓ Branch 1 taken 1 time.
1 boost::system::result<url_view> r = parse_uri( s );
2282 1 url_view u = *r;
2283
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST(!u.buffer().empty());
2284 }
2285 }
2286
2287 {
2288
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://user:pass@example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe+Jingleheimer%2DSchmidt#page%20anchor" );
2289
1/1
✓ Branch 4 taken 1 time.
1 assert(u.scheme() == "https");
2290
1/1
✓ Branch 5 taken 1 time.
1 assert(u.authority().buffer() == "user:pass@example.com:443");
2291
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.userinfo() == "user:pass");
2292
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.user() == "user");
2293
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.password() == "pass");
2294
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.host() == "example.com");
2295
1/1
✓ Branch 4 taken 1 time.
1 assert(u.port() == "443");
2296
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "/path/to/my-file.txt");
2297
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.query() == "id=42&name=John Doe+Jingleheimer-Schmidt");
2298
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.fragment() == "page anchor");
2299 }
2300
2301 {
2302
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://user:pass@example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe+Jingleheimer%2DSchmidt#page%20anchor" );
2303
3/3
✓ Branch 4 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 1 time.
4 for (auto seg: u.segments())
2304
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 std::cout << seg << "\n";
2305
1/1
✓ Branch 1 taken 1 time.
1 std::cout << "\n";
2306
2307
3/3
✓ Branch 4 taken 2 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 1 time.
3 for (auto param: u.params())
2308
4/4
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
2 std::cout << param.key << ": " << param.value << "\n";
2309
1/1
✓ Branch 1 taken 1 time.
1 std::cout << "\n";
2310 }
2311
2312 {
2313
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://user:pass@example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe+Jingleheimer%2DSchmidt#page%20anchor" );
2314
1/1
✓ Branch 2 taken 1 time.
1 std::string h = u.host();
2315
1/1
✓ Branch 2 taken 1 time.
1 assert(h == "example.com");
2316 1 }
2317
2318 {
2319
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://user:pass@example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe+Jingleheimer%2DSchmidt#page%20anchor" );
2320
1/1
✓ Branch 1 taken 1 time.
1 std::string h2 = "host: ";
2321
1/1
✓ Branch 2 taken 1 time.
1 u.host(string_token::append_to(h2));
2322
1/1
✓ Branch 2 taken 1 time.
1 assert(h2 == "host: example.com");
2323 1 }
2324
2325 {
2326
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url_view u = parse_uri( "http://www.example.com" ).value();
2327
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.fragment().empty());
2328
1/1
✓ Branch 2 taken 1 time.
1 assert(!u.has_fragment());
2329 }
2330
2331 {
2332
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url_view u = parse_uri( "http://www.example.com/#" ).value();
2333
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.fragment().empty());
2334
1/1
✓ Branch 2 taken 1 time.
1 assert(u.has_fragment());
2335 }
2336
2337 {
2338
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://user:pass@example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe+Jingleheimer%2DSchmidt#page%20anchor" );
2339 std::cout <<
2340
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 "url : " << u << "\n"
2341 "scheme : " << u.scheme() << "\n"
2342
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 12 taken 1 time.
1 "authority : " << u.encoded_authority() << "\n"
2343
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "userinfo : " << u.encoded_userinfo() << "\n"
2344
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "user : " << u.encoded_user() << "\n"
2345
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "password : " << u.encoded_password() << "\n"
2346
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "host : " << u.encoded_host() << "\n"
2347 "port : " << u.port() << "\n"
2348
4/4
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 12 taken 1 time.
1 "path : " << u.encoded_path() << "\n"
2349
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 "query : " << u.encoded_query() << "\n"
2350
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
1 "fragment : " << u.encoded_fragment() << "\n";
2351 }
2352
2353 {
2354
1/1
✓ Branch 2 taken 1 time.
1 decode_view dv("id=42&name=John%20Doe%20Jingleheimer%2DSchmidt");
2355
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::cout << dv << "\n";
2356 }
2357
2358 {
2359
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 url u1 = parse_uri( "https://www.example.com" ).value();
2360
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 url u2 = parse_uri( "https://boost.org" ).value();
2361
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 u1.set_host(u2.host());
2362
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 10 taken 1 time.
1 BOOST_TEST(u1.host() == u2.host());
2363 1 }
2364
2365 {
2366
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://user:pass@example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe+Jingleheimer%2DSchmidt#page%20anchor" );
2367 1 boost::filesystem::path p;
2368
3/3
✓ Branch 4 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 1 time.
4 for (auto seg: u.segments())
2369 6 p.append(seg.begin(), seg.end());
2370
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
1 std::cout << "path: " << p << "\n";
2371 1 }
2372
2373 {
2374 auto match = [](
2375 std::vector<std::string> const& route,
2376 url_view u)
2377 {
2378 auto segs = u.segments();
2379 if (route.size() != segs.size())
2380 return false;
2381 return std::equal(
2382 route.begin(),
2383 route.end(),
2384 segs.begin());
2385 };
2386 boost::ignore_unused(match);
2387 }
2388
2389 {
2390
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://www.example.com/community/reviews.html" );
2391 1 auto handle_route = [](
2392 std::vector<std::string> const&,
2393 url_view)
2394 {
2395 1 };
2396
2397 1 auto match = [](
2398 std::vector<std::string> const& route,
2399 url_view u)
2400 {
2401 1 auto segs = u.segments();
2402
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 if (route.size() != segs.size())
2403 return false;
2404
1/1
✓ Branch 4 taken 1 time.
1 return std::equal(
2405 route.begin(),
2406 route.end(),
2407 1 segs.begin());
2408 };
2409
2410 std::vector<std::string> route =
2411
1/1
✓ Branch 1 taken 1 time.
2 {"community", "reviews.html"};
2412
2/3
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
1 if (match(route, u))
2413 {
2414 1 handle_route(route, u);
2415 }
2416 1 }
2417
2418 {
2419
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://www.example.com/path/to/file.txt" );
2420 1 segments_encoded_view segs = u.encoded_segments();
2421
2/2
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 1 time.
4 for( auto v : segs )
2422 {
2423
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 std::cout << v << "\n";
2424 }
2425 }
2426
2427 {
2428
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://www.example.com/path/to/my%2dfile.txt" );
2429 1 segments_encoded_view segs2 = u.encoded_segments();
2430
2/2
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 1 time.
4 for( pct_string_view v : segs2 )
2431 {
2432 3 decode_view dv2 = *v;
2433
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 std::cout << dv2 << "\n";
2434 }
2435 }
2436
2437 {
2438
1/1
✓ Branch 1 taken 1 time.
1 url_view u( "https://www.example.com/path/to/file.txt?id=42&name=John%20Doe" );
2439 1 params_encoded_view params_ref = u.encoded_params();
2440
2441
2/2
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1 time.
3 for( auto v : params_ref )
2442 {
2443 2 decode_view dk(v.key);
2444 2 decode_view dv3(v.value);
2445 std::cout <<
2446
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 "key = " << dk <<
2447
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
2 ", value = " << dv3 << "\n";
2448 }
2449 }
2450
2451 {
2452 1 boost::core::string_view s = "https://www.example.com";
2453
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
1 url u = parse_uri( s ).value();
2454
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST(!u.buffer().empty());
2455 1 }
2456
2457 {
2458 1 boost::core::string_view s = "https://www.example.com";
2459
3/3
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
1 static_url<1024> u = parse_uri( s ).value();
2460
1/1
✓ Branch 3 taken 1 time.
1 BOOST_TEST(!u.buffer().empty());
2461 1 }
2462
2463 {
2464
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 url u = parse_uri( "https://www.example.com" ).value();
2465
1/1
✓ Branch 2 taken 1 time.
1 u.set_scheme( "https" );
2466
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.scheme() == "https");
2467 1 }
2468
2469 {
2470
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 url u = parse_uri( "https://www.example.com" ).value();
2471
1/1
✓ Branch 1 taken 1 time.
1 u.set_scheme_id( scheme::https ); // equivalent to u.set_scheme( "https" );
2472
1/1
✓ Branch 4 taken 1 time.
1 BOOST_TEST(u.scheme() == "https");
2473 1 }
2474
2475 {
2476
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 url u = parse_uri( "https://user:pass@example.com:443" ).value();
2477
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
1 u.set_host_ipv4( ipv4_address( "192.168.0.1" ) )
2478
1/1
✓ Branch 1 taken 1 time.
1 .set_port_number( 8080 )
2479 1 .remove_userinfo();
2480
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::cout << u << "\n";
2481 1 }
2482
2483 {
2484
3/3
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
1 url u = parse_uri( "http://www.example.com/?name=John" ).value();
2485 1 params_ref p = u.params();
2486
1/1
✓ Branch 6 taken 1 time.
1 p.replace(p.find("name"), {"name", "John Doe"});
2487
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::cout << u << "\n";
2488 1 }
2489
2490 // I have spent a lot of time on this and have no
2491 // idea how to fix this bug in GCC 4.8 and GCC 5.0
2492 // without help from the pros.
2493 #if !BOOST_WORKAROUND( BOOST_GCC_VERSION, < 60000 )
2494 {
2495
1/1
✓ Branch 2 taken 1 time.
1 url u = format("{}://{}:{}/rfc/{}", "https", "www.ietf.org", 80, "rfc2396.txt");
2496
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://www.ietf.org:80/rfc/rfc2396.txt");
2497 1 }
2498
2499 {
2500
1/1
✓ Branch 2 taken 1 time.
1 url u = format("https://{}/{}", "www.boost.org", "Hello world!");
2501
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://www.boost.org/Hello%20world!");
2502 1 }
2503
2504 {
2505
1/1
✓ Branch 2 taken 1 time.
1 url u = format("{}:{}", "mailto", "someone@example.com");
2506
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "mailto:someone@example.com");
2507
1/1
✓ Branch 4 taken 1 time.
1 assert(u.scheme() == "mailto");
2508
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "someone@example.com");
2509 1 }
2510
2511 {
2512
1/1
✓ Branch 2 taken 1 time.
1 url u = format("{}{}", "mailto:", "someone@example.com");
2513
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "mailto%3Asomeone@example.com");
2514
1/1
✓ Branch 2 taken 1 time.
1 assert(!u.has_scheme());
2515
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 assert(u.path() == "mailto:someone@example.com");
2516
2/3
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
1 assert(u.encoded_path() == "mailto%3Asomeone@example.com");
2517 1 }
2518
2519 {
2520 1 static_url<50> u;
2521
1/1
✓ Branch 2 taken 1 time.
1 format_to(u, "{}://{}:{}/rfc/{}", "https", "www.ietf.org", 80, "rfc2396.txt");
2522
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://www.ietf.org:80/rfc/rfc2396.txt");
2523 1 }
2524
2525 {
2526
1/1
✓ Branch 2 taken 1 time.
1 url u = format("{0}://{2}:{1}/{3}{4}{3}", "https", 80, "www.ietf.org", "abra", "cad");
2527
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://www.ietf.org:80/abracadabra");
2528 1 }
2529
2530 {
2531
2/2
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
1 url u = format("https://example.com/~{username}", arg("username", "mark"));
2532
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://example.com/~mark");
2533 1 }
2534
2535 {
2536 1 boost::core::string_view fmt = "{scheme}://{host}:{port}/{dir}/{file}";
2537
6/6
✓ Branch 2 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 14 taken 1 time.
✓ Branch 18 taken 1 time.
✓ Branch 21 taken 1 time.
1 url u = format(fmt, {{"scheme", "https"}, {"port", 80}, {"host", "example.com"}, {"dir", "path/to"}, {"file", "file.txt"}});
2538
1/1
✓ Branch 4 taken 1 time.
1 assert(u.buffer() == "https://example.com:80/path/to/file.txt");
2539 1 }
2540 #endif
2541 1 }
2542
2543 // tag::snippet_using_static_pool_1[]
2544 // VFALCO NOPE
2545 // end::snippet_using_static_pool_1[]
2546
2547 namespace {
2548
2549 class cout_redirect
2550 {
2551 std::ostream& os_;
2552 std::streambuf* old_;
2553
2554 public:
2555 13 cout_redirect(
2556 std::ostream& os,
2557 std::streambuf* newbuf) noexcept
2558 13 : os_(os)
2559 13 , old_(os.rdbuf(newbuf))
2560 {
2561 13 }
2562
2563 13 ~cout_redirect()
2564 {
2565 13 os_.rdbuf(old_);
2566 13 }
2567 };
2568
2569 void
2570 13 run_silent(void (*fn)())
2571 {
2572
1/1
✓ Branch 1 taken 13 times.
13 std::ostringstream oss;
2573 13 cout_redirect guard(std::cout, oss.rdbuf());
2574 boost::ignore_unused(guard);
2575
1/1
✓ Branch 1 taken 13 times.
13 fn();
2576 13 }
2577
2578 } // namespace
2579
2580 namespace boost {
2581 namespace urls {
2582
2583 class snippets_test
2584 {
2585 public:
2586 void
2587 1 run()
2588 {
2589 1 run_silent(&using_url_views);
2590 1 run_silent(&using_urls);
2591 1 run_silent(&parsing_urls);
2592 1 parsing_components();
2593 1 formatting_components();
2594 1 run_silent(&parsing_scheme);
2595 1 parsing_compound_scheme();
2596 1 run_silent(&parsing_authority);
2597 1 run_silent(&parsing_path);
2598 1 run_silent(&parsing_query);
2599 1 run_silent(&parsing_fragment);
2600 1 run_silent(&using_modifying);
2601 1 run_silent(&grammar_parse);
2602 1 run_silent(&grammar_customization);
2603 1 run_silent(&modifying_path);
2604 1 normalizing();
2605 1 decode_with_token();
2606 1 encoding();
2607 1 decoding_helpers();
2608 1 run_silent(&readme_snippets);
2609
2610 1 BOOST_TEST_PASS();
2611 1 }
2612 };
2613
2614 TEST_SUITE(snippets_test, "boost.url.snippets");
2615
2616 } // urls
2617 } // boost
2618