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