Debian Codegraph @

LspCpp/third_party/uri/src/uri.cpp

raw
1 // Copyright 2012-2017 Glyn Matthews.
2 // Copyright 2012 Google, Inc.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <cassert>
8 #include <locale>
9 #include <algorithm>
10 #include <functional>
11 #include "network/uri/uri.hpp"
12 #include "detail/uri_parse.hpp"
13 #include "detail/uri_advance_parts.hpp"
14 #include "detail/uri_percent_encode.hpp"
15 #include "detail/uri_normalize.hpp"
16 #include "detail/uri_resolve.hpp"
17 #include "detail/algorithm.hpp"
18
19 namespace network {
20 namespace {
21 // With the parser, we use string_views, which are mutable. However,
22 // there are times (e.g. during normalization), when we want a part
23 // to be mutable. This function returns a pair of
24 // std::string::iterators in the same range as the URI part.
25 //
26 inline std::pair<std::string::iterator, std::string::iterator> mutable_part(
27 std::string &str, detail::uri_part part) {
28 auto view = string_view(str);
29
30 auto first_index = std::distance(std::begin(view), std::begin(part));
31 auto first = std::begin(str);
32 std::advance(first, first_index);
33
34 auto last_index = std::distance(std::begin(view), std::end(part));
35 auto last = std::begin(str);
36 std::advance(last, last_index);
37
38 return std::make_pair(first, last);
39 }
40
41 // This is a convenience function that converts a part of a
42 // std::string to a string_view.
43 inline string_view to_string_view(const std::string &uri,
44 detail::uri_part part) {
45 if (!part.empty()) {
46 const char *c_str = uri.c_str();
47 const char *part_begin = &(*(std::begin(part)));
48 std::advance(c_str, std::distance(c_str, part_begin));
49 return string_view(c_str, std::distance(std::begin(part), std::end(part)));
50 }
51 return string_view();
52 }
53
54 inline optional<std::string> make_arg(optional<string_view> view) {
55 if (view) {
56 return view->to_string();
57 }
58 return nullopt;
59 }
60
61 template <class T>
62 inline void ignore(T) {}
63 } // namespace
64
65 void uri::initialize(optional<string_type> scheme,
66 optional<string_type> user_info,
67 optional<string_type> host, optional<string_type> port,
68 optional<string_type> path, optional<string_type> query,
69 optional<string_type> fragment) {
70 if (scheme) {
71 uri_.append(*scheme);
72 }
73
74 if (user_info || host || port) {
75 if (scheme) {
76 uri_.append("://");
77 }
78
79 if (user_info) {
80 uri_.append(*user_info);
81 uri_.append("@");
82 }
83
84 if (host) {
85 uri_.append(*host);
86 } else {
87 throw uri_builder_error();
88 }
89
90 if (port) {
91 uri_.append(":");
92 uri_.append(*port);
93 }
94 } else {
95 if (scheme) {
96 if (path || query || fragment) {
97 uri_.append(":");
98 } else {
99 throw uri_builder_error();
100 }
101 }
102 }
103
104 if (path) {
105 // if the URI is not opaque and the path is not already prefixed
106 // with a '/', add one.
107 if (host && (!path->empty() && path->front() != '/')) {
108 path = "/" + *path;
109 }
110 uri_.append(*path);
111 }
112
113 if (query) {
114 uri_.append("?");
115 uri_.append(*query);
116 }
117
118 if (fragment) {
119 uri_.append("#");
120 uri_.append(*fragment);
121 }
122
123 uri_view_ = string_view(uri_);
124
125 auto it = std::begin(uri_view_);
126 if (scheme) {
127 uri_parts_.scheme = detail::copy_part(*scheme, it);
128 // ignore : and ://
129 if (*it == ':') {
130 ++it;
131 }
132 if (*it == '/' && *(it + 1) == '/') {
133 it += 2;
134 }
135 }
136
137 if (user_info) {
138 uri_parts_.hier_part.user_info = detail::copy_part(*user_info, it);
139 ++it; // ignore @
140 }
141
142 if (host) {
143 uri_parts_.hier_part.host = detail::copy_part(*host, it);
144 }
145
146 if (port) {
147 ++it; // ignore :
148 uri_parts_.hier_part.port = detail::copy_part(*port, it);
149 }
150
151 if (path) {
152 uri_parts_.hier_part.path = detail::copy_part(*path, it);
153 }
154
155 if (query) {
156 ++it; // ignore ?
157 uri_parts_.query = detail::copy_part(*query, it);
158 }
159
160 if (fragment) {
161 ++it; // ignore #
162 uri_parts_.fragment = detail::copy_part(*fragment, it);
163 }
164 }
165
166 uri::uri() : uri_view_(uri_) {}
167
168 uri::uri(const uri &other) : uri_(other.uri_), uri_view_(uri_) {
169 detail::advance_parts(uri_view_, uri_parts_, other.uri_parts_);
170 }
171
172 uri::uri(const uri_builder &builder) {
173 initialize(builder.scheme_, builder.user_info_, builder.host_, builder.port_,
174 builder.path_, builder.query_, builder.fragment_);
175 }
176
177 uri::uri(uri &&other) noexcept
178 : uri_(std::move(other.uri_)),
179 uri_view_(uri_),
180 uri_parts_(std::move(other.uri_parts_)) {
181 detail::advance_parts(uri_view_, uri_parts_, other.uri_parts_);
182 other.uri_.clear();
183 other.uri_view_ = string_view(other.uri_);
184 other.uri_parts_ = detail::uri_parts();
185 }
186
187 uri::~uri() {}
188
189 uri &uri::operator=(uri other) {
190 other.swap(*this);
191 return *this;
192 }
193
194 void uri::swap(uri &other) noexcept {
195 uri_.swap(other.uri_);
196 uri_view_ = uri_;
197 other.uri_view_ = other.uri_;
198
199 const auto this_parts = uri_parts_;
200 uri_parts_.clear();
201 detail::advance_parts(uri_view_, uri_parts_, other.uri_parts_);
202 other.uri_parts_.clear();
203 detail::advance_parts(other.uri_view_, other.uri_parts_, this_parts);
204 }
205
206 uri::const_iterator uri::begin() const noexcept { return uri_view_.begin(); }
207
208 uri::const_iterator uri::end() const noexcept { return uri_view_.end(); }
209
210 bool uri::has_scheme() const noexcept {
211 return static_cast<bool>(uri_parts_.scheme);
212 }
213
214 uri::string_view uri::scheme() const noexcept {
215 return has_scheme() ? to_string_view(uri_, *uri_parts_.scheme)
216 : string_view{};
217 }
218
219 bool uri::has_user_info() const noexcept {
220 return static_cast<bool>(uri_parts_.hier_part.user_info);
221 }
222
223 uri::string_view uri::user_info() const noexcept {
224 return has_user_info() ? to_string_view(uri_, *uri_parts_.hier_part.user_info)
225 : string_view{};
226 }
227
228 bool uri::has_host() const noexcept {
229 return static_cast<bool>(uri_parts_.hier_part.host);
230 }
231
232 uri::string_view uri::host() const noexcept {
233 return has_host() ? to_string_view(uri_, *uri_parts_.hier_part.host)
234 : string_view{};
235 }
236
237 bool uri::has_port() const noexcept {
238 return static_cast<bool>(uri_parts_.hier_part.port);
239 }
240
241 uri::string_view uri::port() const noexcept {
242 return has_port() ? to_string_view(uri_, *uri_parts_.hier_part.port)
243 : string_view{};
244 }
245
246 bool uri::has_path() const noexcept {
247 return static_cast<bool>(uri_parts_.hier_part.path);
248 }
249
250 uri::string_view uri::path() const noexcept {
251 return has_path() ? to_string_view(uri_, *uri_parts_.hier_part.path)
252 : string_view{};
253 }
254
255 bool uri::has_query() const noexcept {
256 return static_cast<bool>(uri_parts_.query);
257 }
258
259 uri::string_view uri::query() const noexcept {
260 return has_query() ? to_string_view(uri_, *uri_parts_.query) : string_view{};
261 }
262
263 uri::query_iterator::query_iterator() : query_{}, kvp_{} {}
264
265 uri::query_iterator::query_iterator(optional<detail::uri_part> query)
266 : query_(query), kvp_{} {
267 if (query_ && query_->empty()) {
268 query_ = nullopt;
269 } else {
270 assign_kvp();
271 }
272 }
273
274 uri::query_iterator::query_iterator(const query_iterator &other)
275 : query_(other.query_), kvp_(other.kvp_) {}
276
277 uri::query_iterator &uri::query_iterator::operator=(
278 const query_iterator &other) {
279 auto tmp(other);
280 swap(tmp);
281 return *this;
282 }
283
284 uri::query_iterator::reference uri::query_iterator::operator++() noexcept {
285 increment();
286 return kvp_;
287 }
288
289 uri::query_iterator::value_type uri::query_iterator::operator++(int) noexcept {
290 auto original = kvp_;
291 increment();
292 return original;
293 }
294
295 uri::query_iterator::reference uri::query_iterator::operator*() const noexcept {
296 return kvp_;
297 }
298
299 uri::query_iterator::pointer uri::query_iterator::operator->() const noexcept {
300 return std::addressof(kvp_);
301 }
302
303 bool uri::query_iterator::operator==(const query_iterator &other) const
304 noexcept {
305 if (!query_ && !other.query_) {
306 return true;
307 } else if (query_ && other.query_) {
308 // since we're comparing substrings, the address of the first
309 // element in each iterator must be the same
310 return std::addressof(kvp_.first) == std::addressof(other.kvp_.first);
311 }
312 return false;
313 }
314
315 void uri::query_iterator::swap(query_iterator &other) noexcept {
316 std::swap(query_, other.query_);
317 std::swap(kvp_, other.kvp_);
318 }
319
320 void uri::query_iterator::advance_to_next_kvp() noexcept {
321 auto first = std::begin(*query_), last = std::end(*query_);
322
323 auto sep_it = std::find_if(
324 first, last, [](char c) -> bool { return c == '&' || c == ';'; });
325
326 if (sep_it != last) {
327 ++sep_it; // skip next separator
328 }
329
330 // reassign query to the next element
331 query_ = detail::uri_part(sep_it, last);
332 }
333
334 void uri::query_iterator::assign_kvp() noexcept {
335 auto first = std::begin(*query_), last = std::end(*query_);
336
337 auto sep_it = std::find_if(
338 first, last, [](char c) -> bool { return c == '&' || c == ';'; });
339 auto eq_it =
340 std::find_if(first, sep_it, [](char c) -> bool { return c == '='; });
341
342 kvp_.first = string_view(std::addressof(*first), std::distance(first, eq_it));
343 if (eq_it != sep_it) {
344 ++eq_it; // skip '=' symbol
345 }
346 kvp_.second =
347 string_view(std::addressof(*eq_it), std::distance(eq_it, sep_it));
348 }
349
350 void uri::query_iterator::increment() noexcept {
351 assert(query_);
352
353 if (!query_->empty()) {
354 advance_to_next_kvp();
355 assign_kvp();
356 }
357
358 if (query_->empty()) {
359 query_ = nullopt;
360 }
361 }
362
363 uri::query_iterator uri::query_begin() const noexcept {
364 return has_query() ? uri::query_iterator{uri_parts_.query}
365 : uri::query_iterator{};
366 }
367
368 uri::query_iterator uri::query_end() const noexcept {
369 return uri::query_iterator{};
370 }
371
372 bool uri::has_fragment() const noexcept {
373 return static_cast<bool>(uri_parts_.fragment);
374 }
375
376 uri::string_view uri::fragment() const noexcept {
377 return has_fragment() ? to_string_view(uri_, *uri_parts_.fragment)
378 : string_view{};
379 }
380
381 bool uri::has_authority() const noexcept { return has_host(); }
382
383 uri::string_view uri::authority() const noexcept {
384 if (!has_host()) {
385 return string_view{};
386 }
387
388 auto host = this->host();
389
390 auto user_info = string_view{};
391 if (has_user_info()) {
392 user_info = this->user_info();
393 }
394
395 auto port = string_view{};
396 if (has_port()) {
397 port = this->port();
398 }
399
400 auto first = std::begin(host), last = std::end(host);
401 if (has_user_info() && !user_info.empty()) {
402 first = std::begin(user_info);
403 } else if (host.empty() && has_port() && !port.empty()) {
404 first = std::begin(port);
405 --first; // include ':' before port
406 }
407
408 if (host.empty()) {
409 if (has_port() && !port.empty()) {
410 last = std::end(port);
411 } else if (has_user_info() && !user_info.empty()) {
412 last = std::end(user_info);
413 ++last; // include '@'
414 }
415 } else if (has_port()) {
416 if (port.empty()) {
417 ++last; // include ':' after host
418 } else {
419 last = std::end(port);
420 }
421 }
422
423 return string_view(first, std::distance(first, last));
424 }
425
426 std::string uri::string() const { return uri_; }
427
428 std::wstring uri::wstring() const {
429 return std::wstring(std::begin(*this), std::end(*this));
430 }
431
432 std::u16string uri::u16string() const {
433 return std::u16string(std::begin(*this), std::end(*this));
434 }
435
436 std::u32string uri::u32string() const {
437 return std::u32string(std::begin(*this), std::end(*this));
438 }
439
440 uri::string_view uri::view() const noexcept { return uri_view_; }
441
442 bool uri::empty() const noexcept { return uri_.empty(); }
443
444 bool uri::is_absolute() const noexcept { return has_scheme(); }
445
446 bool uri::is_opaque() const noexcept {
447 return (is_absolute() && !has_authority());
448 }
449
450 uri uri::normalize(uri_comparison_level level) const {
451 string_type normalized(uri_);
452 string_view normalized_view(normalized);
453 detail::uri_parts parts;
454 detail::advance_parts(normalized_view, parts, uri_parts_);
455
456 if (uri_comparison_level::syntax_based == level) {
457 // All alphabetic characters in the scheme and host are
458 // lower-case...
459 if (parts.scheme) {
460 std::string::iterator first, last;
461 std::tie(first, last) = mutable_part(normalized, *parts.scheme);
462 std::transform(first, last, first,
463 [](char ch) { return std::tolower(ch, std::locale()); });
464 }
465
466 // if (parts.hier_part.host) {
467 // std::string::iterator first, last;
468 // std::tie(first, last) = mutable_part(normalized,
469 // *parts.hier_part.host); std::transform(first, last, first,
470 // [](char ch) { return std::tolower(ch, std::locale());
471 // });
472 // }
473
474 // ...except when used in percent encoding
475 detail::for_each(normalized,
476 detail::percent_encoded_to_upper<std::string>());
477
478 // parts are invalidated here
479 // there's got to be a better way of doing this that doesn't
480 // mean parsing again (twice!)
481 normalized.erase(detail::decode_encoded_unreserved_chars(
482 std::begin(normalized), std::end(normalized)),
483 std::end(normalized));
484 normalized_view = string_view(normalized);
485
486 // need to parse the parts again as the underlying string has changed
487 const_iterator it = std::begin(normalized_view),
488 last = std::end(normalized_view);
489 bool is_valid = detail::parse(it, last, parts);
490 ignore(is_valid);
491 assert(is_valid);
492
493 if (parts.hier_part.path) {
494 uri::string_type path = detail::normalize_path_segments(
495 to_string_view(normalized, *parts.hier_part.path));
496
497 // put the normalized path back into the uri
498 optional<string_type> query, fragment;
499 if (parts.query) {
500 query = parts.query->to_string();
501 }
502
503 if (parts.fragment) {
504 fragment = parts.fragment->to_string();
505 }
506
507 auto path_begin = std::begin(normalized);
508 auto path_range = mutable_part(normalized, *parts.hier_part.path);
509 std::advance(path_begin, std::distance(path_begin, path_range.first));
510 normalized.erase(path_begin, std::end(normalized));
511 normalized.append(path);
512
513 if (query) {
514 normalized.append("?");
515 normalized.append(*query);
516 }
517
518 if (fragment) {
519 normalized.append("#");
520 normalized.append(*fragment);
521 }
522 }
523 }
524
525 return uri(normalized);
526 }
527
528 uri uri::make_relative(const uri &other) const {
529 if (is_opaque() || other.is_opaque()) {
530 return other;
531 }
532
533 if ((!has_scheme() || !other.has_scheme()) ||
534 !detail::equal(scheme(), other.scheme())) {
535 return other;
536 }
537
538 if ((!has_authority() || !other.has_authority()) ||
539 !detail::equal(authority(), other.authority())) {
540 return other;
541 }
542
543 if (!has_path() || !other.has_path()) {
544 return other;
545 }
546
547 auto path =
548 detail::normalize_path(this->path(), uri_comparison_level::syntax_based);
549 auto other_path =
550 detail::normalize_path(other.path(), uri_comparison_level::syntax_based);
551
552 optional<string_type> query, fragment;
553 if (other.has_query()) {
554 query = other.query().to_string();
555 }
556
557 if (other.has_fragment()) {
558 fragment = other.fragment().to_string();
559 }
560
561 network::uri result;
562 result.initialize(optional<string_type>(), optional<string_type>(),
563 optional<string_type>(), optional<string_type>(),
564 other_path, query, fragment);
565 return result;
566 }
567
568 uri uri::resolve(const uri &base) const {
569 // This implementation uses the psuedo-code given in
570 // http://tools.ietf.org/html/rfc3986#section-5.2.2
571
572 if (is_absolute() && !is_opaque()) {
573 // throw an exception ?
574 return *this;
575 }
576
577 if (is_opaque()) {
578 // throw an exception ?
579 return *this;
580 }
581
582 optional<uri::string_type> user_info, host, port, path, query, fragment;
583
584 if (has_authority()) {
585 // g -> http://g
586 if (has_user_info()) {
587 user_info = make_arg(this->user_info());
588 }
589
590 if (has_host()) {
591 host = make_arg(this->host());
592 }
593
594 if (has_port()) {
595 port = make_arg(this->port());
596 }
597
598 if (has_path()) {
599 path = detail::remove_dot_segments(this->path());
600 }
601
602 if (has_query()) {
603 query = make_arg(this->query());
604 }
605 } else {
606 if (!has_path() || this->path().empty()) {
607 if (base.has_path()) {
608 path = make_arg(base.path());
609 }
610
611 if (has_query()) {
612 query = make_arg(this->query());
613 } else if (base.has_query()) {
614 query = make_arg(base.query());
615 }
616 } else {
617 if (this->path().front() == '/') {
618 path = detail::remove_dot_segments(this->path());
619 } else {
620 path = detail::merge_paths(base, *this);
621 }
622
623 if (has_query()) {
624 query = make_arg(this->query());
625 }
626 }
627
628 if (base.has_user_info()) {
629 user_info = make_arg(base.user_info());
630 }
631
632 if (base.has_host()) {
633 host = make_arg(base.host());
634 }
635
636 if (base.has_port()) {
637 port = make_arg(base.port());
638 }
639 }
640
641 if (has_fragment()) {
642 fragment = make_arg(this->fragment());
643 }
644
645 network::uri result;
646 result.initialize(make_arg(base.scheme()), std::move(user_info),
647 std::move(host), std::move(port), std::move(path),
648 std::move(query), std::move(fragment));
649 return result;
650 }
651
652 int uri::compare(const uri &other, uri_comparison_level level) const noexcept {
653 // if both URIs are empty, then we should define them as equal
654 // even though they're still invalid.
655 if (empty() && other.empty()) {
656 return 0;
657 }
658
659 if (empty()) {
660 return -1;
661 }
662
663 if (other.empty()) {
664 return 1;
665 }
666
667 return normalize(level).uri_.compare(other.normalize(level).uri_);
668 }
669
670 bool uri::initialize(const string_type &uri) {
671 uri_ = detail::trim_copy(uri);
672 if (!uri_.empty()) {
673 uri_view_ = string_view(uri_);
674 const_iterator it = std::begin(uri_view_), last = std::end(uri_view_);
675 bool is_valid = detail::parse(it, last, uri_parts_);
676 return is_valid;
677 }
678 return true;
679 }
680
681 void swap(uri &lhs, uri &rhs) noexcept { lhs.swap(rhs); }
682
683 bool operator==(const uri &lhs, const uri &rhs) noexcept {
684 return lhs.view() == rhs.view();
685 }
686
687 bool operator==(const uri &lhs, const char *rhs) noexcept {
688 return lhs.view() == string_view{rhs};
689 }
690
691 bool operator<(const uri &lhs, const uri &rhs) noexcept {
692 return lhs.view() < rhs.view();
693 }
694
695 } // namespace network