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