30 components.
host_end += new_difference;
43template <
bool has_state_overr
ide>
45 const std::string_view input_with_colon) {
46 ada_log(
"url_aggregator::parse_scheme_with_colon ", input_with_colon);
49 std::string_view input{input_with_colon};
50 input.remove_suffix(1);
57 if (is_input_special) {
58 if constexpr (has_state_override) {
75 components.host_start == components.host_end) {
81 set_scheme_from_view_with_colon(input_with_colon);
83 if constexpr (has_state_override) {
85 uint16_t urls_scheme_port = get_special_port();
87 if (urls_scheme_port) {
90 if (components.port == urls_scheme_port) {
96 std::string _buffer(input);
100 unicode::to_lower_ascii(_buffer.data(), _buffer.size());
102 if constexpr (has_state_override) {
111 if (
is_special() != ada::scheme::is_special(_buffer)) {
125 components.host_start == components.host_end) {
132 if constexpr (has_state_override) {
134 uint16_t urls_scheme_port = get_special_port();
136 if (urls_scheme_port) {
139 if (components.port == urls_scheme_port) {
150 ada_log(
"url_aggregator::copy_scheme ", u.buffer);
154 uint32_t new_difference = u.components.protocol_end - components.protocol_end;
157 buffer.erase(0, components.protocol_end);
158 buffer.insert(0, u.get_protocol());
159 components.protocol_end = u.components.protocol_end;
162 if (new_difference == 0) {
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);
176 new_scheme_with_colon.back() ==
':');
179 uint32_t new_difference =
180 uint32_t(new_scheme_with_colon.size()) - components.protocol_end;
182 if (buffer.empty()) {
183 buffer.append(new_scheme_with_colon);
185 buffer.erase(0, components.protocol_end);
186 buffer.insert(0, new_scheme_with_colon);
188 components.protocol_end += new_difference;
194inline void url_aggregator::set_scheme(std::string_view new_scheme) {
195 ada_log(
"url_aggregator::set_scheme ", new_scheme);
200 uint32_t new_difference =
201 uint32_t(new_scheme.size()) - components.protocol_end + 1;
204 if (buffer.empty()) {
205 buffer.append(helpers::concat(new_scheme,
":"));
207 buffer.erase(0, components.protocol_end);
208 buffer.insert(0, helpers::concat(new_scheme,
":"));
210 components.protocol_end = uint32_t(new_scheme.size() + 1);
216bool url_aggregator::needs_rollback_snapshot(
size_t input_len)
const noexcept {
224 ada_log(
"url_aggregator::set_protocol ", input);
227 std::string view(input);
228 helpers::remove_ascii_tab_or_newline(view);
240 std::string::iterator pointer =
241 std::ranges::find_if_not(view, unicode::is_alnum_plus);
243 if (pointer != view.end() && *pointer ==
':') {
244 std::optional<url_aggregator> saved_url;
245 if (needs_rollback_snapshot(view.size())) {
248 bool result = parse_scheme_with_colon<true>(
249 view.substr(0, pointer - view.begin() + 1));
251 *
this = std::move(*saved_url);
260 if (helpers::overlaps(input, buffer)) {
263 ada_log(
"url_aggregator::set_username '", input,
"' ");
266 if (cannot_have_credentials_or_port()) {
269 std::optional<url_aggregator> saved_url;
270 if (needs_rollback_snapshot(input.size())) {
275 if (idx == input.size()) {
276 update_base_username(input);
279 update_base_username(ada::unicode::percent_encode(
283 *
this = std::move(*saved_url);
291 if (helpers::overlaps(input, buffer)) {
294 ada_log(
"url_aggregator::set_password '", input,
"'");
297 if (cannot_have_credentials_or_port()) {
300 std::optional<url_aggregator> saved_url;
301 if (needs_rollback_snapshot(input.size())) {
306 if (idx == input.size()) {
307 update_base_password(input);
310 update_base_password(ada::unicode::percent_encode(
314 *
this = std::move(*saved_url);
322 ada_log(
"url_aggregator::set_port ", input);
325 if (cannot_have_credentials_or_port()) {
334 std::string trimmed(input);
335 helpers::remove_ascii_tab_or_newline(trimmed);
337 if (trimmed.empty()) {
342 if (!ada::unicode::is_ascii_digit(trimmed.front())) {
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());
355 std::optional<url_aggregator> saved_url;
356 if (needs_rollback_snapshot(digits_to_parse.size())) {
359 parse_port(digits_to_parse);
366 *
this = std::move(*saved_url);
373 if (helpers::overlaps(input, buffer)) {
376 ada_log(
"url_aggregator::set_pathname ", input);
382 std::optional<url_aggregator> saved_url;
383 if (needs_rollback_snapshot(input.size())) {
388 if (
get_pathname().starts_with(
"//") && !has_authority() && !has_dash_dot()) {
389 buffer.insert(components.pathname_start,
"/.");
390 components.pathname_start += 2;
392 components.search_start += 2;
395 components.hash_start += 2;
399 *
this = std::move(*saved_url);
407 ada_log(
"url_aggregator::parse_path ", input);
410 std::string tmp_buffer;
411 std::string_view internal_input;
412 if (unicode::has_tabs_or_newline(input)) {
416 helpers::remove_ascii_tab_or_newline(tmp_buffer);
417 internal_input = tmp_buffer;
419 internal_input = input;
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));
429 consume_prepared_path(internal_input);
431 }
else if (!internal_input.empty()) {
432 if (internal_input[0] ==
'/') {
433 consume_prepared_path(internal_input.substr(1));
435 consume_prepared_path(internal_input);
440 if (components.host_start == components.host_end && !has_authority()) {
441 update_base_pathname(
"/");
448 if (helpers::overlaps(input, buffer)) {
452 ada_log(
"url_aggregator::set_search ", input);
457 helpers::strip_trailing_spaces_from_opaque_path(*
this);
461 std::string_view stripped = input[0] ==
'?' ? input.substr(1) : input;
465 std::string_view effective = stripped;
466 if (unicode::has_tabs_or_newline(stripped)) {
468 helpers::remove_ascii_tab_or_newline(cleaned);
472 auto query_percent_encode_set =
476 std::optional<url_aggregator> saved_url;
477 if (needs_rollback_snapshot(effective.size())) {
480 update_base_search(effective, query_percent_encode_set);
482 *
this = std::move(*saved_url);
489 if (helpers::overlaps(input, buffer)) {
493 ada_log(
"url_aggregator::set_hash ", input);
498 buffer.resize(components.hash_start);
501 helpers::strip_trailing_spaces_from_opaque_path(*
this);
505 std::string_view stripped = input[0] ==
'#' ? input.substr(1) : input;
509 std::string_view effective = stripped;
510 if (unicode::has_tabs_or_newline(stripped)) {
512 helpers::remove_ascii_tab_or_newline(cleaned);
515 std::optional<url_aggregator> saved_url;
516 if (needs_rollback_snapshot(effective.size())) {
519 update_unencoded_base_hash(effective);
521 *
this = std::move(*saved_url);
529 ada_log(
"url_aggregator::set_href ", input,
" [", input.size(),
" bytes]");
531 ada_log(
"url_aggregator::set_href, success :", out.has_value());
539 ada_log(
"url_aggregator::set_href, parsed ", out->to_string());
541 *
this = std::move(*out);
544 return out.has_value();
548 ada_log(
"url_aggregator:parse_host \"", input,
"\" [", input.size(),
556 if (input[0] ==
'[') {
558 if (input.back() !=
']') {
561 ada_log(
"parse_host ipv6");
565 input.remove_prefix(1);
566 input.remove_suffix(1);
567 return parse_ipv6(input);
573 return parse_opaque_host(input);
588 if (!input.empty() && input.back() ==
'.') {
589 update_base_hostname(input.substr(0, input.size() - 1));
591 update_base_hostname(input);
595 ada_log(
"parse_host fast path decimal ipv4");
599 uint8_t is_forbidden_or_upper =
600 unicode::contains_forbidden_domain_code_point_or_upper(input.data(),
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) {
611 if (checkers::is_ipv4(lowered)) {
612 ada_log(
"parse_host fast path ipv4");
613 return parse_ipv4(lowered,
false);
615 update_base_hostname(lowered);
620 }
else if (input.find(
'-') == std::string_view::npos ||
621 input.find(xn_dash) == std::string_view::npos) {
623 if (checkers::is_ipv4(input)) {
624 ada_log(
"parse_host fast path ipv4");
625 return parse_ipv4(input,
false);
627 update_base_hostname(input);
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(),
638 if ((decoded_flags & 1) == 0) {
639 if ((decoded_flags & 2) != 0) {
640 unicode::to_lower_ascii(decoded.data(), decoded.size());
642 if (decoded.find(
'-') == std::string_view::npos ||
643 decoded.find(xn_dash) == std::string_view::npos) {
645 if (checkers::is_ipv4(decoded)) {
646 return parse_ipv4(decoded,
false);
648 update_base_hostname(decoded);
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(
'%'));
662 ada_log(
"parse_host to_ascii returns false");
665 ada_log(
"parse_host to_ascii succeeded ", *host,
" [", host->size(),
668 if (std::ranges::any_of(host.value(),
669 ada::unicode::is_forbidden_domain_code_point)) {
675 if (checkers::is_ipv4(host.value())) {
676 ada_log(
"parse_host got ipv4 ", *host);
677 return parse_ipv4(host.value(),
false);
680 update_base_hostname(host.value());
685template <
bool overr
ide_hostname>
686bool url_aggregator::set_host_or_hostname(
const std::string_view input) {
687 ada_log(
"url_aggregator::set_host_or_hostname ", input);
702 std::optional<url_aggregator> saved_url;
707 bool take_snapshot = needs_rollback_snapshot(input.size());
708 if (!take_snapshot) {
709 const size_t host_end = input.find(
'#');
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;
723 auto restore_on_failure = [&]() {
725 *
this = std::move(*saved_url);
733 size_t host_end_pos = input.find(
'#');
734 std::string_view host_input(
736 host_end_pos != std::string_view::npos ? host_end_pos : input.size());
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;
746 new_host = host_input;
749 auto check_url_size = [&]() ->
bool {
751 *
this = std::move(*saved_url);
760 std::string_view host_view = new_host;
761 auto [location, found_colon] =
762 helpers::get_host_delimiter_location(
is_special(), host_view);
770 std::string_view host_buffer = host_view.substr(0, location);
771 if (host_buffer.empty()) {
777 if constexpr (override_hostname) {
782 bool succeeded = parse_host(host_buffer);
784 restore_on_failure();
786 }
else if (has_dash_dot()) {
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()) {
803 return check_url_size();
827 }
else if (has_dash_dot()) {
828 add_authority_slashes_if_needed();
833 add_authority_slashes_if_needed();
835 return check_url_size();
838 bool succeeded = parse_host(host_view);
840 restore_on_failure();
842 }
else if (has_dash_dot()) {
846 return check_url_size();
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);
855 if (new_host.empty()) {
860 if (!parse_host(new_host)) {
861 restore_on_failure();
866 if (helpers::substring(buffer, components.host_start,
867 components.host_end) ==
"localhost") {
872 return check_url_size();
876 if (helpers::overlaps(input, buffer)) {
877 return set_host(std::string(input));
879 ada_log(
"url_aggregator::set_host '", input,
"'");
882 return set_host_or_hostname<false>(input);
886 if (helpers::overlaps(input, buffer)) {
889 ada_log(
"url_aggregator::set_hostname '", input,
"'");
892 return set_host_or_hostname<true>(input);
896 ada_log(
"url_aggregator::get_origin");
913 return helpers::concat(out->get_protocol(),
"//", out->get_host());
924 ada_log(
"url_aggregator::get_username");
926 return helpers::substring(buffer, components.protocol_end + 2,
927 components.username_end);
934 ada_log(
"url_aggregator::get_password");
936 return helpers::substring(buffer, components.username_end + 1,
937 components.host_start);
944 ada_log(
"url_aggregator::get_port");
948 return helpers::substring(buffer, components.host_end + 1,
949 components.pathname_start);
954 ada_log(
"url_aggregator::get_hash");
960 if (buffer.size() - components.hash_start <= 1) {
963 return helpers::substring(buffer, components.hash_start);
968 ada_log(
"url_aggregator::get_host");
972 size_t start = components.host_start;
973 if (components.host_end > components.host_start &&
974 buffer[components.host_start] ==
'@') {
979 if (start == components.host_end) {
982 return helpers::substring(buffer, start, components.pathname_start);
987 ada_log(
"url_aggregator::get_hostname");
991 size_t start = components.host_start;
993 if (components.host_end > components.host_start &&
994 buffer[components.host_start] ==
'@') {
997 return helpers::substring(buffer, start, components.host_end);
1002 ada_log(
"url_aggregator::get_search");
1008 auto ending_index = uint32_t(buffer.size());
1010 ending_index = components.hash_start;
1012 if (ending_index - components.search_start <= 1) {
1015 return helpers::substring(buffer, components.search_start, ending_index);
1020 ada_log(
"url_aggregator::get_protocol");
1021 return helpers::substring(buffer, 0, components.protocol_end);
1025 ada_log(
"url_aggregator::to_string buffer:", buffer,
" [", buffer.size(),
1032 auto back = std::back_insert_iterator(answer);
1033 answer.append(
"{\n");
1035 answer.append(
"\t\"buffer\":\"");
1036 helpers::encode_json(buffer, back);
1037 answer.append(
"\",\n");
1039 answer.append(
"\t\"protocol\":\"");
1041 answer.append(
"\",\n");
1044 answer.append(
"\t\"username\":\"");
1046 answer.append(
"\",\n");
1047 answer.append(
"\t\"password\":\"");
1049 answer.append(
"\",\n");
1052 answer.append(
"\t\"host\":\"");
1053 helpers::encode_json(
get_host(), back);
1054 answer.append(
"\",\n");
1056 answer.append(
"\t\"path\":\"");
1058 answer.append(
"\",\n");
1059 answer.append(
"\t\"opaque path\":");
1061 answer.append(
",\n");
1064 answer.append(
"\t\"query\":\"");
1066 answer.append(
"\",\n");
1069 answer.append(
"\t\"fragment\":\"");
1070 helpers::encode_json(
get_hash(), back);
1071 answer.append(
"\",\n");
1074 auto convert_offset_to_string = [](uint32_t offset) -> std::string {
1078 return std::to_string(offset);
1082 answer.append(
"\t\"protocol_end\":");
1083 answer.append(convert_offset_to_string(components.protocol_end));
1084 answer.append(
",\n");
1086 answer.append(
"\t\"username_end\":");
1087 answer.append(convert_offset_to_string(components.username_end));
1088 answer.append(
",\n");
1090 answer.append(
"\t\"host_start\":");
1091 answer.append(convert_offset_to_string(components.host_start));
1092 answer.append(
",\n");
1094 answer.append(
"\t\"host_end\":");
1095 answer.append(convert_offset_to_string(components.host_end));
1096 answer.append(
",\n");
1098 answer.append(
"\t\"port\":");
1099 answer.append(convert_offset_to_string(components.port));
1100 answer.append(
",\n");
1102 answer.append(
"\t\"pathname_start\":");
1103 answer.append(convert_offset_to_string(components.pathname_start));
1104 answer.append(
",\n");
1106 answer.append(
"\t\"search_start\":");
1107 answer.append(convert_offset_to_string(components.search_start));
1108 answer.append(
",\n");
1110 answer.append(
"\t\"hash_start\":");
1111 answer.append(convert_offset_to_string(components.hash_start));
1112 answer.append(
"\n}");
1118 if (components.host_start == components.host_end) {
1122 size_t start = components.host_start;
1123 if (components.host_end > components.host_start &&
1124 buffer[components.host_start] ==
'@') {
1127 return checkers::verify_dns_length(
1128 std::string_view(buffer.data() + start, components.host_end - start));
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()) {
1139 const bool trailing_dot = (input.back() ==
'.');
1141 input.remove_suffix(1);
1142 if (input.empty()) {
1151 if (!(in_place && !trailing_dot)) {
1152 if (helpers::overlaps(input, buffer)) {
1153 update_base_hostname(std::string(input));
1155 update_base_hostname(input);
1163 const char* p = input.data();
1164 const char* end = p + input.size();
1166 int digit_count = 0;
1167 int pure_decimal_count = 0;
1169 for (; digit_count < 4 && p < end; ++digit_count) {
1170 uint64_t segment = 0;
1176 ++pure_decimal_count;
1179 const unsigned shift =
static_cast<unsigned>(32 - digit_count * 8);
1180 if (segment >= (uint64_t{1} << shift)) {
1186 if (segment > 255 || *p !=
'.') {
1192 if (digit_count != 4 || p != end) {
1196 ada_log(
"url_aggregator::parse_ipv4 completed ",
get_href(),
1198 if (in_place && pure_decimal_count == 4 && !trailing_dot) {
1200 "url_aggregator::parse_ipv4 completed and was already correct in the "
1210bool url_aggregator::parse_ipv6(std::string_view input) {
1211 ada_log(
"parse_ipv6 ", input,
" [", input.size(),
" bytes]");
1214 if (input.empty() || input.size() > 45) [[unlikely]] {
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]] {
1226 update_base_hostname(serialized);
1233 const char* pointer = input.data();
1234 const char*
const end = pointer + input.size();
1235 int piece_index = 0;
1238 if (*pointer ==
':') {
1239 if (input.size() == 1 || pointer[1] !=
':') [[unlikely]] {
1243 compress = ++piece_index;
1246 while (pointer != end) {
1247 if (piece_index == 8) [[unlikely]] {
1250 if (*pointer ==
':') {
1251 if (compress != -1) [[unlikely]] {
1255 compress = ++piece_index;
1262 if (pointer != end && *pointer ==
'.') {
1263 if (length == 0) [[unlikely]] {
1267 if (piece_index > 6) [[unlikely]] {
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) {
1281 if (pointer == end || *pointer <
'0' || *pointer >
'9') [[unlikely]] {
1284 ipv4_piece = *pointer -
'0';
1286 if (pointer != end && *pointer >=
'0' && *pointer <=
'9') {
1287 if (ipv4_piece == 0) [[unlikely]] {
1290 ipv4_piece = ipv4_piece * 10 + (*pointer -
'0');
1292 if (pointer != end && *pointer >=
'0' && *pointer <=
'9') {
1293 ipv4_piece = ipv4_piece * 10 + (*pointer -
'0');
1295 if (ipv4_piece > 255) [[unlikely]] {
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));
1304 if (numbers_seen == 2 || numbers_seen == 4) {
1308 if (numbers_seen != 4) [[unlikely]] {
1314 if (length == 0) [[unlikely]] {
1318 if (pointer != end && *pointer ==
':') {
1320 if (pointer == end) [[unlikely]] {
1323 }
else if (pointer != end) [[unlikely]] {
1327 address[
static_cast<size_t>(piece_index)] = value;
1331 if (compress != -1) {
1332 const int right = piece_index - compress;
1334 const size_t dest =
static_cast<size_t>(8 - right);
1335 const size_t src =
static_cast<size_t>(compress);
1337 for (
size_t i =
static_cast<size_t>(right); i-- > 0;) {
1338 address[dest + i] = address[src + i];
1339 address[src + i] = 0;
1343 }
else if (piece_index != 8) [[unlikely]] {
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");
1354 update_base_hostname(serialized);
1362bool url_aggregator::parse_opaque_host(std::string_view input) {
1363 ada_log(
"parse_opaque_host ", input,
" [", input.size(),
" bytes]");
1366 if (std::ranges::any_of(input, ada::unicode::is_forbidden_host_code_point)) {
1374 if (idx == input.size()) {
1375 update_base_hostname(input);
1378 update_base_hostname(ada::unicode::percent_encode(
1390 answer.append(buffer);
1391 answer.append(
" [");
1392 answer.append(std::to_string(buffer.size()));
1393 answer.append(
" bytes]");
1394 answer.append(
"\n");
1397 line1.resize(buffer.size(),
' ');
1399 line1[components.hash_start] =
'|';
1402 line1[components.search_start] =
'|';
1404 if (components.pathname_start != buffer.size()) {
1405 line1[components.pathname_start] =
'|';
1407 if (components.host_end != buffer.size()) {
1408 line1[components.host_end] =
'|';
1410 if (components.host_start != buffer.size()) {
1411 line1[components.host_start] =
'|';
1413 if (components.username_end != buffer.size()) {
1414 line1[components.username_end] =
'|';
1416 if (components.protocol_end != buffer.size()) {
1417 line1[components.protocol_end] =
'|';
1419 answer.append(line1);
1420 answer.append(
"\n");
1422 std::string line2 = line1;
1424 line2[components.hash_start] =
'`';
1425 line1[components.hash_start] =
' ';
1427 for (
size_t i = components.hash_start + 1; i < line2.size(); i++) {
1430 line2.append(
" hash_start");
1431 answer.append(line2);
1432 answer.append(
"\n");
1435 std::string line3 = line1;
1437 line3[components.search_start] =
'`';
1438 line1[components.search_start] =
' ';
1440 for (
size_t i = components.search_start + 1; i < line3.size(); i++) {
1443 line3.append(
" search_start ");
1444 line3.append(std::to_string(components.search_start));
1445 answer.append(line3);
1446 answer.append(
"\n");
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++) {
1456 line4.append(
" pathname_start ");
1457 line4.append(std::to_string(components.pathname_start));
1458 answer.append(line4);
1459 answer.append(
"\n");
1462 std::string line5 = line1;
1463 if (components.host_end != buffer.size()) {
1464 line5[components.host_end] =
'`';
1465 line1[components.host_end] =
' ';
1467 for (
size_t i = components.host_end + 1; i < line5.size(); i++) {
1470 line5.append(
" host_end ");
1471 line5.append(std::to_string(components.host_end));
1472 answer.append(line5);
1473 answer.append(
"\n");
1476 std::string line6 = line1;
1477 if (components.host_start != buffer.size()) {
1478 line6[components.host_start] =
'`';
1479 line1[components.host_start] =
' ';
1481 for (
size_t i = components.host_start + 1; i < line6.size(); i++) {
1484 line6.append(
" host_start ");
1485 line6.append(std::to_string(components.host_start));
1486 answer.append(line6);
1487 answer.append(
"\n");
1490 std::string line7 = line1;
1491 if (components.username_end != buffer.size()) {
1492 line7[components.username_end] =
'`';
1493 line1[components.username_end] =
' ';
1495 for (
size_t i = components.username_end + 1; i < line7.size(); i++) {
1498 line7.append(
" username_end ");
1499 line7.append(std::to_string(components.username_end));
1500 answer.append(line7);
1501 answer.append(
"\n");
1504 std::string line8 = line1;
1505 if (components.protocol_end != buffer.size()) {
1506 line8[components.protocol_end] =
'`';
1507 line1[components.protocol_end] =
' ';
1509 for (
size_t i = components.protocol_end + 1; i < line8.size(); i++) {
1512 line8.append(
" protocol_end ");
1513 line8.append(std::to_string(components.protocol_end));
1514 answer.append(line8);
1515 answer.append(
"\n");
1519 answer.append(
"note: hash omitted\n");
1522 answer.append(
"note: search omitted\n");
1524 if (components.protocol_end > buffer.size()) {
1525 answer.append(
"warning: protocol_end overflows\n");
1527 if (components.username_end > buffer.size()) {
1528 answer.append(
"warning: username_end overflows\n");
1530 if (components.host_start > buffer.size()) {
1531 answer.append(
"warning: host_start overflows\n");
1533 if (components.host_end > buffer.size()) {
1534 answer.append(
"warning: host_end overflows\n");
1536 if (components.pathname_start > buffer.size()) {
1537 answer.append(
"warning: pathname_start overflows\n");
1542void url_aggregator::delete_dash_dot() {
1543 ada_log(
"url_aggregator::delete_dash_dot");
1546 buffer.erase(components.
host_end, 2);
1558inline void url_aggregator::consume_prepared_path(std::string_view input) {
1559 ada_log(
"url_aggregator::consume_prepared_path ", input);
1569 uint8_t accumulator = checkers::path_signature(input);
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;
1582 (special ? (accumulator == 0)
1583 : ((accumulator & (need_encoding | dot_char | percent_char)) ==
1585 (!may_need_slow_file_handling);
1586 if (accumulator == dot_char && !may_need_slow_file_handling) {
1594 if (input[0] !=
'.') {
1595 size_t slashdot = 0;
1596 bool dot_is_file =
true;
1598 slashdot = input.find(
"/.", slashdot);
1599 if (slashdot == std::string_view::npos) {
1604 dot_is_file &= !(slashdot == input.size() || input[slashdot] ==
'.' ||
1605 input[slashdot] ==
'/');
1608 trivial_path = dot_is_file;
1611 if (trivial_path && is_at_path()) {
1612 ada_log(
"parse_path trivial");
1624 (accumulator & (need_encoding | backslash_char | percent_char)) == 0) &&
1627 ada_log(
"parse_prepared_path fast");
1632 size_t previous_location = 0;
1634 size_t new_location = input.find(
'/', previous_location);
1637 if (new_location == std::string_view::npos) {
1638 std::string_view path_view = input.substr(previous_location);
1639 if (path_view ==
"..") {
1643 update_base_pathname(path);
1647 if (path.back() ==
'/') {
1648 update_base_pathname(path);
1653 path.resize(path.rfind(
'/') + 1);
1654 update_base_pathname(path);
1658 if (path_view !=
".") {
1659 path.append(path_view);
1661 update_base_pathname(path);
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);
1673 }
else if (path_view !=
".") {
1675 path.append(path_view);
1680 ada_log(
"parse_path slow");
1682 bool needs_percent_encoding = (accumulator & 1);
1684 size_t location = (special && (accumulator & 2))
1685 ? input.find_first_of(
"/\\")
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);
1693 if (unicode::is_double_dot_path_segment(path_view)) {
1694 helpers::shorten_path(path, type);
1695 if (location == std::string_view::npos) {
1698 }
else if (unicode::is_single_dot_path_segment(path_view) &&
1699 (location == std::string_view::npos)) {
1703 else if (!unicode::is_single_dot_path_segment(path_view)) {
1708 path_view.size() == 2 &&
1711 path += path_view[0];
1713 path_view.remove_prefix(2);
1714 path.append(path_view);
1718 if (!needs_percent_encoding ||
1721 path.append(path_view);
1725 if (location == std::string_view::npos) {
1726 update_base_pathname(path);
Definitions for URL specific checkers used within Ada.
#define ADA_ASSERT_TRUE(COND)
#define ada_lifetime_bound
#define ada_really_inline
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
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 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)
ada_really_inline size_t percent_encode_index(const std::string_view input, const uint8_t character_set[])
template bool percent_encode< true >(std::string_view input, const uint8_t character_set[], std::string &out)
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
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.