Ada 2.9.2
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"
11
12#include <string>
13#include <string_view>
14
15namespace ada {
16template <bool has_state_override>
17[[nodiscard]] ada_really_inline bool url_aggregator::parse_scheme_with_colon(
18 const std::string_view input_with_colon) {
19 ada_log("url_aggregator::parse_scheme_with_colon ", input_with_colon);
21 ADA_ASSERT_TRUE(!helpers::overlaps(input_with_colon, buffer));
22 std::string_view input{input_with_colon};
23 input.remove_suffix(1);
24 auto parsed_type = ada::scheme::get_scheme_type(input);
25 const bool is_input_special = (parsed_type != ada::scheme::NOT_SPECIAL);
30 if (is_input_special) { // fast path!!!
31 if constexpr (has_state_override) {
32 // If url's scheme is not a special scheme and buffer is a special scheme,
33 // then return.
34 if (is_special() != is_input_special) {
35 return false;
36 }
37
38 // If url includes credentials or has a non-null port, and buffer is
39 // "file", then return.
40 if ((has_credentials() || components.port != url_components::omitted) &&
41 parsed_type == ada::scheme::type::FILE) {
42 return false;
43 }
44
45 // If url's scheme is "file" and its host is an empty host, then return.
46 // An empty host is the empty string.
47 if (type == ada::scheme::type::FILE &&
48 components.host_start == components.host_end) {
49 return false;
50 }
51 }
52
53 type = parsed_type;
54 set_scheme_from_view_with_colon(input_with_colon);
55
56 if constexpr (has_state_override) {
57 // This is uncommon.
58 uint16_t urls_scheme_port = get_special_port();
59
60 // If url's port is url's scheme's default port, then set url's port to
61 // null.
62 if (components.port == urls_scheme_port) {
63 clear_port();
64 }
65 }
66 } else { // slow path
67 std::string _buffer(input);
68 // Next function is only valid if the input is ASCII and returns false
69 // otherwise, but it seems that we always have ascii content so we do not
70 // need to check the return value.
71 unicode::to_lower_ascii(_buffer.data(), _buffer.size());
72
73 if constexpr (has_state_override) {
74 // If url's scheme is a special scheme and buffer is not a special scheme,
75 // then return. If url's scheme is not a special scheme and buffer is a
76 // special scheme, then return.
77 if (is_special() != ada::scheme::is_special(_buffer)) {
78 return true;
79 }
80
81 // If url includes credentials or has a non-null port, and buffer is
82 // "file", then return.
83 if ((has_credentials() || components.port != url_components::omitted) &&
84 _buffer == "file") {
85 return true;
86 }
87
88 // If url's scheme is "file" and its host is an empty host, then return.
89 // An empty host is the empty string.
90 if (type == ada::scheme::type::FILE &&
91 components.host_start == components.host_end) {
92 return true;
93 }
94 }
95
96 set_scheme(_buffer);
97
98 if constexpr (has_state_override) {
99 // This is uncommon.
100 uint16_t urls_scheme_port = get_special_port();
101
102 // If url's port is url's scheme's default port, then set url's port to
103 // null.
104 if (components.port == urls_scheme_port) {
105 clear_port();
106 }
107 }
108 }
110 return true;
111}
112
113inline void url_aggregator::copy_scheme(const url_aggregator& u) noexcept {
114 ada_log("url_aggregator::copy_scheme ", u.buffer);
115 ADA_ASSERT_TRUE(validate());
116 // next line could overflow but unsigned arithmetic has well-defined
117 // overflows.
118 uint32_t new_difference = u.components.protocol_end - components.protocol_end;
119 type = u.type;
120 buffer.erase(0, components.protocol_end);
121 buffer.insert(0, u.get_protocol());
122 components.protocol_end = u.components.protocol_end;
123
124 // No need to update the components
125 if (new_difference == 0) {
126 return;
127 }
128
129 // Update the rest of the components.
130 components.username_end += new_difference;
131 components.host_start += new_difference;
132 components.host_end += new_difference;
133 components.pathname_start += new_difference;
134 if (components.search_start != url_components::omitted) {
135 components.search_start += new_difference;
136 }
137 if (components.hash_start != url_components::omitted) {
138 components.hash_start += new_difference;
139 }
140 ADA_ASSERT_TRUE(validate());
141}
142
143inline void url_aggregator::set_scheme_from_view_with_colon(
144 std::string_view new_scheme_with_colon) noexcept {
145 ada_log("url_aggregator::set_scheme_from_view_with_colon ",
146 new_scheme_with_colon);
147 ADA_ASSERT_TRUE(validate());
148 ADA_ASSERT_TRUE(!new_scheme_with_colon.empty() &&
149 new_scheme_with_colon.back() == ':');
150 // next line could overflow but unsigned arithmetic has well-defined
151 // overflows.
152 uint32_t new_difference =
153 uint32_t(new_scheme_with_colon.size()) - components.protocol_end;
154
155 if (buffer.empty()) {
156 buffer.append(new_scheme_with_colon);
157 } else {
158 buffer.erase(0, components.protocol_end);
159 buffer.insert(0, new_scheme_with_colon);
160 }
161 components.protocol_end += new_difference;
162
163 // Update the rest of the components.
164 components.username_end += new_difference;
165 components.host_start += new_difference;
166 components.host_end += new_difference;
167 components.pathname_start += new_difference;
168 if (components.search_start != url_components::omitted) {
169 components.search_start += new_difference;
170 }
171 if (components.hash_start != url_components::omitted) {
172 components.hash_start += new_difference;
173 }
174 ADA_ASSERT_TRUE(validate());
175}
176
177inline void url_aggregator::set_scheme(std::string_view new_scheme) noexcept {
178 ada_log("url_aggregator::set_scheme ", new_scheme);
179 ADA_ASSERT_TRUE(validate());
180 ADA_ASSERT_TRUE(new_scheme.empty() || new_scheme.back() != ':');
181 // next line could overflow but unsigned arithmetic has well-defined
182 // overflows.
183 uint32_t new_difference =
184 uint32_t(new_scheme.size()) - components.protocol_end + 1;
185
187 if (buffer.empty()) {
188 buffer.append(helpers::concat(new_scheme, ":"));
189 } else {
190 buffer.erase(0, components.protocol_end);
191 buffer.insert(0, helpers::concat(new_scheme, ":"));
192 }
193 components.protocol_end = uint32_t(new_scheme.size() + 1);
194
195 // Update the rest of the components.
196 components.username_end += new_difference;
197 components.host_start += new_difference;
198 components.host_end += new_difference;
199 components.pathname_start += new_difference;
200 if (components.search_start != url_components::omitted) {
201 components.search_start += new_difference;
202 }
203 if (components.hash_start != url_components::omitted) {
204 components.hash_start += new_difference;
205 }
206 ADA_ASSERT_TRUE(validate());
207}
208
209bool url_aggregator::set_protocol(const std::string_view input) {
210 ada_log("url_aggregator::set_protocol ", input);
212 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
213 std::string view(input);
214 helpers::remove_ascii_tab_or_newline(view);
215 if (view.empty()) {
216 return true;
217 }
218
219 // Schemes should start with alpha values.
220 if (!checkers::is_alpha(view[0])) {
221 return false;
222 }
223
224 view.append(":");
225
226 std::string::iterator pointer =
227 std::ranges::find_if_not(view, unicode::is_alnum_plus);
228
229 if (pointer != view.end() && *pointer == ':') {
230 return parse_scheme_with_colon<true>(
231 std::string_view(view.data(), pointer - view.begin() + 1));
232 }
233 return false;
234}
235
236bool url_aggregator::set_username(const std::string_view input) {
237 ada_log("url_aggregator::set_username '", input, "' ");
239 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
240 if (cannot_have_credentials_or_port()) {
241 return false;
242 }
245 if (idx == input.size()) {
246 update_base_username(input);
247 } else {
248 // We only create a temporary string if we have to!
249 update_base_username(ada::unicode::percent_encode(
251 }
253 return true;
254}
255
256bool url_aggregator::set_password(const std::string_view input) {
257 ada_log("url_aggregator::set_password '", input, "'");
259 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
260 if (cannot_have_credentials_or_port()) {
261 return false;
262 }
265 if (idx == input.size()) {
266 update_base_password(input);
267 } else {
268 // We only create a temporary string if we have to!
269 update_base_password(ada::unicode::percent_encode(
271 }
273 return true;
274}
275
276bool url_aggregator::set_port(const std::string_view input) {
277 ada_log("url_aggregator::set_port ", input);
279 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
280 if (cannot_have_credentials_or_port()) {
281 return false;
282 }
283 std::string trimmed(input);
284 helpers::remove_ascii_tab_or_newline(trimmed);
285 if (trimmed.empty()) {
286 clear_port();
287 return true;
288 }
289 // Input should not start with control characters.
290 if (ada::unicode::is_c0_control_or_space(trimmed.front())) {
291 return false;
292 }
293 // Input should contain at least one ascii digit.
294 if (input.find_first_of("0123456789") == std::string_view::npos) {
295 return false;
296 }
297
298 // Revert changes if parse_port fails.
299 uint32_t previous_port = components.port;
300 parse_port(trimmed);
301 if (is_valid) {
302 return true;
303 }
304 update_base_port(previous_port);
305 is_valid = true;
307 return false;
308}
309
310bool url_aggregator::set_pathname(const std::string_view input) {
311 ada_log("url_aggregator::set_pathname ", input);
313 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
314 if (has_opaque_path) {
315 return false;
316 }
317 clear_pathname();
318 parse_path(input);
319 if (get_pathname().starts_with("//") && !has_authority() && !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 constexpr (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
651 ada_log("url_aggregator::get_username");
653 return helpers::substring(buffer, components.protocol_end + 2,
654 components.username_end);
655 }
656 return "";
657}
658
659[[nodiscard]] std::string_view url_aggregator::get_password() const noexcept
661 ada_log("url_aggregator::get_password");
663 return helpers::substring(buffer, components.username_end + 1,
664 components.host_start);
665 }
666 return "";
667}
668
669[[nodiscard]] std::string_view url_aggregator::get_port() const noexcept
671 ada_log("url_aggregator::get_port");
672 if (components.port == url_components::omitted) {
673 return "";
674 }
675 return helpers::substring(buffer, components.host_end + 1,
676 components.pathname_start);
677}
678
679[[nodiscard]] std::string_view url_aggregator::get_hash() const noexcept
681 ada_log("url_aggregator::get_hash");
682 // If this's URL's fragment is either null or the empty string, then return
683 // the empty string. Return U+0023 (#), followed by this's URL's fragment.
684 if (components.hash_start == url_components::omitted) {
685 return "";
686 }
687 if (buffer.size() - components.hash_start <= 1) {
688 return "";
689 }
690 return helpers::substring(buffer, components.hash_start);
691}
692
693[[nodiscard]] std::string_view url_aggregator::get_host() const noexcept
695 ada_log("url_aggregator::get_host");
696 // Technically, we should check if there is a hostname, but
697 // the code below works even if there isn't.
698 // if(!has_hostname()) { return ""; }
699 size_t start = components.host_start;
700 if (components.host_end > components.host_start &&
701 buffer[components.host_start] == '@') {
702 start++;
703 }
704 // if we have an empty host, then the space between components.host_end and
705 // components.pathname_start may be occupied by /.
706 if (start == components.host_end) {
707 return {};
708 }
709 return helpers::substring(buffer, start, components.pathname_start);
710}
711
712[[nodiscard]] std::string_view url_aggregator::get_hostname() const noexcept
714 ada_log("url_aggregator::get_hostname");
715 // Technically, we should check if there is a hostname, but
716 // the code below works even if there isn't.
717 // if(!has_hostname()) { return ""; }
718 size_t start = components.host_start;
719 // So host_start is not where the host begins.
720 if (components.host_end > components.host_start &&
721 buffer[components.host_start] == '@') {
722 start++;
723 }
724 return helpers::substring(buffer, start, components.host_end);
725}
726
727[[nodiscard]] std::string_view url_aggregator::get_search() const noexcept
729 ada_log("url_aggregator::get_search");
730 // If this's URL's query is either null or the empty string, then return the
731 // empty string. Return U+003F (?), followed by this's URL's query.
732 if (components.search_start == url_components::omitted) {
733 return "";
734 }
735 auto ending_index = uint32_t(buffer.size());
736 if (components.hash_start != url_components::omitted) {
737 ending_index = components.hash_start;
738 }
739 if (ending_index - components.search_start <= 1) {
740 return "";
741 }
742 return helpers::substring(buffer, components.search_start, ending_index);
743}
744
745[[nodiscard]] std::string_view url_aggregator::get_protocol() const noexcept
747 ada_log("url_aggregator::get_protocol");
748 return helpers::substring(buffer, 0, components.protocol_end);
749}
750
751[[nodiscard]] std::string ada::url_aggregator::to_string() const {
752 ada_log("url_aggregator::to_string buffer:", buffer, " [", buffer.size(),
753 " bytes]");
754 if (!is_valid) {
755 return "null";
756 }
757
758 std::string answer;
759 auto back = std::back_insert_iterator(answer);
760 answer.append("{\n");
761
762 answer.append("\t\"buffer\":\"");
763 helpers::encode_json(buffer, back);
764 answer.append("\",\n");
765
766 answer.append("\t\"protocol\":\"");
767 helpers::encode_json(get_protocol(), back);
768 answer.append("\",\n");
769
770 if (has_credentials()) {
771 answer.append("\t\"username\":\"");
772 helpers::encode_json(get_username(), back);
773 answer.append("\",\n");
774 answer.append("\t\"password\":\"");
775 helpers::encode_json(get_password(), back);
776 answer.append("\",\n");
777 }
778
779 answer.append("\t\"host\":\"");
780 helpers::encode_json(get_host(), back);
781 answer.append("\",\n");
782
783 answer.append("\t\"path\":\"");
784 helpers::encode_json(get_pathname(), back);
785 answer.append("\",\n");
786 answer.append("\t\"opaque path\":");
787 answer.append((has_opaque_path ? "true" : "false"));
788 answer.append(",\n");
789
790 if (components.search_start != url_components::omitted) {
791 answer.append("\t\"query\":\"");
792 helpers::encode_json(get_search(), back);
793 answer.append("\",\n");
794 }
795 if (components.hash_start != url_components::omitted) {
796 answer.append("\t\"fragment\":\"");
797 helpers::encode_json(get_hash(), back);
798 answer.append("\",\n");
799 }
800
801 auto convert_offset_to_string = [](uint32_t offset) -> std::string {
802 if (offset == url_components::omitted) {
803 return "null";
804 } else {
805 return std::to_string(offset);
806 }
807 };
808
809 answer.append("\t\"protocol_end\":");
810 answer.append(convert_offset_to_string(components.protocol_end));
811 answer.append(",\n");
812
813 answer.append("\t\"username_end\":");
814 answer.append(convert_offset_to_string(components.username_end));
815 answer.append(",\n");
816
817 answer.append("\t\"host_start\":");
818 answer.append(convert_offset_to_string(components.host_start));
819 answer.append(",\n");
820
821 answer.append("\t\"host_end\":");
822 answer.append(convert_offset_to_string(components.host_end));
823 answer.append(",\n");
824
825 answer.append("\t\"port\":");
826 answer.append(convert_offset_to_string(components.port));
827 answer.append(",\n");
828
829 answer.append("\t\"pathname_start\":");
830 answer.append(convert_offset_to_string(components.pathname_start));
831 answer.append(",\n");
832
833 answer.append("\t\"search_start\":");
834 answer.append(convert_offset_to_string(components.search_start));
835 answer.append(",\n");
836
837 answer.append("\t\"hash_start\":");
838 answer.append(convert_offset_to_string(components.hash_start));
839 answer.append("\n}");
840
841 return answer;
842}
843
844[[nodiscard]] bool url_aggregator::has_valid_domain() const noexcept {
845 if (components.host_start == components.host_end) {
846 return false;
847 }
848 return checkers::verify_dns_length(get_hostname());
849}
850
851bool url_aggregator::parse_ipv4(std::string_view input, bool in_place) {
852 ada_log("parse_ipv4 ", input, " [", input.size(),
853 " bytes], overlaps with buffer: ",
854 helpers::overlaps(input, buffer) ? "yes" : "no");
856 const bool trailing_dot = (input.back() == '.');
857 if (trailing_dot) {
858 input.remove_suffix(1);
859 }
860 size_t digit_count{0};
861 int pure_decimal_count = 0; // entries that are decimal
862 uint64_t ipv4{0};
863 // we could unroll for better performance?
864 for (; (digit_count < 4) && !(input.empty()); digit_count++) {
865 uint32_t
866 segment_result{}; // If any number exceeds 32 bits, we have an error.
867 bool is_hex = checkers::has_hex_prefix(input);
868 if (is_hex && ((input.length() == 2) ||
869 ((input.length() > 2) && (input[2] == '.')))) {
870 // special case
871 segment_result = 0;
872 input.remove_prefix(2);
873 } else {
874 std::from_chars_result r{};
875 if (is_hex) {
876 ada_log("parse_ipv4 trying to parse hex number");
877 r = std::from_chars(input.data() + 2, input.data() + input.size(),
878 segment_result, 16);
879 } else if ((input.length() >= 2) && input[0] == '0' &&
880 checkers::is_digit(input[1])) {
881 ada_log("parse_ipv4 trying to parse octal number");
882 r = std::from_chars(input.data() + 1, input.data() + input.size(),
883 segment_result, 8);
884 } else {
885 ada_log("parse_ipv4 trying to parse decimal number");
886 pure_decimal_count++;
887 r = std::from_chars(input.data(), input.data() + input.size(),
888 segment_result, 10);
889 }
890 if (r.ec != std::errc()) {
891 ada_log("parse_ipv4 parsing failed");
892 return is_valid = false;
893 }
894 ada_log("parse_ipv4 parsed ", segment_result);
895 input.remove_prefix(r.ptr - input.data());
896 }
897 if (input.empty()) {
898 // We have the last value.
899 // At this stage, ipv4 contains digit_count*8 bits.
900 // So we have 32-digit_count*8 bits left.
901 if (segment_result >= (uint64_t(1) << (32 - digit_count * 8))) {
902 return is_valid = false;
903 }
904 ipv4 <<= (32 - digit_count * 8);
905 ipv4 |= segment_result;
906 goto final;
907 } else {
908 // There is more, so that the value must no be larger than 255
909 // and we must have a '.'.
910 if ((segment_result > 255) || (input[0] != '.')) {
911 return is_valid = false;
912 }
913 ipv4 <<= 8;
914 ipv4 |= segment_result;
915 input.remove_prefix(1); // remove '.'
916 }
917 }
918 if ((digit_count != 4) || (!input.empty())) {
919 ada_log("parse_ipv4 found invalid (more than 4 numbers or empty) ");
920 return is_valid = false;
921 }
922final:
923 ada_log("url_aggregator::parse_ipv4 completed ", get_href(),
924 " host: ", get_host());
925
926 // We could also check r.ptr to see where the parsing ended.
927 if (in_place && pure_decimal_count == 4 && !trailing_dot) {
928 ada_log(
929 "url_aggregator::parse_ipv4 completed and was already correct in the "
930 "buffer");
931 // The original input was already all decimal and we validated it. So we
932 // don't need to do anything.
933 } else {
934 ada_log("url_aggregator::parse_ipv4 completed and we need to update it");
935 // Optimization opportunity: Get rid of unnecessary string return in ipv4
936 // serializer.
937 // TODO: This is likely a bug because it goes back update_base_hostname, not
938 // what we want to do.
939 update_base_hostname(
940 ada::serializers::ipv4(ipv4)); // We have to reserialize the address.
941 }
942 host_type = IPV4;
944 return true;
945}
946
947bool url_aggregator::parse_ipv6(std::string_view input) {
948 // TODO: Implement in_place optimization: we know that input points
949 // in the buffer, so we can just check whether the buffer is already
950 // well formatted.
951 // TODO: Find a way to merge parse_ipv6 with url.cpp implementation.
952 ada_log("parse_ipv6 ", input, " [", input.size(), " bytes]");
954 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
955 if (input.empty()) {
956 return is_valid = false;
957 }
958 // Let address be a new IPv6 address whose IPv6 pieces are all 0.
959 std::array<uint16_t, 8> address{};
960
961 // Let pieceIndex be 0.
962 int piece_index = 0;
963
964 // Let compress be null.
965 std::optional<int> compress{};
966
967 // Let pointer be a pointer for input.
968 std::string_view::iterator pointer = input.begin();
969
970 // If c is U+003A (:), then:
971 if (input[0] == ':') {
972 // If remaining does not start with U+003A (:), validation error, return
973 // failure.
974 if (input.size() == 1 || input[1] != ':') {
975 ada_log("parse_ipv6 starts with : but the rest does not start with :");
976 return is_valid = false;
977 }
978
979 // Increase pointer by 2.
980 pointer += 2;
981
982 // Increase pieceIndex by 1 and then set compress to pieceIndex.
983 compress = ++piece_index;
984 }
985
986 // While c is not the EOF code point:
987 while (pointer != input.end()) {
988 // If pieceIndex is 8, validation error, return failure.
989 if (piece_index == 8) {
990 ada_log("parse_ipv6 piece_index == 8");
991 return is_valid = false;
992 }
993
994 // If c is U+003A (:), then:
995 if (*pointer == ':') {
996 // If compress is non-null, validation error, return failure.
997 if (compress.has_value()) {
998 ada_log("parse_ipv6 compress is non-null");
999 return is_valid = false;
1000 }
1001
1002 // Increase pointer and pieceIndex by 1, set compress to pieceIndex, and
1003 // then continue.
1004 pointer++;
1005 compress = ++piece_index;
1006 continue;
1007 }
1008
1009 // Let value and length be 0.
1010 uint16_t value = 0, length = 0;
1011
1012 // While length is less than 4 and c is an ASCII hex digit,
1013 // set value to value times 0x10 + c interpreted as hexadecimal number, and
1014 // increase pointer and length by 1.
1015 while (length < 4 && pointer != input.end() &&
1016 unicode::is_ascii_hex_digit(*pointer)) {
1017 // https://stackoverflow.com/questions/39060852/why-does-the-addition-of-two-shorts-return-an-int
1018 value = uint16_t(value * 0x10 + unicode::convert_hex_to_binary(*pointer));
1019 pointer++;
1020 length++;
1021 }
1022
1023 // If c is U+002E (.), then:
1024 if (pointer != input.end() && *pointer == '.') {
1025 // If length is 0, validation error, return failure.
1026 if (length == 0) {
1027 ada_log("parse_ipv6 length is 0");
1028 return is_valid = false;
1029 }
1030
1031 // Decrease pointer by length.
1032 pointer -= length;
1033
1034 // If pieceIndex is greater than 6, validation error, return failure.
1035 if (piece_index > 6) {
1036 ada_log("parse_ipv6 piece_index > 6");
1037 return is_valid = false;
1038 }
1039
1040 // Let numbersSeen be 0.
1041 int numbers_seen = 0;
1042
1043 // While c is not the EOF code point:
1044 while (pointer != input.end()) {
1045 // Let ipv4Piece be null.
1046 std::optional<uint16_t> ipv4_piece{};
1047
1048 // If numbersSeen is greater than 0, then:
1049 if (numbers_seen > 0) {
1050 // If c is a U+002E (.) and numbersSeen is less than 4, then increase
1051 // pointer by 1.
1052 if (*pointer == '.' && numbers_seen < 4) {
1053 pointer++;
1054 } else {
1055 // Otherwise, validation error, return failure.
1056 ada_log("parse_ipv6 Otherwise, validation error, return failure");
1057 return is_valid = false;
1058 }
1059 }
1060
1061 // If c is not an ASCII digit, validation error, return failure.
1062 if (pointer == input.end() || !checkers::is_digit(*pointer)) {
1063 ada_log(
1064 "parse_ipv6 If c is not an ASCII digit, validation error, return "
1065 "failure");
1066 return is_valid = false;
1067 }
1068
1069 // While c is an ASCII digit:
1070 while (pointer != input.end() && checkers::is_digit(*pointer)) {
1071 // Let number be c interpreted as decimal number.
1072 int number = *pointer - '0';
1073
1074 // If ipv4Piece is null, then set ipv4Piece to number.
1075 if (!ipv4_piece.has_value()) {
1076 ipv4_piece = number;
1077 }
1078 // Otherwise, if ipv4Piece is 0, validation error, return failure.
1079 else if (ipv4_piece == 0) {
1080 ada_log("parse_ipv6 if ipv4Piece is 0, validation error");
1081 return is_valid = false;
1082 }
1083 // Otherwise, set ipv4Piece to ipv4Piece times 10 + number.
1084 else {
1085 ipv4_piece = *ipv4_piece * 10 + number;
1086 }
1087
1088 // If ipv4Piece is greater than 255, validation error, return failure.
1089 if (ipv4_piece > 255) {
1090 ada_log("parse_ipv6 ipv4_piece > 255");
1091 return is_valid = false;
1092 }
1093
1094 // Increase pointer by 1.
1095 pointer++;
1096 }
1097
1098 // Set address[pieceIndex] to address[pieceIndex] times 0x100 +
1099 // ipv4Piece.
1100 // https://stackoverflow.com/questions/39060852/why-does-the-addition-of-two-shorts-return-an-int
1101 address[piece_index] =
1102 uint16_t(address[piece_index] * 0x100 + *ipv4_piece);
1103
1104 // Increase numbersSeen by 1.
1105 numbers_seen++;
1106
1107 // If numbersSeen is 2 or 4, then increase pieceIndex by 1.
1108 if (numbers_seen == 2 || numbers_seen == 4) {
1109 piece_index++;
1110 }
1111 }
1112
1113 // If numbersSeen is not 4, validation error, return failure.
1114 if (numbers_seen != 4) {
1115 return is_valid = false;
1116 }
1117
1118 // Break.
1119 break;
1120 }
1121 // Otherwise, if c is U+003A (:):
1122 else if ((pointer != input.end()) && (*pointer == ':')) {
1123 // Increase pointer by 1.
1124 pointer++;
1125
1126 // If c is the EOF code point, validation error, return failure.
1127 if (pointer == input.end()) {
1128 ada_log(
1129 "parse_ipv6 If c is the EOF code point, validation error, return "
1130 "failure");
1131 return is_valid = false;
1132 }
1133 }
1134 // Otherwise, if c is not the EOF code point, validation error, return
1135 // failure.
1136 else if (pointer != input.end()) {
1137 ada_log(
1138 "parse_ipv6 Otherwise, if c is not the EOF code point, validation "
1139 "error, return failure");
1140 return is_valid = false;
1141 }
1142
1143 // Set address[pieceIndex] to value.
1144 address[piece_index] = value;
1145
1146 // Increase pieceIndex by 1.
1147 piece_index++;
1148 }
1149
1150 // If compress is non-null, then:
1151 if (compress.has_value()) {
1152 // Let swaps be pieceIndex - compress.
1153 int swaps = piece_index - *compress;
1154
1155 // Set pieceIndex to 7.
1156 piece_index = 7;
1157
1158 // While pieceIndex is not 0 and swaps is greater than 0,
1159 // swap address[pieceIndex] with address[compress + swaps - 1], and then
1160 // decrease both pieceIndex and swaps by 1.
1161 while (piece_index != 0 && swaps > 0) {
1162 std::swap(address[piece_index], address[*compress + swaps - 1]);
1163 piece_index--;
1164 swaps--;
1165 }
1166 }
1167 // Otherwise, if compress is null and pieceIndex is not 8, validation error,
1168 // return failure.
1169 else if (piece_index != 8) {
1170 ada_log(
1171 "parse_ipv6 if compress is null and pieceIndex is not 8, validation "
1172 "error, return failure");
1173 return is_valid = false;
1174 }
1175 // TODO: Optimization opportunity: Get rid of unnecessary string creation.
1176 // TODO: This is likely a bug because it goes back update_base_hostname, not
1177 // what we want to do.
1178 update_base_hostname(ada::serializers::ipv6(address));
1179 ada_log("parse_ipv6 ", get_hostname());
1181 host_type = IPV6;
1182 return true;
1183}
1184
1185bool url_aggregator::parse_opaque_host(std::string_view input) {
1186 ada_log("parse_opaque_host ", input, " [", input.size(), " bytes]");
1188 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
1189 if (std::any_of(input.begin(), input.end(),
1190 ada::unicode::is_forbidden_host_code_point)) {
1191 return is_valid = false;
1192 }
1193
1194 // Return the result of running UTF-8 percent-encode on input using the C0
1195 // control percent-encode set.
1198 if (idx == input.size()) {
1199 update_base_hostname(input);
1200 } else {
1201 // We only create a temporary string if we need to.
1202 update_base_hostname(ada::unicode::percent_encode(
1204 }
1206 return true;
1207}
1208
1209[[nodiscard]] std::string url_aggregator::to_diagram() const {
1210 if (!is_valid) {
1211 return "invalid";
1212 }
1213 std::string answer;
1214 answer.append(buffer);
1215 answer.append(" [");
1216 answer.append(std::to_string(buffer.size()));
1217 answer.append(" bytes]");
1218 answer.append("\n");
1219 // first line
1220 std::string line1;
1221 line1.resize(buffer.size(), ' ');
1222 if (components.hash_start != url_components::omitted) {
1223 line1[components.hash_start] = '|';
1224 }
1225 if (components.search_start != url_components::omitted) {
1226 line1[components.search_start] = '|';
1227 }
1228 if (components.pathname_start != buffer.size()) {
1229 line1[components.pathname_start] = '|';
1230 }
1231 if (components.host_end != buffer.size()) {
1232 line1[components.host_end] = '|';
1233 }
1234 if (components.host_start != buffer.size()) {
1235 line1[components.host_start] = '|';
1236 }
1237 if (components.username_end != buffer.size()) {
1238 line1[components.username_end] = '|';
1239 }
1240 if (components.protocol_end != buffer.size()) {
1241 line1[components.protocol_end] = '|';
1242 }
1243 answer.append(line1);
1244 answer.append("\n");
1245
1246 std::string line2 = line1;
1247 if (components.hash_start != url_components::omitted) {
1248 line2[components.hash_start] = '`';
1249 line1[components.hash_start] = ' ';
1250
1251 for (size_t i = components.hash_start + 1; i < line2.size(); i++) {
1252 line2[i] = '-';
1253 }
1254 line2.append(" hash_start");
1255 answer.append(line2);
1256 answer.append("\n");
1257 }
1258
1259 std::string line3 = line1;
1260 if (components.search_start != url_components::omitted) {
1261 line3[components.search_start] = '`';
1262 line1[components.search_start] = ' ';
1263
1264 for (size_t i = components.search_start + 1; i < line3.size(); i++) {
1265 line3[i] = '-';
1266 }
1267 line3.append(" search_start ");
1268 line3.append(std::to_string(components.search_start));
1269 answer.append(line3);
1270 answer.append("\n");
1271 }
1272
1273 std::string line4 = line1;
1274 if (components.pathname_start != buffer.size()) {
1275 line4[components.pathname_start] = '`';
1276 line1[components.pathname_start] = ' ';
1277 for (size_t i = components.pathname_start + 1; i < line4.size(); i++) {
1278 line4[i] = '-';
1279 }
1280 line4.append(" pathname_start ");
1281 line4.append(std::to_string(components.pathname_start));
1282 answer.append(line4);
1283 answer.append("\n");
1284 }
1285
1286 std::string line5 = line1;
1287 if (components.host_end != buffer.size()) {
1288 line5[components.host_end] = '`';
1289 line1[components.host_end] = ' ';
1290
1291 for (size_t i = components.host_end + 1; i < line5.size(); i++) {
1292 line5[i] = '-';
1293 }
1294 line5.append(" host_end ");
1295 line5.append(std::to_string(components.host_end));
1296 answer.append(line5);
1297 answer.append("\n");
1298 }
1299
1300 std::string line6 = line1;
1301 if (components.host_start != buffer.size()) {
1302 line6[components.host_start] = '`';
1303 line1[components.host_start] = ' ';
1304
1305 for (size_t i = components.host_start + 1; i < line6.size(); i++) {
1306 line6[i] = '-';
1307 }
1308 line6.append(" host_start ");
1309 line6.append(std::to_string(components.host_start));
1310 answer.append(line6);
1311 answer.append("\n");
1312 }
1313
1314 std::string line7 = line1;
1315 if (components.username_end != buffer.size()) {
1316 line7[components.username_end] = '`';
1317 line1[components.username_end] = ' ';
1318
1319 for (size_t i = components.username_end + 1; i < line7.size(); i++) {
1320 line7[i] = '-';
1321 }
1322 line7.append(" username_end ");
1323 line7.append(std::to_string(components.username_end));
1324 answer.append(line7);
1325 answer.append("\n");
1326 }
1327
1328 std::string line8 = line1;
1329 if (components.protocol_end != buffer.size()) {
1330 line8[components.protocol_end] = '`';
1331 line1[components.protocol_end] = ' ';
1332
1333 for (size_t i = components.protocol_end + 1; i < line8.size(); i++) {
1334 line8[i] = '-';
1335 }
1336 line8.append(" protocol_end ");
1337 line8.append(std::to_string(components.protocol_end));
1338 answer.append(line8);
1339 answer.append("\n");
1340 }
1341
1342 if (components.hash_start == url_components::omitted) {
1343 answer.append("note: hash omitted\n");
1344 }
1345 if (components.search_start == url_components::omitted) {
1346 answer.append("note: search omitted\n");
1347 }
1348 if (components.protocol_end > buffer.size()) {
1349 answer.append("warning: protocol_end overflows\n");
1350 }
1351 if (components.username_end > buffer.size()) {
1352 answer.append("warning: username_end overflows\n");
1353 }
1354 if (components.host_start > buffer.size()) {
1355 answer.append("warning: host_start overflows\n");
1356 }
1357 if (components.host_end > buffer.size()) {
1358 answer.append("warning: host_end overflows\n");
1359 }
1360 if (components.pathname_start > buffer.size()) {
1361 answer.append("warning: pathname_start overflows\n");
1362 }
1363 return answer;
1364}
1365
1366void url_aggregator::delete_dash_dot() {
1367 ada_log("url_aggregator::delete_dash_dot");
1369 ADA_ASSERT_TRUE(has_dash_dot());
1370 buffer.erase(components.host_end, 2);
1371 components.pathname_start -= 2;
1372 if (components.search_start != url_components::omitted) {
1373 components.search_start -= 2;
1374 }
1375 if (components.hash_start != url_components::omitted) {
1376 components.hash_start -= 2;
1377 }
1379 ADA_ASSERT_TRUE(!has_dash_dot());
1380}
1381
1382inline void url_aggregator::consume_prepared_path(std::string_view input) {
1383 ada_log("url_aggregator::consume_prepared_path ", input);
1384
1393 uint8_t accumulator = checkers::path_signature(input);
1394 // Let us first detect a trivial case.
1395 // If it is special, we check that we have no dot, no %, no \ and no
1396 // character needing percent encoding. Otherwise, we check that we have no %,
1397 // no dot, and no character needing percent encoding.
1398 constexpr uint8_t need_encoding = 1;
1399 constexpr uint8_t backslash_char = 2;
1400 constexpr uint8_t dot_char = 4;
1401 constexpr uint8_t percent_char = 8;
1402 bool special = type != ada::scheme::NOT_SPECIAL;
1403 bool may_need_slow_file_handling = (type == ada::scheme::type::FILE &&
1405 bool trivial_path =
1406 (special ? (accumulator == 0)
1407 : ((accumulator & (need_encoding | dot_char | percent_char)) ==
1408 0)) &&
1409 (!may_need_slow_file_handling);
1410 if (accumulator == dot_char && !may_need_slow_file_handling) {
1411 // '4' means that we have at least one dot, but nothing that requires
1412 // percent encoding or decoding. The only part that is not trivial is
1413 // that we may have single dots and double dots path segments.
1414 // If we have such segments, then we either have a path that begins
1415 // with '.' (easy to check), or we have the sequence './'.
1416 // Note: input cannot be empty, it must at least contain one character ('.')
1417 // Note: we know that '\' is not present.
1418 if (input[0] != '.') {
1419 size_t slashdot = input.find("/.");
1420 if (slashdot == std::string_view::npos) { // common case
1421 trivial_path = true;
1422 } else { // uncommon
1423 // only three cases matter: /./, /.. or a final /
1424 trivial_path =
1425 !(slashdot + 2 == input.size() || input[slashdot + 2] == '.' ||
1426 input[slashdot + 2] == '/');
1427 }
1428 }
1429 }
1430 if (trivial_path && is_at_path()) {
1431 ada_log("parse_path trivial");
1432 buffer += '/';
1433 buffer += input;
1434 return;
1435 }
1436 std::string path = std::string(get_pathname());
1437 // We are going to need to look a bit at the path, but let us see if we can
1438 // ignore percent encoding *and* backslashes *and* percent characters.
1439 // Except for the trivial case, this is likely to capture 99% of paths out
1440 // there.
1441 bool fast_path =
1442 (special &&
1443 (accumulator & (need_encoding | backslash_char | percent_char)) == 0) &&
1444 (type != ada::scheme::type::FILE);
1445 if (fast_path) {
1446 ada_log("parse_prepared_path fast");
1447 // Here we don't need to worry about \ or percent encoding.
1448 // We also do not have a file protocol. We might have dots, however,
1449 // but dots must as appear as '.', and they cannot be encoded because
1450 // the symbol '%' is not present.
1451 size_t previous_location = 0; // We start at 0.
1452 do {
1453 size_t new_location = input.find('/', previous_location);
1454 // std::string_view path_view = input;
1455 // We process the last segment separately:
1456 if (new_location == std::string_view::npos) {
1457 std::string_view path_view = input.substr(previous_location);
1458 if (path_view == "..") { // The path ends with ..
1459 // e.g., if you receive ".." with an empty path, you go to "/".
1460 if (path.empty()) {
1461 path = '/';
1462 update_base_pathname(path);
1463 return;
1464 }
1465 // Fast case where we have nothing to do:
1466 if (path.back() == '/') {
1467 update_base_pathname(path);
1468 return;
1469 }
1470 // If you have the path "/joe/myfriend",
1471 // then you delete 'myfriend'.
1472 path.resize(path.rfind('/') + 1);
1473 update_base_pathname(path);
1474 return;
1475 }
1476 path += '/';
1477 if (path_view != ".") {
1478 path.append(path_view);
1479 }
1480 update_base_pathname(path);
1481 return;
1482 } else {
1483 // This is a non-final segment.
1484 std::string_view path_view =
1485 input.substr(previous_location, new_location - previous_location);
1486 previous_location = new_location + 1;
1487 if (path_view == "..") {
1488 size_t last_delimiter = path.rfind('/');
1489 if (last_delimiter != std::string::npos) {
1490 path.erase(last_delimiter);
1491 }
1492 } else if (path_view != ".") {
1493 path += '/';
1494 path.append(path_view);
1495 }
1496 }
1497 } while (true);
1498 } else {
1499 ada_log("parse_path slow");
1500 // we have reached the general case
1501 bool needs_percent_encoding = (accumulator & 1);
1502 std::string path_buffer_tmp;
1503 do {
1504 size_t location = (special && (accumulator & 2))
1505 ? input.find_first_of("/\\")
1506 : input.find('/');
1507 std::string_view path_view = input;
1508 if (location != std::string_view::npos) {
1509 path_view.remove_suffix(path_view.size() - location);
1510 input.remove_prefix(location + 1);
1511 }
1512 // path_buffer is either path_view or it might point at a percent encoded
1513 // temporary string.
1514 std::string_view path_buffer =
1515 (needs_percent_encoding &&
1516 ada::unicode::percent_encode<false>(
1517 path_view, character_sets::PATH_PERCENT_ENCODE, path_buffer_tmp))
1518 ? path_buffer_tmp
1519 : path_view;
1520 if (unicode::is_double_dot_path_segment(path_buffer)) {
1521 if ((helpers::shorten_path(path, type) || special) &&
1522 location == std::string_view::npos) {
1523 path += '/';
1524 }
1525 } else if (unicode::is_single_dot_path_segment(path_buffer) &&
1526 (location == std::string_view::npos)) {
1527 path += '/';
1528 }
1529 // Otherwise, if path_buffer is not a single-dot path segment, then:
1530 else if (!unicode::is_single_dot_path_segment(path_buffer)) {
1531 // If url's scheme is "file", url's path is empty, and path_buffer is a
1532 // Windows drive letter, then replace the second code point in
1533 // path_buffer with U+003A (:).
1534 if (type == ada::scheme::type::FILE && path.empty() &&
1535 checkers::is_windows_drive_letter(path_buffer)) {
1536 path += '/';
1537 path += path_buffer[0];
1538 path += ':';
1539 path_buffer.remove_prefix(2);
1540 path.append(path_buffer);
1541 } else {
1542 // Append path_buffer to url's path.
1543 path += '/';
1544 path.append(path_buffer);
1545 }
1546 }
1547 if (location == std::string_view::npos) {
1548 update_base_pathname(path);
1549 return;
1550 }
1551 } while (true);
1552 }
1553}
1554} // namespace ada
Includes all definitions for 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:77
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:31
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.
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.