Ada 3.1.0
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_aggregator-inl.h
Go to the documentation of this file.
1
5#ifndef ADA_URL_AGGREGATOR_INL_H
6#define ADA_URL_AGGREGATOR_INL_H
7
9#include "ada/helpers.h"
10#include "ada/unicode-inl.h"
11#include "ada/url_aggregator.h"
12#include "ada/url_components.h"
13#include "ada/scheme.h"
14#include "ada/log.h"
15
16#include <charconv>
17#include <ostream>
18#include <string_view>
19
20namespace ada {
21
22inline void url_aggregator::update_base_authority(
23 std::string_view base_buffer, const ada::url_components &base) {
24 std::string_view input = base_buffer.substr(
25 base.protocol_end, base.host_start - base.protocol_end);
26 ada_log("url_aggregator::update_base_authority ", input);
27
28 bool input_starts_with_dash = input.starts_with("//");
29 uint32_t diff = components.host_start - components.protocol_end;
30
31 buffer.erase(components.protocol_end,
32 components.host_start - components.protocol_end);
33 components.username_end = components.protocol_end;
34
35 if (input_starts_with_dash) {
36 input.remove_prefix(2);
37 diff += 2; // add "//"
38 buffer.insert(components.protocol_end, "//");
39 components.username_end += 2;
40 }
41
42 size_t password_delimiter = input.find(':');
43
44 // Check if input contains both username and password by checking the
45 // delimiter: ":" A typical input that contains authority would be "user:pass"
46 if (password_delimiter != std::string_view::npos) {
47 // Insert both username and password
48 std::string_view username = input.substr(0, password_delimiter);
49 std::string_view password = input.substr(password_delimiter + 1);
50
51 buffer.insert(components.protocol_end + diff, username);
52 diff += uint32_t(username.size());
53 buffer.insert(components.protocol_end + diff, ":");
54 components.username_end = components.protocol_end + diff;
55 buffer.insert(components.protocol_end + diff + 1, password);
56 diff += uint32_t(password.size()) + 1;
57 } else if (!input.empty()) {
58 // Insert only username
59 buffer.insert(components.protocol_end + diff, input);
60 components.username_end =
61 components.protocol_end + diff + uint32_t(input.size());
62 diff += uint32_t(input.size());
63 }
64
65 components.host_start += diff;
66
67 if (buffer.size() > base.host_start && buffer[base.host_start] != '@') {
68 buffer.insert(components.host_start, "@");
69 diff++;
70 }
71 components.host_end += diff;
72 components.pathname_start += diff;
73 if (components.search_start != url_components::omitted) {
74 components.search_start += diff;
75 }
76 if (components.hash_start != url_components::omitted) {
77 components.hash_start += diff;
78 }
79}
80
81inline void url_aggregator::update_unencoded_base_hash(std::string_view input) {
82 ada_log("url_aggregator::update_unencoded_base_hash ", input, " [",
83 input.size(), " bytes], buffer is '", buffer, "' [", buffer.size(),
84 " bytes] components.hash_start = ", components.hash_start);
86 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
87 if (components.hash_start != url_components::omitted) {
88 buffer.resize(components.hash_start);
89 }
90 components.hash_start = uint32_t(buffer.size());
91 buffer += "#";
92 bool encoding_required = unicode::percent_encode<true>(
94 // When encoding_required is false, then buffer is left unchanged, and percent
95 // encoding was not deemed required.
96 if (!encoding_required) {
97 buffer.append(input);
98 }
99 ada_log("url_aggregator::update_unencoded_base_hash final buffer is '",
100 buffer, "' [", buffer.size(), " bytes]");
102}
103
104ada_really_inline uint32_t url_aggregator::replace_and_resize(
105 uint32_t start, uint32_t end, std::string_view input) {
106 uint32_t current_length = end - start;
107 uint32_t input_size = uint32_t(input.size());
108 uint32_t new_difference = input_size - current_length;
109
110 if (current_length == 0) {
111 buffer.insert(start, input);
112 } else if (input_size == current_length) {
113 buffer.replace(start, input_size, input);
114 } else if (input_size < current_length) {
115 buffer.erase(start, current_length - input_size);
116 buffer.replace(start, input_size, input);
117 } else {
118 buffer.replace(start, current_length, input.substr(0, current_length));
119 buffer.insert(start + current_length, input.substr(current_length));
120 }
121
122 return new_difference;
123}
124
125inline void url_aggregator::update_base_hostname(const std::string_view input) {
126 ada_log("url_aggregator::update_base_hostname ", input, " [", input.size(),
127 " bytes], buffer is '", buffer, "' [", buffer.size(), " bytes]");
129 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
130
131 // This next line is required for when parsing a URL like `foo://`
132 add_authority_slashes_if_needed();
133
134 bool has_credentials = components.protocol_end + 2 < components.host_start;
135 uint32_t new_difference =
136 replace_and_resize(components.host_start, components.host_end, input);
137
138 if (has_credentials) {
139 buffer.insert(components.host_start, "@");
140 new_difference++;
141 }
142 components.host_end += new_difference;
143 components.pathname_start += new_difference;
144 if (components.search_start != url_components::omitted) {
145 components.search_start += new_difference;
146 }
147 if (components.hash_start != url_components::omitted) {
148 components.hash_start += new_difference;
149 }
151}
152
153[[nodiscard]] ada_really_inline uint32_t
155 ada_log("url_aggregator::get_pathname_length");
156 uint32_t ending_index = uint32_t(buffer.size());
157 if (components.search_start != url_components::omitted) {
158 ending_index = components.search_start;
159 } else if (components.hash_start != url_components::omitted) {
160 ending_index = components.hash_start;
161 }
162 return ending_index - components.pathname_start;
163}
164
165[[nodiscard]] ada_really_inline bool url_aggregator::is_at_path()
166 const noexcept {
167 return buffer.size() == components.pathname_start;
168}
169
170inline void url_aggregator::update_base_search(std::string_view input) {
171 ada_log("url_aggregator::update_base_search ", input);
173 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
174 if (input.empty()) {
175 clear_search();
176 return;
177 }
178
179 if (input[0] == '?') {
180 input.remove_prefix(1);
181 }
182
183 if (components.hash_start == url_components::omitted) {
184 if (components.search_start == url_components::omitted) {
185 components.search_start = uint32_t(buffer.size());
186 buffer += "?";
187 } else {
188 buffer.resize(components.search_start + 1);
189 }
190
191 buffer.append(input);
192 } else {
193 if (components.search_start == url_components::omitted) {
194 components.search_start = components.hash_start;
195 } else {
196 buffer.erase(components.search_start,
197 components.hash_start - components.search_start);
198 components.hash_start = components.search_start;
199 }
200
201 buffer.insert(components.search_start, "?");
202 buffer.insert(components.search_start + 1, input);
203 components.hash_start += uint32_t(input.size() + 1); // Do not forget `?`
204 }
205
207}
208
209inline void url_aggregator::update_base_search(
210 std::string_view input, const uint8_t query_percent_encode_set[]) {
211 ada_log("url_aggregator::update_base_search ", input,
212 " with encoding parameter ", to_string(), "\n", to_diagram());
214 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
215
216 if (components.hash_start == url_components::omitted) {
217 if (components.search_start == url_components::omitted) {
218 components.search_start = uint32_t(buffer.size());
219 buffer += "?";
220 } else {
221 buffer.resize(components.search_start + 1);
222 }
223
224 bool encoding_required =
225 unicode::percent_encode<true>(input, query_percent_encode_set, buffer);
226 // When encoding_required is false, then buffer is left unchanged, and
227 // percent encoding was not deemed required.
228 if (!encoding_required) {
229 buffer.append(input);
230 }
231 } else {
232 if (components.search_start == url_components::omitted) {
233 components.search_start = components.hash_start;
234 } else {
235 buffer.erase(components.search_start,
236 components.hash_start - components.search_start);
237 components.hash_start = components.search_start;
238 }
239
240 buffer.insert(components.search_start, "?");
241 size_t idx =
242 ada::unicode::percent_encode_index(input, query_percent_encode_set);
243 if (idx == input.size()) {
244 buffer.insert(components.search_start + 1, input);
245 components.hash_start += uint32_t(input.size() + 1); // Do not forget `?`
246 } else {
247 buffer.insert(components.search_start + 1, input, 0, idx);
248 input.remove_prefix(idx);
249 // We only create a temporary string if we need percent encoding and
250 // we attempt to create as small a temporary string as we can.
251 std::string encoded =
252 ada::unicode::percent_encode(input, query_percent_encode_set);
253 buffer.insert(components.search_start + idx + 1, encoded);
254 components.hash_start +=
255 uint32_t(encoded.size() + idx + 1); // Do not forget `?`
256 }
257 }
258
260}
261
262inline void url_aggregator::update_base_pathname(const std::string_view input) {
263 ada_log("url_aggregator::update_base_pathname '", input, "' [", input.size(),
264 " bytes] \n", to_diagram());
265 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
267
268 const bool begins_with_dashdash = input.starts_with("//");
269 if (!begins_with_dashdash && has_dash_dot()) {
270 // We must delete the ./
271 delete_dash_dot();
272 }
273
274 if (begins_with_dashdash && !has_opaque_path && !has_authority() &&
275 !has_dash_dot()) {
276 // If url's host is null, url does not have an opaque path, url's path's
277 // size is greater than 1, then append U+002F (/) followed by U+002E (.) to
278 // output.
279 buffer.insert(components.pathname_start, "/.");
280 components.pathname_start += 2;
281 }
282
283 uint32_t difference = replace_and_resize(
284 components.pathname_start,
285 components.pathname_start + get_pathname_length(), input);
286 if (components.search_start != url_components::omitted) {
287 components.search_start += difference;
288 }
289 if (components.hash_start != url_components::omitted) {
290 components.hash_start += difference;
291 }
293}
294
295inline void url_aggregator::append_base_pathname(const std::string_view input) {
296 ada_log("url_aggregator::append_base_pathname ", input, " ", to_string(),
297 "\n", to_diagram());
299 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
300#if ADA_DEVELOPMENT_CHECKS
301 // computing the expected password.
302 std::string path_expected(get_pathname());
303 path_expected.append(input);
304#endif // ADA_DEVELOPMENT_CHECKS
305 uint32_t ending_index = uint32_t(buffer.size());
306 if (components.search_start != url_components::omitted) {
307 ending_index = components.search_start;
308 } else if (components.hash_start != url_components::omitted) {
309 ending_index = components.hash_start;
310 }
311 buffer.insert(ending_index, input);
312
313 if (components.search_start != url_components::omitted) {
314 components.search_start += uint32_t(input.size());
315 }
316 if (components.hash_start != url_components::omitted) {
317 components.hash_start += uint32_t(input.size());
318 }
319#if ADA_DEVELOPMENT_CHECKS
320 std::string path_after = std::string(get_pathname());
322 path_expected, path_after,
323 "append_base_pathname problem after inserting " + std::string(input));
324#endif // ADA_DEVELOPMENT_CHECKS
326}
327
328inline void url_aggregator::update_base_username(const std::string_view input) {
329 ada_log("url_aggregator::update_base_username '", input, "' ", to_string(),
330 "\n", to_diagram());
332 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
333
334 add_authority_slashes_if_needed();
335
337 bool host_starts_with_at = buffer.size() > components.host_start &&
338 buffer[components.host_start] == '@';
339 uint32_t diff = replace_and_resize(components.protocol_end + 2,
340 components.username_end, input);
341
342 components.username_end += diff;
343 components.host_start += diff;
344
345 if (!input.empty() && !host_starts_with_at) {
346 buffer.insert(components.host_start, "@");
347 diff++;
348 } else if (input.empty() && host_starts_with_at && !has_password) {
349 // Input is empty, there is no password, and we need to remove "@" from
350 // hostname
351 buffer.erase(components.host_start, 1);
352 diff--;
353 }
354
355 components.host_end += diff;
356 components.pathname_start += diff;
357 if (components.search_start != url_components::omitted) {
358 components.search_start += diff;
359 }
360 if (components.hash_start != url_components::omitted) {
361 components.hash_start += diff;
362 }
364}
365
366inline void url_aggregator::append_base_username(const std::string_view input) {
367 ada_log("url_aggregator::append_base_username ", input);
369 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
370#if ADA_DEVELOPMENT_CHECKS
371 // computing the expected password.
372 std::string username_expected(get_username());
373 username_expected.append(input);
374#endif // ADA_DEVELOPMENT_CHECKS
375 add_authority_slashes_if_needed();
376
377 // If input is empty, do nothing.
378 if (input.empty()) {
379 return;
380 }
381
382 uint32_t difference = uint32_t(input.size());
383 buffer.insert(components.username_end, input);
384 components.username_end += difference;
385 components.host_start += difference;
386
387 if (buffer[components.host_start] != '@' &&
388 components.host_start != components.host_end) {
389 buffer.insert(components.host_start, "@");
390 difference++;
391 }
392
393 components.host_end += difference;
394 components.pathname_start += difference;
395 if (components.search_start != url_components::omitted) {
396 components.search_start += difference;
397 }
398 if (components.hash_start != url_components::omitted) {
399 components.hash_start += difference;
400 }
401#if ADA_DEVELOPMENT_CHECKS
402 std::string username_after(get_username());
404 username_expected, username_after,
405 "append_base_username problem after inserting " + std::string(input));
406#endif // ADA_DEVELOPMENT_CHECKS
408}
409
410constexpr void url_aggregator::clear_password() {
411 ada_log("url_aggregator::clear_password ", to_string());
413 if (!has_password()) {
414 return;
415 }
416
417 uint32_t diff = components.host_start - components.username_end;
418 buffer.erase(components.username_end, diff);
419 components.host_start -= diff;
420 components.host_end -= diff;
421 components.pathname_start -= diff;
422 if (components.search_start != url_components::omitted) {
423 components.search_start -= diff;
424 }
425 if (components.hash_start != url_components::omitted) {
426 components.hash_start -= diff;
427 }
428}
429
430inline void url_aggregator::update_base_password(const std::string_view input) {
431 ada_log("url_aggregator::update_base_password ", input);
433 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
434
435 add_authority_slashes_if_needed();
436
437 // TODO: Optimization opportunity. Merge the following removal functions.
438 if (input.empty()) {
439 clear_password();
440
441 // Remove username too, if it is empty.
442 if (!has_non_empty_username()) {
443 update_base_username("");
444 }
445
446 return;
447 }
448
449 bool password_exists = has_password();
450 uint32_t difference = uint32_t(input.size());
451
452 if (password_exists) {
453 uint32_t current_length =
454 components.host_start - components.username_end - 1;
455 buffer.erase(components.username_end + 1, current_length);
456 difference -= current_length;
457 } else {
458 buffer.insert(components.username_end, ":");
459 difference++;
460 }
461
462 buffer.insert(components.username_end + 1, input);
463 components.host_start += difference;
464
465 // The following line is required to add "@" to hostname. When updating
466 // password if hostname does not start with "@", it is "update_base_password"s
467 // responsibility to set it.
468 if (buffer[components.host_start] != '@') {
469 buffer.insert(components.host_start, "@");
470 difference++;
471 }
472
473 components.host_end += difference;
474 components.pathname_start += difference;
475 if (components.search_start != url_components::omitted) {
476 components.search_start += difference;
477 }
478 if (components.hash_start != url_components::omitted) {
479 components.hash_start += difference;
480 }
482}
483
484inline void url_aggregator::append_base_password(const std::string_view input) {
485 ada_log("url_aggregator::append_base_password ", input, " ", to_string(),
486 "\n", to_diagram());
488 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
489#if ADA_DEVELOPMENT_CHECKS
490 // computing the expected password.
491 std::string password_expected = std::string(get_password());
492 password_expected.append(input);
493#endif // ADA_DEVELOPMENT_CHECKS
494 add_authority_slashes_if_needed();
495
496 // If input is empty, do nothing.
497 if (input.empty()) {
498 return;
499 }
500
501 uint32_t difference = uint32_t(input.size());
502 if (has_password()) {
503 buffer.insert(components.host_start, input);
504 } else {
505 difference++; // Increment for ":"
506 buffer.insert(components.username_end, ":");
507 buffer.insert(components.username_end + 1, input);
508 }
509 components.host_start += difference;
510
511 // The following line is required to add "@" to hostname. When updating
512 // password if hostname does not start with "@", it is "append_base_password"s
513 // responsibility to set it.
514 if (buffer[components.host_start] != '@') {
515 buffer.insert(components.host_start, "@");
516 difference++;
517 }
518
519 components.host_end += difference;
520 components.pathname_start += difference;
521 if (components.search_start != url_components::omitted) {
522 components.search_start += difference;
523 }
524 if (components.hash_start != url_components::omitted) {
525 components.hash_start += difference;
526 }
527#if ADA_DEVELOPMENT_CHECKS
528 std::string password_after(get_password());
530 password_expected, password_after,
531 "append_base_password problem after inserting " + std::string(input));
532#endif // ADA_DEVELOPMENT_CHECKS
534}
535
536inline void url_aggregator::update_base_port(uint32_t input) {
537 ada_log("url_aggregator::update_base_port");
539 if (input == url_components::omitted) {
540 clear_port();
541 return;
542 }
543 // calling std::to_string(input.value()) is unfortunate given that the port
544 // value is probably already available as a string.
545 std::string value = helpers::concat(":", std::to_string(input));
546 uint32_t difference = uint32_t(value.size());
547
548 if (components.port != url_components::omitted) {
549 difference -= components.pathname_start - components.host_end;
550 buffer.erase(components.host_end,
551 components.pathname_start - components.host_end);
552 }
553
554 buffer.insert(components.host_end, value);
555 components.pathname_start += difference;
556 if (components.search_start != url_components::omitted) {
557 components.search_start += difference;
558 }
559 if (components.hash_start != url_components::omitted) {
560 components.hash_start += difference;
561 }
562 components.port = input;
564}
565
567 ada_log("url_aggregator::clear_port");
569 if (components.port == url_components::omitted) {
570 return;
571 }
572 uint32_t length = components.pathname_start - components.host_end;
573 buffer.erase(components.host_end, length);
574 components.pathname_start -= length;
575 if (components.search_start != url_components::omitted) {
576 components.search_start -= length;
577 }
578 if (components.hash_start != url_components::omitted) {
579 components.hash_start -= length;
580 }
581 components.port = url_components::omitted;
583}
584
585[[nodiscard]] inline uint32_t url_aggregator::retrieve_base_port() const {
586 ada_log("url_aggregator::retrieve_base_port");
587 return components.port;
588}
589
591 ada_log("url_aggregator::clear_search");
593 if (components.search_start == url_components::omitted) {
594 return;
595 }
596
597 if (components.hash_start == url_components::omitted) {
598 buffer.resize(components.search_start);
599 } else {
600 buffer.erase(components.search_start,
601 components.hash_start - components.search_start);
602 components.hash_start = components.search_start;
603 }
604
605 components.search_start = url_components::omitted;
606
607#if ADA_DEVELOPMENT_CHECKS
609 "search should have been cleared on buffer=" + buffer +
610 " with " + components.to_string() + "\n" + to_diagram());
611#endif
613}
614
616 ada_log("url_aggregator::clear_hash");
618 if (components.hash_start == url_components::omitted) {
619 return;
620 }
621 buffer.resize(components.hash_start);
622 components.hash_start = url_components::omitted;
623
624#if ADA_DEVELOPMENT_CHECKS
626 "hash should have been cleared on buffer=" + buffer +
627 " with " + components.to_string() + "\n" + to_diagram());
628#endif
630}
631
632constexpr void url_aggregator::clear_pathname() {
633 ada_log("url_aggregator::clear_pathname");
635 uint32_t ending_index = uint32_t(buffer.size());
636 if (components.search_start != url_components::omitted) {
637 ending_index = components.search_start;
638 } else if (components.hash_start != url_components::omitted) {
639 ending_index = components.hash_start;
640 }
641 uint32_t pathname_length = ending_index - components.pathname_start;
642 buffer.erase(components.pathname_start, pathname_length);
643 uint32_t difference = pathname_length;
644 if (components.pathname_start == components.host_end + 2 &&
645 buffer[components.host_end] == '/' &&
646 buffer[components.host_end + 1] == '.') {
647 components.pathname_start -= 2;
648 buffer.erase(components.host_end, 2);
649 difference += 2;
650 }
651 if (components.search_start != url_components::omitted) {
652 components.search_start -= difference;
653 }
654 if (components.hash_start != url_components::omitted) {
655 components.hash_start -= difference;
656 }
657 ada_log("url_aggregator::clear_pathname completed, running checks...");
658#if ADA_DEVELOPMENT_CHECKS
660 "pathname should have been cleared on buffer=" + buffer +
661 " with " + components.to_string() + "\n" + to_diagram());
662#endif
664 ada_log("url_aggregator::clear_pathname completed, running checks... ok");
665}
666
667constexpr void url_aggregator::clear_hostname() {
668 ada_log("url_aggregator::clear_hostname");
670 if (!has_authority()) {
671 return;
672 }
673 ADA_ASSERT_TRUE(has_authority());
674
675 uint32_t hostname_length = components.host_end - components.host_start;
676 uint32_t start = components.host_start;
677
678 // If hostname starts with "@", we should not remove that character.
679 if (hostname_length > 0 && buffer[start] == '@') {
680 start++;
681 hostname_length--;
682 }
683 buffer.erase(start, hostname_length);
684 components.host_end = start;
685 components.pathname_start -= hostname_length;
686 if (components.search_start != url_components::omitted) {
687 components.search_start -= hostname_length;
688 }
689 if (components.hash_start != url_components::omitted) {
690 components.hash_start -= hostname_length;
691 }
692#if ADA_DEVELOPMENT_CHECKS
694 "hostname should have been cleared on buffer=" + buffer +
695 " with " + components.to_string() + "\n" + to_diagram());
696#endif
697 ADA_ASSERT_TRUE(has_authority());
699 "hostname should have been cleared on buffer=" + buffer +
700 " with " + components.to_string() + "\n" + to_diagram());
702}
703
704[[nodiscard]] constexpr bool url_aggregator::has_hash() const noexcept {
705 ada_log("url_aggregator::has_hash");
706 return components.hash_start != url_components::omitted;
707}
708
709[[nodiscard]] constexpr bool url_aggregator::has_search() const noexcept {
710 ada_log("url_aggregator::has_search");
711 return components.search_start != url_components::omitted;
712}
713
714constexpr bool url_aggregator::has_credentials() const noexcept {
715 ada_log("url_aggregator::has_credentials");
717}
718
719constexpr bool url_aggregator::cannot_have_credentials_or_port() const {
720 ada_log("url_aggregator::cannot_have_credentials_or_port");
721 return type == ada::scheme::type::FILE ||
722 components.host_start == components.host_end;
723}
724
725[[nodiscard]] ada_really_inline const ada::url_components &
727 return components;
728}
729
730[[nodiscard]] constexpr bool ada::url_aggregator::has_authority()
731 const noexcept {
732 ada_log("url_aggregator::has_authority");
733 // Performance: instead of doing this potentially expensive check, we could
734 // have a boolean in the struct.
735 return components.protocol_end + 2 <= components.host_start &&
736 helpers::substring(buffer, components.protocol_end,
737 components.protocol_end + 2) == "//";
738}
739
740inline void ada::url_aggregator::add_authority_slashes_if_needed() noexcept {
741 ada_log("url_aggregator::add_authority_slashes_if_needed");
742 ADA_ASSERT_TRUE(validate());
743 // Protocol setter will insert `http:` to the URL. It is up to hostname setter
744 // to insert
745 // `//` initially to the buffer, since it depends on the hostname existence.
746 if (has_authority()) {
747 return;
748 }
749 // Performance: the common case is components.protocol_end == buffer.size()
750 // Optimization opportunity: in many cases, the "//" is part of the input and
751 // the insert could be fused with another insert.
752 buffer.insert(components.protocol_end, "//");
753 components.username_end += 2;
754 components.host_start += 2;
755 components.host_end += 2;
756 components.pathname_start += 2;
757 if (components.search_start != url_components::omitted) {
758 components.search_start += 2;
759 }
760 if (components.hash_start != url_components::omitted) {
761 components.hash_start += 2;
762 }
763 ADA_ASSERT_TRUE(validate());
764}
765
766constexpr void ada::url_aggregator::reserve(uint32_t capacity) {
767 buffer.reserve(capacity);
768}
769
770constexpr bool url_aggregator::has_non_empty_username() const noexcept {
771 ada_log("url_aggregator::has_non_empty_username");
772 return components.protocol_end + 2 < components.username_end;
773}
774
775constexpr bool url_aggregator::has_non_empty_password() const noexcept {
776 ada_log("url_aggregator::has_non_empty_password");
777 return components.host_start - components.username_end > 0;
778}
779
780constexpr bool url_aggregator::has_password() const noexcept {
781 ada_log("url_aggregator::has_password");
782 // This function does not care about the length of the password
783 return components.host_start > components.username_end &&
784 buffer[components.username_end] == ':';
785}
786
787constexpr bool url_aggregator::has_empty_hostname() const noexcept {
788 if (!has_hostname()) {
789 return false;
790 }
791 if (components.host_start == components.host_end) {
792 return true;
793 }
794 if (components.host_end > components.host_start + 1) {
795 return false;
796 }
797 return components.username_end != components.host_start;
798}
799
800constexpr bool url_aggregator::has_hostname() const noexcept {
801 return has_authority();
802}
803
804constexpr bool url_aggregator::has_port() const noexcept {
805 ada_log("url_aggregator::has_port");
806 // A URL cannot have a username/password/port if its host is null or the empty
807 // string, or its scheme is "file".
808 return has_hostname() && components.pathname_start != components.host_end;
809}
810
811[[nodiscard]] constexpr bool url_aggregator::has_dash_dot() const noexcept {
812 // If url's host is null, url does not have an opaque path, url's path's size
813 // is greater than 1, and url's path[0] is the empty string, then append
814 // U+002F (/) followed by U+002E (.) to output.
815 ada_log("url_aggregator::has_dash_dot");
816#if ADA_DEVELOPMENT_CHECKS
817 // If pathname_start and host_end are exactly two characters apart, then we
818 // either have a one-digit port such as http://test.com:5?param=1 or else we
819 // have a /.: sequence such as "non-spec:/.//". We test that this is the case.
820 if (components.pathname_start == components.host_end + 2) {
821 ADA_ASSERT_TRUE((buffer[components.host_end] == '/' &&
822 buffer[components.host_end + 1] == '.') ||
823 (buffer[components.host_end] == ':' &&
824 checkers::is_digit(buffer[components.host_end + 1])));
825 }
826 if (components.pathname_start == components.host_end + 2 &&
827 buffer[components.host_end] == '/' &&
828 buffer[components.host_end + 1] == '.') {
829 ADA_ASSERT_TRUE(components.pathname_start + 1 < buffer.size());
830 ADA_ASSERT_TRUE(buffer[components.pathname_start] == '/');
831 ADA_ASSERT_TRUE(buffer[components.pathname_start + 1] == '/');
832 }
833#endif
834 // Performance: it should be uncommon for components.pathname_start ==
835 // components.host_end + 2 to be true. So we put this check first in the
836 // sequence. Most times, we do not have an opaque path. Checking for '/.' is
837 // more expensive, but should be uncommon.
838 return components.pathname_start == components.host_end + 2 &&
839 !has_opaque_path && buffer[components.host_end] == '/' &&
840 buffer[components.host_end + 1] == '.';
841}
842
843[[nodiscard]] constexpr std::string_view url_aggregator::get_href()
844 const noexcept ada_lifetime_bound {
845 ada_log("url_aggregator::get_href");
846 return buffer;
847}
848
849ada_really_inline size_t url_aggregator::parse_port(
850 std::string_view view, bool check_trailing_content) noexcept {
851 ada_log("url_aggregator::parse_port('", view, "') ", view.size());
852 if (!view.empty() && view[0] == '-') {
853 ada_log("parse_port: view[0] == '0' && view.size() > 1");
854 is_valid = false;
855 return 0;
856 }
857 uint16_t parsed_port{};
858 auto r = std::from_chars(view.data(), view.data() + view.size(), parsed_port);
859 if (r.ec == std::errc::result_out_of_range) {
860 ada_log("parse_port: r.ec == std::errc::result_out_of_range");
861 is_valid = false;
862 return 0;
863 }
864 ada_log("parse_port: ", parsed_port);
865 const size_t consumed = size_t(r.ptr - view.data());
866 ada_log("parse_port: consumed ", consumed);
867 if (check_trailing_content) {
868 is_valid &=
869 (consumed == view.size() || view[consumed] == '/' ||
870 view[consumed] == '?' || (is_special() && view[consumed] == '\\'));
871 }
872 ada_log("parse_port: is_valid = ", is_valid);
873 if (is_valid) {
874 ada_log("parse_port", r.ec == std::errc());
875 // scheme_default_port can return 0, and we should allow 0 as a base port.
876 auto default_port = scheme_default_port();
877 bool is_port_valid = (default_port == 0 && parsed_port == 0) ||
878 (default_port != parsed_port);
879 if (r.ec == std::errc() && is_port_valid) {
880 update_base_port(parsed_port);
881 } else {
882 clear_port();
883 }
884 }
885 return consumed;
886}
887
888constexpr void url_aggregator::set_protocol_as_file() {
889 ada_log("url_aggregator::set_protocol_as_file ");
892 // next line could overflow but unsigned arithmetic has well-defined
893 // overflows.
894 uint32_t new_difference = 5 - components.protocol_end;
895
896 if (buffer.empty()) {
897 buffer.append("file:");
898 } else {
899 buffer.erase(0, components.protocol_end);
900 buffer.insert(0, "file:");
901 }
902 components.protocol_end = 5;
903
904 // Update the rest of the components.
905 components.username_end += new_difference;
906 components.host_start += new_difference;
907 components.host_end += new_difference;
908 components.pathname_start += new_difference;
909 if (components.search_start != url_components::omitted) {
910 components.search_start += new_difference;
911 }
912 if (components.hash_start != url_components::omitted) {
913 components.hash_start += new_difference;
914 }
916}
917
918[[nodiscard]] constexpr bool url_aggregator::validate() const noexcept {
919 if (!is_valid) {
920 return true;
921 }
922 if (!components.check_offset_consistency()) {
923 ada_log("url_aggregator::validate inconsistent components \n",
924 to_diagram());
925 return false;
926 }
927 // We have a credible components struct, but let us investivate more
928 // carefully:
941 if (components.protocol_end == url_components::omitted) {
942 ada_log("url_aggregator::validate omitted protocol_end \n", to_diagram());
943 return false;
944 }
945 if (components.username_end == url_components::omitted) {
946 ada_log("url_aggregator::validate omitted username_end \n", to_diagram());
947 return false;
948 }
949 if (components.host_start == url_components::omitted) {
950 ada_log("url_aggregator::validate omitted host_start \n", to_diagram());
951 return false;
952 }
953 if (components.host_end == url_components::omitted) {
954 ada_log("url_aggregator::validate omitted host_end \n", to_diagram());
955 return false;
956 }
957 if (components.pathname_start == url_components::omitted) {
958 ada_log("url_aggregator::validate omitted pathname_start \n", to_diagram());
959 return false;
960 }
961
962 if (components.protocol_end > buffer.size()) {
963 ada_log("url_aggregator::validate protocol_end overflow \n", to_diagram());
964 return false;
965 }
966 if (components.username_end > buffer.size()) {
967 ada_log("url_aggregator::validate username_end overflow \n", to_diagram());
968 return false;
969 }
970 if (components.host_start > buffer.size()) {
971 ada_log("url_aggregator::validate host_start overflow \n", to_diagram());
972 return false;
973 }
974 if (components.host_end > buffer.size()) {
975 ada_log("url_aggregator::validate host_end overflow \n", to_diagram());
976 return false;
977 }
978 if (components.pathname_start > buffer.size()) {
979 ada_log("url_aggregator::validate pathname_start overflow \n",
980 to_diagram());
981 return false;
982 }
983
984 if (components.protocol_end > 0) {
985 if (buffer[components.protocol_end - 1] != ':') {
986 ada_log(
987 "url_aggregator::validate missing : at the end of the protocol \n",
988 to_diagram());
989 return false;
990 }
991 }
992
993 if (components.username_end != buffer.size() &&
994 components.username_end > components.protocol_end + 2) {
995 if (buffer[components.username_end] != ':' &&
996 buffer[components.username_end] != '@') {
997 ada_log(
998 "url_aggregator::validate missing : or @ at the end of the username "
999 "\n",
1000 to_diagram());
1001 return false;
1002 }
1003 }
1004
1005 if (components.host_start != buffer.size()) {
1006 if (components.host_start > components.username_end) {
1007 if (buffer[components.host_start] != '@') {
1008 ada_log(
1009 "url_aggregator::validate missing @ at the end of the password \n",
1010 to_diagram());
1011 return false;
1012 }
1013 } else if (components.host_start == components.username_end &&
1014 components.host_end > components.host_start) {
1015 if (components.host_start == components.protocol_end + 2) {
1016 if (buffer[components.protocol_end] != '/' ||
1017 buffer[components.protocol_end + 1] != '/') {
1018 ada_log(
1019 "url_aggregator::validate missing // between protocol and host "
1020 "\n",
1021 to_diagram());
1022 return false;
1023 }
1024 } else {
1025 if (components.host_start > components.protocol_end &&
1026 buffer[components.host_start] != '@') {
1027 ada_log(
1028 "url_aggregator::validate missing @ at the end of the username "
1029 "\n",
1030 to_diagram());
1031 return false;
1032 }
1033 }
1034 } else {
1035 if (components.host_end != components.host_start) {
1036 ada_log("url_aggregator::validate expected omitted host \n",
1037 to_diagram());
1038 return false;
1039 }
1040 }
1041 }
1042 if (components.host_end != buffer.size() &&
1043 components.pathname_start > components.host_end) {
1044 if (components.pathname_start == components.host_end + 2 &&
1045 buffer[components.host_end] == '/' &&
1046 buffer[components.host_end + 1] == '.') {
1047 if (components.pathname_start + 1 >= buffer.size() ||
1048 buffer[components.pathname_start] != '/' ||
1049 buffer[components.pathname_start + 1] != '/') {
1050 ada_log(
1051 "url_aggregator::validate expected the path to begin with // \n",
1052 to_diagram());
1053 return false;
1054 }
1055 } else if (buffer[components.host_end] != ':') {
1056 ada_log("url_aggregator::validate missing : at the port \n",
1057 to_diagram());
1058 return false;
1059 }
1060 }
1061 if (components.pathname_start != buffer.size() &&
1062 components.pathname_start < components.search_start &&
1063 components.pathname_start < components.hash_start && !has_opaque_path) {
1064 if (buffer[components.pathname_start] != '/') {
1065 ada_log("url_aggregator::validate missing / at the path \n",
1066 to_diagram());
1067 return false;
1068 }
1069 }
1070 if (components.search_start != url_components::omitted) {
1071 if (buffer[components.search_start] != '?') {
1072 ada_log("url_aggregator::validate missing ? at the search \n",
1073 to_diagram());
1074 return false;
1075 }
1076 }
1077 if (components.hash_start != url_components::omitted) {
1078 if (buffer[components.hash_start] != '#') {
1079 ada_log("url_aggregator::validate missing # at the hash \n",
1080 to_diagram());
1081 return false;
1082 }
1083 }
1084
1085 return true;
1086}
1087
1088[[nodiscard]] constexpr std::string_view url_aggregator::get_pathname()
1089 const noexcept ada_lifetime_bound {
1090 ada_log("url_aggregator::get_pathname pathname_start = ",
1091 components.pathname_start, " buffer.size() = ", buffer.size(),
1092 " components.search_start = ", components.search_start,
1093 " components.hash_start = ", components.hash_start);
1094 auto ending_index = uint32_t(buffer.size());
1095 if (components.search_start != url_components::omitted) {
1096 ending_index = components.search_start;
1097 } else if (components.hash_start != url_components::omitted) {
1098 ending_index = components.hash_start;
1099 }
1100 return helpers::substring(buffer, components.pathname_start, ending_index);
1101}
1102
1103inline std::ostream &operator<<(std::ostream &out,
1104 const ada::url_aggregator &u) {
1105 return out << u.to_string();
1106}
1107
1108void url_aggregator::update_host_to_base_host(
1109 const std::string_view input) noexcept {
1110 ada_log("url_aggregator::update_host_to_base_host ", input);
1111 ADA_ASSERT_TRUE(validate());
1112 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
1113 if (type != ada::scheme::type::FILE) {
1114 // Let host be the result of host parsing host_view with url is not special.
1115 if (input.empty() && !is_special()) {
1116 if (has_hostname()) {
1117 clear_hostname();
1118 } else if (has_dash_dot()) {
1119 add_authority_slashes_if_needed();
1120 delete_dash_dot();
1121 }
1122 return;
1123 }
1124 }
1125 update_base_hostname(input);
1126 ADA_ASSERT_TRUE(validate());
1127 return;
1128}
1129} // namespace ada
1130
1131#endif // ADA_URL_AGGREGATOR_INL_H
Definitions of the character sets used by unicode functions.
#define ADA_ASSERT_TRUE(COND)
#define ada_lifetime_bound
#define ADA_ASSERT_EQUAL(LHS, RHS, MESSAGE)
#define ada_really_inline
Definition common_defs.h:81
Definitions for helper functions used within Ada.
constexpr uint8_t FRAGMENT_PERCENT_ENCODE[32]
constexpr bool is_digit(char x) noexcept
constexpr int32_t base
ada_really_inline size_t percent_encode_index(const std::string_view input, const uint8_t character_set[])
Definition unicode-inl.h:19
Definition ada_idna.h:13
std::ostream & operator<<(std::ostream &out, const ada::url &u)
Definition url-inl.h:38
Declarations for the URL scheme.
Lightweight URL struct.
ada_really_inline const url_components & get_components() const noexcept
constexpr bool has_non_empty_password() const noexcept
constexpr bool validate() const noexcept
void clear_search() override
std::string_view get_hostname() const noexcept ada_lifetime_bound
std::string to_string() const override
std::string_view get_hash() const noexcept ada_lifetime_bound
std::string to_diagram() const
constexpr bool has_hostname() const noexcept
constexpr bool has_search() const noexcept override
constexpr std::string_view get_href() const noexcept ada_lifetime_bound
constexpr bool has_empty_hostname() const noexcept
constexpr bool has_password() const noexcept
std::string_view get_search() const noexcept ada_lifetime_bound
ada_really_inline uint32_t get_pathname_length() const noexcept
constexpr std::string_view get_pathname() const noexcept ada_lifetime_bound
std::string_view get_password() const noexcept ada_lifetime_bound
constexpr bool has_hash() const noexcept override
constexpr bool has_port() const noexcept
ada_really_inline constexpr bool has_credentials() const noexcept
constexpr bool has_non_empty_username() const noexcept
std::string_view get_username() const noexcept ada_lifetime_bound
bool is_valid
Definition url_base.h:50
bool has_opaque_path
Definition url_base.h:55
URL Component representations using offsets.
static constexpr uint32_t omitted
Definitions for unicode operations.
Declaration for the basic URL definitions.
Declaration for the URL Components.