Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url.cpp
Go to the documentation of this file.
1#include "ada/scheme-inl.h"
3#include "ada/log.h"
4#include "ada/serializers.h"
5#include "ada/unicode-inl.h"
6#include "ada/checkers-inl.h"
7#include "ada/url_ip-inl.h"
8
9#include <algorithm>
10#include <array>
11#include <cstddef>
12#include <cstdint>
13#include <iterator>
14#include <numeric>
15#include <optional>
16#include <ranges>
17#include <string>
18#include <string_view>
19#include <utility>
20
21namespace ada {
22
23bool url::parse_opaque_host(std::string_view input) {
24 ada_log("parse_opaque_host ", input, " [", input.size(), " bytes]");
25 if (std::ranges::any_of(input, ada::unicode::is_forbidden_host_code_point)) {
26 return is_valid = false;
27 }
28
29 // Return the result of running UTF-8 percent-encode on input using the C0
30 // control percent-encode set.
31 host = ada::unicode::percent_encode(
33 return true;
34}
35
36bool url::parse_ipv4(std::string_view input) {
37 ada_log("parse_ipv4 ", input, " [", input.size(), " bytes]");
38 if (input.empty()) {
39 return is_valid = false;
40 }
41 std::string_view original_input = input;
42 if (original_input.back() == '.') {
43 original_input.remove_suffix(1);
44 }
45 if (input.back() == '.') {
46 input.remove_suffix(1);
47 if (input.empty()) {
48 return is_valid = false;
49 }
50 }
51
52 const uint64_t fast = checkers::try_parse_ipv4_fast(input);
53 if (fast < checkers::ipv4_fast_fail) [[likely]] {
54 host = original_input;
56 return true;
57 }
58
59 const char* p = input.data();
60 const char* end = p + input.size();
61 uint64_t ipv4 = 0;
62 int digit_count = 0;
63 int pure_decimal_count = 0;
64
65 for (; digit_count < 4 && p < end; ++digit_count) {
66 uint64_t segment = 0;
67 bool pure = false;
68 if (!detail::parse_ipv4_number(p, end, segment, pure)) {
69 return is_valid = false;
70 }
71 if (pure) {
72 ++pure_decimal_count;
73 }
74 if (p >= end) {
75 const unsigned shift = static_cast<unsigned>(32 - digit_count * 8);
76 if (segment >= (uint64_t{1} << shift)) {
77 return is_valid = false;
78 }
79 ipv4 = (ipv4 << shift) | segment;
80 host = (pure_decimal_count == 4) ? std::string(original_input)
83 return true;
84 }
85 if (segment > 255 || *p != '.') {
86 return is_valid = false;
87 }
88 ipv4 = (ipv4 << 8) | segment;
89 ++p;
90 }
91 if (digit_count != 4 || p != end) {
92 return is_valid = false;
93 }
94 host = (pure_decimal_count == 4) ? std::string(original_input)
97 return true;
98}
99
100bool url::parse_ipv6(std::string_view input) {
101 ada_log("parse_ipv6 ", input, " [", input.size(), " bytes]");
102 if (input.empty() || input.size() > 45) [[unlikely]] {
103 return is_valid = false;
104 }
105 std::array<uint16_t, 8> address{};
106#if defined(ADA_AVX512_IPV6)
107 if (bool simd_valid; detail::try_parse_ipv6_avx512(input.data(), input.size(),
108 address, simd_valid)) {
109 if (!simd_valid) [[unlikely]] {
110 return is_valid = false;
111 }
112 host = ada::serializers::ipv6(address);
113 host_type = IPV6;
114 return true;
115 }
116 address = {};
117#endif
118 const char* pointer = input.data();
119 const char* const end = pointer + input.size();
120 int piece_index = 0;
121 int compress = -1;
122
123 if (*pointer == ':') {
124 if (input.size() == 1 || pointer[1] != ':') [[unlikely]] {
125 return is_valid = false;
126 }
127 pointer += 2;
128 compress = ++piece_index;
129 }
130
131 while (pointer != end) {
132 if (piece_index == 8) [[unlikely]] {
133 return is_valid = false;
134 }
135 if (*pointer == ':') {
136 if (compress != -1) [[unlikely]] {
137 return is_valid = false;
138 }
139 ++pointer;
140 compress = ++piece_index;
141 continue;
142 }
143
144 uint16_t value = 0;
145 const int length = detail::parse_hex_piece(pointer, end, value);
146
147 if (pointer != end && *pointer == '.') {
148 if (length == 0) [[unlikely]] {
149 return is_valid = false;
150 }
151 pointer -= length;
152 if (piece_index > 6) [[unlikely]] {
153 return is_valid = false;
154 }
155
156 int numbers_seen = 0;
157 while (pointer != end) {
158 int ipv4_piece = -1;
159 if (numbers_seen > 0) {
160 if (*pointer == '.' && numbers_seen < 4) {
161 ++pointer;
162 } else {
163 return is_valid = false;
164 }
165 }
166 if (pointer == end || *pointer < '0' || *pointer > '9') [[unlikely]] {
167 return is_valid = false;
168 }
169 ipv4_piece = *pointer - '0';
170 ++pointer;
171 if (pointer != end && *pointer >= '0' && *pointer <= '9') {
172 if (ipv4_piece == 0) [[unlikely]] {
173 return is_valid = false;
174 }
175 ipv4_piece = ipv4_piece * 10 + (*pointer - '0');
176 ++pointer;
177 if (pointer != end && *pointer >= '0' && *pointer <= '9') {
178 ipv4_piece = ipv4_piece * 10 + (*pointer - '0');
179 ++pointer;
180 if (ipv4_piece > 255) [[unlikely]] {
181 return is_valid = false;
182 }
183 }
184 }
185 address[static_cast<size_t>(piece_index)] = static_cast<uint16_t>(
186 address[static_cast<size_t>(piece_index)] * 0x100 +
187 static_cast<uint16_t>(ipv4_piece));
188 ++numbers_seen;
189 if (numbers_seen == 2 || numbers_seen == 4) {
190 ++piece_index;
191 }
192 }
193 if (numbers_seen != 4) [[unlikely]] {
194 return is_valid = false;
195 }
196 break;
197 }
198
199 if (length == 0) [[unlikely]] {
200 return is_valid = false;
201 }
202
203 if (pointer != end && *pointer == ':') {
204 ++pointer;
205 if (pointer == end) [[unlikely]] {
206 return is_valid = false;
207 }
208 } else if (pointer != end) [[unlikely]] {
209 return is_valid = false;
210 }
211
212 address[static_cast<size_t>(piece_index)] = value;
213 ++piece_index;
214 }
215
216 if (compress != -1) {
217 const int right = piece_index - compress;
218 if (right > 0) {
219 const size_t dest = static_cast<size_t>(8 - right);
220 const size_t src = static_cast<size_t>(compress);
221 if (dest != src) {
222 for (size_t i = static_cast<size_t>(right); i-- > 0;) {
223 address[dest + i] = address[src + i];
224 address[src + i] = 0;
225 }
226 }
227 }
228 } else if (piece_index != 8) [[unlikely]] {
229 return is_valid = false;
230 }
231
232 host = ada::serializers::ipv6(address);
233 ada_log("parse_ipv6 ", *host);
234 host_type = IPV6;
235 return true;
236}
237
238template <bool has_state_override>
239ada_really_inline bool url::parse_scheme(const std::string_view input) {
240 auto parsed_type = ada::scheme::get_scheme_type(input);
241 bool is_input_special = (parsed_type != ada::scheme::NOT_SPECIAL);
246 if (is_input_special) { // fast path!!!
247 if constexpr (has_state_override) {
248 // If url's scheme is not a special scheme and buffer is a special scheme,
249 // then return.
250 if (is_special() != is_input_special) {
251 return false;
252 }
253
254 // If url includes credentials or has a non-null port, and buffer is
255 // "file", then return.
256 if ((has_credentials() || port.has_value()) &&
257 parsed_type == ada::scheme::type::FILE) {
258 return false;
259 }
260
261 // If url's scheme is "file" and its host is an empty host, then return.
262 // An empty host is the empty string.
263 if (type == ada::scheme::type::FILE && host.has_value() &&
264 host->empty()) {
265 return false;
266 }
267 }
268
269 type = parsed_type;
270
271 if constexpr (has_state_override) {
272 // This is uncommon.
273 uint16_t urls_scheme_port = get_special_port();
274
275 if (urls_scheme_port) {
276 // If url's port is url's scheme's default port, then set url's port to
277 // null.
278 if (port.has_value() && *port == urls_scheme_port) {
279 port = std::nullopt;
280 }
281 }
282 }
283 } else { // slow path
284 std::string _buffer(input);
285 // Next function is only valid if the input is ASCII and returns false
286 // otherwise, but it seems that we always have ascii content so we do not
287 // need to check the return value.
288 // bool is_ascii =
289 unicode::to_lower_ascii(_buffer.data(), _buffer.size());
290
291 if constexpr (has_state_override) {
292 // The state-override validation errors below ("return" in the WHATWG URL
293 // parser) leave the URL unchanged. The setter contract is
294 // "true on success, false if the scheme is invalid" -- the fast path
295 // above already returns false here, so the slow path must agree.
296
297 // If url's scheme is a special scheme and buffer is not a special scheme,
298 // then return. If url's scheme is not a special scheme and buffer is a
299 // special scheme, then return.
300 if (is_special() != ada::scheme::is_special(_buffer)) {
301 return false;
302 }
303
304 // If url includes credentials or has a non-null port, and buffer is
305 // "file", then return.
306 if ((has_credentials() || port.has_value()) && _buffer == "file") {
307 return false;
308 }
309
310 // If url's scheme is "file" and its host is an empty host, then return.
311 // An empty host is the empty string.
312 if (type == ada::scheme::type::FILE && host.has_value() &&
313 host->empty()) {
314 return false;
315 }
316 }
317
318 set_scheme(std::move(_buffer));
319
320 if constexpr (has_state_override) {
321 // This is uncommon.
322 uint16_t urls_scheme_port = get_special_port();
323
324 if (urls_scheme_port) {
325 // If url's port is url's scheme's default port, then set url's port to
326 // null.
327 if (port.has_value() && *port == urls_scheme_port) {
328 port = std::nullopt;
329 }
330 }
331 }
332 }
333
334 return true;
335}
336
337ada_really_inline bool url::parse_host(std::string_view input) {
338 ada_log("parse_host ", input, " [", input.size(), " bytes]");
339 if (input.empty()) {
340 return is_valid = false;
341 } // technically unnecessary.
342 // If input starts with U+005B ([), then:
343 if (input[0] == '[') {
344 // If input does not end with U+005D (]), validation error, return failure.
345 if (input.back() != ']') {
346 return is_valid = false;
347 }
348 ada_log("parse_host ipv6");
349
350 // Return the result of IPv6 parsing input with its leading U+005B ([) and
351 // trailing U+005D (]) removed.
352 input.remove_prefix(1);
353 input.remove_suffix(1);
354 return parse_ipv6(input);
355 }
356
357 // If isNotSpecial is true, then return the result of opaque-host parsing
358 // input.
359 if (!is_special()) {
360 return parse_opaque_host(input);
361 }
362
363 // Fast path: try to parse as pure decimal IPv4 first.
364 const uint64_t fast_result = checkers::try_parse_ipv4_fast(input);
365 if (fast_result < checkers::ipv4_fast_fail) {
366 if (!input.empty() && input.back() == '.') {
367 host = input.substr(0, input.size() - 1);
368 } else {
369 host = input;
370 }
371 host_type = IPV4;
372 is_valid = true;
373 ada_log("parse_host fast path decimal ipv4");
374 return true;
375 }
376 // Let domain be the result of running UTF-8 decode without BOM on the
377 // percent-decoding of input. Let asciiDomain be the result of running domain
378 // to ASCII with domain and false. The most common case is an ASCII input, in
379 // which case we do not need to call the expensive 'to_ascii' if a few
380 // conditions are met: no '%' and no 'xn-' subsequence.
381 const uint8_t forbidden_or_upper =
382 unicode::contains_forbidden_domain_code_point_or_upper(input.data(),
383 input.size());
384 static constexpr std::string_view xn_dash{"xn-", 3};
385 if ((forbidden_or_upper & 1) == 0) {
386 host = std::string(input);
387 if ((forbidden_or_upper & 2) != 0) {
388 unicode::to_lower_ascii(host->data(), host->size());
389 }
390 if (host->find('-') == std::string_view::npos ||
391 host->find(xn_dash) == std::string_view::npos) {
392 if (checkers::is_ipv4(*host)) {
393 ada_log("parse_host fast path ipv4");
394 return parse_ipv4(*host);
395 }
396 ada_log("parse_host fast path ", *host);
397 is_valid = true;
398 return true;
399 }
400 } else if (const size_t first_percent = input.find('%');
401 first_percent != std::string_view::npos) {
402 std::string decoded = unicode::percent_decode(input, first_percent);
403 const uint8_t decoded_flags =
404 unicode::contains_forbidden_domain_code_point_or_upper(decoded.data(),
405 decoded.size());
406 if ((decoded_flags & 1) == 0) {
407 if ((decoded_flags & 2) != 0) {
408 unicode::to_lower_ascii(decoded.data(), decoded.size());
409 }
410 if (decoded.find('-') == std::string_view::npos ||
411 decoded.find(xn_dash) == std::string_view::npos) {
412 host = std::move(decoded);
413 if (checkers::is_ipv4(*host)) {
414 return parse_ipv4(*host);
415 }
416 is_valid = true;
417 return true;
418 }
419 }
420 }
421 ada_log("parse_host calling to_ascii");
422 is_valid = ada::unicode::to_ascii(host, input, input.find('%'));
423 if (!is_valid || !host.has_value()) {
424 ada_log("parse_host to_ascii returns false");
425 return is_valid = false;
426 }
427 ada_log("parse_host to_ascii succeeded ", *host, " [", host->size(),
428 " bytes]");
429
430 if (std::any_of(host->begin(), host->end(),
431 ada::unicode::is_forbidden_domain_code_point)) {
432 host = std::nullopt;
433 return is_valid = false;
434 }
435
436 // If asciiDomain ends in a number, then return the result of IPv4 parsing
437 // asciiDomain.
438 if (checkers::is_ipv4(*host)) {
439 ada_log("parse_host got ipv4 ", *host);
440 return parse_ipv4(*host);
441 }
442
443 return true;
444}
445
446ada_really_inline void url::parse_path(std::string_view input) {
447 ada_log("parse_path ", input);
448 std::string tmp_buffer;
449 std::string_view internal_input;
450 if (unicode::has_tabs_or_newline(input)) {
451 tmp_buffer = input;
452 // Optimization opportunity: Instead of copying and then pruning, we could
453 // just directly build the string from user_input.
454 helpers::remove_ascii_tab_or_newline(tmp_buffer);
455 internal_input = tmp_buffer;
456 } else {
457 internal_input = input;
458 }
459
460 // If url is special, then:
461 if (is_special()) {
462 if (internal_input.empty()) {
463 path = "/";
464 } else if ((internal_input[0] == '/') || (internal_input[0] == '\\')) {
465 helpers::parse_prepared_path(internal_input.substr(1), type, path);
466 } else {
467 helpers::parse_prepared_path(internal_input, type, path);
468 }
469 } else if (!internal_input.empty()) {
470 if (internal_input[0] == '/') {
471 helpers::parse_prepared_path(internal_input.substr(1), type, path);
472 } else {
473 helpers::parse_prepared_path(internal_input, type, path);
474 }
475 } else {
476 if (!host.has_value()) {
477 path = "/";
478 }
479 }
480}
481
482[[nodiscard]] std::string url::to_string() const {
483 if (!is_valid) {
484 return "null";
485 }
486 std::string answer;
487 auto back = std::back_insert_iterator(answer);
488 answer.append("{\n");
489 answer.append("\t\"protocol\":\"");
490 helpers::encode_json(get_protocol(), back);
491 answer.append("\",\n");
492 if (has_credentials()) {
493 answer.append("\t\"username\":\"");
494 helpers::encode_json(username, back);
495 answer.append("\",\n");
496 answer.append("\t\"password\":\"");
497 helpers::encode_json(password, back);
498 answer.append("\",\n");
499 }
500 if (host.has_value()) {
501 answer.append("\t\"host\":\"");
502 helpers::encode_json(host.value(), back);
503 answer.append("\",\n");
504 }
505 if (port.has_value()) {
506 answer.append("\t\"port\":\"");
507 answer.append(std::to_string(port.value()));
508 answer.append("\",\n");
509 }
510 answer.append("\t\"path\":\"");
511 helpers::encode_json(path, back);
512 answer.append("\",\n");
513 answer.append("\t\"opaque path\":");
514 answer.append((has_opaque_path ? "true" : "false"));
515 if (has_search()) {
516 answer.append(",\n");
517 answer.append("\t\"query\":\"");
518 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
519 helpers::encode_json(query.value(), back);
520 answer.append("\"");
521 }
522 if (hash.has_value()) {
523 answer.append(",\n");
524 answer.append("\t\"hash\":\"");
525 helpers::encode_json(hash.value(), back);
526 answer.append("\"");
527 }
528 answer.append("\n}");
529 return answer;
530}
531
532[[nodiscard]] bool url::has_valid_domain() const noexcept {
533 if (!host.has_value()) {
534 return false;
535 }
536 return checkers::verify_dns_length(*host);
537}
538
539[[nodiscard]] std::string url::get_origin() const {
540 if (is_special()) {
541 // Return a new opaque origin.
542 if (type == scheme::FILE) {
543 return "null";
544 }
545 return ada::helpers::concat(get_protocol(), "//", get_host());
546 }
547
548 if (non_special_scheme == "blob") {
549 if (!path.empty()) {
550 auto result = ada::parse<ada::url>(path);
551 if (result &&
552 (result->type == scheme::HTTP || result->type == scheme::HTTPS)) {
553 // If pathURL's scheme is not "http" and not "https", then return a
554 // new opaque origin.
555 return ada::helpers::concat(result->get_protocol(), "//",
556 result->get_host());
557 }
558 }
559 }
560
561 // Return a new opaque origin.
562 return "null";
563}
564
565[[nodiscard]] std::string url::get_protocol() const {
566 if (is_special()) {
567 return helpers::concat(ada::scheme::details::is_special_list[type], ":");
568 }
569 // We only move the 'scheme' if it is non-special.
570 return helpers::concat(non_special_scheme, ":");
571}
572
573[[nodiscard]] std::string url::get_host() const {
574 // If url's host is null, then return the empty string.
575 // If url's port is null, return url's host, serialized.
576 // Return url's host, serialized, followed by U+003A (:) and url's port,
577 // serialized.
578 if (!host.has_value()) {
579 return "";
580 }
581 if (port.has_value()) {
582 return host.value() + ":" + get_port();
583 }
584 return host.value();
585}
586
587[[nodiscard]] std::string url::get_hostname() const {
588 return host.value_or("");
589}
590
591[[nodiscard]] std::string url::get_search() const {
592 // If this's URL's query is either null or the empty string, then return the
593 // empty string. Return U+003F (?), followed by this's URL's query.
594 return (!query.has_value() || (query->empty())) ? "" : "?" + query.value();
595}
596
597[[nodiscard]] const std::string& url::get_username() const noexcept {
598 return username;
599}
600
601[[nodiscard]] const std::string& url::get_password() const noexcept {
602 return password;
603}
604
605[[nodiscard]] std::string url::get_port() const {
606 return port.has_value() ? std::to_string(port.value()) : "";
607}
608
609[[nodiscard]] std::string url::get_hash() const {
610 // If this's URL's fragment is either null or the empty string, then return
611 // the empty string. Return U+0023 (#), followed by this's URL's fragment.
612 return (!hash.has_value() || (hash->empty())) ? "" : "#" + hash.value();
613}
614
615bool url::needs_rollback_snapshot(size_t input_len) const noexcept {
616 return get_href_size() + input_len + 16 > ada::get_max_input_length();
617}
618
619template <bool override_hostname>
620bool url::set_host_or_hostname(const std::string_view input) {
621 if (has_opaque_path) {
622 return false;
623 }
624
625 url saved_url(*this);
626
627 size_t host_end_pos = input.find('#');
628 std::string _host(input.data(), host_end_pos != std::string_view::npos
629 ? host_end_pos
630 : input.size());
631 helpers::remove_ascii_tab_or_newline(_host);
632 std::string_view new_host(_host);
633
634 auto check_url_size = [&]() -> bool {
636 *this = std::move(saved_url);
637 return false;
638 }
639 return true;
640 };
641
642 // If url's scheme is "file", then set state to file host state, instead of
643 // host state.
644 if (type != ada::scheme::type::FILE) {
645 std::string_view host_view(_host.data(), _host.length());
646 auto [location, found_colon] =
647 helpers::get_host_delimiter_location(is_special(), host_view);
648
649 // Otherwise, if c is U+003A (:) and insideBrackets is false, then:
650 // Note: the 'found_colon' value is true if and only if a colon was
651 // encountered while not inside brackets.
652 if (found_colon) {
653 // If buffer is the empty string, host-missing validation error, return
654 // failure.
655 std::string_view buffer = host_view.substr(0, location);
656 if (buffer.empty()) {
657 return false;
658 }
659
660 // If state override is given and state override is hostname state, then
661 // return failure.
662 if constexpr (override_hostname) {
663 return false;
664 }
665
666 // Let host be the result of host parsing buffer with url is not special.
667 bool succeeded = parse_host(buffer);
668 if (!succeeded) {
669 *this = std::move(saved_url);
670 return false;
671 }
672
673 // Set url's host to host, buffer to the empty string, and state to port
674 // state.
675 // Not set_port(): its size check and rollback cover only the port.
676 std::string_view port_buffer = new_host.substr(location + 1);
677 std::string_view digits =
678 port_buffer.substr(0, port_buffer.find_first_not_of("0123456789"));
679 if (!digits.empty() && !cannot_have_credentials_or_port()) {
680 parse_port(digits);
681 is_valid = true; // an out-of-range port is ignored
682 }
683 return check_url_size();
684 }
685 // Otherwise, if one of the following is true:
686 // - c is the EOF code point, U+002F (/), U+003F (?), or U+0023 (#)
687 // - url is special and c is U+005C (\‍)
688 else {
689 // If url is special and host_view is the empty string, host-missing
690 // validation error, return failure.
691 if (host_view.empty() && is_special()) {
692 return false;
693 }
694
695 // Otherwise, if state override is given, host_view is the empty string,
696 // and either url includes credentials or url's port is non-null, then
697 // return failure.
698 if (host_view.empty() && (has_credentials() || port.has_value())) {
699 return false;
700 }
701
702 // Let host be the result of host parsing host_view with url is not
703 // special.
704 if (host_view.empty() && !is_special()) {
705 host = "";
706 return check_url_size();
707 }
708
709 bool succeeded = parse_host(host_view);
710 if (!succeeded) {
711 *this = std::move(saved_url);
712 return false;
713 }
714 return check_url_size();
715 }
716 }
717
718 size_t location = new_host.find_first_of("/\\?");
719 if (location != std::string_view::npos) {
720 new_host.remove_suffix(new_host.length() - location);
721 }
722
723 if (new_host.empty()) {
724 // Set url's host to the empty string.
725 host = "";
726 } else {
727 // Let host be the result of host parsing buffer with url is not special.
728 if (!parse_host(new_host)) {
729 *this = std::move(saved_url);
730 return false;
731 }
732
733 // If host is "localhost", then set host to the empty string.
734 if (host == "localhost") {
735 host = "";
736 }
737 }
738 return check_url_size();
739}
740
741bool url::set_host(const std::string_view input) {
742 return set_host_or_hostname<false>(input);
743}
744
745bool url::set_hostname(const std::string_view input) {
746 return set_host_or_hostname<true>(input);
747}
748
749bool url::set_username(const std::string_view input) {
750 if (helpers::overlaps(input, username)) {
751 return set_username(std::string(input));
752 }
753 if (cannot_have_credentials_or_port()) {
754 return false;
755 }
756 auto previous_username = std::move(username);
757 username = ada::unicode::percent_encode(
760 username = std::move(previous_username);
761 return false;
762 }
763 return true;
764}
765
766bool url::set_password(const std::string_view input) {
767 if (helpers::overlaps(input, password)) {
768 return set_password(std::string(input));
769 }
770 if (cannot_have_credentials_or_port()) {
771 return false;
772 }
773 auto previous_password = std::move(password);
774 password = ada::unicode::percent_encode(
777 password = std::move(previous_password);
778 return false;
779 }
780 return true;
781}
782
783bool url::set_port(const std::string_view input) {
784 if (cannot_have_credentials_or_port()) {
785 return false;
786 }
787
788 if (input.empty()) {
789 port = std::nullopt;
790 return true;
791 }
792
793 std::string trimmed(input);
794 helpers::remove_ascii_tab_or_newline(trimmed);
795
796 if (trimmed.empty()) {
797 return true;
798 }
799
800 // Input should not start with a non-digit character.
801 if (!ada::unicode::is_ascii_digit(trimmed.front())) {
802 return false;
803 }
804
805 // Find the first non-digit character to determine the length of digits
806 auto first_non_digit =
807 std::ranges::find_if_not(trimmed, ada::unicode::is_ascii_digit);
808 std::string_view digits_to_parse =
809 std::string_view(trimmed.data(), first_non_digit - trimmed.begin());
810
811 // Revert changes if parse_port fails.
812 std::optional<uint16_t> previous_port = port;
813 parse_port(digits_to_parse);
814 if (is_valid) {
816 port = std::move(previous_port);
817 return false;
818 }
819 return true;
820 }
821 port = std::move(previous_port);
822 is_valid = true;
823 return false;
824}
825
826void url::set_hash(const std::string_view input) {
827 if (input.empty()) {
828 hash = std::nullopt;
829 helpers::strip_trailing_spaces_from_opaque_path(*this);
830 return;
831 }
832
833 std::string new_value;
834 new_value = input[0] == '#' ? input.substr(1) : input;
835 helpers::remove_ascii_tab_or_newline(new_value);
836 auto previous_hash = std::move(hash);
837 hash = unicode::percent_encode(new_value,
840 hash = std::move(previous_hash);
841 }
842}
843
844void url::set_search(const std::string_view input) {
845 if (input.empty()) {
846 query = std::nullopt;
847 helpers::strip_trailing_spaces_from_opaque_path(*this);
848 return;
849 }
850
851 std::string new_value;
852 new_value = input[0] == '?' ? input.substr(1) : input;
853 helpers::remove_ascii_tab_or_newline(new_value);
854
855 auto query_percent_encode_set =
858
859 auto previous_query = std::move(query);
860 query = ada::unicode::percent_encode(new_value, query_percent_encode_set);
862 query = std::move(previous_query);
863 }
864}
865
866bool url::set_pathname(const std::string_view input) {
867 if (helpers::overlaps(input, path)) {
868 return set_pathname(std::string(input));
869 }
870 if (has_opaque_path) {
871 return false;
872 }
873 auto previous_path = std::move(path);
874 path.clear();
875 parse_path(input);
877 path = std::move(previous_path);
878 return false;
879 }
880 return true;
881}
882
883bool url::set_protocol(const std::string_view input) {
884 std::string view(input);
885 helpers::remove_ascii_tab_or_newline(view);
886 if (view.empty()) {
887 return true;
888 }
889
890 // Schemes should start with alpha values.
891 if (!checkers::is_alpha(view[0])) {
892 return false;
893 }
894
895 view.append(":");
896
897 std::string::iterator pointer =
898 std::ranges::find_if_not(view, unicode::is_alnum_plus);
899
900 if (pointer != view.end() && *pointer == ':') {
901 std::optional<url> saved_url;
902 if (needs_rollback_snapshot(view.size())) {
903 saved_url = *this;
904 }
905 bool result = parse_scheme<true>(
906 std::string_view(view.data(), pointer - view.begin()));
907 if (result && saved_url && get_href_size() > ada::get_max_input_length()) {
908 *this = std::move(*saved_url);
909 return false;
910 }
911 return result;
912 }
913 return false;
914}
915
916bool url::set_href(const std::string_view input) {
918
919 if (out) {
920 // The parser enforces get_max_input_length() on both the input and the
921 // normalized result. This is a defense-in-depth check.
922 if (out->get_href_size() > ada::get_max_input_length()) {
923 return false;
924 }
925 *this = std::move(*out);
926 }
927
928 return out.has_value();
929}
930
931} // namespace ada
Definitions for URL specific checkers used within Ada.
#define ada_really_inline
Definition common_defs.h:85
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 C0_CONTROL_PERCENT_ENCODE[32]
constexpr uint8_t USERINFO_PERCENT_ENCODE[32]
constexpr uint8_t FRAGMENT_PERCENT_ENCODE[32]
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 std::string_view is_special_list[]
Definition scheme-inl.h:23
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)
@ IPV6
Definition url_base.h:34
@ IPV4
Definition url_base.h:32
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)
Definitions for the URL scheme.
IP address serialization utilities.
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
void set_hash(std::string_view input)
Definition url.cpp:826
bool set_hostname(std::string_view input)
Definition url.cpp:745
bool set_host(std::string_view input)
Definition url.cpp:741
ada_really_inline bool has_credentials() const noexcept
Definition url-inl.h:25
bool set_password(std::string_view input)
Definition url.cpp:766
void set_search(std::string_view input)
Definition url.cpp:844
std::string get_search() const
Definition url.cpp:591
bool set_href(std::string_view input)
Definition url.cpp:916
std::string get_port() const
Definition url.cpp:605
std::string get_protocol() const
Definition url.cpp:565
std::string get_host() const
Definition url.cpp:573
bool set_username(std::string_view input)
Definition url.cpp:749
size_t get_href_size() const noexcept
Definition url-inl.h:283
url()=default
bool set_pathname(std::string_view input)
Definition url.cpp:866
std::string get_hostname() const
Definition url.cpp:587
std::string get_origin() const override
Definition url.cpp:539
const std::string & get_password() const noexcept
Definition url.cpp:601
bool set_protocol(std::string_view input)
Definition url.cpp:883
const std::string & get_username() const noexcept
Definition url.cpp:597
bool set_port(std::string_view input)
Definition url.cpp:783
constexpr bool has_search() const noexcept override
Definition url-inl.h:172
std::string to_string() const override
Definition url.cpp:482
std::string get_hash() const
Definition url.cpp:609
bool has_valid_domain() const noexcept override
Definition url.cpp:532
Definitions for unicode operations.
Shared IPv4/IPv6 parsing helpers used by url and url_aggregator.