Ada 3.3.0
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_aggregator.cpp
Go to the documentation of this file.
1#include "ada/checkers-inl.h"
2#include "ada/helpers.h"
4#include "ada/scheme.h"
5#include "ada/unicode-inl.h"
10
11#include <iterator>
12#include <ranges>
13#include <string>
14#include <string_view>
15
16namespace ada {
17template <bool has_state_override>
18[[nodiscard]] ada_really_inline bool url_aggregator::parse_scheme_with_colon(
19 const std::string_view input_with_colon) {
20 ada_log("url_aggregator::parse_scheme_with_colon ", input_with_colon);
22 ADA_ASSERT_TRUE(!helpers::overlaps(input_with_colon, buffer));
23 std::string_view input{input_with_colon};
24 input.remove_suffix(1);
25 auto parsed_type = ada::scheme::get_scheme_type(input);
26 const bool is_input_special = (parsed_type != ada::scheme::NOT_SPECIAL);
31 if (is_input_special) { // fast path!!!
32 if constexpr (has_state_override) {
33 // If url's scheme is not a special scheme and buffer is a special scheme,
34 // then return.
35 if (is_special() != is_input_special) {
36 return false;
37 }
38
39 // If url includes credentials or has a non-null port, and buffer is
40 // "file", then return.
41 if ((has_credentials() || components.port != url_components::omitted) &&
42 parsed_type == ada::scheme::type::FILE) {
43 return false;
44 }
45
46 // If url's scheme is "file" and its host is an empty host, then return.
47 // An empty host is the empty string.
48 if (type == ada::scheme::type::FILE &&
49 components.host_start == components.host_end) {
50 return false;
51 }
52 }
53
54 type = parsed_type;
55 set_scheme_from_view_with_colon(input_with_colon);
56
57 if constexpr (has_state_override) {
58 // This is uncommon.
59 uint16_t urls_scheme_port = get_special_port();
60
61 // If url's port is url's scheme's default port, then set url's port to
62 // null.
63 if (components.port == urls_scheme_port) {
64 clear_port();
65 }
66 }
67 } else { // slow path
68 std::string _buffer(input);
69 // Next function is only valid if the input is ASCII and returns false
70 // otherwise, but it seems that we always have ascii content so we do not
71 // need to check the return value.
72 unicode::to_lower_ascii(_buffer.data(), _buffer.size());
73
74 if constexpr (has_state_override) {
75 // If url's scheme is a special scheme and buffer is not a special scheme,
76 // then return. If url's scheme is not a special scheme and buffer is a
77 // special scheme, then return.
78 if (is_special() != ada::scheme::is_special(_buffer)) {
79 return true;
80 }
81
82 // If url includes credentials or has a non-null port, and buffer is
83 // "file", then return.
84 if ((has_credentials() || components.port != url_components::omitted) &&
85 _buffer == "file") {
86 return true;
87 }
88
89 // If url's scheme is "file" and its host is an empty host, then return.
90 // An empty host is the empty string.
91 if (type == ada::scheme::type::FILE &&
92 components.host_start == components.host_end) {
93 return true;
94 }
95 }
96
97 set_scheme(_buffer);
98
99 if constexpr (has_state_override) {
100 // This is uncommon.
101 uint16_t urls_scheme_port = get_special_port();
102
103 // If url's port is url's scheme's default port, then set url's port to
104 // null.
105 if (components.port == urls_scheme_port) {
106 clear_port();
107 }
108 }
109 }
111 return true;
112}
113
114inline void url_aggregator::copy_scheme(const url_aggregator& u) noexcept {
115 ada_log("url_aggregator::copy_scheme ", u.buffer);
116 ADA_ASSERT_TRUE(validate());
117 // next line could overflow but unsigned arithmetic has well-defined
118 // overflows.
119 uint32_t new_difference = u.components.protocol_end - components.protocol_end;
120 type = u.type;
121 buffer.erase(0, components.protocol_end);
122 buffer.insert(0, u.get_protocol());
123 components.protocol_end = u.components.protocol_end;
124
125 // No need to update the components
126 if (new_difference == 0) {
127 return;
128 }
129
130 // Update the rest of the components.
131 components.username_end += new_difference;
132 components.host_start += new_difference;
133 components.host_end += new_difference;
134 components.pathname_start += new_difference;
135 if (components.search_start != url_components::omitted) {
136 components.search_start += new_difference;
137 }
138 if (components.hash_start != url_components::omitted) {
139 components.hash_start += new_difference;
140 }
141 ADA_ASSERT_TRUE(validate());
142}
143
144inline void url_aggregator::set_scheme_from_view_with_colon(
145 std::string_view new_scheme_with_colon) noexcept {
146 ada_log("url_aggregator::set_scheme_from_view_with_colon ",
147 new_scheme_with_colon);
148 ADA_ASSERT_TRUE(validate());
149 ADA_ASSERT_TRUE(!new_scheme_with_colon.empty() &&
150 new_scheme_with_colon.back() == ':');
151 // next line could overflow but unsigned arithmetic has well-defined
152 // overflows.
153 uint32_t new_difference =
154 uint32_t(new_scheme_with_colon.size()) - components.protocol_end;
155
156 if (buffer.empty()) {
157 buffer.append(new_scheme_with_colon);
158 } else {
159 buffer.erase(0, components.protocol_end);
160 buffer.insert(0, new_scheme_with_colon);
161 }
162 components.protocol_end += new_difference;
163
164 // Update the rest of the components.
165 components.username_end += new_difference;
166 components.host_start += new_difference;
167 components.host_end += new_difference;
168 components.pathname_start += new_difference;
169 if (components.search_start != url_components::omitted) {
170 components.search_start += new_difference;
171 }
172 if (components.hash_start != url_components::omitted) {
173 components.hash_start += new_difference;
174 }
175 ADA_ASSERT_TRUE(validate());
176}
177
178inline void url_aggregator::set_scheme(std::string_view new_scheme) noexcept {
179 ada_log("url_aggregator::set_scheme ", new_scheme);
180 ADA_ASSERT_TRUE(validate());
181 ADA_ASSERT_TRUE(new_scheme.empty() || new_scheme.back() != ':');
182 // next line could overflow but unsigned arithmetic has well-defined
183 // overflows.
184 uint32_t new_difference =
185 uint32_t(new_scheme.size()) - components.protocol_end + 1;
186
188 if (buffer.empty()) {
189 buffer.append(helpers::concat(new_scheme, ":"));
190 } else {
191 buffer.erase(0, components.protocol_end);
192 buffer.insert(0, helpers::concat(new_scheme, ":"));
193 }
194 components.protocol_end = uint32_t(new_scheme.size() + 1);
195
196 // Update the rest of the components.
197 components.username_end += new_difference;
198 components.host_start += new_difference;
199 components.host_end += new_difference;
200 components.pathname_start += new_difference;
201 if (components.search_start != url_components::omitted) {
202 components.search_start += new_difference;
203 }
204 if (components.hash_start != url_components::omitted) {
205 components.hash_start += new_difference;
206 }
207 ADA_ASSERT_TRUE(validate());
208}
209
210bool url_aggregator::set_protocol(const std::string_view input) {
211 ada_log("url_aggregator::set_protocol ", input);
213 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
214 std::string view(input);
215 helpers::remove_ascii_tab_or_newline(view);
216 if (view.empty()) {
217 return true;
218 }
219
220 // Schemes should start with alpha values.
221 if (!checkers::is_alpha(view[0])) {
222 return false;
223 }
224
225 view.append(":");
226
227 std::string::iterator pointer =
228 std::ranges::find_if_not(view, unicode::is_alnum_plus);
229
230 if (pointer != view.end() && *pointer == ':') {
231 return parse_scheme_with_colon<true>(
232 view.substr(0, pointer - view.begin() + 1));
233 }
234 return false;
235}
236
237bool url_aggregator::set_username(const std::string_view input) {
238 ada_log("url_aggregator::set_username '", input, "' ");
240 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
241 if (cannot_have_credentials_or_port()) {
242 return false;
243 }
246 if (idx == input.size()) {
247 update_base_username(input);
248 } else {
249 // We only create a temporary string if we have to!
250 update_base_username(ada::unicode::percent_encode(
252 }
254 return true;
255}
256
257bool url_aggregator::set_password(const std::string_view input) {
258 ada_log("url_aggregator::set_password '", input, "'");
260 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
261 if (cannot_have_credentials_or_port()) {
262 return false;
263 }
266 if (idx == input.size()) {
267 update_base_password(input);
268 } else {
269 // We only create a temporary string if we have to!
270 update_base_password(ada::unicode::percent_encode(
272 }
274 return true;
275}
276
277bool url_aggregator::set_port(const std::string_view input) {
278 ada_log("url_aggregator::set_port ", input);
280 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
281 if (cannot_have_credentials_or_port()) {
282 return false;
283 }
284
285 if (input.empty()) {
286 clear_port();
287 return true;
288 }
289
290 std::string trimmed(input);
291 helpers::remove_ascii_tab_or_newline(trimmed);
292
293 if (trimmed.empty()) {
294 return true;
295 }
296
297 // Input should not start with a non-digit character.
298 if (!ada::unicode::is_ascii_digit(trimmed.front())) {
299 return false;
300 }
301
302 // Find the first non-digit character to determine the length of digits
303 auto first_non_digit =
304 std::ranges::find_if_not(trimmed, ada::unicode::is_ascii_digit);
305 std::string_view digits_to_parse =
306 std::string_view(trimmed.data(), first_non_digit - trimmed.begin());
307
308 // Revert changes if parse_port fails.
309 uint32_t previous_port = components.port;
310 parse_port(digits_to_parse);
311 if (is_valid) {
312 return true;
313 }
314 update_base_port(previous_port);
315 is_valid = true;
317 return false;
318}
319
320bool url_aggregator::set_pathname(const std::string_view input) {
321 ada_log("url_aggregator::set_pathname ", input);
323 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
324 if (has_opaque_path) {
325 return false;
326 }
327 clear_pathname();
328 parse_path(input);
329 if (get_pathname().starts_with("//") && !has_authority() && !has_dash_dot()) {
330 buffer.insert(components.pathname_start, "/.");
331 components.pathname_start += 2;
332 }
334 return true;
335}
336
337ada_really_inline void url_aggregator::parse_path(std::string_view input) {
338 ada_log("url_aggregator::parse_path ", input);
340 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
341 std::string tmp_buffer;
342 std::string_view internal_input;
343 if (unicode::has_tabs_or_newline(input)) {
344 tmp_buffer = input;
345 // Optimization opportunity: Instead of copying and then pruning, we could
346 // just directly build the string from user_input.
347 helpers::remove_ascii_tab_or_newline(tmp_buffer);
348 internal_input = tmp_buffer;
349 } else {
350 internal_input = input;
351 }
352
353 // If url is special, then:
354 if (is_special()) {
355 if (internal_input.empty()) {
356 update_base_pathname("/");
357 } else if ((internal_input[0] == '/') || (internal_input[0] == '\\')) {
358 consume_prepared_path(internal_input.substr(1));
359 } else {
360 consume_prepared_path(internal_input);
361 }
362 } else if (!internal_input.empty()) {
363 if (internal_input[0] == '/') {
364 consume_prepared_path(internal_input.substr(1));
365 } else {
366 consume_prepared_path(internal_input);
367 }
368 } else {
369 // Non-special URLs with an empty host can have their paths erased
370 // Path-only URLs cannot have their paths erased
371 if (components.host_start == components.host_end && !has_authority()) {
372 update_base_pathname("/");
373 }
374 }
376}
377
378void url_aggregator::set_search(const std::string_view input) {
379 ada_log("url_aggregator::set_search ", input);
381 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
382 if (input.empty()) {
383 clear_search();
384 helpers::strip_trailing_spaces_from_opaque_path(*this);
385 return;
386 }
387
388 std::string new_value;
389 new_value = input[0] == '?' ? input.substr(1) : input;
390 helpers::remove_ascii_tab_or_newline(new_value);
391
392 auto query_percent_encode_set =
395
396 update_base_search(new_value, query_percent_encode_set);
398}
399
400void url_aggregator::set_hash(const std::string_view input) {
401 ada_log("url_aggregator::set_hash ", input);
403 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
404 if (input.empty()) {
405 if (components.hash_start != url_components::omitted) {
406 buffer.resize(components.hash_start);
407 components.hash_start = url_components::omitted;
408 }
409 helpers::strip_trailing_spaces_from_opaque_path(*this);
410 return;
411 }
412
413 std::string new_value;
414 new_value = input[0] == '#' ? input.substr(1) : input;
415 helpers::remove_ascii_tab_or_newline(new_value);
416 update_unencoded_base_hash(new_value);
418}
419
420bool url_aggregator::set_href(const std::string_view input) {
421 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
422 ada_log("url_aggregator::set_href ", input, " [", input.size(), " bytes]");
424 ada_log("url_aggregator::set_href, success :", out.has_value());
425
426 if (out) {
427 ada_log("url_aggregator::set_href, parsed ", out->to_string());
428 // TODO: Figure out why the following line puts test to never finish.
429 *this = *out;
430 }
431
432 return out.has_value();
433}
434
435ada_really_inline bool url_aggregator::parse_host(std::string_view input) {
436 ada_log("url_aggregator:parse_host \"", input, "\" [", input.size(),
437 " bytes]");
439 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
440 if (input.empty()) {
441 return is_valid = false;
442 } // technically unnecessary.
443 // If input starts with U+005B ([), then:
444 if (input[0] == '[') {
445 // If input does not end with U+005D (]), validation error, return failure.
446 if (input.back() != ']') {
447 return is_valid = false;
448 }
449 ada_log("parse_host ipv6");
450
451 // Return the result of IPv6 parsing input with its leading U+005B ([) and
452 // trailing U+005D (]) removed.
453 input.remove_prefix(1);
454 input.remove_suffix(1);
455 return parse_ipv6(input);
456 }
457
458 // If isNotSpecial is true, then return the result of opaque-host parsing
459 // input.
460 if (!is_special()) {
461 return parse_opaque_host(input);
462 }
463 // Let domain be the result of running UTF-8 decode without BOM on the
464 // percent-decoding of input. Let asciiDomain be the result of running domain
465 // to ASCII with domain and false. The most common case is an ASCII input, in
466 // which case we do not need to call the expensive 'to_ascii' if a few
467 // conditions are met: no '%' and no 'xn-' subsequence.
468
469 // Often, the input does not contain any forbidden code points, and no upper
470 // case ASCII letter, then we can just copy it to the buffer. We want to
471 // optimize for such a common case.
472 uint8_t is_forbidden_or_upper =
473 unicode::contains_forbidden_domain_code_point_or_upper(input.data(),
474 input.size());
475 // Minor optimization opportunity:
476 // contains_forbidden_domain_code_point_or_upper could be extend to check for
477 // the presence of characters that cannot appear in the ipv4 address and we
478 // could also check whether x and n and - are present, and so we could skip
479 // some of the checks below. However, the gains are likely to be small, and
480 // the code would be more complex.
481 if (is_forbidden_or_upper == 0 &&
482 input.find("xn-") == std::string_view::npos) {
483 // fast path
484 update_base_hostname(input);
485 if (checkers::is_ipv4(get_hostname())) {
486 ada_log("parse_host fast path ipv4");
487 return parse_ipv4(get_hostname(), true);
488 }
489 ada_log("parse_host fast path ", get_hostname());
490 return true;
491 }
492 // We have encountered at least one forbidden code point or the input contains
493 // 'xn-' (case insensitive), so we need to call 'to_ascii' to perform the full
494 // conversion.
495
496 ada_log("parse_host calling to_ascii");
497 std::optional<std::string> host = std::string(get_hostname());
498 is_valid = ada::unicode::to_ascii(host, input, input.find('%'));
499 if (!is_valid) {
500 ada_log("parse_host to_ascii returns false");
501 return is_valid = false;
502 }
503 ada_log("parse_host to_ascii succeeded ", *host, " [", host->size(),
504 " bytes]");
505
506 if (std::ranges::any_of(host.value(),
507 ada::unicode::is_forbidden_domain_code_point)) {
508 return is_valid = false;
509 }
510
511 // If asciiDomain ends in a number, then return the result of IPv4 parsing
512 // asciiDomain.
513 if (checkers::is_ipv4(host.value())) {
514 ada_log("parse_host got ipv4 ", *host);
515 return parse_ipv4(host.value(), false);
516 }
517
518 update_base_hostname(host.value());
520 return true;
521}
522
523template <bool override_hostname>
524bool url_aggregator::set_host_or_hostname(const std::string_view input) {
525 ada_log("url_aggregator::set_host_or_hostname ", input);
527 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
528 if (has_opaque_path) {
529 return false;
530 }
531
532 std::string previous_host(get_hostname());
533 uint32_t previous_port = components.port;
534
535 size_t host_end_pos = input.find('#');
536 std::string _host(input.data(), host_end_pos != std::string_view::npos
537 ? host_end_pos
538 : input.size());
539 helpers::remove_ascii_tab_or_newline(_host);
540 std::string_view new_host(_host);
541
542 // If url's scheme is "file", then set state to file host state, instead of
543 // host state.
544 if (type != ada::scheme::type::FILE) {
545 std::string_view host_view(_host.data(), _host.length());
546 auto [location, found_colon] =
547 helpers::get_host_delimiter_location(is_special(), host_view);
548
549 // Otherwise, if c is U+003A (:) and insideBrackets is false, then:
550 // Note: the 'found_colon' value is true if and only if a colon was
551 // encountered while not inside brackets.
552 if (found_colon) {
553 // If buffer is the empty string, host-missing validation error, return
554 // failure.
555 std::string_view host_buffer = host_view.substr(0, location);
556 if (host_buffer.empty()) {
557 return false;
558 }
559
560 // If state override is given and state override is hostname state, then
561 // return failure.
562 if constexpr (override_hostname) {
563 return false;
564 }
565
566 // Let host be the result of host parsing buffer with url is not special.
567 bool succeeded = parse_host(host_buffer);
568 if (!succeeded) {
569 update_base_hostname(previous_host);
570 update_base_port(previous_port);
571 return false;
572 }
573
574 // Set url's host to host, buffer to the empty string, and state to port
575 // state.
576 std::string_view port_buffer = new_host.substr(location + 1);
577 if (!port_buffer.empty()) {
578 set_port(port_buffer);
579 }
580 return true;
581 }
582 // Otherwise, if one of the following is true:
583 // - c is the EOF code point, U+002F (/), U+003F (?), or U+0023 (#)
584 // - url is special and c is U+005C (\‍)
585 else {
586 // If url is special and host_view is the empty string, host-missing
587 // validation error, return failure.
588 if (host_view.empty() && is_special()) {
589 return false;
590 }
591
592 // Otherwise, if state override is given, host_view is the empty string,
593 // and either url includes credentials or url's port is non-null, then
594 // return failure.
595 if (host_view.empty() && (has_credentials() || has_port())) {
596 return false;
597 }
598
599 // Let host be the result of host parsing host_view with url is not
600 // special.
601 if (host_view.empty() && !is_special()) {
602 if (has_hostname()) {
603 clear_hostname(); // easy!
604 } else if (has_dash_dot()) {
605 add_authority_slashes_if_needed();
606 delete_dash_dot();
607 }
608 return true;
609 }
610
611 bool succeeded = parse_host(host_view);
612 if (!succeeded) {
613 update_base_hostname(previous_host);
614 update_base_port(previous_port);
615 return false;
616 } else if (has_dash_dot()) {
617 // Should remove dash_dot from pathname
618 delete_dash_dot();
619 }
620 return true;
621 }
622 }
623
624 size_t location = new_host.find_first_of("/\\?");
625 if (location != std::string_view::npos) {
626 new_host.remove_suffix(new_host.length() - location);
627 }
628
629 if (new_host.empty()) {
630 // Set url's host to the empty string.
631 clear_hostname();
632 } else {
633 // Let host be the result of host parsing buffer with url is not special.
634 if (!parse_host(new_host)) {
635 update_base_hostname(previous_host);
636 update_base_port(previous_port);
637 return false;
638 }
639
640 // If host is "localhost", then set host to the empty string.
641 if (helpers::substring(buffer, components.host_start,
642 components.host_end) == "localhost") {
643 clear_hostname();
644 }
645 }
647 return true;
648}
649
650bool url_aggregator::set_host(const std::string_view input) {
651 ada_log("url_aggregator::set_host '", input, "'");
653 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
654 return set_host_or_hostname<false>(input);
655}
656
657bool url_aggregator::set_hostname(const std::string_view input) {
658 ada_log("url_aggregator::set_hostname '", input, "'");
660 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
661 return set_host_or_hostname<true>(input);
662}
663
664[[nodiscard]] std::string url_aggregator::get_origin() const noexcept {
665 ada_log("url_aggregator::get_origin");
666 if (is_special()) {
667 // Return a new opaque origin.
668 if (type == scheme::FILE) {
669 return "null";
670 }
671
672 return helpers::concat(get_protocol(), "//", get_host());
673 }
674
675 if (get_protocol() == "blob:") {
676 std::string_view path = get_pathname();
677 if (!path.empty()) {
678 auto out = ada::parse<ada::url_aggregator>(path);
679 if (out && (out->type == scheme::HTTP || out->type == scheme::HTTPS)) {
680 // If pathURL's scheme is not "http" and not "https", then return a
681 // new opaque origin.
682 return helpers::concat(out->get_protocol(), "//", out->get_host());
683 }
684 }
685 }
686
687 // Return a new opaque origin.
688 return "null";
689}
690
691[[nodiscard]] std::string_view url_aggregator::get_username() const noexcept
693 ada_log("url_aggregator::get_username");
695 return helpers::substring(buffer, components.protocol_end + 2,
696 components.username_end);
697 }
698 return "";
699}
700
701[[nodiscard]] std::string_view url_aggregator::get_password() const noexcept
703 ada_log("url_aggregator::get_password");
705 return helpers::substring(buffer, components.username_end + 1,
706 components.host_start);
707 }
708 return "";
709}
710
711[[nodiscard]] std::string_view url_aggregator::get_port() const noexcept
713 ada_log("url_aggregator::get_port");
714 if (components.port == url_components::omitted) {
715 return "";
716 }
717 return helpers::substring(buffer, components.host_end + 1,
718 components.pathname_start);
719}
720
721[[nodiscard]] std::string_view url_aggregator::get_hash() const noexcept
723 ada_log("url_aggregator::get_hash");
724 // If this's URL's fragment is either null or the empty string, then return
725 // the empty string. Return U+0023 (#), followed by this's URL's fragment.
726 if (components.hash_start == url_components::omitted) {
727 return "";
728 }
729 if (buffer.size() - components.hash_start <= 1) {
730 return "";
731 }
732 return helpers::substring(buffer, components.hash_start);
733}
734
735[[nodiscard]] std::string_view url_aggregator::get_host() const noexcept
737 ada_log("url_aggregator::get_host");
738 // Technically, we should check if there is a hostname, but
739 // the code below works even if there isn't.
740 // if(!has_hostname()) { return ""; }
741 size_t start = components.host_start;
742 if (components.host_end > components.host_start &&
743 buffer[components.host_start] == '@') {
744 start++;
745 }
746 // if we have an empty host, then the space between components.host_end and
747 // components.pathname_start may be occupied by /.
748 if (start == components.host_end) {
749 return {};
750 }
751 return helpers::substring(buffer, start, components.pathname_start);
752}
753
754[[nodiscard]] std::string_view url_aggregator::get_hostname() const noexcept
756 ada_log("url_aggregator::get_hostname");
757 // Technically, we should check if there is a hostname, but
758 // the code below works even if there isn't.
759 // if(!has_hostname()) { return ""; }
760 size_t start = components.host_start;
761 // So host_start is not where the host begins.
762 if (components.host_end > components.host_start &&
763 buffer[components.host_start] == '@') {
764 start++;
765 }
766 return helpers::substring(buffer, start, components.host_end);
767}
768
769[[nodiscard]] std::string_view url_aggregator::get_search() const noexcept
771 ada_log("url_aggregator::get_search");
772 // If this's URL's query is either null or the empty string, then return the
773 // empty string. Return U+003F (?), followed by this's URL's query.
774 if (components.search_start == url_components::omitted) {
775 return "";
776 }
777 auto ending_index = uint32_t(buffer.size());
778 if (components.hash_start != url_components::omitted) {
779 ending_index = components.hash_start;
780 }
781 if (ending_index - components.search_start <= 1) {
782 return "";
783 }
784 return helpers::substring(buffer, components.search_start, ending_index);
785}
786
787[[nodiscard]] std::string_view url_aggregator::get_protocol() const noexcept
789 ada_log("url_aggregator::get_protocol");
790 return helpers::substring(buffer, 0, components.protocol_end);
791}
792
793[[nodiscard]] std::string ada::url_aggregator::to_string() const {
794 ada_log("url_aggregator::to_string buffer:", buffer, " [", buffer.size(),
795 " bytes]");
796 if (!is_valid) {
797 return "null";
798 }
799
800 std::string answer;
801 auto back = std::back_insert_iterator(answer);
802 answer.append("{\n");
803
804 answer.append("\t\"buffer\":\"");
805 helpers::encode_json(buffer, back);
806 answer.append("\",\n");
807
808 answer.append("\t\"protocol\":\"");
809 helpers::encode_json(get_protocol(), back);
810 answer.append("\",\n");
811
812 if (has_credentials()) {
813 answer.append("\t\"username\":\"");
814 helpers::encode_json(get_username(), back);
815 answer.append("\",\n");
816 answer.append("\t\"password\":\"");
817 helpers::encode_json(get_password(), back);
818 answer.append("\",\n");
819 }
820
821 answer.append("\t\"host\":\"");
822 helpers::encode_json(get_host(), back);
823 answer.append("\",\n");
824
825 answer.append("\t\"path\":\"");
826 helpers::encode_json(get_pathname(), back);
827 answer.append("\",\n");
828 answer.append("\t\"opaque path\":");
829 answer.append((has_opaque_path ? "true" : "false"));
830 answer.append(",\n");
831
832 if (components.search_start != url_components::omitted) {
833 answer.append("\t\"query\":\"");
834 helpers::encode_json(get_search(), back);
835 answer.append("\",\n");
836 }
837 if (components.hash_start != url_components::omitted) {
838 answer.append("\t\"fragment\":\"");
839 helpers::encode_json(get_hash(), back);
840 answer.append("\",\n");
841 }
842
843 auto convert_offset_to_string = [](uint32_t offset) -> std::string {
844 if (offset == url_components::omitted) {
845 return "null";
846 } else {
847 return std::to_string(offset);
848 }
849 };
850
851 answer.append("\t\"protocol_end\":");
852 answer.append(convert_offset_to_string(components.protocol_end));
853 answer.append(",\n");
854
855 answer.append("\t\"username_end\":");
856 answer.append(convert_offset_to_string(components.username_end));
857 answer.append(",\n");
858
859 answer.append("\t\"host_start\":");
860 answer.append(convert_offset_to_string(components.host_start));
861 answer.append(",\n");
862
863 answer.append("\t\"host_end\":");
864 answer.append(convert_offset_to_string(components.host_end));
865 answer.append(",\n");
866
867 answer.append("\t\"port\":");
868 answer.append(convert_offset_to_string(components.port));
869 answer.append(",\n");
870
871 answer.append("\t\"pathname_start\":");
872 answer.append(convert_offset_to_string(components.pathname_start));
873 answer.append(",\n");
874
875 answer.append("\t\"search_start\":");
876 answer.append(convert_offset_to_string(components.search_start));
877 answer.append(",\n");
878
879 answer.append("\t\"hash_start\":");
880 answer.append(convert_offset_to_string(components.hash_start));
881 answer.append("\n}");
882
883 return answer;
884}
885
886[[nodiscard]] bool url_aggregator::has_valid_domain() const noexcept {
887 if (components.host_start == components.host_end) {
888 return false;
889 }
890 return checkers::verify_dns_length(get_hostname());
891}
892
893bool url_aggregator::parse_ipv4(std::string_view input, bool in_place) {
894 ada_log("parse_ipv4 ", input, " [", input.size(),
895 " bytes], overlaps with buffer: ",
896 helpers::overlaps(input, buffer) ? "yes" : "no");
898 const bool trailing_dot = (input.back() == '.');
899 if (trailing_dot) {
900 input.remove_suffix(1);
901 }
902 size_t digit_count{0};
903 int pure_decimal_count = 0; // entries that are decimal
904 uint64_t ipv4{0};
905 // we could unroll for better performance?
906 for (; (digit_count < 4) && !(input.empty()); digit_count++) {
907 uint32_t
908 segment_result{}; // If any number exceeds 32 bits, we have an error.
909 bool is_hex = checkers::has_hex_prefix(input);
910 if (is_hex && ((input.length() == 2) ||
911 ((input.length() > 2) && (input[2] == '.')))) {
912 // special case
913 segment_result = 0;
914 input.remove_prefix(2);
915 } else {
916 std::from_chars_result r{};
917 if (is_hex) {
918 ada_log("parse_ipv4 trying to parse hex number");
919 r = std::from_chars(input.data() + 2, input.data() + input.size(),
920 segment_result, 16);
921 } else if ((input.length() >= 2) && input[0] == '0' &&
922 checkers::is_digit(input[1])) {
923 ada_log("parse_ipv4 trying to parse octal number");
924 r = std::from_chars(input.data() + 1, input.data() + input.size(),
925 segment_result, 8);
926 } else {
927 ada_log("parse_ipv4 trying to parse decimal number");
928 pure_decimal_count++;
929 r = std::from_chars(input.data(), input.data() + input.size(),
930 segment_result, 10);
931 }
932 if (r.ec != std::errc()) {
933 ada_log("parse_ipv4 parsing failed");
934 return is_valid = false;
935 }
936 ada_log("parse_ipv4 parsed ", segment_result);
937 input.remove_prefix(r.ptr - input.data());
938 }
939 if (input.empty()) {
940 // We have the last value.
941 // At this stage, ipv4 contains digit_count*8 bits.
942 // So we have 32-digit_count*8 bits left.
943 if (segment_result >= (uint64_t(1) << (32 - digit_count * 8))) {
944 return is_valid = false;
945 }
946 ipv4 <<= (32 - digit_count * 8);
947 ipv4 |= segment_result;
948 goto final;
949 } else {
950 // There is more, so that the value must no be larger than 255
951 // and we must have a '.'.
952 if ((segment_result > 255) || (input[0] != '.')) {
953 return is_valid = false;
954 }
955 ipv4 <<= 8;
956 ipv4 |= segment_result;
957 input.remove_prefix(1); // remove '.'
958 }
959 }
960 if ((digit_count != 4) || (!input.empty())) {
961 ada_log("parse_ipv4 found invalid (more than 4 numbers or empty) ");
962 return is_valid = false;
963 }
964final:
965 ada_log("url_aggregator::parse_ipv4 completed ", get_href(),
966 " host: ", get_host());
967
968 // We could also check r.ptr to see where the parsing ended.
969 if (in_place && pure_decimal_count == 4 && !trailing_dot) {
970 ada_log(
971 "url_aggregator::parse_ipv4 completed and was already correct in the "
972 "buffer");
973 // The original input was already all decimal and we validated it. So we
974 // don't need to do anything.
975 } else {
976 ada_log("url_aggregator::parse_ipv4 completed and we need to update it");
977 // Optimization opportunity: Get rid of unnecessary string return in ipv4
978 // serializer.
979 // TODO: This is likely a bug because it goes back update_base_hostname, not
980 // what we want to do.
981 update_base_hostname(
982 ada::serializers::ipv4(ipv4)); // We have to reserialize the address.
983 }
984 host_type = IPV4;
986 return true;
987}
988
989bool url_aggregator::parse_ipv6(std::string_view input) {
990 // TODO: Implement in_place optimization: we know that input points
991 // in the buffer, so we can just check whether the buffer is already
992 // well formatted.
993 // TODO: Find a way to merge parse_ipv6 with url.cpp implementation.
994 ada_log("parse_ipv6 ", input, " [", input.size(), " bytes]");
996 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
997 if (input.empty()) {
998 return is_valid = false;
999 }
1000 // Let address be a new IPv6 address whose IPv6 pieces are all 0.
1001 std::array<uint16_t, 8> address{};
1002
1003 // Let pieceIndex be 0.
1004 int piece_index = 0;
1005
1006 // Let compress be null.
1007 std::optional<int> compress{};
1008
1009 // Let pointer be a pointer for input.
1010 std::string_view::iterator pointer = input.begin();
1011
1012 // If c is U+003A (:), then:
1013 if (input[0] == ':') {
1014 // If remaining does not start with U+003A (:), validation error, return
1015 // failure.
1016 if (input.size() == 1 || input[1] != ':') {
1017 ada_log("parse_ipv6 starts with : but the rest does not start with :");
1018 return is_valid = false;
1019 }
1020
1021 // Increase pointer by 2.
1022 pointer += 2;
1023
1024 // Increase pieceIndex by 1 and then set compress to pieceIndex.
1025 compress = ++piece_index;
1026 }
1027
1028 // While c is not the EOF code point:
1029 while (pointer != input.end()) {
1030 // If pieceIndex is 8, validation error, return failure.
1031 if (piece_index == 8) {
1032 ada_log("parse_ipv6 piece_index == 8");
1033 return is_valid = false;
1034 }
1035
1036 // If c is U+003A (:), then:
1037 if (*pointer == ':') {
1038 // If compress is non-null, validation error, return failure.
1039 if (compress.has_value()) {
1040 ada_log("parse_ipv6 compress is non-null");
1041 return is_valid = false;
1042 }
1043
1044 // Increase pointer and pieceIndex by 1, set compress to pieceIndex, and
1045 // then continue.
1046 pointer++;
1047 compress = ++piece_index;
1048 continue;
1049 }
1050
1051 // Let value and length be 0.
1052 uint16_t value = 0, length = 0;
1053
1054 // While length is less than 4 and c is an ASCII hex digit,
1055 // set value to value times 0x10 + c interpreted as hexadecimal number, and
1056 // increase pointer and length by 1.
1057 while (length < 4 && pointer != input.end() &&
1058 unicode::is_ascii_hex_digit(*pointer)) {
1059 // https://stackoverflow.com/questions/39060852/why-does-the-addition-of-two-shorts-return-an-int
1060 value = uint16_t(value * 0x10 + unicode::convert_hex_to_binary(*pointer));
1061 pointer++;
1062 length++;
1063 }
1064
1065 // If c is U+002E (.), then:
1066 if (pointer != input.end() && *pointer == '.') {
1067 // If length is 0, validation error, return failure.
1068 if (length == 0) {
1069 ada_log("parse_ipv6 length is 0");
1070 return is_valid = false;
1071 }
1072
1073 // Decrease pointer by length.
1074 pointer -= length;
1075
1076 // If pieceIndex is greater than 6, validation error, return failure.
1077 if (piece_index > 6) {
1078 ada_log("parse_ipv6 piece_index > 6");
1079 return is_valid = false;
1080 }
1081
1082 // Let numbersSeen be 0.
1083 int numbers_seen = 0;
1084
1085 // While c is not the EOF code point:
1086 while (pointer != input.end()) {
1087 // Let ipv4Piece be null.
1088 std::optional<uint16_t> ipv4_piece{};
1089
1090 // If numbersSeen is greater than 0, then:
1091 if (numbers_seen > 0) {
1092 // If c is a U+002E (.) and numbersSeen is less than 4, then increase
1093 // pointer by 1.
1094 if (*pointer == '.' && numbers_seen < 4) {
1095 pointer++;
1096 } else {
1097 // Otherwise, validation error, return failure.
1098 ada_log("parse_ipv6 Otherwise, validation error, return failure");
1099 return is_valid = false;
1100 }
1101 }
1102
1103 // If c is not an ASCII digit, validation error, return failure.
1104 if (pointer == input.end() || !checkers::is_digit(*pointer)) {
1105 ada_log(
1106 "parse_ipv6 If c is not an ASCII digit, validation error, return "
1107 "failure");
1108 return is_valid = false;
1109 }
1110
1111 // While c is an ASCII digit:
1112 while (pointer != input.end() && checkers::is_digit(*pointer)) {
1113 // Let number be c interpreted as decimal number.
1114 int number = *pointer - '0';
1115
1116 // If ipv4Piece is null, then set ipv4Piece to number.
1117 if (!ipv4_piece.has_value()) {
1118 ipv4_piece = number;
1119 }
1120 // Otherwise, if ipv4Piece is 0, validation error, return failure.
1121 else if (ipv4_piece == 0) {
1122 ada_log("parse_ipv6 if ipv4Piece is 0, validation error");
1123 return is_valid = false;
1124 }
1125 // Otherwise, set ipv4Piece to ipv4Piece times 10 + number.
1126 else {
1127 ipv4_piece = *ipv4_piece * 10 + number;
1128 }
1129
1130 // If ipv4Piece is greater than 255, validation error, return failure.
1131 if (ipv4_piece > 255) {
1132 ada_log("parse_ipv6 ipv4_piece > 255");
1133 return is_valid = false;
1134 }
1135
1136 // Increase pointer by 1.
1137 pointer++;
1138 }
1139
1140 // Set address[pieceIndex] to address[pieceIndex] times 0x100 +
1141 // ipv4Piece.
1142 // https://stackoverflow.com/questions/39060852/why-does-the-addition-of-two-shorts-return-an-int
1143 address[piece_index] =
1144 uint16_t(address[piece_index] * 0x100 + *ipv4_piece);
1145
1146 // Increase numbersSeen by 1.
1147 numbers_seen++;
1148
1149 // If numbersSeen is 2 or 4, then increase pieceIndex by 1.
1150 if (numbers_seen == 2 || numbers_seen == 4) {
1151 piece_index++;
1152 }
1153 }
1154
1155 // If numbersSeen is not 4, validation error, return failure.
1156 if (numbers_seen != 4) {
1157 return is_valid = false;
1158 }
1159
1160 // Break.
1161 break;
1162 }
1163 // Otherwise, if c is U+003A (:):
1164 else if ((pointer != input.end()) && (*pointer == ':')) {
1165 // Increase pointer by 1.
1166 pointer++;
1167
1168 // If c is the EOF code point, validation error, return failure.
1169 if (pointer == input.end()) {
1170 ada_log(
1171 "parse_ipv6 If c is the EOF code point, validation error, return "
1172 "failure");
1173 return is_valid = false;
1174 }
1175 }
1176 // Otherwise, if c is not the EOF code point, validation error, return
1177 // failure.
1178 else if (pointer != input.end()) {
1179 ada_log(
1180 "parse_ipv6 Otherwise, if c is not the EOF code point, validation "
1181 "error, return failure");
1182 return is_valid = false;
1183 }
1184
1185 // Set address[pieceIndex] to value.
1186 address[piece_index] = value;
1187
1188 // Increase pieceIndex by 1.
1189 piece_index++;
1190 }
1191
1192 // If compress is non-null, then:
1193 if (compress.has_value()) {
1194 // Let swaps be pieceIndex - compress.
1195 int swaps = piece_index - *compress;
1196
1197 // Set pieceIndex to 7.
1198 piece_index = 7;
1199
1200 // While pieceIndex is not 0 and swaps is greater than 0,
1201 // swap address[pieceIndex] with address[compress + swaps - 1], and then
1202 // decrease both pieceIndex and swaps by 1.
1203 while (piece_index != 0 && swaps > 0) {
1204 std::swap(address[piece_index], address[*compress + swaps - 1]);
1205 piece_index--;
1206 swaps--;
1207 }
1208 }
1209 // Otherwise, if compress is null and pieceIndex is not 8, validation error,
1210 // return failure.
1211 else if (piece_index != 8) {
1212 ada_log(
1213 "parse_ipv6 if compress is null and pieceIndex is not 8, validation "
1214 "error, return failure");
1215 return is_valid = false;
1216 }
1217 // TODO: Optimization opportunity: Get rid of unnecessary string creation.
1218 // TODO: This is likely a bug because it goes back update_base_hostname, not
1219 // what we want to do.
1220 update_base_hostname(ada::serializers::ipv6(address));
1221 ada_log("parse_ipv6 ", get_hostname());
1223 host_type = IPV6;
1224 return true;
1225}
1226
1227bool url_aggregator::parse_opaque_host(std::string_view input) {
1228 ada_log("parse_opaque_host ", input, " [", input.size(), " bytes]");
1230 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
1231 if (std::ranges::any_of(input, ada::unicode::is_forbidden_host_code_point)) {
1232 return is_valid = false;
1233 }
1234
1235 // Return the result of running UTF-8 percent-encode on input using the C0
1236 // control percent-encode set.
1239 if (idx == input.size()) {
1240 update_base_hostname(input);
1241 } else {
1242 // We only create a temporary string if we need to.
1243 update_base_hostname(ada::unicode::percent_encode(
1245 }
1247 return true;
1248}
1249
1250[[nodiscard]] std::string url_aggregator::to_diagram() const {
1251 if (!is_valid) {
1252 return "invalid";
1253 }
1254 std::string answer;
1255 answer.append(buffer);
1256 answer.append(" [");
1257 answer.append(std::to_string(buffer.size()));
1258 answer.append(" bytes]");
1259 answer.append("\n");
1260 // first line
1261 std::string line1;
1262 line1.resize(buffer.size(), ' ');
1263 if (components.hash_start != url_components::omitted) {
1264 line1[components.hash_start] = '|';
1265 }
1266 if (components.search_start != url_components::omitted) {
1267 line1[components.search_start] = '|';
1268 }
1269 if (components.pathname_start != buffer.size()) {
1270 line1[components.pathname_start] = '|';
1271 }
1272 if (components.host_end != buffer.size()) {
1273 line1[components.host_end] = '|';
1274 }
1275 if (components.host_start != buffer.size()) {
1276 line1[components.host_start] = '|';
1277 }
1278 if (components.username_end != buffer.size()) {
1279 line1[components.username_end] = '|';
1280 }
1281 if (components.protocol_end != buffer.size()) {
1282 line1[components.protocol_end] = '|';
1283 }
1284 answer.append(line1);
1285 answer.append("\n");
1286
1287 std::string line2 = line1;
1288 if (components.hash_start != url_components::omitted) {
1289 line2[components.hash_start] = '`';
1290 line1[components.hash_start] = ' ';
1291
1292 for (size_t i = components.hash_start + 1; i < line2.size(); i++) {
1293 line2[i] = '-';
1294 }
1295 line2.append(" hash_start");
1296 answer.append(line2);
1297 answer.append("\n");
1298 }
1299
1300 std::string line3 = line1;
1301 if (components.search_start != url_components::omitted) {
1302 line3[components.search_start] = '`';
1303 line1[components.search_start] = ' ';
1304
1305 for (size_t i = components.search_start + 1; i < line3.size(); i++) {
1306 line3[i] = '-';
1307 }
1308 line3.append(" search_start ");
1309 line3.append(std::to_string(components.search_start));
1310 answer.append(line3);
1311 answer.append("\n");
1312 }
1313
1314 std::string line4 = line1;
1315 if (components.pathname_start != buffer.size()) {
1316 line4[components.pathname_start] = '`';
1317 line1[components.pathname_start] = ' ';
1318 for (size_t i = components.pathname_start + 1; i < line4.size(); i++) {
1319 line4[i] = '-';
1320 }
1321 line4.append(" pathname_start ");
1322 line4.append(std::to_string(components.pathname_start));
1323 answer.append(line4);
1324 answer.append("\n");
1325 }
1326
1327 std::string line5 = line1;
1328 if (components.host_end != buffer.size()) {
1329 line5[components.host_end] = '`';
1330 line1[components.host_end] = ' ';
1331
1332 for (size_t i = components.host_end + 1; i < line5.size(); i++) {
1333 line5[i] = '-';
1334 }
1335 line5.append(" host_end ");
1336 line5.append(std::to_string(components.host_end));
1337 answer.append(line5);
1338 answer.append("\n");
1339 }
1340
1341 std::string line6 = line1;
1342 if (components.host_start != buffer.size()) {
1343 line6[components.host_start] = '`';
1344 line1[components.host_start] = ' ';
1345
1346 for (size_t i = components.host_start + 1; i < line6.size(); i++) {
1347 line6[i] = '-';
1348 }
1349 line6.append(" host_start ");
1350 line6.append(std::to_string(components.host_start));
1351 answer.append(line6);
1352 answer.append("\n");
1353 }
1354
1355 std::string line7 = line1;
1356 if (components.username_end != buffer.size()) {
1357 line7[components.username_end] = '`';
1358 line1[components.username_end] = ' ';
1359
1360 for (size_t i = components.username_end + 1; i < line7.size(); i++) {
1361 line7[i] = '-';
1362 }
1363 line7.append(" username_end ");
1364 line7.append(std::to_string(components.username_end));
1365 answer.append(line7);
1366 answer.append("\n");
1367 }
1368
1369 std::string line8 = line1;
1370 if (components.protocol_end != buffer.size()) {
1371 line8[components.protocol_end] = '`';
1372 line1[components.protocol_end] = ' ';
1373
1374 for (size_t i = components.protocol_end + 1; i < line8.size(); i++) {
1375 line8[i] = '-';
1376 }
1377 line8.append(" protocol_end ");
1378 line8.append(std::to_string(components.protocol_end));
1379 answer.append(line8);
1380 answer.append("\n");
1381 }
1382
1383 if (components.hash_start == url_components::omitted) {
1384 answer.append("note: hash omitted\n");
1385 }
1386 if (components.search_start == url_components::omitted) {
1387 answer.append("note: search omitted\n");
1388 }
1389 if (components.protocol_end > buffer.size()) {
1390 answer.append("warning: protocol_end overflows\n");
1391 }
1392 if (components.username_end > buffer.size()) {
1393 answer.append("warning: username_end overflows\n");
1394 }
1395 if (components.host_start > buffer.size()) {
1396 answer.append("warning: host_start overflows\n");
1397 }
1398 if (components.host_end > buffer.size()) {
1399 answer.append("warning: host_end overflows\n");
1400 }
1401 if (components.pathname_start > buffer.size()) {
1402 answer.append("warning: pathname_start overflows\n");
1403 }
1404 return answer;
1405}
1406
1407void url_aggregator::delete_dash_dot() {
1408 ada_log("url_aggregator::delete_dash_dot");
1410 ADA_ASSERT_TRUE(has_dash_dot());
1411 buffer.erase(components.host_end, 2);
1412 components.pathname_start -= 2;
1413 if (components.search_start != url_components::omitted) {
1414 components.search_start -= 2;
1415 }
1416 if (components.hash_start != url_components::omitted) {
1417 components.hash_start -= 2;
1418 }
1420 ADA_ASSERT_TRUE(!has_dash_dot());
1421}
1422
1423inline void url_aggregator::consume_prepared_path(std::string_view input) {
1424 ada_log("url_aggregator::consume_prepared_path ", input);
1425
1434 uint8_t accumulator = checkers::path_signature(input);
1435 // Let us first detect a trivial case.
1436 // If it is special, we check that we have no dot, no %, no \ and no
1437 // character needing percent encoding. Otherwise, we check that we have no %,
1438 // no dot, and no character needing percent encoding.
1439 constexpr uint8_t need_encoding = 1;
1440 constexpr uint8_t backslash_char = 2;
1441 constexpr uint8_t dot_char = 4;
1442 constexpr uint8_t percent_char = 8;
1443 bool special = type != ada::scheme::NOT_SPECIAL;
1444 bool may_need_slow_file_handling = (type == ada::scheme::type::FILE &&
1446 bool trivial_path =
1447 (special ? (accumulator == 0)
1448 : ((accumulator & (need_encoding | dot_char | percent_char)) ==
1449 0)) &&
1450 (!may_need_slow_file_handling);
1451 if (accumulator == dot_char && !may_need_slow_file_handling) {
1452 // '4' means that we have at least one dot, but nothing that requires
1453 // percent encoding or decoding. The only part that is not trivial is
1454 // that we may have single dots and double dots path segments.
1455 // If we have such segments, then we either have a path that begins
1456 // with '.' (easy to check), or we have the sequence './'.
1457 // Note: input cannot be empty, it must at least contain one character ('.')
1458 // Note: we know that '\' is not present.
1459 if (input[0] != '.') {
1460 size_t slashdot = 0;
1461 bool dot_is_file = true;
1462 for (;;) {
1463 slashdot = input.find("/.", slashdot);
1464 if (slashdot == std::string_view::npos) { // common case
1465 break;
1466 } else { // uncommon
1467 // only three cases matter: /./, /.. or a final /
1468 slashdot += 2;
1469 dot_is_file &= !(slashdot == input.size() || input[slashdot] == '.' ||
1470 input[slashdot] == '/');
1471 }
1472 }
1473 trivial_path = dot_is_file;
1474 }
1475 }
1476 if (trivial_path && is_at_path()) {
1477 ada_log("parse_path trivial");
1478 buffer += '/';
1479 buffer += input;
1480 return;
1481 }
1482 std::string path = std::string(get_pathname());
1483 // We are going to need to look a bit at the path, but let us see if we can
1484 // ignore percent encoding *and* backslashes *and* percent characters.
1485 // Except for the trivial case, this is likely to capture 99% of paths out
1486 // there.
1487 bool fast_path =
1488 (special &&
1489 (accumulator & (need_encoding | backslash_char | percent_char)) == 0) &&
1490 (type != ada::scheme::type::FILE);
1491 if (fast_path) {
1492 ada_log("parse_prepared_path fast");
1493 // Here we don't need to worry about \ or percent encoding.
1494 // We also do not have a file protocol. We might have dots, however,
1495 // but dots must as appear as '.', and they cannot be encoded because
1496 // the symbol '%' is not present.
1497 size_t previous_location = 0; // We start at 0.
1498 do {
1499 size_t new_location = input.find('/', previous_location);
1500 // std::string_view path_view = input;
1501 // We process the last segment separately:
1502 if (new_location == std::string_view::npos) {
1503 std::string_view path_view = input.substr(previous_location);
1504 if (path_view == "..") { // The path ends with ..
1505 // e.g., if you receive ".." with an empty path, you go to "/".
1506 if (path.empty()) {
1507 path = '/';
1508 update_base_pathname(path);
1509 return;
1510 }
1511 // Fast case where we have nothing to do:
1512 if (path.back() == '/') {
1513 update_base_pathname(path);
1514 return;
1515 }
1516 // If you have the path "/joe/myfriend",
1517 // then you delete 'myfriend'.
1518 path.resize(path.rfind('/') + 1);
1519 update_base_pathname(path);
1520 return;
1521 }
1522 path += '/';
1523 if (path_view != ".") {
1524 path.append(path_view);
1525 }
1526 update_base_pathname(path);
1527 return;
1528 } else {
1529 // This is a non-final segment.
1530 std::string_view path_view =
1531 input.substr(previous_location, new_location - previous_location);
1532 previous_location = new_location + 1;
1533 if (path_view == "..") {
1534 size_t last_delimiter = path.rfind('/');
1535 if (last_delimiter != std::string::npos) {
1536 path.erase(last_delimiter);
1537 }
1538 } else if (path_view != ".") {
1539 path += '/';
1540 path.append(path_view);
1541 }
1542 }
1543 } while (true);
1544 } else {
1545 ada_log("parse_path slow");
1546 // we have reached the general case
1547 bool needs_percent_encoding = (accumulator & 1);
1548 std::string path_buffer_tmp;
1549 do {
1550 size_t location = (special && (accumulator & 2))
1551 ? input.find_first_of("/\\")
1552 : input.find('/');
1553 std::string_view path_view = input;
1554 if (location != std::string_view::npos) {
1555 path_view.remove_suffix(path_view.size() - location);
1556 input.remove_prefix(location + 1);
1557 }
1558 // path_buffer is either path_view or it might point at a percent encoded
1559 // temporary string.
1560 std::string_view path_buffer =
1561 (needs_percent_encoding &&
1562 ada::unicode::percent_encode<false>(
1563 path_view, character_sets::PATH_PERCENT_ENCODE, path_buffer_tmp))
1564 ? path_buffer_tmp
1565 : path_view;
1566 if (unicode::is_double_dot_path_segment(path_buffer)) {
1567 helpers::shorten_path(path, type);
1568 if (location == std::string_view::npos) {
1569 path += '/';
1570 }
1571 } else if (unicode::is_single_dot_path_segment(path_buffer) &&
1572 (location == std::string_view::npos)) {
1573 path += '/';
1574 }
1575 // Otherwise, if path_buffer is not a single-dot path segment, then:
1576 else if (!unicode::is_single_dot_path_segment(path_buffer)) {
1577 // If url's scheme is "file", url's path is empty, and path_buffer is a
1578 // Windows drive letter, then replace the second code point in
1579 // path_buffer with U+003A (:).
1580 if (type == ada::scheme::type::FILE && path.empty() &&
1581 checkers::is_windows_drive_letter(path_buffer)) {
1582 path += '/';
1583 path += path_buffer[0];
1584 path += ':';
1585 path_buffer.remove_prefix(2);
1586 path.append(path_buffer);
1587 } else {
1588 // Append path_buffer to url's path.
1589 path += '/';
1590 path.append(path_buffer);
1591 }
1592 }
1593 if (location == std::string_view::npos) {
1594 update_base_pathname(path);
1595 return;
1596 }
1597 } while (true);
1598 }
1599}
1600} // namespace ada
Definitions for URL specific checkers used within Ada.
#define ADA_ASSERT_TRUE(COND)
#define ada_lifetime_bound
#define ada_really_inline
Definition common_defs.h:81
Definitions for helper functions used within Ada.
Definitions for user facing functions for parsing URL and it's components.
constexpr uint8_t QUERY_PERCENT_ENCODE[32]
constexpr uint8_t SPECIAL_QUERY_PERCENT_ENCODE[32]
constexpr uint8_t PATH_PERCENT_ENCODE[32]
constexpr uint8_t C0_CONTROL_PERCENT_ENCODE[32]
constexpr uint8_t USERINFO_PERCENT_ENCODE[32]
constexpr bool has_hex_prefix(std::string_view input)
constexpr bool is_windows_drive_letter(std::string_view input) noexcept
constexpr bool is_alpha(char x) noexcept
constexpr bool is_digit(char x) noexcept
constexpr ada::scheme::type get_scheme_type(std::string_view scheme) noexcept
Definition scheme-inl.h:72
@ NOT_SPECIAL
Definition scheme.h:30
std::string ipv6(const std::array< uint16_t, 8 > &address) noexcept
std::string ipv4(uint64_t address) noexcept
ada_really_inline size_t percent_encode_index(const std::string_view input, const uint8_t character_set[])
Definition unicode-inl.h:19
Definition ada_idna.h:13
@ IPV6
Definition url_base.h:32
@ IPV4
Definition url_base.h:27
template ada::result< url_aggregator > parse< url_aggregator >(std::string_view input, const url_aggregator *base_url)
tl::expected< result_type, ada::errors > result
ada_warn_unused ada::result< result_type > parse(std::string_view input, const result_type *base_url=nullptr)
Declarations for the URL scheme.
Lightweight URL struct.
constexpr bool has_non_empty_password() const noexcept
void set_hash(std::string_view input)
constexpr bool validate() const noexcept
void clear_search() override
std::string_view get_hostname() const noexcept ada_lifetime_bound
std::string to_string() const override
std::string_view get_hash() const noexcept ada_lifetime_bound
std::string to_diagram() const
constexpr bool has_hostname() const noexcept
bool set_protocol(std::string_view input)
std::string get_origin() const noexcept override
constexpr std::string_view get_href() const noexcept ada_lifetime_bound
std::string_view get_search() const noexcept ada_lifetime_bound
bool has_valid_domain() const noexcept override
bool set_hostname(std::string_view input)
bool set_password(std::string_view input)
constexpr std::string_view get_pathname() const noexcept ada_lifetime_bound
bool set_pathname(std::string_view input)
std::string_view get_protocol() const noexcept ada_lifetime_bound
std::string_view get_password() const noexcept ada_lifetime_bound
bool set_href(std::string_view input)
void set_search(std::string_view input)
std::string_view get_port() const noexcept ada_lifetime_bound
constexpr bool has_port() const noexcept
ada_really_inline constexpr bool has_credentials() const noexcept
bool set_host(std::string_view input)
std::string_view get_host() const noexcept ada_lifetime_bound
bool set_port(std::string_view input)
constexpr bool has_non_empty_username() const noexcept
std::string_view get_username() const noexcept ada_lifetime_bound
bool set_username(std::string_view input)
ada_really_inline constexpr bool is_special() const noexcept
url_host_type host_type
Definition url_base.h:60
bool is_valid
Definition url_base.h:50
bool has_opaque_path
Definition url_base.h:55
static constexpr uint32_t omitted
Definitions for unicode operations.
Inline functions for url aggregator.
Declaration for the basic URL definitions.
Declaration for the URL Components.