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