Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_pattern_helpers-inl.h
Go to the documentation of this file.
1
5#ifndef ADA_URL_PATTERN_HELPERS_INL_H
6#define ADA_URL_PATTERN_HELPERS_INL_H
7
8#include <cstddef>
9#include <cstdint>
10#include <optional>
11#include <string>
12#include <string_view>
13#include <utility>
14#include <vector>
15
16#include "ada/common_defs.h"
17#include "ada/expected.h"
19#include "ada/implementation.h"
20
21#if ADA_INCLUDE_URL_PATTERN
22namespace ada::url_pattern_helpers {
23#if defined(ADA_TESTING) || defined(ADA_LOGGING)
24inline std::string to_string(token_type type) {
25 switch (type) {
26 case token_type::INVALID_CHAR:
27 return "INVALID_CHAR";
28 case token_type::OPEN:
29 return "OPEN";
30 case token_type::CLOSE:
31 return "CLOSE";
32 case token_type::REGEXP:
33 return "REGEXP";
34 case token_type::NAME:
35 return "NAME";
36 case token_type::CHAR:
37 return "CHAR";
38 case token_type::ESCAPED_CHAR:
39 return "ESCAPED_CHAR";
40 case token_type::OTHER_MODIFIER:
41 return "OTHER_MODIFIER";
42 case token_type::ASTERISK:
43 return "ASTERISK";
44 case token_type::END:
45 return "END";
46 default:
48 }
49}
50#endif // defined(ADA_TESTING) || defined(ADA_LOGGING)
51
52template <url_pattern_regex::regex_concept regex_provider>
53constexpr void constructor_string_parser<regex_provider>::rewind() {
54 // Set parser's token index to parser's component start.
55 token_index = component_start;
56 // Set parser's token increment to 0.
57 token_increment = 0;
58}
59
60template <url_pattern_regex::regex_concept regex_provider>
61constexpr bool constructor_string_parser<regex_provider>::is_hash_prefix() {
62 // Return the result of running is a non-special pattern char given parser,
63 // parser's token index and "#".
64 return is_non_special_pattern_char(token_index, '#');
65}
66
67template <url_pattern_regex::regex_concept regex_provider>
68constexpr bool constructor_string_parser<regex_provider>::is_search_prefix() {
69 // If result of running is a non-special pattern char given parser, parser's
70 // token index and "?" is true, then return true.
71 if (is_non_special_pattern_char(token_index, '?')) {
72 return true;
73 }
74
75 // If parser's token list[parser's token index]'s value is not "?", then
76 // return false.
77 if (token_list[token_index].value != "?") {
78 return false;
79 }
80
81 // If previous index is less than 0, then return true.
82 if (token_index == 0) return true;
83 // Let previous index be parser's token index - 1.
84 auto previous_index = token_index - 1;
85 // Let previous token be the result of running get a safe token given parser
86 // and previous index.
87 auto previous_token = get_safe_token(previous_index);
88 ADA_ASSERT_TRUE(previous_token);
89 // If any of the following are true, then return false:
90 // - previous token's type is "name".
91 // - previous token's type is "regexp".
92 // - previous token's type is "close".
93 // - previous token's type is "asterisk".
94 return !(previous_token->type == token_type::NAME ||
95 previous_token->type == token_type::REGEXP ||
96 previous_token->type == token_type::CLOSE ||
97 previous_token->type == token_type::ASTERISK);
98}
99
100template <url_pattern_regex::regex_concept regex_provider>
101constexpr bool
102constructor_string_parser<regex_provider>::is_non_special_pattern_char(
103 size_t index, uint32_t value) const {
104 // Let token be the result of running get a safe token given parser and index.
105 auto token = get_safe_token(index);
106 ADA_ASSERT_TRUE(token);
107
108 // If token's value is not value, then return false.
109 // TODO: Remove this once we make sure get_safe_token returns a non-empty
110 // string.
111 if (!token->value.empty() &&
112 static_cast<uint32_t>(token->value[0]) != value) {
113 return false;
114 }
115
116 // If any of the following are true:
117 // - token's type is "char";
118 // - token's type is "escaped-char"; or
119 // - token's type is "invalid-char",
120 // - then return true.
121 return token->type == token_type::CHAR ||
122 token->type == token_type::ESCAPED_CHAR ||
123 token->type == token_type::INVALID_CHAR;
124}
125
126template <url_pattern_regex::regex_concept regex_provider>
127constexpr const token*
128constructor_string_parser<regex_provider>::get_safe_token(size_t index) const {
129 // If index is less than parser's token list's size, then return parser's
130 // token list[index].
131 if (index < token_list.size()) [[likely]] {
132 return &token_list[index];
133 }
134
135 // Assert: parser's token list's size is greater than or equal to 1.
136 ADA_ASSERT_TRUE(!token_list.empty());
137
138 // Let token be parser's token list[last index].
139 // Assert: token's type is "end".
140 ADA_ASSERT_TRUE(token_list.back().type == token_type::END);
141
142 // Return token.
143 return &token_list.back();
144}
145
146template <url_pattern_regex::regex_concept regex_provider>
147constexpr bool constructor_string_parser<regex_provider>::is_group_open()
148 const {
149 // If parser's token list[parser's token index]'s type is "open", then return
150 // true.
151 return token_list[token_index].type == token_type::OPEN;
152}
153
154template <url_pattern_regex::regex_concept regex_provider>
155constexpr bool constructor_string_parser<regex_provider>::is_group_close()
156 const {
157 // If parser's token list[parser's token index]'s type is "close", then return
158 // true.
159 return token_list[token_index].type == token_type::CLOSE;
160}
161
162template <url_pattern_regex::regex_concept regex_provider>
163constexpr bool
164constructor_string_parser<regex_provider>::next_is_authority_slashes() const {
165 // If the result of running is a non-special pattern char given parser,
166 // parser's token index + 1, and "/" is false, then return false.
167 if (!is_non_special_pattern_char(token_index + 1, '/')) {
168 return false;
169 }
170 // If the result of running is a non-special pattern char given parser,
171 // parser's token index + 2, and "/" is false, then return false.
172 if (!is_non_special_pattern_char(token_index + 2, '/')) {
173 return false;
174 }
175 return true;
176}
177
178template <url_pattern_regex::regex_concept regex_provider>
179constexpr bool constructor_string_parser<regex_provider>::is_protocol_suffix()
180 const {
181 // Return the result of running is a non-special pattern char given parser,
182 // parser's token index, and ":".
183 return is_non_special_pattern_char(token_index, ':');
184}
185
186template <url_pattern_regex::regex_concept regex_provider>
187void constructor_string_parser<regex_provider>::change_state(State new_state,
188 size_t skip) {
189 // If parser's state is not "init", not "authority", and not "done", then set
190 // parser's result[parser's state] to the result of running make a component
191 // string given parser.
192 if (state != State::INIT && state != State::AUTHORITY &&
193 state != State::DONE) {
194 auto value = make_component_string();
195 // TODO: Simplify this.
196 switch (state) {
197 case State::PROTOCOL: {
198 result.protocol = value;
199 break;
200 }
201 case State::USERNAME: {
202 result.username = value;
203 break;
204 }
205 case State::PASSWORD: {
206 result.password = value;
207 break;
208 }
209 case State::HOSTNAME: {
210 result.hostname = value;
211 break;
212 }
213 case State::PORT: {
214 result.port = value;
215 break;
216 }
217 case State::PATHNAME: {
218 result.pathname = value;
219 break;
220 }
221 case State::SEARCH: {
222 result.search = value;
223 break;
224 }
225 case State::HASH: {
226 result.hash = value;
227 break;
228 }
229 default:
231 }
232 }
233
234 // If parser's state is not "init" and new state is not "done", then:
235 if (state != State::INIT && new_state != State::DONE) {
236 // If parser's state is "protocol", "authority", "username", or "password";
237 // new state is "port", "pathname", "search", or "hash"; and parser's
238 // result["hostname"] does not exist, then set parser's result["hostname"]
239 // to the empty string.
240 if ((state == State::PROTOCOL || state == State::AUTHORITY ||
241 state == State::USERNAME || state == State::PASSWORD) &&
242 (new_state == State::PORT || new_state == State::PATHNAME ||
243 new_state == State::SEARCH || new_state == State::HASH) &&
244 !result.hostname)
245 result.hostname = "";
246 }
247
248 // If parser's state is "protocol", "authority", "username", "password",
249 // "hostname", or "port"; new state is "search" or "hash"; and parser's
250 // result["pathname"] does not exist, then:
251 if ((state == State::PROTOCOL || state == State::AUTHORITY ||
252 state == State::USERNAME || state == State::PASSWORD ||
253 state == State::HOSTNAME || state == State::PORT) &&
254 (new_state == State::SEARCH || new_state == State::HASH) &&
255 !result.pathname) {
256 if (protocol_matches_a_special_scheme_flag) {
257 result.pathname = "/";
258 } else {
259 // Otherwise, set parser's result["pathname"] to the empty string.
260 result.pathname = "";
261 }
262 }
263
264 // If parser's state is "protocol", "authority", "username", "password",
265 // "hostname", "port", or "pathname"; new state is "hash"; and parser's
266 // result["search"] does not exist, then set parser's result["search"] to
267 // the empty string.
268 if ((state == State::PROTOCOL || state == State::AUTHORITY ||
269 state == State::USERNAME || state == State::PASSWORD ||
270 state == State::HOSTNAME || state == State::PORT ||
271 state == State::PATHNAME) &&
272 new_state == State::HASH && !result.search) {
273 result.search = "";
274 }
275
276 // Set parser's state to new state.
277 state = new_state;
278 // Increment parser's token index by skip.
279 token_index += skip;
280 // Set parser's component start to parser's token index.
281 component_start = token_index;
282 // Set parser's token increment to 0.
283 token_increment = 0;
284}
285
286template <url_pattern_regex::regex_concept regex_provider>
287std::string constructor_string_parser<regex_provider>::make_component_string() {
288 // Assert: parser's token index is less than parser's token list's size.
289 ADA_ASSERT_TRUE(token_index < token_list.size());
290
291 // Let token be parser's token list[parser's token index].
292 // Let end index be token's index.
293 const auto end_index = token_list[token_index].index;
294 // Let component start token be the result of running get a safe token given
295 // parser and parser's component start.
296 const auto component_start_token = get_safe_token(component_start);
297 ADA_ASSERT_TRUE(component_start_token);
298 // Let component start input index be component start token's index.
299 const auto component_start_input_index = component_start_token->index;
300 // Return the code point substring from component start input index to end
301 // index within parser's input.
302 return std::string(input.substr(component_start_input_index,
303 end_index - component_start_input_index));
304}
305
306template <url_pattern_regex::regex_concept regex_provider>
307constexpr bool
308constructor_string_parser<regex_provider>::is_an_identity_terminator() const {
309 // Return the result of running is a non-special pattern char given parser,
310 // parser's token index, and "@".
311 return is_non_special_pattern_char(token_index, '@');
312}
313
314template <url_pattern_regex::regex_concept regex_provider>
315constexpr bool constructor_string_parser<regex_provider>::is_pathname_start()
316 const {
317 // Return the result of running is a non-special pattern char given parser,
318 // parser's token index, and "/".
319 return is_non_special_pattern_char(token_index, '/');
320}
321
322template <url_pattern_regex::regex_concept regex_provider>
323constexpr bool constructor_string_parser<regex_provider>::is_password_prefix()
324 const {
325 // Return the result of running is a non-special pattern char given parser,
326 // parser's token index, and ":".
327 return is_non_special_pattern_char(token_index, ':');
328}
329
330template <url_pattern_regex::regex_concept regex_provider>
331constexpr bool constructor_string_parser<regex_provider>::is_an_ipv6_open()
332 const {
333 // Return the result of running is a non-special pattern char given parser,
334 // parser's token index, and "[".
335 return is_non_special_pattern_char(token_index, '[');
336}
337
338template <url_pattern_regex::regex_concept regex_provider>
339constexpr bool constructor_string_parser<regex_provider>::is_an_ipv6_close()
340 const {
341 // Return the result of running is a non-special pattern char given parser,
342 // parser's token index, and "]".
343 return is_non_special_pattern_char(token_index, ']');
344}
345
346template <url_pattern_regex::regex_concept regex_provider>
347constexpr bool constructor_string_parser<regex_provider>::is_port_prefix()
348 const {
349 // Return the result of running is a non-special pattern char given parser,
350 // parser's token index, and ":".
351 return is_non_special_pattern_char(token_index, ':');
352}
353
354constexpr void Tokenizer::get_next_code_point() {
355 ada_log("Tokenizer::get_next_code_point called with index=", next_index);
356 ADA_ASSERT_TRUE(next_index < input.size());
357 // Decode the next UTF-8 code point. If malformed or truncated, mark it as
358 // invalid, return the offending byte as the code point, and advance by one
359 // to guarantee forward progress.
360 invalid_code_point = false;
361 code_point = 0;
362 size_t number_bytes = 0;
363 const size_t initial_index = next_index;
364 unsigned char first_byte = input[next_index];
365
366 if ((first_byte & 0x80) == 0) {
367 // 1-byte character (ASCII)
368 next_index++;
369 code_point = first_byte;
370 ada_log("Tokenizer::get_next_code_point returning ASCII code point=",
371 uint32_t(code_point));
372 ada_log("Tokenizer::get_next_code_point next_index =", next_index,
373 " input.size()=", input.size());
374 return;
375 }
376 ada_log("Tokenizer::get_next_code_point read first byte=",
377 uint32_t(first_byte));
378 if ((first_byte & 0xE0) == 0xC0) {
379 code_point = first_byte & 0x1F;
380 number_bytes = 2;
381 ada_log("Tokenizer::get_next_code_point two bytes");
382 } else if ((first_byte & 0xF0) == 0xE0) {
383 code_point = first_byte & 0x0F;
384 number_bytes = 3;
385 ada_log("Tokenizer::get_next_code_point three bytes");
386 } else if ((first_byte & 0xF8) == 0xF0) {
387 code_point = first_byte & 0x07;
388 number_bytes = 4;
389 ada_log("Tokenizer::get_next_code_point four bytes");
390 }
391
392 // Invalid leading bytes that still match a multi-byte prefix.
393 if ((number_bytes == 2 && first_byte < 0xC2) ||
394 (number_bytes == 4 && first_byte > 0xF4)) {
395 invalid_code_point = true;
396 code_point = first_byte;
397 next_index = initial_index + 1;
398 return;
399 }
400
401 // Invalid leading byte (e.g., continuation byte outside a sequence).
402 if (number_bytes == 0) {
403 invalid_code_point = true;
404 code_point = first_byte;
405 next_index = initial_index + 1;
406 return;
407 }
408
409 // Truncated UTF-8 sequence.
410 if (number_bytes + next_index > input.size()) {
411 invalid_code_point = true;
412 code_point = first_byte;
413 next_index = initial_index + 1;
414 return;
415 }
416
417 for (size_t i = 1 + next_index; i < number_bytes + next_index; ++i) {
418 unsigned char byte = input[i];
419 if ((byte & 0xC0) != 0x80) {
420 invalid_code_point = true;
421 code_point = first_byte;
422 next_index = initial_index + 1;
423 return;
424 }
425 ada_log("Tokenizer::get_next_code_point read byte=", uint32_t(byte));
426 code_point = (code_point << 6) | (byte & 0x3F);
427 }
428 ada_log("Tokenizer::get_next_code_point returning non-ASCII code point=",
429 uint32_t(code_point));
430 ada_log("Tokenizer::get_next_code_point next_index =", next_index,
431 " input.size()=", input.size());
432 next_index += number_bytes;
433}
434
435constexpr void Tokenizer::seek_and_get_next_code_point(size_t new_index) {
436 ada_log("Tokenizer::seek_and_get_next_code_point called with new_index=",
437 new_index);
438 // Set tokenizer's next index to index.
439 next_index = new_index;
440 // Run get the next code point given tokenizer.
441 get_next_code_point();
442}
443
444inline void Tokenizer::add_token(token_type type, size_t next_position,
445 size_t value_position, size_t value_length) {
446 ada_log("Tokenizer::add_token called with type=", to_string(type),
447 " next_position=", next_position, " value_position=", value_position);
448 ADA_ASSERT_TRUE(next_position >= value_position);
449
450 // Let token be a new token.
451 // Set token's type to type.
452 // Set token's index to tokenizer's index.
453 // Set token's value to the code point substring from value position with
454 // length value length within tokenizer's input.
455 // Append token to the back of tokenizer's token list.
456 token_list.emplace_back(type, index,
457 input.substr(value_position, value_length));
458 // Set tokenizer's index to next position.
459 index = next_position;
460}
461
462inline void Tokenizer::add_token_with_default_length(token_type type,
463 size_t next_position,
464 size_t value_position) {
465 // Let computed length be next position - value position.
466 auto computed_length = next_position - value_position;
467 // Run add a token given tokenizer, type, next position, value position, and
468 // computed length.
469 add_token(type, next_position, value_position, computed_length);
470}
471
472inline void Tokenizer::add_token_with_defaults(token_type type) {
473 ada_log("Tokenizer::add_token_with_defaults called with type=",
474 to_string(type));
475 // Run add a token with default length given tokenizer, type, tokenizer's next
476 // index, and tokenizer's index.
477 add_token_with_default_length(type, next_index, index);
478}
479
480inline ada_warn_unused std::optional<errors>
481Tokenizer::process_tokenizing_error(size_t next_position,
482 size_t value_position) {
483 // If tokenizer's policy is "strict", then throw a TypeError.
484 if (policy == token_policy::strict) {
485 ada_log("process_tokenizing_error failed with next_position=",
486 next_position, " value_position=", value_position);
487 return errors::type_error;
488 }
489 // Assert: tokenizer's policy is "lenient".
490 ADA_ASSERT_TRUE(policy == token_policy::lenient);
491 // Run add a token with default length given tokenizer, "invalid-char", next
492 // position, and value position.
493 add_token_with_default_length(token_type::INVALID_CHAR, next_position,
494 value_position);
495 return std::nullopt;
496}
497
498template <url_pattern_encoding_callback F>
499token* url_pattern_parser<F>::try_consume_modifier_token() {
500 // Let token be the result of running try to consume a token given parser and
501 // "other-modifier".
502 auto token = try_consume_token(token_type::OTHER_MODIFIER);
503 // If token is not null, then return token.
504 if (token) return token;
505 // Set token to the result of running try to consume a token given parser and
506 // "asterisk".
507 // Return token.
508 return try_consume_token(token_type::ASTERISK);
509}
510
511template <url_pattern_encoding_callback F>
512token* url_pattern_parser<F>::try_consume_regexp_or_wildcard_token(
513 const token* name_token) {
514 // Let token be the result of running try to consume a token given parser and
515 // "regexp".
516 auto token = try_consume_token(token_type::REGEXP);
517 // If name token is null and token is null, then set token to the result of
518 // running try to consume a token given parser and "asterisk".
519 if (!name_token && !token) {
520 token = try_consume_token(token_type::ASTERISK);
521 }
522 // Return token.
523 return token;
524}
525
526template <url_pattern_encoding_callback F>
527token* url_pattern_parser<F>::try_consume_token(token_type type) {
528 ada_log("url_pattern_parser::try_consume_token called with type=",
529 to_string(type));
530 // Assert: parser's index is less than parser's token list size.
531 ADA_ASSERT_TRUE(index < tokens.size());
532 // Let next token be parser's token list[parser's index].
533 auto& next_token = tokens[index];
534 // If next token's type is not type return null.
535 if (next_token.type != type) return nullptr;
536 // Increase parser's index by 1.
537 index++;
538 // Return next token.
539 return &next_token;
540}
541
542template <url_pattern_encoding_callback F>
543std::string url_pattern_parser<F>::consume_text() {
544 // Let result be the empty string.
545 std::string result{};
546 // While true:
547 while (true) {
548 // Let token be the result of running try to consume a token given parser
549 // and "char".
550 auto token = try_consume_token(token_type::CHAR);
551 // If token is null, then set token to the result of running try to consume
552 // a token given parser and "escaped-char".
553 if (!token) token = try_consume_token(token_type::ESCAPED_CHAR);
554 // If token is null, then break.
555 if (!token) break;
556 // Append token's value to the end of result.
557 result.append(token->value);
558 }
559 // Return result.
560 return result;
561}
562
563template <url_pattern_encoding_callback F>
564bool url_pattern_parser<F>::consume_required_token(token_type type) {
565 ada_log("url_pattern_parser::consume_required_token called with type=",
566 to_string(type));
567 // Let result be the result of running try to consume a token given parser and
568 // type.
569 return try_consume_token(type) != nullptr;
570}
571
572template <url_pattern_encoding_callback F>
573std::optional<errors>
574url_pattern_parser<F>::maybe_add_part_from_the_pending_fixed_value() {
575 // If parser's pending fixed value is the empty string, then return.
576 if (pending_fixed_value.empty()) {
577 ada_log("pending_fixed_value is empty");
578 return std::nullopt;
579 }
580 // Let encoded value be the result of running parser's encoding callback given
581 // parser's pending fixed value.
582 auto encoded_value = encoding_callback(pending_fixed_value);
583 if (!encoded_value) {
584 ada_log("failed to encode pending_fixed_value: ", pending_fixed_value);
585 return encoded_value.error();
586 }
587 // Set parser's pending fixed value to the empty string.
588 pending_fixed_value.clear();
589 // Let part be a new part whose type is "fixed-text", value is encoded value,
590 // and modifier is "none".
591 // Append part to parser's part list.
592 parts.emplace_back(url_pattern_part_type::FIXED_TEXT,
593 std::move(*encoded_value),
594 url_pattern_part_modifier::none);
595 return std::nullopt;
596}
597
598template <url_pattern_encoding_callback F>
599std::optional<errors> url_pattern_parser<F>::add_part(
600 std::string_view prefix, token* name_token, token* regexp_or_wildcard_token,
601 std::string_view suffix, token* modifier_token) {
602 // Let modifier be "none".
603 auto modifier = url_pattern_part_modifier::none;
604 // If modifier token is not null:
605 if (modifier_token) {
606 // If modifier token's value is "?" then set modifier to "optional".
607 if (modifier_token->value == "?") {
608 modifier = url_pattern_part_modifier::optional;
609 } else if (modifier_token->value == "*") {
610 // Otherwise if modifier token's value is "*" then set modifier to
611 // "zero-or-more".
612 modifier = url_pattern_part_modifier::zero_or_more;
613 } else if (modifier_token->value == "+") {
614 // Otherwise if modifier token's value is "+" then set modifier to
615 // "one-or-more".
616 modifier = url_pattern_part_modifier::one_or_more;
617 }
618 }
619 // If name token is null and regexp or wildcard token is null and modifier
620 // is "none":
621 if (!name_token && !regexp_or_wildcard_token &&
622 modifier == url_pattern_part_modifier::none) {
623 // Append prefix to the end of parser's pending fixed value.
624 pending_fixed_value.append(prefix);
625 return std::nullopt;
626 }
627 // Run maybe add a part from the pending fixed value given parser.
628 if (auto error = maybe_add_part_from_the_pending_fixed_value()) {
629 return *error;
630 }
631 // If name token is null and regexp or wildcard token is null:
632 if (!name_token && !regexp_or_wildcard_token) {
633 // Assert: suffix is the empty string.
634 ADA_ASSERT_TRUE(suffix.empty());
635 // If prefix is the empty string, then return.
636 if (prefix.empty()) return std::nullopt;
637 // Let encoded value be the result of running parser's encoding callback
638 // given prefix.
639 auto encoded_value = encoding_callback(prefix);
640 if (!encoded_value) {
641 return encoded_value.error();
642 }
643 // Let part be a new part whose type is "fixed-text", value is encoded
644 // value, and modifier is modifier.
645 // Append part to parser's part list.
646 parts.emplace_back(url_pattern_part_type::FIXED_TEXT,
647 std::move(*encoded_value), modifier);
648 return std::nullopt;
649 }
650 // Let regexp value be the empty string.
651 std::string regexp_value{};
652 // If regexp or wildcard token is null, then set regexp value to parser's
653 // segment wildcard regexp.
654 if (!regexp_or_wildcard_token) {
655 regexp_value = segment_wildcard_regexp;
656 } else if (regexp_or_wildcard_token->type == token_type::ASTERISK) {
657 // Otherwise if regexp or wildcard token's type is "asterisk", then set
658 // regexp value to the full wildcard regexp value.
659 regexp_value = ".*";
660 } else {
661 // Otherwise set regexp value to regexp or wildcard token's value.
662 regexp_value = regexp_or_wildcard_token->value;
663 }
664 // Let type be "regexp".
665 auto type = url_pattern_part_type::REGEXP;
666 // If regexp value is parser's segment wildcard regexp:
667 if (regexp_value == segment_wildcard_regexp) {
668 // Set type to "segment-wildcard".
669 type = url_pattern_part_type::SEGMENT_WILDCARD;
670 // Set regexp value to the empty string.
671 regexp_value.clear();
672 } else if (regexp_value == ".*") {
673 // Otherwise if regexp value is the full wildcard regexp value:
674 // Set type to "full-wildcard".
675 type = url_pattern_part_type::FULL_WILDCARD;
676 // Set regexp value to the empty string.
677 regexp_value.clear();
678 }
679 // Let name be the empty string.
680 std::string name{};
681 // If name token is not null, then set name to name token's value.
682 if (name_token) {
683 name = name_token->value;
684 } else if (regexp_or_wildcard_token != nullptr) {
685 // Otherwise if regexp or wildcard token is not null:
686 // Set name to parser's next numeric name, serialized.
687 name = std::to_string(next_numeric_name);
688 // Increment parser's next numeric name by 1.
689 next_numeric_name++;
690 }
691 // If the result of running is a duplicate name given parser and name is
692 // true, then throw a TypeError. Tracked in a set so this stays O(1): a
693 // linear scan of the part list here is O(n^2) over a pattern string with n
694 // named parts (e.g. "/*" repeated), an algorithmic-complexity DoS.
695 if (!name.empty() && !seen_names.insert(name).second) {
696 return errors::type_error;
697 }
698 // Let encoded prefix be the result of running parser's encoding callback
699 // given prefix.
700 auto encoded_prefix = encoding_callback(prefix);
701 if (!encoded_prefix) return encoded_prefix.error();
702 // Let encoded suffix be the result of running parser's encoding callback
703 // given suffix.
704 auto encoded_suffix = encoding_callback(suffix);
705 if (!encoded_suffix) return encoded_suffix.error();
706 // Let part be a new part whose type is type, value is regexp value,
707 // modifier is modifier, name is name, prefix is encoded prefix, and suffix
708 // is encoded suffix.
709 // Append part to parser's part list.
710 parts.emplace_back(type, std::move(regexp_value), modifier, std::move(name),
711 std::move(*encoded_prefix), std::move(*encoded_suffix));
712 return std::nullopt;
713}
714
715template <url_pattern_encoding_callback F>
716tl::expected<std::vector<url_pattern_part>, errors> parse_pattern_string(
717 std::string_view input, url_pattern_compile_component_options& options,
718 F& encoding_callback) {
719 ada_log("parse_pattern_string input=", input);
720 // Let parser be a new pattern parser whose encoding callback is encoding
721 // callback and segment wildcard regexp is the result of running generate a
722 // segment wildcard regexp given options.
723 auto parser = url_pattern_parser<F>(
724 encoding_callback, generate_segment_wildcard_regexp(options));
725 // Set parser's token list to the result of running tokenize given input and
726 // "strict".
727 auto tokenize_result = tokenize(input, token_policy::strict);
728 if (!tokenize_result) {
729 ada_log("parse_pattern_string tokenize failed");
730 return tl::unexpected(tokenize_result.error());
731 }
732 parser.tokens = std::move(*tokenize_result);
733
734 // While parser's index is less than parser's token list's size:
735 while (parser.can_continue()) {
736 // Let char token be the result of running try to consume a token given
737 // parser and "char".
738 auto char_token = parser.try_consume_token(token_type::CHAR);
739 // Let name token be the result of running try to consume a token given
740 // parser and "name".
741 auto name_token = parser.try_consume_token(token_type::NAME);
742 // Let regexp or wildcard token be the result of running try to consume a
743 // regexp or wildcard token given parser and name token.
744 auto regexp_or_wildcard_token =
745 parser.try_consume_regexp_or_wildcard_token(name_token);
746 // If name token is not null or regexp or wildcard token is not null:
747 if (name_token || regexp_or_wildcard_token) {
748 // Let prefix be the empty string.
749 std::string prefix{};
750 // If char token is not null then set prefix to char token's value.
751 if (char_token) prefix = char_token->value;
752 // If prefix is not the empty string and not options's prefix code point:
753 if (!prefix.empty() && prefix != options.get_prefix()) {
754 // Append prefix to the end of parser's pending fixed value.
755 parser.pending_fixed_value.append(prefix);
756 // Set prefix to the empty string.
757 prefix.clear();
758 }
759 // Run maybe add a part from the pending fixed value given parser.
760 if (auto error = parser.maybe_add_part_from_the_pending_fixed_value()) {
761 ada_log("maybe_add_part_from_the_pending_fixed_value failed");
762 return tl::unexpected(*error);
763 }
764 // Let modifier token be the result of running try to consume a modifier
765 // token given parser.
766 auto modifier_token = parser.try_consume_modifier_token();
767 // Run add a part given parser, prefix, name token, regexp or wildcard
768 // token, the empty string, and modifier token.
769 if (auto error =
770 parser.add_part(prefix, name_token, regexp_or_wildcard_token, "",
771 modifier_token)) {
772 ada_log("parser.add_part failed");
773 return tl::unexpected(*error);
774 }
775 // Continue.
776 continue;
777 }
778
779 // Let fixed token be char token.
780 auto fixed_token = char_token;
781 // If fixed token is null, then set fixed token to the result of running try
782 // to consume a token given parser and "escaped-char".
783 if (!fixed_token)
784 fixed_token = parser.try_consume_token(token_type::ESCAPED_CHAR);
785 // If fixed token is not null:
786 if (fixed_token) {
787 // Append fixed token's value to parser's pending fixed value.
788 parser.pending_fixed_value.append(fixed_token->value);
789 // Continue.
790 continue;
791 }
792 // Let open token be the result of running try to consume a token given
793 // parser and "open".
794 auto open_token = parser.try_consume_token(token_type::OPEN);
795 // If open token is not null:
796 if (open_token) {
797 // Set prefix be the result of running consume text given parser.
798 auto prefix_ = parser.consume_text();
799 // Set name token to the result of running try to consume a token given
800 // parser and "name".
801 name_token = parser.try_consume_token(token_type::NAME);
802 // Set regexp or wildcard token to the result of running try to consume a
803 // regexp or wildcard token given parser and name token.
804 regexp_or_wildcard_token =
805 parser.try_consume_regexp_or_wildcard_token(name_token);
806 // Let suffix be the result of running consume text given parser.
807 auto suffix_ = parser.consume_text();
808 // Run consume a required token given parser and "close".
809 if (!parser.consume_required_token(token_type::CLOSE)) {
810 ada_log("parser.consume_required_token failed");
811 return tl::unexpected(errors::type_error);
812 }
813 // Set modifier token to the result of running try to consume a modifier
814 // token given parser.
815 auto modifier_token = parser.try_consume_modifier_token();
816 // Run add a part given parser, prefix, name token, regexp or wildcard
817 // token, suffix, and modifier token.
818 if (auto error =
819 parser.add_part(prefix_, name_token, regexp_or_wildcard_token,
820 suffix_, modifier_token)) {
821 return tl::unexpected(*error);
822 }
823 // Continue.
824 continue;
825 }
826 // Run maybe add a part from the pending fixed value given parser.
827 if (auto error = parser.maybe_add_part_from_the_pending_fixed_value()) {
828 ada_log("maybe_add_part_from_the_pending_fixed_value failed on line 992");
829 return tl::unexpected(*error);
830 }
831 // Run consume a required token given parser and "end".
832 if (!parser.consume_required_token(token_type::END)) {
833 return tl::unexpected(errors::type_error);
834 }
835 }
836 ada_log("parser.parts size is: ", parser.parts.size());
837 // Return parser's part list.
838 return parser.parts;
839}
840
841template <url_pattern_regex::regex_concept regex_provider>
842bool protocol_component_matches_special_scheme(
843 url_pattern_component<regex_provider>& component) {
844 // Optimization: Use fast_test for simple patterns to avoid regex overhead
845 switch (component.type) {
846 case url_pattern_component_type::EMPTY:
847 // Empty pattern can't match any special scheme
848 return false;
849 case url_pattern_component_type::EXACT_MATCH:
850 // Direct string comparison for exact match patterns
851 return component.exact_match_value == "http" ||
852 component.exact_match_value == "https" ||
853 component.exact_match_value == "ws" ||
854 component.exact_match_value == "wss" ||
855 component.exact_match_value == "ftp" ||
856 component.exact_match_value == "file";
857 case url_pattern_component_type::FULL_WILDCARD:
858 // Full wildcard matches everything including special schemes
859 return true;
860 case url_pattern_component_type::REGEXP:
861 // Fall back to regex matching for complex patterns
862 auto& regex = component.regexp;
863 return regex_provider::regex_match("http", regex) ||
864 regex_provider::regex_match("https", regex) ||
865 regex_provider::regex_match("ws", regex) ||
866 regex_provider::regex_match("wss", regex) ||
867 regex_provider::regex_match("ftp", regex) ||
868 regex_provider::regex_match("file", regex);
869 }
871}
872
873template <url_pattern_regex::regex_concept regex_provider>
874inline std::optional<errors> constructor_string_parser<
875 regex_provider>::compute_protocol_matches_special_scheme_flag() {
876 ada_log(
877 "constructor_string_parser::compute_protocol_matches_special_scheme_"
878 "flag");
879 // Let protocol string be the result of running make a component string given
880 // parser.
881 auto protocol_string = make_component_string();
882 // Let protocol component be the result of compiling a component given
883 // protocol string, canonicalize a protocol, and default options.
884 auto protocol_component = url_pattern_component<regex_provider>::compile(
885 protocol_string, canonicalize_protocol,
886 url_pattern_compile_component_options::DEFAULT);
887 if (!protocol_component) {
888 ada_log("url_pattern_component::compile failed for protocol_string ",
889 protocol_string);
890 return protocol_component.error();
891 }
892 // If the result of running protocol component matches a special scheme given
893 // protocol component is true, then set parser's protocol matches a special
894 // scheme flag to true.
895 if (protocol_component_matches_special_scheme(*protocol_component)) {
896 protocol_matches_a_special_scheme_flag = true;
897 }
898 return std::nullopt;
899}
900
901template <url_pattern_regex::regex_concept regex_provider>
902tl::expected<url_pattern_init, errors>
903constructor_string_parser<regex_provider>::parse(std::string_view input) {
904 ada_log("constructor_string_parser::parse input=", input);
905 // Let parser be a new constructor string parser whose input is input and
906 // token list is the result of running tokenize given input and "lenient".
907 auto token_list = tokenize(input, token_policy::lenient);
908 if (!token_list) {
909 return tl::unexpected(token_list.error());
910 }
911 auto parser = constructor_string_parser(input, std::move(*token_list));
912
913 // While parser's token index is less than parser's token list size:
914 while (parser.token_index < parser.token_list.size()) {
915 // Set parser's token increment to 1.
916 parser.token_increment = 1;
917
918 // If parser's token list[parser's token index]'s type is "end" then:
919 if (parser.token_list[parser.token_index].type == token_type::END) {
920 // If parser's state is "init":
921 if (parser.state == State::INIT) {
922 // Run rewind given parser.
923 parser.rewind();
924 // If the result of running is a hash prefix given parser is true, then
925 // run change state given parser, "hash" and 1.
926 if (parser.is_hash_prefix()) {
927 parser.change_state(State::HASH, 1);
928 } else if (parser.is_search_prefix()) {
929 // Otherwise if the result of running is a search prefix given parser
930 // is true: Run change state given parser, "search" and 1.
931 parser.change_state(State::SEARCH, 1);
932 } else {
933 // Run change state given parser, "pathname" and 0.
934 parser.change_state(State::PATHNAME, 0);
935 }
936 // Increment parser's token index by parser's token increment.
937 parser.token_index += parser.token_increment;
938 // Continue.
939 continue;
940 }
941
942 if (parser.state == State::AUTHORITY) {
943 // If parser's state is "authority":
944 // Run rewind and set state given parser, and "hostname".
945 parser.rewind();
946 parser.change_state(State::HOSTNAME, 0);
947 // Increment parser's token index by parser's token increment.
948 parser.token_index += parser.token_increment;
949 // Continue.
950 continue;
951 }
952
953 // Run change state given parser, "done" and 0.
954 parser.change_state(State::DONE, 0);
955 // Break.
956 break;
957 }
958
959 // If the result of running is a group open given parser is true:
960 if (parser.is_group_open()) {
961 // Increment parser's group depth by 1.
962 parser.group_depth += 1;
963 // Increment parser's token index by parser's token increment.
964 parser.token_index += parser.token_increment;
965 // Continue.
966 continue;
967 }
968
969 // If parser's group depth is greater than 0:
970 if (parser.group_depth > 0) {
971 // If the result of running is a group close given parser is true, then
972 // decrement parser's group depth by 1.
973 if (parser.is_group_close()) {
974 parser.group_depth -= 1;
975 } else {
976 // Increment parser's token index by parser's token increment.
977 parser.token_index += parser.token_increment;
978 continue;
979 }
980 }
981
982 // Switch on parser's state and run the associated steps:
983 switch (parser.state) {
984 case State::INIT: {
985 // If the result of running is a protocol suffix given parser is true:
986 if (parser.is_protocol_suffix()) {
987 // Run rewind and set state given parser and "protocol".
988 parser.rewind();
989 parser.change_state(State::PROTOCOL, 0);
990 }
991 break;
992 }
993 case State::PROTOCOL: {
994 // If the result of running is a protocol suffix given parser is true:
995 if (parser.is_protocol_suffix()) {
996 // Run compute protocol matches a special scheme flag given parser.
997 if (const auto error =
998 parser.compute_protocol_matches_special_scheme_flag()) {
999 ada_log("compute_protocol_matches_special_scheme_flag failed");
1000 return tl::unexpected(*error);
1001 }
1002 // Let next state be "pathname".
1003 auto next_state = State::PATHNAME;
1004 // Let skip be 1.
1005 auto skip = 1;
1006 // If the result of running next is authority slashes given parser is
1007 // true:
1008 if (parser.next_is_authority_slashes()) {
1009 // Set next state to "authority".
1010 next_state = State::AUTHORITY;
1011 // Set skip to 3.
1012 skip = 3;
1013 } else if (parser.protocol_matches_a_special_scheme_flag) {
1014 // Otherwise if parser's protocol matches a special scheme flag is
1015 // true, then set next state to "authority".
1016 next_state = State::AUTHORITY;
1017 }
1018
1019 // Run change state given parser, next state, and skip.
1020 parser.change_state(next_state, skip);
1021 }
1022 break;
1023 }
1024 case State::AUTHORITY: {
1025 // If the result of running is an identity terminator given parser is
1026 // true, then run rewind and set state given parser and "username".
1027 if (parser.is_an_identity_terminator()) {
1028 parser.rewind();
1029 parser.change_state(State::USERNAME, 0);
1030 } else if (parser.is_pathname_start() || parser.is_search_prefix() ||
1031 parser.is_hash_prefix()) {
1032 // Otherwise if any of the following are true:
1033 // - the result of running is a pathname start given parser;
1034 // - the result of running is a search prefix given parser; or
1035 // - the result of running is a hash prefix given parser,
1036 // then run rewind and set state given parser and "hostname".
1037 parser.rewind();
1038 parser.change_state(State::HOSTNAME, 0);
1039 }
1040 break;
1041 }
1042 case State::USERNAME: {
1043 // If the result of running is a password prefix given parser is true,
1044 // then run change state given parser, "password", and 1.
1045 if (parser.is_password_prefix()) {
1046 parser.change_state(State::PASSWORD, 1);
1047 } else if (parser.is_an_identity_terminator()) {
1048 // Otherwise if the result of running is an identity terminator given
1049 // parser is true, then run change state given parser, "hostname",
1050 // and 1.
1051 parser.change_state(State::HOSTNAME, 1);
1052 }
1053 break;
1054 }
1055 case State::PASSWORD: {
1056 // If the result of running is an identity terminator given parser is
1057 // true, then run change state given parser, "hostname", and 1.
1058 if (parser.is_an_identity_terminator()) {
1059 parser.change_state(State::HOSTNAME, 1);
1060 }
1061 break;
1062 }
1063 case State::HOSTNAME: {
1064 // If the result of running is an IPv6 open given parser is true, then
1065 // increment parser's hostname IPv6 bracket depth by 1.
1066 if (parser.is_an_ipv6_open()) {
1067 parser.hostname_ipv6_bracket_depth += 1;
1068 } else if (parser.is_an_ipv6_close()) {
1069 // Otherwise if the result of running is an IPv6 close given parser is
1070 // true, then decrement parser's hostname IPv6 bracket depth by 1.
1071 parser.hostname_ipv6_bracket_depth -= 1;
1072 } else if (parser.is_port_prefix() &&
1073 parser.hostname_ipv6_bracket_depth == 0) {
1074 // Otherwise if the result of running is a port prefix given parser is
1075 // true and parser's hostname IPv6 bracket depth is zero, then run
1076 // change state given parser, "port", and 1.
1077 parser.change_state(State::PORT, 1);
1078 } else if (parser.is_pathname_start()) {
1079 // Otherwise if the result of running is a pathname start given parser
1080 // is true, then run change state given parser, "pathname", and 0.
1081 parser.change_state(State::PATHNAME, 0);
1082 } else if (parser.is_search_prefix()) {
1083 // Otherwise if the result of running is a search prefix given parser
1084 // is true, then run change state given parser, "search", and 1.
1085 parser.change_state(State::SEARCH, 1);
1086 } else if (parser.is_hash_prefix()) {
1087 // Otherwise if the result of running is a hash prefix given parser is
1088 // true, then run change state given parser, "hash", and 1.
1089 parser.change_state(State::HASH, 1);
1090 }
1091
1092 break;
1093 }
1094 case State::PORT: {
1095 // If the result of running is a pathname start given parser is true,
1096 // then run change state given parser, "pathname", and 0.
1097 if (parser.is_pathname_start()) {
1098 parser.change_state(State::PATHNAME, 0);
1099 } else if (parser.is_search_prefix()) {
1100 // Otherwise if the result of running is a search prefix given parser
1101 // is true, then run change state given parser, "search", and 1.
1102 parser.change_state(State::SEARCH, 1);
1103 } else if (parser.is_hash_prefix()) {
1104 // Otherwise if the result of running is a hash prefix given parser is
1105 // true, then run change state given parser, "hash", and 1.
1106 parser.change_state(State::HASH, 1);
1107 }
1108 break;
1109 }
1110 case State::PATHNAME: {
1111 // If the result of running is a search prefix given parser is true,
1112 // then run change state given parser, "search", and 1.
1113 if (parser.is_search_prefix()) {
1114 parser.change_state(State::SEARCH, 1);
1115 } else if (parser.is_hash_prefix()) {
1116 // Otherwise if the result of running is a hash prefix given parser is
1117 // true, then run change state given parser, "hash", and 1.
1118 parser.change_state(State::HASH, 1);
1119 }
1120 break;
1121 }
1122 case State::SEARCH: {
1123 // If the result of running is a hash prefix given parser is true, then
1124 // run change state given parser, "hash", and 1.
1125 if (parser.is_hash_prefix()) {
1126 parser.change_state(State::HASH, 1);
1127 }
1128 break;
1129 }
1130 case State::HASH: {
1131 // Do nothing
1132 break;
1133 }
1134 default: {
1135 // Assert: This step is never reached.
1136 unreachable();
1137 }
1138 }
1139
1140 // Increment parser's token index by parser's token increment.
1141 parser.token_index += parser.token_increment;
1142 }
1143
1144 // If parser's result contains "hostname" and not "port", then set parser's
1145 // result["port"] to the empty string.
1146 if (parser.result.hostname && !parser.result.port) {
1147 parser.result.port = "";
1148 }
1149
1150 // Return parser's result.
1151 return parser.result;
1152}
1153
1154} // namespace ada::url_pattern_helpers
1155#endif // ADA_INCLUDE_URL_PATTERN
1156#endif
Cross-platform compiler macros and common definitions.
#define ADA_ASSERT_TRUE(COND)
#define ada_warn_unused
Definition common_defs.h:89
User-facing functions for URL parsing and manipulation.
type
Enumeration of URL scheme types.
Definition scheme.h:43
errors
Error codes for URL parsing operations.
Definition errors.h:17
state
States in the URL parsing state machine.
Definition state.h:27
ada_warn_unused std::string_view to_string(encoding_type type)
void unreachable()
tl::expected< result_type, ada::errors > result
ada::url_pattern_regex::std_regex_provider regex_provider
Definition url_pattern.cc:9
Declaration for the URLPattern helpers.