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)) {
31 host = ada::unicode::percent_encode(
36bool url::parse_ipv4(std::string_view input) {
37 ada_log(
"parse_ipv4 ", input,
" [", input.size(),
" bytes]");
41 std::string_view original_input = input;
42 if (original_input.back() ==
'.') {
43 original_input.remove_suffix(1);
45 if (input.back() ==
'.') {
46 input.remove_suffix(1);
54 host = original_input;
59 const char* p = input.data();
60 const char* end = p + input.size();
63 int pure_decimal_count = 0;
65 for (; digit_count < 4 && p < end; ++digit_count) {
75 const unsigned shift =
static_cast<unsigned>(32 - digit_count * 8);
76 if (segment >= (uint64_t{1} << shift)) {
80 host = (pure_decimal_count == 4) ? std::string(original_input)
85 if (segment > 255 || *p !=
'.') {
91 if (digit_count != 4 || p != end) {
94 host = (pure_decimal_count == 4) ? std::string(original_input)
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]] {
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]] {
118 const char* pointer = input.data();
119 const char*
const end = pointer + input.size();
123 if (*pointer ==
':') {
124 if (input.size() == 1 || pointer[1] !=
':') [[unlikely]] {
128 compress = ++piece_index;
131 while (pointer != end) {
132 if (piece_index == 8) [[unlikely]] {
135 if (*pointer ==
':') {
136 if (compress != -1) [[unlikely]] {
140 compress = ++piece_index;
147 if (pointer != end && *pointer ==
'.') {
148 if (length == 0) [[unlikely]] {
152 if (piece_index > 6) [[unlikely]] {
156 int numbers_seen = 0;
157 while (pointer != end) {
159 if (numbers_seen > 0) {
160 if (*pointer ==
'.' && numbers_seen < 4) {
166 if (pointer == end || *pointer <
'0' || *pointer >
'9') [[unlikely]] {
169 ipv4_piece = *pointer -
'0';
171 if (pointer != end && *pointer >=
'0' && *pointer <=
'9') {
172 if (ipv4_piece == 0) [[unlikely]] {
175 ipv4_piece = ipv4_piece * 10 + (*pointer -
'0');
177 if (pointer != end && *pointer >=
'0' && *pointer <=
'9') {
178 ipv4_piece = ipv4_piece * 10 + (*pointer -
'0');
180 if (ipv4_piece > 255) [[unlikely]] {
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));
189 if (numbers_seen == 2 || numbers_seen == 4) {
193 if (numbers_seen != 4) [[unlikely]] {
199 if (length == 0) [[unlikely]] {
203 if (pointer != end && *pointer ==
':') {
205 if (pointer == end) [[unlikely]] {
208 }
else if (pointer != end) [[unlikely]] {
212 address[
static_cast<size_t>(piece_index)] = value;
216 if (compress != -1) {
217 const int right = piece_index - compress;
219 const size_t dest =
static_cast<size_t>(8 - right);
220 const size_t src =
static_cast<size_t>(compress);
222 for (
size_t i =
static_cast<size_t>(right); i-- > 0;) {
223 address[dest + i] = address[src + i];
224 address[src + i] = 0;
228 }
else if (piece_index != 8) [[unlikely]] {
233 ada_log(
"parse_ipv6 ", *host);
238template <
bool has_state_overr
ide>
246 if (is_input_special) {
247 if constexpr (has_state_override) {
271 if constexpr (has_state_override) {
273 uint16_t urls_scheme_port = get_special_port();
275 if (urls_scheme_port) {
278 if (port.has_value() && *port == urls_scheme_port) {
284 std::string _buffer(input);
289 unicode::to_lower_ascii(_buffer.data(), _buffer.size());
291 if constexpr (has_state_override) {
300 if (
is_special() != ada::scheme::is_special(_buffer)) {
318 set_scheme(std::move(_buffer));
320 if constexpr (has_state_override) {
322 uint16_t urls_scheme_port = get_special_port();
324 if (urls_scheme_port) {
327 if (port.has_value() && *port == urls_scheme_port) {
338 ada_log(
"parse_host ", input,
" [", input.size(),
" bytes]");
343 if (input[0] ==
'[') {
345 if (input.back() !=
']') {
348 ada_log(
"parse_host ipv6");
352 input.remove_prefix(1);
353 input.remove_suffix(1);
354 return parse_ipv6(input);
360 return parse_opaque_host(input);
366 if (!input.empty() && input.back() ==
'.') {
367 host = input.substr(0, input.size() - 1);
373 ada_log(
"parse_host fast path decimal ipv4");
381 const uint8_t forbidden_or_upper =
382 unicode::contains_forbidden_domain_code_point_or_upper(input.data(),
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());
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);
396 ada_log(
"parse_host fast path ", *host);
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(),
406 if ((decoded_flags & 1) == 0) {
407 if ((decoded_flags & 2) != 0) {
408 unicode::to_lower_ascii(decoded.data(), decoded.size());
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);
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");
427 ada_log(
"parse_host to_ascii succeeded ", *host,
" [", host->size(),
430 if (std::any_of(host->begin(), host->end(),
431 ada::unicode::is_forbidden_domain_code_point)) {
438 if (checkers::is_ipv4(*host)) {
439 ada_log(
"parse_host got ipv4 ", *host);
440 return parse_ipv4(*host);
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)) {
454 helpers::remove_ascii_tab_or_newline(tmp_buffer);
455 internal_input = tmp_buffer;
457 internal_input = input;
462 if (internal_input.empty()) {
464 }
else if ((internal_input[0] ==
'/') || (internal_input[0] ==
'\\')) {
465 helpers::parse_prepared_path(internal_input.substr(1), type, path);
467 helpers::parse_prepared_path(internal_input, type, path);
469 }
else if (!internal_input.empty()) {
470 if (internal_input[0] ==
'/') {
471 helpers::parse_prepared_path(internal_input.substr(1), type, path);
473 helpers::parse_prepared_path(internal_input, type, path);
476 if (!host.has_value()) {
487 auto back = std::back_insert_iterator(answer);
488 answer.append(
"{\n");
489 answer.append(
"\t\"protocol\":\"");
491 answer.append(
"\",\n");
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");
500 if (host.has_value()) {
501 answer.append(
"\t\"host\":\"");
502 helpers::encode_json(host.value(), back);
503 answer.append(
"\",\n");
505 if (port.has_value()) {
506 answer.append(
"\t\"port\":\"");
507 answer.append(std::to_string(port.value()));
508 answer.append(
"\",\n");
510 answer.append(
"\t\"path\":\"");
511 helpers::encode_json(path, back);
512 answer.append(
"\",\n");
513 answer.append(
"\t\"opaque path\":");
516 answer.append(
",\n");
517 answer.append(
"\t\"query\":\"");
519 helpers::encode_json(query.value(), back);
522 if (hash.has_value()) {
523 answer.append(
",\n");
524 answer.append(
"\t\"hash\":\"");
525 helpers::encode_json(hash.value(), back);
528 answer.append(
"\n}");
533 if (!host.has_value()) {
536 return checkers::verify_dns_length(*host);
548 if (non_special_scheme ==
"blob") {
555 return ada::helpers::concat(
result->get_protocol(),
"//",
570 return helpers::concat(non_special_scheme,
":");
578 if (!host.has_value()) {
581 if (port.has_value()) {
582 return host.value() +
":" +
get_port();
588 return host.value_or(
"");
594 return (!query.has_value() || (query->empty())) ?
"" :
"?" + query.value();
606 return port.has_value() ? std::to_string(port.value()) :
"";
612 return (!hash.has_value() || (hash->empty())) ?
"" :
"#" + hash.value();
615bool url::needs_rollback_snapshot(
size_t input_len)
const noexcept {
619template <
bool overr
ide_hostname>
620bool url::set_host_or_hostname(
const std::string_view input) {
625 url saved_url(*
this);
627 size_t host_end_pos = input.find(
'#');
628 std::string _host(input.data(), host_end_pos != std::string_view::npos
631 helpers::remove_ascii_tab_or_newline(_host);
632 std::string_view new_host(_host);
634 auto check_url_size = [&]() ->
bool {
636 *
this = std::move(saved_url);
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);
655 std::string_view buffer = host_view.substr(0, location);
656 if (buffer.empty()) {
662 if constexpr (override_hostname) {
667 bool succeeded = parse_host(buffer);
669 *
this = std::move(saved_url);
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()) {
683 return check_url_size();
706 return check_url_size();
709 bool succeeded = parse_host(host_view);
711 *
this = std::move(saved_url);
714 return check_url_size();
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);
723 if (new_host.empty()) {
728 if (!parse_host(new_host)) {
729 *
this = std::move(saved_url);
734 if (host ==
"localhost") {
738 return check_url_size();
742 return set_host_or_hostname<false>(input);
746 return set_host_or_hostname<true>(input);
750 if (helpers::overlaps(input, username)) {
753 if (cannot_have_credentials_or_port()) {
756 auto previous_username = std::move(username);
757 username = ada::unicode::percent_encode(
760 username = std::move(previous_username);
767 if (helpers::overlaps(input, password)) {
770 if (cannot_have_credentials_or_port()) {
773 auto previous_password = std::move(password);
774 password = ada::unicode::percent_encode(
777 password = std::move(previous_password);
784 if (cannot_have_credentials_or_port()) {
793 std::string trimmed(input);
794 helpers::remove_ascii_tab_or_newline(trimmed);
796 if (trimmed.empty()) {
801 if (!ada::unicode::is_ascii_digit(trimmed.front())) {
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());
812 std::optional<uint16_t> previous_port = port;
813 parse_port(digits_to_parse);
816 port = std::move(previous_port);
821 port = std::move(previous_port);
829 helpers::strip_trailing_spaces_from_opaque_path(*
this);
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);
846 query = std::nullopt;
847 helpers::strip_trailing_spaces_from_opaque_path(*
this);
851 std::string new_value;
852 new_value = input[0] ==
'?' ? input.substr(1) : input;
853 helpers::remove_ascii_tab_or_newline(new_value);
855 auto query_percent_encode_set =
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);
867 if (helpers::overlaps(input, path)) {
873 auto previous_path = std::move(path);
877 path = std::move(previous_path);
884 std::string view(input);
885 helpers::remove_ascii_tab_or_newline(view);
897 std::string::iterator pointer =
898 std::ranges::find_if_not(view, unicode::is_alnum_plus);
900 if (pointer != view.end() && *pointer ==
':') {
901 std::optional<url> saved_url;
902 if (needs_rollback_snapshot(view.size())) {
905 bool result = parse_scheme<true>(
906 std::string_view(view.data(), pointer - view.begin()));
908 *
this = std::move(*saved_url);
925 *
this = std::move(*out);
928 return out.has_value();
Definitions for URL specific checkers used within Ada.
#define ada_really_inline
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
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
int parse_hex_piece(const char *&pointer, const char *end, uint16_t &value) noexcept
constexpr std::string_view is_special_list[]
constexpr ada::scheme::type get_scheme_type(std::string_view scheme) noexcept
std::string ipv6(const std::array< uint16_t, 8 > &address)
std::string ipv4(uint64_t address)
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
void set_hash(std::string_view input)
bool set_hostname(std::string_view input)
bool set_host(std::string_view input)
ada_really_inline bool has_credentials() const noexcept
bool set_password(std::string_view input)
void set_search(std::string_view input)
std::string get_search() const
bool set_href(std::string_view input)
std::string get_port() const
std::string get_protocol() const
std::string get_host() const
bool set_username(std::string_view input)
size_t get_href_size() const noexcept
bool set_pathname(std::string_view input)
std::string get_hostname() const
std::string get_origin() const override
const std::string & get_password() const noexcept
bool set_protocol(std::string_view input)
const std::string & get_username() const noexcept
bool set_port(std::string_view input)
constexpr bool has_search() const noexcept override
std::string to_string() const override
std::string get_hash() const
bool has_valid_domain() const noexcept override
Definitions for unicode operations.
Shared IPv4/IPv6 parsing helpers used by url and url_aggregator.