5#ifndef ADA_URL_AGGREGATOR_INL_H
6#define ADA_URL_AGGREGATOR_INL_H
22#include <system_error>
26inline void url_aggregator::update_base_authority(
27 std::string_view base_buffer,
const ada::url_components& base) {
28 std::string_view input = base_buffer.substr(
30 ada_log(
"url_aggregator::update_base_authority ", input);
32 bool input_starts_with_dash = input.starts_with(
"//");
33 uint32_t diff = components.host_start - components.protocol_end;
35 buffer.erase(components.protocol_end,
36 components.host_start - components.protocol_end);
37 components.username_end = components.protocol_end;
39 if (input_starts_with_dash) {
40 input.remove_prefix(2);
42 buffer.insert(components.protocol_end,
"//");
43 components.username_end += 2;
46 size_t password_delimiter = input.find(
':');
50 if (password_delimiter != std::string_view::npos) {
52 std::string_view username = input.substr(0, password_delimiter);
53 std::string_view password = input.substr(password_delimiter + 1);
55 buffer.insert(components.protocol_end + diff, username);
56 diff += uint32_t(username.size());
57 buffer.insert(components.protocol_end + diff,
":");
58 components.username_end = components.protocol_end + diff;
59 buffer.insert(components.protocol_end + diff + 1, password);
60 diff += uint32_t(password.size()) + 1;
61 }
else if (!input.empty()) {
63 buffer.insert(components.protocol_end + diff, input);
64 components.username_end =
65 components.protocol_end + diff + uint32_t(input.size());
66 diff += uint32_t(input.size());
69 components.host_start += diff;
71 if (buffer.size() >
base.host_start && buffer[
base.host_start] !=
'@') {
72 buffer.insert(components.host_start,
"@");
75 components.host_end += diff;
76 components.pathname_start += diff;
78 components.search_start += diff;
81 components.hash_start += diff;
85inline void url_aggregator::update_unencoded_base_hash(std::string_view input) {
86 ada_log(
"url_aggregator::update_unencoded_base_hash ", input,
" [",
87 input.size(),
" bytes], buffer is '", buffer,
"' [", buffer.size(),
88 " bytes] components.hash_start = ", components.hash_start);
92 buffer.resize(components.hash_start);
94 components.hash_start = uint32_t(buffer.size());
100 if (!encoding_required) {
101 buffer.append(input);
103 ada_log(
"url_aggregator::update_unencoded_base_hash final buffer is '",
104 buffer,
"' [", buffer.size(),
" bytes]");
109 uint32_t start, uint32_t end, std::string_view input) {
110 uint32_t current_length = end - start;
111 uint32_t input_size = uint32_t(input.size());
112 uint32_t new_difference = input_size - current_length;
114 if (current_length == 0) {
115 buffer.insert(start, input);
116 }
else if (input_size == current_length) {
117 if (input_size != 0) {
118 std::memmove(buffer.data() + start, input.data(), input_size);
120 }
else if (input_size < current_length) {
121 buffer.erase(start, current_length - input_size);
122 buffer.replace(start, input_size, input);
124 buffer.replace(start, current_length, input.substr(0, current_length));
125 buffer.insert(start + current_length, input.substr(current_length));
128 return new_difference;
131inline void url_aggregator::update_base_hostname(
const std::string_view input) {
132 ada_log(
"url_aggregator::update_base_hostname ", input,
" [", input.size(),
133 " bytes], buffer is '", buffer,
"' [", buffer.size(),
" bytes]");
138 add_authority_slashes_if_needed();
140 bool has_credentials = components.protocol_end + 2 < components.host_start;
141 uint32_t new_difference =
142 replace_and_resize(components.host_start, components.host_end, input);
145 buffer.insert(components.host_start,
"@");
148 components.host_end += new_difference;
149 components.pathname_start += new_difference;
151 components.search_start += new_difference;
154 components.hash_start += new_difference;
161 ada_log(
"url_aggregator::get_pathname_length");
162 uint32_t ending_index = uint32_t(buffer.size());
164 ending_index = components.search_start;
166 ending_index = components.hash_start;
168 return ending_index - components.pathname_start;
176inline void url_aggregator::update_base_search(std::string_view input) {
177 ada_log(
"url_aggregator::update_base_search ", input);
185 if (input[0] ==
'?') {
186 input.remove_prefix(1);
191 components.search_start = uint32_t(buffer.size());
194 buffer.resize(components.search_start + 1);
197 buffer.append(input);
199 const uint32_t difference = replace_and_resize(
200 components.search_start + 1, components.hash_start, input);
201 components.hash_start += difference;
203 components.search_start = components.hash_start;
204 buffer.insert(components.search_start, input.size() + 1,
'?');
205 if (!input.empty()) {
206 std::memmove(buffer.data() + components.search_start + 1, input.data(),
209 components.hash_start += uint32_t(input.size() + 1);
215inline void url_aggregator::update_base_search(
216 std::string_view input,
const uint8_t query_percent_encode_set[]) {
217 ada_log(
"url_aggregator::update_base_search ", input,
224 components.search_start = uint32_t(buffer.size());
227 buffer.resize(components.search_start + 1);
230 bool encoding_required =
234 if (!encoding_required) {
235 buffer.append(input);
241 std::string_view replacement = input;
242 if (idx != input.size()) {
244 ada::unicode::percent_encode(input, query_percent_encode_set, idx);
245 replacement = encoded;
249 const uint32_t difference = replace_and_resize(
250 components.search_start + 1, components.hash_start, replacement);
251 components.hash_start += difference;
253 components.search_start = components.hash_start;
254 buffer.insert(components.search_start, replacement.size() + 1,
'?');
255 if (!replacement.empty()) {
256 std::memmove(buffer.data() + components.search_start + 1,
257 replacement.data(), replacement.size());
259 components.hash_start +=
260 uint32_t(replacement.size() + 1);
267inline void url_aggregator::update_base_pathname(
const std::string_view input) {
268 ada_log(
"url_aggregator::update_base_pathname '", input,
"' [", input.size(),
273 const bool begins_with_dashdash = input.starts_with(
"//");
274 if (!begins_with_dashdash && has_dash_dot()) {
284 buffer.insert(components.pathname_start,
"/.");
285 components.pathname_start += 2;
287 components.search_start += 2;
290 components.hash_start += 2;
294 uint32_t difference = replace_and_resize(
295 components.pathname_start,
298 components.search_start += difference;
301 components.hash_start += difference;
306inline void url_aggregator::append_base_pathname(
const std::string_view input) {
307 ada_log(
"url_aggregator::append_base_pathname ", input,
" ",
to_string(),
311#if ADA_DEVELOPMENT_CHECKS
314 path_expected.append(input);
316 uint32_t ending_index = uint32_t(buffer.size());
318 ending_index = components.search_start;
320 ending_index = components.hash_start;
322 buffer.insert(ending_index, input);
325 components.search_start += uint32_t(input.size());
328 components.hash_start += uint32_t(input.size());
330#if ADA_DEVELOPMENT_CHECKS
333 path_expected, path_after,
334 "append_base_pathname problem after inserting " + std::string(input));
339inline void url_aggregator::update_base_username(
const std::string_view input) {
340 ada_log(
"url_aggregator::update_base_username '", input,
"' ",
to_string(),
345 add_authority_slashes_if_needed();
348 bool host_starts_with_at = buffer.size() > components.host_start &&
349 buffer[components.host_start] ==
'@';
350 uint32_t diff = replace_and_resize(components.protocol_end + 2,
351 components.username_end, input);
353 components.username_end += diff;
354 components.host_start += diff;
356 if (!input.empty() && !host_starts_with_at) {
357 buffer.insert(components.host_start,
"@");
359 }
else if (input.empty() && host_starts_with_at && !
has_password) {
362 buffer.erase(components.host_start, 1);
366 components.host_end += diff;
367 components.pathname_start += diff;
369 components.search_start += diff;
372 components.hash_start += diff;
377inline void url_aggregator::append_base_username(
const std::string_view input) {
378 ada_log(
"url_aggregator::append_base_username ", input);
381#if ADA_DEVELOPMENT_CHECKS
384 username_expected.append(input);
386 add_authority_slashes_if_needed();
393 uint32_t difference = uint32_t(input.size());
394 buffer.insert(components.username_end, input);
395 components.username_end += difference;
396 components.host_start += difference;
398 if (buffer[components.host_start] !=
'@' &&
399 components.host_start != components.host_end) {
400 buffer.insert(components.host_start,
"@");
404 components.host_end += difference;
405 components.pathname_start += difference;
407 components.search_start += difference;
410 components.hash_start += difference;
412#if ADA_DEVELOPMENT_CHECKS
415 username_expected, username_after,
416 "append_base_username problem after inserting " + std::string(input));
421constexpr void url_aggregator::clear_password() {
422 ada_log(
"url_aggregator::clear_password ",
to_string());
428 uint32_t diff = components.host_start - components.username_end;
429 buffer.erase(components.username_end, diff);
430 components.host_start -= diff;
431 components.host_end -= diff;
432 components.pathname_start -= diff;
434 components.search_start -= diff;
437 components.hash_start -= diff;
441inline void url_aggregator::update_base_password(
const std::string_view input) {
442 ada_log(
"url_aggregator::update_base_password ", input);
446 add_authority_slashes_if_needed();
454 update_base_username(
"");
461 uint32_t difference = uint32_t(input.size());
463 if (password_exists) {
464 difference = replace_and_resize(components.username_end + 1,
465 components.host_start, input);
467 buffer.insert(components.username_end, input.size() + 1,
':');
468 std::memmove(buffer.data() + components.username_end + 1, input.data(),
472 components.host_start += difference;
477 if (buffer[components.host_start] !=
'@') {
478 buffer.insert(components.host_start,
"@");
482 components.host_end += difference;
483 components.pathname_start += difference;
485 components.search_start += difference;
488 components.hash_start += difference;
493inline void url_aggregator::append_base_password(
const std::string_view input) {
494 ada_log(
"url_aggregator::append_base_password ", input,
" ",
to_string(),
498#if ADA_DEVELOPMENT_CHECKS
500 std::string password_expected = std::string(
get_password());
501 password_expected.append(input);
503 add_authority_slashes_if_needed();
510 uint32_t difference = uint32_t(input.size());
512 buffer.insert(components.host_start, input);
515 buffer.insert(components.username_end,
":");
516 buffer.insert(components.username_end + 1, input);
518 components.host_start += difference;
523 if (buffer[components.host_start] !=
'@') {
524 buffer.insert(components.host_start,
"@");
528 components.host_end += difference;
529 components.pathname_start += difference;
531 components.search_start += difference;
534 components.hash_start += difference;
536#if ADA_DEVELOPMENT_CHECKS
539 password_expected, password_after,
540 "append_base_password problem after inserting " + std::string(input));
545inline void url_aggregator::update_base_port(uint32_t input) {
546 ada_log(
"url_aggregator::update_base_port");
554 std::string value = helpers::concat(
":", std::to_string(input));
555 uint32_t difference = uint32_t(value.size());
558 difference -= components.pathname_start - components.host_end;
559 buffer.erase(components.host_end,
560 components.pathname_start - components.host_end);
563 buffer.insert(components.host_end, value);
564 components.pathname_start += difference;
566 components.search_start += difference;
569 components.hash_start += difference;
571 components.port = input;
576 ada_log(
"url_aggregator::clear_port");
581 uint32_t length = components.pathname_start - components.host_end;
582 buffer.erase(components.host_end, length);
583 components.pathname_start -= length;
585 components.search_start -= length;
588 components.hash_start -= length;
594[[nodiscard]]
inline uint32_t url_aggregator::retrieve_base_port()
const {
595 ada_log(
"url_aggregator::retrieve_base_port");
596 return components.
port;
600 ada_log(
"url_aggregator::clear_search");
607 buffer.resize(components.search_start);
609 buffer.erase(components.search_start,
610 components.hash_start - components.search_start);
611 components.hash_start = components.search_start;
616#if ADA_DEVELOPMENT_CHECKS
618 "search should have been cleared on buffer=" + buffer +
619 " with " + components.to_string() +
"\n" +
to_diagram());
625 ada_log(
"url_aggregator::clear_hash");
630 buffer.resize(components.hash_start);
633#if ADA_DEVELOPMENT_CHECKS
635 "hash should have been cleared on buffer=" + buffer +
636 " with " + components.to_string() +
"\n" +
to_diagram());
641constexpr void url_aggregator::clear_pathname() {
642 ada_log(
"url_aggregator::clear_pathname");
644 uint32_t ending_index = uint32_t(buffer.size());
650 uint32_t pathname_length = ending_index - components.pathname_start;
651 buffer.erase(components.pathname_start, pathname_length);
652 uint32_t difference = pathname_length;
653 if (components.pathname_start == components.host_end + 2 &&
654 buffer[components.host_end] ==
'/' &&
655 buffer[components.host_end + 1] ==
'.') {
656 components.pathname_start -= 2;
657 buffer.erase(components.host_end, 2);
661 components.search_start -= difference;
664 components.hash_start -= difference;
666 ada_log(
"url_aggregator::clear_pathname completed, running checks...");
667#if ADA_DEVELOPMENT_CHECKS
669 "pathname should have been cleared on buffer=" + buffer +
670 " with " + components.to_string() +
"\n" +
to_diagram());
673 ada_log(
"url_aggregator::clear_pathname completed, running checks... ok");
676constexpr void url_aggregator::clear_hostname() {
677 ada_log(
"url_aggregator::clear_hostname");
679 if (!has_authority()) {
684 uint32_t hostname_length = components.host_end - components.host_start;
685 uint32_t start = components.host_start;
688 if (hostname_length > 0 && buffer[start] ==
'@') {
692 buffer.erase(start, hostname_length);
693 components.host_end = start;
694 components.pathname_start -= hostname_length;
696 components.search_start -= hostname_length;
699 components.hash_start -= hostname_length;
701#if ADA_DEVELOPMENT_CHECKS
703 "hostname should have been cleared on buffer=" + buffer +
704 " with " + components.to_string() +
"\n" +
to_diagram());
708 "hostname should have been cleared on buffer=" + buffer +
709 " with " + components.to_string() +
"\n" +
to_diagram());
714 ada_log(
"url_aggregator::has_hash");
719 ada_log(
"url_aggregator::has_search");
724 ada_log(
"url_aggregator::has_credentials");
728constexpr bool url_aggregator::cannot_have_credentials_or_port()
const {
729 ada_log(
"url_aggregator::cannot_have_credentials_or_port");
739[[nodiscard]]
constexpr bool ada::url_aggregator::has_authority()
741 ada_log(
"url_aggregator::has_authority");
744 return components.protocol_end + 2 <= components.host_start &&
745 buffer[components.protocol_end] ==
'/' &&
746 buffer[components.protocol_end + 1] ==
'/';
749inline void ada::url_aggregator::add_authority_slashes_if_needed() {
750 ada_log(
"url_aggregator::add_authority_slashes_if_needed");
755 if (has_authority()) {
761 buffer.insert(components.protocol_end,
"//");
762 components.username_end += 2;
763 components.host_start += 2;
764 components.host_end += 2;
765 components.pathname_start += 2;
767 components.search_start += 2;
770 components.hash_start += 2;
775constexpr void ada::url_aggregator::reserve(uint32_t capacity) {
776 buffer.reserve(capacity);
780 ada_log(
"url_aggregator::has_non_empty_username");
781 return components.protocol_end + 2 < components.username_end;
785 ada_log(
"url_aggregator::has_non_empty_password");
786 return components.host_start > components.username_end;
790 ada_log(
"url_aggregator::has_password");
792 return components.host_start > components.username_end &&
793 buffer[components.username_end] ==
':';
800 if (components.host_start == components.host_end) {
803 if (components.host_end > components.host_start + 1) {
806 return components.username_end != components.host_start;
810 return has_authority();
814 ada_log(
"url_aggregator::has_port");
817 return has_hostname() && components.pathname_start != components.host_end;
820[[nodiscard]]
constexpr bool url_aggregator::has_dash_dot() const noexcept {
824 ada_log(
"url_aggregator::has_dash_dot");
825#if ADA_DEVELOPMENT_CHECKS
831 buffer[components.
host_end + 1] ==
'.') ||
832 (buffer[components.
host_end] ==
':' &&
836 buffer[components.
host_end] ==
'/' &&
837 buffer[components.
host_end + 1] ==
'.') {
847 return components.pathname_start == components.host_end + 2 &&
849 buffer[components.host_end + 1] ==
'.';
854 ada_log(
"url_aggregator::get_href");
859 return buffer.size();
863url_aggregator::parse_port(std::string_view view,
bool check_trailing_content) {
864 ada_log(
"url_aggregator::parse_port('", view,
"') ", view.size());
865 if (!view.empty() && view[0] ==
'-') {
866 ada_log(
"parse_port: view[0] == '0' && view.size() > 1");
870 uint16_t parsed_port{};
871 auto r = std::from_chars(view.data(), view.data() + view.size(), parsed_port);
872 if (
r.ec == std::errc::result_out_of_range) {
873 ada_log(
"parse_port: r.ec == std::errc::result_out_of_range");
877 ada_log(
"parse_port: ", parsed_port);
878 const size_t consumed = size_t(
r.ptr - view.data());
879 ada_log(
"parse_port: consumed ", consumed);
880 if (check_trailing_content) {
882 (consumed == view.size() || view[consumed] ==
'/' ||
883 view[consumed] ==
'?' || (
is_special() && view[consumed] ==
'\\'));
885 ada_log(
"parse_port: is_valid = ",
is_valid);
887 ada_log(
"parse_port",
r.ec == std::errc());
889 auto default_port = scheme_default_port();
890 bool is_port_valid = (default_port == 0 && parsed_port == 0) ||
891 (default_port != parsed_port);
892 if (
r.ec == std::errc() && is_port_valid) {
893 update_base_port(parsed_port);
901constexpr void url_aggregator::set_protocol_as_file() {
902 ada_log(
"url_aggregator::set_protocol_as_file ");
907 uint32_t new_difference = 5 - components.protocol_end;
909 if (buffer.empty()) {
910 buffer.append(
"file:");
912 buffer.erase(0, components.protocol_end);
913 buffer.insert(0,
"file:");
915 components.protocol_end = 5;
918 components.username_end += new_difference;
919 components.host_start += new_difference;
920 components.host_end += new_difference;
921 components.pathname_start += new_difference;
923 components.search_start += new_difference;
926 components.hash_start += new_difference;
935 if (!components.check_offset_consistency()) {
936 ada_log(
"url_aggregator::validate inconsistent components \n",
955 ada_log(
"url_aggregator::validate omitted protocol_end \n",
to_diagram());
959 ada_log(
"url_aggregator::validate omitted username_end \n",
to_diagram());
963 ada_log(
"url_aggregator::validate omitted host_start \n",
to_diagram());
967 ada_log(
"url_aggregator::validate omitted host_end \n",
to_diagram());
971 ada_log(
"url_aggregator::validate omitted pathname_start \n",
to_diagram());
975 if (components.protocol_end > buffer.size()) {
976 ada_log(
"url_aggregator::validate protocol_end overflow \n",
to_diagram());
979 if (components.username_end > buffer.size()) {
980 ada_log(
"url_aggregator::validate username_end overflow \n",
to_diagram());
983 if (components.host_start > buffer.size()) {
984 ada_log(
"url_aggregator::validate host_start overflow \n",
to_diagram());
987 if (components.host_end > buffer.size()) {
988 ada_log(
"url_aggregator::validate host_end overflow \n",
to_diagram());
991 if (components.pathname_start > buffer.size()) {
992 ada_log(
"url_aggregator::validate pathname_start overflow \n",
997 if (components.protocol_end > 0) {
998 if (buffer[components.protocol_end - 1] !=
':') {
1000 "url_aggregator::validate missing : at the end of the protocol \n",
1006 if (components.username_end != buffer.size() &&
1007 components.username_end > components.protocol_end + 2) {
1008 if (buffer[components.username_end] !=
':' &&
1009 buffer[components.username_end] !=
'@') {
1011 "url_aggregator::validate missing : or @ at the end of the username "
1018 if (components.host_start != buffer.size()) {
1019 if (components.host_start > components.username_end) {
1020 if (buffer[components.host_start] !=
'@') {
1022 "url_aggregator::validate missing @ at the end of the password \n",
1026 }
else if (components.host_start == components.username_end &&
1027 components.host_end > components.host_start) {
1028 if (components.host_start == components.protocol_end + 2) {
1029 if (buffer[components.protocol_end] !=
'/' ||
1030 buffer[components.protocol_end + 1] !=
'/') {
1032 "url_aggregator::validate missing // between protocol and host "
1038 if (components.host_start > components.protocol_end &&
1039 buffer[components.host_start] !=
'@') {
1041 "url_aggregator::validate missing @ at the end of the username "
1048 if (components.host_end != components.host_start) {
1049 ada_log(
"url_aggregator::validate expected omitted host \n",
1055 if (components.host_end != buffer.size() &&
1056 components.pathname_start > components.host_end) {
1057 if (components.pathname_start == components.host_end + 2 &&
1058 buffer[components.host_end] ==
'/' &&
1059 buffer[components.host_end + 1] ==
'.') {
1060 if (components.pathname_start + 1 >= buffer.size() ||
1061 buffer[components.pathname_start] !=
'/' ||
1062 buffer[components.pathname_start + 1] !=
'/') {
1064 "url_aggregator::validate expected the path to begin with // \n",
1068 }
else if (buffer[components.host_end] !=
':') {
1069 ada_log(
"url_aggregator::validate missing : at the port \n",
1074 if (components.pathname_start != buffer.size() &&
1075 components.pathname_start < components.search_start &&
1076 components.pathname_start < components.hash_start && !
has_opaque_path) {
1077 if (buffer[components.pathname_start] !=
'/') {
1078 ada_log(
"url_aggregator::validate missing / at the path \n",
1084 if (buffer[components.search_start] !=
'?') {
1085 ada_log(
"url_aggregator::validate missing ? at the search \n",
1091 if (buffer[components.hash_start] !=
'#') {
1092 ada_log(
"url_aggregator::validate missing # at the hash \n",
1103 ada_log(
"url_aggregator::get_pathname pathname_start = ",
1104 components.pathname_start,
" buffer.size() = ", buffer.size(),
1105 " components.search_start = ", components.search_start,
1106 " components.hash_start = ", components.hash_start);
1107 auto ending_index = uint32_t(buffer.size());
1109 ending_index = components.search_start;
1111 ending_index = components.hash_start;
1113 return helpers::substring(buffer, components.pathname_start, ending_index);
1121void url_aggregator::update_host_to_base_host(
const std::string_view input) {
1122 ada_log(
"url_aggregator::update_host_to_base_host ", input);
1130 }
else if (has_dash_dot()) {
1131 add_authority_slashes_if_needed();
1137 update_base_hostname(input);
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
Definitions for helper functions used within Ada.
constexpr uint8_t FRAGMENT_PERCENT_ENCODE[32]
constexpr bool is_digit(char x) noexcept
ada_really_inline size_t percent_encode_index(const std::string_view input, const uint8_t character_set[])
template bool percent_encode< true >(std::string_view input, const uint8_t character_set[], std::string &out)
std::ostream & operator<<(std::ostream &out, const ada::url &u)
URL scheme type definitions and utilities.
Memory-efficient URL representation using a single buffer.
ada_really_inline const url_components & get_components() const noexcept
constexpr bool has_non_empty_password() const noexcept
constexpr size_t get_href_size() const noexcept
constexpr bool validate() const noexcept
void clear_search() override
std::string_view get_search() const ada_lifetime_bound
std::string_view get_hash() const ada_lifetime_bound
std::string to_string() const override
std::string_view get_password() const ada_lifetime_bound
std::string_view get_username() const 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
ada_really_inline uint32_t get_pathname_length() const noexcept
constexpr bool has_hash() const noexcept override
constexpr std::string_view get_pathname() const ada_lifetime_bound
std::string_view get_hostname() const ada_lifetime_bound
constexpr bool has_port() const noexcept
ada_really_inline constexpr bool has_credentials() const noexcept
constexpr bool has_non_empty_username() const noexcept
ada_really_inline constexpr bool is_special() const noexcept
Stores byte offsets for URL components within a buffer.
static constexpr uint32_t omitted
Definitions for unicode operations.
Declaration for the ada::url_aggregator class.
Declaration for the URL Components.