1#if ADA_INCLUDE_URL_PATTERN
13#include <system_error>
24namespace ada::url_pattern_helpers {
26std::tuple<std::string, std::vector<std::string>>
27generate_regular_expression_and_name_list(
28 const std::vector<url_pattern_part>& part_list,
29 url_pattern_compile_component_options options) {
33 result.reserve(part_list.size() * 16);
36 std::vector<std::string> name_list{};
37 name_list.reserve(part_list.size());
40 std::string segment_wildcard_regexp;
43 for (
const url_pattern_part& part : part_list) {
45 if (part.type == url_pattern_part_type::FIXED_TEXT) {
47 if (part.modifier == url_pattern_part_modifier::none) {
48 result.append(escape_regexp_string(part.value));
52 result.append(escape_regexp_string(part.value));
54 result.append(convert_modifier_to_string(part.modifier));
61 name_list.push_back(part.name);
64 std::string_view regexp_value = part.value;
66 if (part.type == url_pattern_part_type::SEGMENT_WILDCARD) {
68 if (segment_wildcard_regexp.empty()) {
69 segment_wildcard_regexp = generate_segment_wildcard_regexp(options);
71 regexp_value = segment_wildcard_regexp;
72 }
else if (part.type == url_pattern_part_type::FULL_WILDCARD) {
78 if (part.prefix.empty() && part.suffix.empty()) {
80 if (part.modifier == url_pattern_part_modifier::none ||
81 part.modifier == url_pattern_part_modifier::optional) {
84 result.append(regexp_value);
86 result.append(convert_modifier_to_string(part.modifier));
90 result.append(regexp_value);
92 result.append(convert_modifier_to_string(part.modifier));
99 if (part.modifier == url_pattern_part_modifier::none ||
100 part.modifier == url_pattern_part_modifier::optional) {
103 result.append(escape_regexp_string(part.prefix));
105 result.append(regexp_value);
107 result.append(escape_regexp_string(part.suffix));
109 result.append(convert_modifier_to_string(part.modifier));
114 ADA_ASSERT_TRUE(part.modifier == url_pattern_part_modifier::zero_or_more ||
115 part.modifier == url_pattern_part_modifier::one_or_more);
127 result.append(escape_regexp_string(part.prefix));
131 result.append(regexp_value);
136 result.append(escape_regexp_string(part.suffix));
139 result.append(escape_regexp_string(part.prefix));
143 result.append(regexp_value);
148 result.append(escape_regexp_string(part.suffix));
153 if (part.modifier == url_pattern_part_modifier::zero_or_more) {
162 return {std::move(result), std::move(name_list)};
165bool is_ipv6_address(std::string_view input)
noexcept {
167 if (input.size() < 2)
return false;
171 if (input.front() ==
'[')
return true;
174 if (input.starts_with(
"{["))
return true;
177 return input.starts_with(
"\\[");
180std::string_view convert_modifier_to_string(
181 url_pattern_part_modifier modifier) {
184 case url_pattern_part_modifier::zero_or_more:
187 case url_pattern_part_modifier::optional:
190 case url_pattern_part_modifier::one_or_more:
198std::string generate_segment_wildcard_regexp(
199 url_pattern_compile_component_options options) {
201 std::string
result =
"[^";
204 result.append(escape_regexp_string(options.get_delimiter()));
208 ada_log(
"generate_segment_wildcard_regexp result: ", result);
215constexpr uint8_t CHAR_SCHEME = 1;
216constexpr uint8_t CHAR_UPPER = 2;
217constexpr uint8_t CHAR_SIMPLE_HOSTNAME = 4;
218constexpr uint8_t CHAR_SIMPLE_PATHNAME =
221constexpr std::array<uint8_t, 256> char_class_table = []()
consteval {
222 std::array<uint8_t, 256> table{};
223 for (
int c =
'a'; c <=
'z'; c++)
224 table[c] = CHAR_SCHEME | CHAR_SIMPLE_HOSTNAME | CHAR_SIMPLE_PATHNAME;
225 for (
int c =
'A'; c <=
'Z'; c++)
226 table[c] = CHAR_SCHEME | CHAR_UPPER | CHAR_SIMPLE_PATHNAME;
227 for (
int c =
'0'; c <=
'9'; c++)
228 table[c] = CHAR_SCHEME | CHAR_SIMPLE_HOSTNAME | CHAR_SIMPLE_PATHNAME;
229 table[
'+'] = CHAR_SCHEME;
230 table[
'-'] = CHAR_SCHEME | CHAR_SIMPLE_HOSTNAME | CHAR_SIMPLE_PATHNAME;
232 CHAR_SCHEME | CHAR_SIMPLE_HOSTNAME;
233 table[
'/'] = CHAR_SIMPLE_PATHNAME;
234 table[
'_'] = CHAR_SIMPLE_PATHNAME;
235 table[
'~'] = CHAR_SIMPLE_PATHNAME;
240tl::expected<std::string, errors> canonicalize_protocol(
241 std::string_view input) {
242 ada_log(
"canonicalize_protocol called with input=", input);
243 if (input.empty()) [[unlikely]] {
247 if (input.ends_with(
":")) {
248 input.remove_suffix(1);
249 if (input.empty()) [[unlikely]] {
250 return tl::unexpected(errors::type_error);
255 if (scheme::is_special(input)) {
256 return std::string(input);
261 uint8_t first_flags = char_class_table[
static_cast<uint8_t
>(input[0])];
262 if (!(first_flags & CHAR_SCHEME) || input[0] ==
'+' || input[0] ==
'-' ||
263 input[0] ==
'.' || unicode::is_ascii_digit(input[0])) {
264 return tl::unexpected(errors::type_error);
267 uint8_t needs_lowercase = first_flags & CHAR_UPPER;
268 for (
size_t i = 1; i < input.size(); i++) {
269 uint8_t flags = char_class_table[
static_cast<uint8_t
>(input[i])];
270 if (!(flags & CHAR_SCHEME)) {
271 return tl::unexpected(errors::type_error);
273 needs_lowercase |= flags & CHAR_UPPER;
276 if (needs_lowercase == 0) {
277 return std::string(input);
280 std::string
result(input);
285tl::expected<std::string, errors> canonicalize_username(
286 std::string_view input) {
288 if (input.empty()) [[unlikely]] {
293 input, character_sets::USERINFO_PERCENT_ENCODE);
294 if (idx == input.size()) {
296 return std::string(input);
299 return ada::unicode::percent_encode(
300 input, character_sets::USERINFO_PERCENT_ENCODE, idx);
303tl::expected<std::string, errors> canonicalize_password(
304 std::string_view input) {
306 if (input.empty()) [[unlikely]] {
311 input, character_sets::USERINFO_PERCENT_ENCODE);
312 if (idx == input.size()) {
314 return std::string(input);
317 return ada::unicode::percent_encode(
318 input, character_sets::USERINFO_PERCENT_ENCODE, idx);
321tl::expected<std::string, errors> canonicalize_hostname(
322 std::string_view input) {
323 ada_log(
"canonicalize_hostname input=", input);
324 if (input.empty()) [[unlikely]] {
337 bool needs_processing =
false;
338 for (
char c : input) {
340 !(char_class_table[
static_cast<uint8_t
>(c)] & CHAR_SIMPLE_HOSTNAME);
342 if (!needs_processing && !checkers::is_ipv4(input)) {
343 return std::string(input);
355 if (!
url->set_hostname(input)) {
357 return tl::unexpected(errors::type_error);
360 return std::string(
url->get_hostname());
363tl::expected<std::string, errors> canonicalize_ipv6_hostname(
364 std::string_view input) {
365 ada_log(
"canonicalize_ipv6_hostname input=", input);
367 if (std::ranges::any_of(input, [](
char c) {
368 return c !=
'[' && c !=
']' && c !=
':' &&
369 !unicode::is_ascii_hex_digit(c);
371 return tl::unexpected(errors::type_error);
375 auto hostname = std::string(input);
376 unicode::to_lower_ascii(hostname.data(), hostname.size());
380tl::expected<std::string, errors> canonicalize_port(
381 std::string_view port_value) {
383 if (port_value.empty()) [[unlikely]] {
388 std::string trimmed(port_value);
389 helpers::remove_ascii_tab_or_newline(trimmed);
391 if (trimmed.empty()) {
396 if (!unicode::is_ascii_digit(trimmed.front())) {
397 return tl::unexpected(errors::type_error);
401 auto first_non_digit =
402 std::ranges::find_if_not(trimmed, unicode::is_ascii_digit);
403 std::string_view digits_to_parse =
404 std::string_view(trimmed.data(), first_non_digit - trimmed.begin());
412 size_t first_significant = digits_to_parse.find_first_not_of(
'0');
413 if (first_significant == std::string_view::npos) {
417 digits_to_parse.remove_prefix(first_significant);
422 if (digits_to_parse.size() == 5) {
423 if (digits_to_parse >
"65535") {
424 return tl::unexpected(errors::type_error);
426 }
else if (digits_to_parse.size() > 5) {
427 return tl::unexpected(errors::type_error);
430 return std::string(digits_to_parse);
433tl::expected<std::string, errors> canonicalize_port_with_protocol(
434 std::string_view port_value, std::string_view protocol) {
436 if (port_value.empty()) [[unlikely]] {
441 if (protocol.empty()) {
443 }
else if (protocol.ends_with(
":")) {
444 protocol.remove_suffix(1);
448 std::string trimmed(port_value);
449 helpers::remove_ascii_tab_or_newline(trimmed);
451 if (trimmed.empty()) {
456 if (!unicode::is_ascii_digit(trimmed.front())) {
457 return tl::unexpected(errors::type_error);
461 auto first_non_digit =
462 std::ranges::find_if_not(trimmed, unicode::is_ascii_digit);
463 std::string_view digits_to_parse =
464 std::string_view(trimmed.data(), first_non_digit - trimmed.begin());
467 uint16_t parsed_port{};
469 auto result = std::from_chars(digits_to_parse.data(),
470 digits_to_parse.data() + digits_to_parse.size(),
473 if (
result.ec == std::errc::result_out_of_range) {
474 return tl::unexpected(errors::type_error);
477 if (
result.ec == std::errc()) {
479 uint16_t default_port = scheme::get_special_port(protocol);
482 if (default_port != 0 && default_port == parsed_port) {
487 return std::to_string(parsed_port);
490 return tl::unexpected(errors::type_error);
493tl::expected<std::string, errors> canonicalize_pathname(
494 std::string_view input) {
495 if (input.empty()) [[unlikely]] {
501 bool needs_processing =
false;
502 for (
char c : input) {
504 !(char_class_table[
static_cast<uint8_t
>(c)] & CHAR_SIMPLE_PATHNAME);
506 if (!needs_processing) {
507 return std::string(input);
512 const bool leading_slash = input.starts_with(
"/");
515 const auto modified_value =
516 (leading_slash ? std::string() : std::string(
"/-")) + std::string(input);
530 if (!
url->set_pathname(modified_value)) {
532 return tl::unexpected(errors::type_error);
534 const auto pathname =
url->get_pathname();
537 if (!leading_slash) {
540 if (pathname.size() < 2) {
541 return tl::unexpected(errors::type_error);
543 return std::string(pathname.substr(2));
545 return std::string(pathname);
548tl::expected<std::string, errors> canonicalize_opaque_pathname(
549 std::string_view input) {
551 if (input.empty()) [[unlikely]] {
555 std::string value(input);
556 helpers::remove_ascii_tab_or_newline(value);
557 return ada::unicode::percent_encode(
558 value, character_sets::C0_CONTROL_PERCENT_ENCODE);
561tl::expected<std::string, errors> canonicalize_search(std::string_view input) {
563 if (input.empty()) [[unlikely]] {
569 std::string new_value(input);
571 helpers::remove_ascii_tab_or_newline(new_value);
573 if (new_value.empty()) {
579 new_value, character_sets::SPECIAL_QUERY_PERCENT_ENCODE);
580 if (idx == new_value.size()) {
585 return ada::unicode::percent_encode(
586 new_value, character_sets::SPECIAL_QUERY_PERCENT_ENCODE, idx);
589tl::expected<std::string, errors> canonicalize_hash(std::string_view input) {
591 if (input.empty()) [[unlikely]] {
597 std::string new_value(input);
599 helpers::remove_ascii_tab_or_newline(new_value);
601 if (new_value.empty()) {
607 new_value, character_sets::FRAGMENT_PERCENT_ENCODE);
608 if (idx == new_value.size()) {
613 return ada::unicode::percent_encode(
614 new_value, character_sets::FRAGMENT_PERCENT_ENCODE, idx);
617tl::expected<std::vector<token>,
errors> tokenize(std::string_view input,
618 token_policy policy) {
619 ada_log(
"tokenize input: ", input);
623 auto tokenizer = Tokenizer(input, policy);
625 while (tokenizer.index < tokenizer.input.size()) {
628 tokenizer.seek_and_get_next_code_point(tokenizer.index);
632 if (tokenizer.had_invalid_code_point()) {
633 if (
auto error = tokenizer.process_tokenizing_error(tokenizer.next_index,
635 return tl::unexpected(*error);
641 if (tokenizer.code_point ==
'*') {
644 tokenizer.add_token_with_defaults(token_type::ASTERISK);
645 ada_log(
"add ASTERISK token");
651 if (tokenizer.code_point ==
'+' || tokenizer.code_point ==
'?') {
654 tokenizer.add_token_with_defaults(token_type::OTHER_MODIFIER);
660 if (tokenizer.code_point ==
'\\') {
663 if (tokenizer.index == tokenizer.input.size() - 1) {
666 if (
auto error = tokenizer.process_tokenizing_error(
667 tokenizer.next_index, tokenizer.index)) {
668 ada_log(
"process_tokenizing_error failed");
669 return tl::unexpected(*error);
675 auto escaped_index = tokenizer.next_index;
677 tokenizer.get_next_code_point();
678 if (tokenizer.had_invalid_code_point()) {
679 if (
auto error = tokenizer.process_tokenizing_error(
680 tokenizer.next_index, escaped_index)) {
681 return tl::unexpected(*error);
687 tokenizer.add_token_with_default_length(
688 token_type::ESCAPED_CHAR, tokenizer.next_index, escaped_index);
689 ada_log(
"add ESCAPED_CHAR token on next_index ", tokenizer.next_index,
690 " with escaped index ", escaped_index);
696 if (tokenizer.code_point ==
'{') {
699 tokenizer.add_token_with_defaults(token_type::OPEN);
700 ada_log(
"add OPEN token");
705 if (tokenizer.code_point ==
'}') {
708 tokenizer.add_token_with_defaults(token_type::CLOSE);
709 ada_log(
"add CLOSE token");
714 if (tokenizer.code_point ==
':') {
716 auto name_position = tokenizer.next_index;
718 auto name_start = name_position;
719 bool invalid_name =
false;
721 while (name_position < tokenizer.input.size()) {
724 tokenizer.seek_and_get_next_code_point(name_position);
725 if (tokenizer.had_invalid_code_point()) {
726 if (
auto error = tokenizer.process_tokenizing_error(
727 tokenizer.next_index, name_position)) {
728 return tl::unexpected(*error);
735 bool first_code_point = name_position == name_start;
738 auto valid_code_point =
739 idna::valid_name_code_point(tokenizer.code_point, first_code_point);
740 ada_log(
"tokenizer.code_point=", uint32_t(tokenizer.code_point),
741 " first_code_point=", first_code_point,
742 " valid_code_point=", valid_code_point);
744 if (!valid_code_point)
break;
746 name_position = tokenizer.next_index;
754 if (name_position <= name_start) {
757 if (
auto error = tokenizer.process_tokenizing_error(name_start,
759 ada_log(
"process_tokenizing_error failed");
760 return tl::unexpected(*error);
768 tokenizer.add_token_with_default_length(token_type::NAME, name_position,
774 if (tokenizer.code_point ==
'(') {
778 auto regexp_position = tokenizer.next_index;
780 auto regexp_start = regexp_position;
786 while (regexp_position < tokenizer.input.size()) {
789 tokenizer.seek_and_get_next_code_point(regexp_position);
790 if (tokenizer.had_invalid_code_point()) {
791 if (
auto process_error = tokenizer.process_tokenizing_error(
792 tokenizer.next_index, regexp_position)) {
793 return tl::unexpected(*process_error);
802 if (!unicode::is_ascii(tokenizer.code_point)) {
805 if (
auto process_error = tokenizer.process_tokenizing_error(
806 regexp_start, tokenizer.index)) {
807 return tl::unexpected(*process_error);
816 if (regexp_position == regexp_start && tokenizer.code_point ==
'?') {
819 if (
auto process_error = tokenizer.process_tokenizing_error(
820 regexp_start, tokenizer.index)) {
821 return tl::unexpected(*process_error);
829 if (tokenizer.code_point ==
'\\') {
831 if (regexp_position == tokenizer.input.size() - 1) {
834 if (
auto process_error = tokenizer.process_tokenizing_error(
835 regexp_start, tokenizer.index)) {
836 return tl::unexpected(*process_error);
843 auto escaped_index = tokenizer.next_index;
844 tokenizer.get_next_code_point();
845 if (tokenizer.had_invalid_code_point()) {
846 if (
auto process_error = tokenizer.process_tokenizing_error(
847 tokenizer.next_index, escaped_index)) {
848 return tl::unexpected(*process_error);
855 if (!unicode::is_ascii(tokenizer.code_point)) {
858 if (
auto process_error = tokenizer.process_tokenizing_error(
859 regexp_start, tokenizer.index);
860 process_error.has_value()) {
861 return tl::unexpected(*process_error);
868 regexp_position = tokenizer.next_index;
873 if (tokenizer.code_point ==
')') {
879 regexp_position = tokenizer.next_index;
883 }
else if (tokenizer.code_point ==
'(') {
889 if (regexp_position == tokenizer.input.size() - 1) {
892 if (
auto process_error = tokenizer.process_tokenizing_error(
893 regexp_start, tokenizer.index)) {
894 return tl::unexpected(*process_error);
901 auto temporary_position = tokenizer.next_index;
903 tokenizer.get_next_code_point();
904 if (tokenizer.had_invalid_code_point()) {
905 if (
auto process_error = tokenizer.process_tokenizing_error(
906 tokenizer.next_index, temporary_position)) {
907 return tl::unexpected(*process_error);
913 if (tokenizer.code_point !=
'?') {
916 if (
auto process_error = tokenizer.process_tokenizing_error(
917 regexp_start, tokenizer.index)) {
918 return tl::unexpected(*process_error);
925 tokenizer.next_index = temporary_position;
928 regexp_position = tokenizer.next_index;
937 if (
auto process_error = tokenizer.process_tokenizing_error(
938 regexp_start, tokenizer.index)) {
939 return tl::unexpected(*process_error);
944 auto regexp_length = regexp_position - regexp_start - 1;
946 if (regexp_length == 0) {
949 if (
auto process_error = tokenizer.process_tokenizing_error(
950 regexp_start, tokenizer.index)) {
951 ada_log(
"process_tokenizing_error failed");
952 return tl::unexpected(*process_error);
958 tokenizer.add_token(token_type::REGEXP, regexp_position, regexp_start,
964 tokenizer.add_token_with_defaults(token_type::CHAR);
968 tokenizer.add_token_with_default_length(token_type::END, tokenizer.index,
971 ada_log(
"tokenizer.token_list size is: ", tokenizer.token_list.size());
973 return tokenizer.token_list;
977constexpr std::array<uint8_t, 256> escape_pattern_table = []()
consteval {
978 std::array<uint8_t, 256> out{};
979 for (
auto& c : {
'+',
'*',
'?',
':',
'{',
'}',
'(',
')',
'\\'}) {
985constexpr bool should_escape_pattern_char(
char c) {
986 return escape_pattern_table[
static_cast<uint8_t
>(c)];
990std::string escape_pattern_string(std::string_view input) {
991 ada_log(
"escape_pattern_string called with input=", input);
992 if (input.empty()) [[unlikely]] {
1000 result.reserve(input.size() * 2);
1003 for (
const char c : input) {
1004 if (should_escape_pattern_char(c)) {
1016constexpr std::array<uint8_t, 256> escape_regexp_table = []()
consteval {
1017 std::array<uint8_t, 256> out{};
1018 for (
auto& c : {
'.',
'+',
'*',
'?',
'^',
'$',
'{',
'}',
'(',
')',
'[',
']',
1025constexpr bool should_escape_regexp_char(
char c) {
1026 return escape_regexp_table[(uint8_t)c];
1030std::string escape_regexp_string(std::string_view input) {
1036 result.reserve(input.size() * 2);
1037 for (
const char c : input) {
1038 if (should_escape_regexp_char(c)) {
1049std::string process_base_url_string(std::string_view input,
1050 url_pattern_init::process_type type) {
1052 if (type != url_pattern_init::process_type::pattern) {
1053 return std::string(input);
1056 return escape_pattern_string(input);
1059constexpr bool is_absolute_pathname(
1060 std::string_view input, url_pattern_init::process_type type)
noexcept {
1062 if (input.empty()) [[unlikely]] {
1066 if (input.starts_with(
"/"))
return true;
1068 if (type == url_pattern_init::process_type::url)
return false;
1070 if (input.size() < 2)
return false;
1074 return input[1] ==
'/' && (input[0] ==
'\\' || input[0] ==
'{');
1077std::string generate_pattern_string(
1078 std::vector<url_pattern_part>& part_list,
1079 url_pattern_compile_component_options& options) {
1084 for (
size_t index = 0; index < part_list.size(); index++) {
1087 const auto& part = part_list[index];
1091 const url_pattern_part* previous_part =
1092 index == 0 ? nullptr : &part_list[index - 1];
1095 const url_pattern_part* next_part =
1096 index < part_list.size() - 1 ? &part_list[index + 1] :
nullptr;
1098 if (part.type == url_pattern_part_type::FIXED_TEXT) {
1100 if (part.modifier == url_pattern_part_modifier::none) {
1103 result.append(escape_pattern_string(part.value));
1110 result.append(escape_pattern_string(part.value));
1115 result.append(convert_modifier_to_string(part.modifier));
1120 bool custom_name = !unicode::is_ascii_digit(part.name[0]);
1128 bool needs_grouping =
1129 !part.suffix.empty() ||
1130 (!part.prefix.empty() && part.prefix != options.get_prefix());
1140 if (!needs_grouping && custom_name &&
1141 part.type == url_pattern_part_type::SEGMENT_WILDCARD &&
1142 part.modifier == url_pattern_part_modifier::none && next_part &&
1143 next_part->prefix.empty() && next_part->suffix.empty()) {
1145 if (next_part->type == url_pattern_part_type::FIXED_TEXT) {
1149 if (idna::valid_name_code_point(next_part->value[0],
false)) {
1150 needs_grouping =
true;
1154 needs_grouping = !next_part->name.empty() &&
1155 unicode::is_ascii_digit(next_part->name[0]);
1166 if (!needs_grouping && part.prefix.empty() && previous_part &&
1167 previous_part->type == url_pattern_part_type::FIXED_TEXT &&
1168 !previous_part->value.empty() && !options.get_prefix().empty() &&
1169 previous_part->value.back() == options.get_prefix()[0]) {
1170 needs_grouping =
true;
1177 if (needs_grouping) {
1183 result.append(escape_pattern_string(part.prefix));
1190 result.append(part.name);
1194 if (part.type == url_pattern_part_type::REGEXP) {
1198 result.append(part.value);
1201 }
else if (part.type == url_pattern_part_type::SEGMENT_WILDCARD &&
1208 result.append(generate_segment_wildcard_regexp(options));
1211 }
else if (part.type == url_pattern_part_type::FULL_WILDCARD) {
1222 previous_part->type == url_pattern_part_type::FIXED_TEXT ||
1223 previous_part->modifier != url_pattern_part_modifier::none ||
1224 needs_grouping || !part.prefix.empty())) {
1241 if (part.type == url_pattern_part_type::SEGMENT_WILDCARD && custom_name &&
1242 !part.suffix.empty() &&
1243 idna::valid_name_code_point(part.suffix[0],
false)) {
1249 result.append(escape_pattern_string(part.suffix));
1251 if (needs_grouping)
result.append(
"}");
1254 result.append(convert_modifier_to_string(part.modifier));
Declaration of the character sets used by unicode functions.
Declarations for URL specific checkers used within Ada.
#define ADA_ASSERT_TRUE(COND)
Definitions for helper functions used within Ada.
bool constexpr is_ascii(std::u32string_view view)
ada_really_inline size_t percent_encode_index(const std::string_view input, const uint8_t character_set[])
errors
Error codes for URL parsing operations.
template ada::result< url_aggregator > parse< url_aggregator >(std::string_view input, const url_aggregator *base_url)
tl::expected< result_type, ada::errors > result
URL scheme type definitions and utilities.
Definitions for all unicode specific functions.
Declaration for the URLPattern helpers.