5#ifndef ADA_URL_AGGREGATOR_INL_H
6#define ADA_URL_AGGREGATOR_INL_H
24inline void url_aggregator::update_base_authority(
26 std::string_view input = base_buffer.substr(
28 ada_log(
"url_aggregator::update_base_authority ", input);
30 bool input_starts_with_dash = input.starts_with(
"//");
37 if (input_starts_with_dash) {
38 input.remove_prefix(2);
44 size_t password_delimiter = input.find(
':');
48 if (password_delimiter != std::string_view::npos) {
50 std::string_view username = input.substr(0, password_delimiter);
51 std::string_view password = input.substr(password_delimiter + 1);
54 diff += uint32_t(username.size());
57 buffer.insert(components.
protocol_end + diff + 1, password);
58 diff += uint32_t(password.size()) + 1;
59 }
else if (!input.empty()) {
64 diff += uint32_t(input.size());
69 if (buffer.size() >
base.host_start && buffer[
base.host_start] !=
'@') {
83inline void url_aggregator::update_unencoded_base_hash(std::string_view input) {
84 ada_log(
"url_aggregator::update_unencoded_base_hash ", input,
" [",
85 input.size(),
" bytes], buffer is '", buffer,
"' [", buffer.size(),
86 " bytes] components.hash_start = ", components.
hash_start);
92 components.
hash_start = uint32_t(buffer.size());
94 bool encoding_required = unicode::percent_encode<true>(
98 if (!encoding_required) {
101 ada_log(
"url_aggregator::update_unencoded_base_hash final buffer is '",
102 buffer,
"' [", buffer.size(),
" bytes]");
107 uint32_t start, uint32_t end, std::string_view input) {
108 uint32_t current_length = end - start;
109 uint32_t input_size = uint32_t(input.size());
110 uint32_t new_difference = input_size - current_length;
112 if (current_length == 0) {
113 buffer.insert(start, input);
114 }
else if (input_size == current_length) {
115 buffer.replace(start, input_size, input);
116 }
else if (input_size < current_length) {
117 buffer.erase(start, current_length - input_size);
118 buffer.replace(start, input_size, input);
120 buffer.replace(start, current_length, input.substr(0, current_length));
121 buffer.insert(start + current_length, input.substr(current_length));
124 return new_difference;
127inline void url_aggregator::update_base_hostname(
const std::string_view input) {
128 ada_log(
"url_aggregator::update_base_hostname ", input,
" [", input.size(),
129 " bytes], buffer is '", buffer,
"' [", buffer.size(),
" bytes]");
134 add_authority_slashes_if_needed();
137 uint32_t new_difference =
144 components.
host_end += new_difference;
157 ada_log(
"url_aggregator::get_pathname_length");
158 uint32_t ending_index = uint32_t(buffer.size());
172inline void url_aggregator::update_base_search(std::string_view input) {
173 ada_log(
"url_aggregator::update_base_search ", input);
181 if (input[0] ==
'?') {
182 input.remove_prefix(1);
193 buffer.append(input);
205 components.
hash_start += uint32_t(input.size() + 1);
211inline void url_aggregator::update_base_search(
212 std::string_view input,
const uint8_t query_percent_encode_set[]) {
213 ada_log(
"url_aggregator::update_base_search ", input,
226 bool encoding_required =
227 unicode::percent_encode<true>(input, query_percent_encode_set, buffer);
230 if (!encoding_required) {
231 buffer.append(input);
245 if (idx == input.size()) {
247 components.
hash_start += uint32_t(input.size() + 1);
249 buffer.insert(components.
search_start + 1, input, 0, idx);
250 input.remove_prefix(idx);
253 std::string encoded =
254 ada::unicode::percent_encode(input, query_percent_encode_set);
255 buffer.insert(components.
search_start + idx + 1, encoded);
257 uint32_t(encoded.size() + idx + 1);
264inline void url_aggregator::update_base_pathname(
const std::string_view input) {
265 ada_log(
"url_aggregator::update_base_pathname '", input,
"' [", input.size(),
270 const bool begins_with_dashdash = input.starts_with(
"//");
271 if (!begins_with_dashdash && has_dash_dot()) {
272 ada_log(
"url_aggregator::update_base_pathname has /.: \n",
to_diagram());
286 uint32_t difference = replace_and_resize(
295 ada_log(
"url_aggregator::update_base_pathname end '", input,
"' [",
300inline void url_aggregator::append_base_pathname(
const std::string_view input) {
301 ada_log(
"url_aggregator::append_base_pathname ", input,
" ",
to_string(),
305#if ADA_DEVELOPMENT_CHECKS
308 path_expected.append(input);
310 uint32_t ending_index = uint32_t(buffer.size());
316 buffer.insert(ending_index, input);
322 components.
hash_start += uint32_t(input.size());
324#if ADA_DEVELOPMENT_CHECKS
327 path_expected, path_after,
328 "append_base_pathname problem after inserting " + std::string(input));
333inline void url_aggregator::update_base_username(
const std::string_view input) {
334 ada_log(
"url_aggregator::update_base_username '", input,
"' ",
to_string(),
339 add_authority_slashes_if_needed();
342 bool host_starts_with_at = buffer.size() > components.
host_start &&
344 uint32_t diff = replace_and_resize(components.
protocol_end + 2,
350 if (!input.empty() && !host_starts_with_at) {
353 }
else if (input.empty() && host_starts_with_at && !
has_password) {
371inline void url_aggregator::append_base_username(
const std::string_view input) {
372 ada_log(
"url_aggregator::append_base_username ", input);
375#if ADA_DEVELOPMENT_CHECKS
378 username_expected.append(input);
380 add_authority_slashes_if_needed();
387 uint32_t difference = uint32_t(input.size());
406#if ADA_DEVELOPMENT_CHECKS
409 username_expected, username_after,
410 "append_base_username problem after inserting " + std::string(input));
415constexpr void url_aggregator::clear_password() {
435inline void url_aggregator::update_base_password(
const std::string_view input) {
436 ada_log(
"url_aggregator::update_base_password ", input);
440 add_authority_slashes_if_needed();
448 update_base_username(
"");
455 uint32_t difference = uint32_t(input.size());
457 if (password_exists) {
458 uint32_t current_length =
460 buffer.erase(components.
username_end + 1, current_length);
461 difference -= current_length;
489inline void url_aggregator::append_base_password(
const std::string_view input) {
490 ada_log(
"url_aggregator::append_base_password ", input,
" ",
to_string(),
494#if ADA_DEVELOPMENT_CHECKS
496 std::string password_expected = std::string(
get_password());
497 password_expected.append(input);
499 add_authority_slashes_if_needed();
506 uint32_t difference = uint32_t(input.size());
532#if ADA_DEVELOPMENT_CHECKS
535 password_expected, password_after,
536 "append_base_password problem after inserting " + std::string(input));
541inline void url_aggregator::update_base_port(uint32_t input) {
542 ada_log(
"url_aggregator::update_base_port");
550 std::string value = helpers::concat(
":", std::to_string(input));
551 uint32_t difference = uint32_t(value.size());
559 buffer.insert(components.
host_end, value);
567 components.
port = input;
572 ada_log(
"url_aggregator::clear_port");
578 buffer.erase(components.
host_end, length);
590[[nodiscard]]
inline uint32_t url_aggregator::retrieve_base_port()
const {
591 ada_log(
"url_aggregator::retrieve_base_port");
592 return components.
port;
596 ada_log(
"url_aggregator::clear_search");
612#if ADA_DEVELOPMENT_CHECKS
614 "search should have been cleared on buffer=" + buffer +
621 ada_log(
"url_aggregator::clear_hash");
629#if ADA_DEVELOPMENT_CHECKS
631 "hash should have been cleared on buffer=" + buffer +
637constexpr void url_aggregator::clear_pathname() {
638 ada_log(
"url_aggregator::clear_pathname");
640 uint32_t ending_index = uint32_t(buffer.size());
646 uint32_t pathname_length = ending_index - components.
pathname_start;
648 uint32_t difference = pathname_length;
650 buffer[components.
host_end] ==
'/' &&
651 buffer[components.
host_end + 1] ==
'.') {
653 buffer.erase(components.
host_end, 2);
662 ada_log(
"url_aggregator::clear_pathname completed, running checks...");
663#if ADA_DEVELOPMENT_CHECKS
665 "pathname should have been cleared on buffer=" + buffer +
669 ada_log(
"url_aggregator::clear_pathname completed, running checks... ok");
672constexpr void url_aggregator::clear_hostname() {
673 ada_log(
"url_aggregator::clear_hostname");
675 if (!has_authority()) {
684 if (hostname_length > 0 && buffer[start] ==
'@') {
688 buffer.erase(start, hostname_length);
697#if ADA_DEVELOPMENT_CHECKS
699 "hostname should have been cleared on buffer=" + buffer +
704 "hostname should have been cleared on buffer=" + buffer +
710 ada_log(
"url_aggregator::has_hash");
715 ada_log(
"url_aggregator::has_search");
720 ada_log(
"url_aggregator::has_credentials");
724constexpr bool url_aggregator::cannot_have_credentials_or_port()
const {
725 ada_log(
"url_aggregator::cannot_have_credentials_or_port");
735[[nodiscard]]
constexpr bool ada::url_aggregator::has_authority()
737 ada_log(
"url_aggregator::has_authority");
740 return components.protocol_end + 2 <= components.host_start &&
741 helpers::substring(buffer, components.protocol_end,
742 components.protocol_end + 2) ==
"//";
745inline void ada::url_aggregator::add_authority_slashes_if_needed() noexcept {
746 ada_log(
"url_aggregator::add_authority_slashes_if_needed");
751 if (has_authority()) {
757 buffer.insert(components.protocol_end,
"//");
758 components.username_end += 2;
759 components.host_start += 2;
760 components.host_end += 2;
761 components.pathname_start += 2;
763 components.search_start += 2;
766 components.hash_start += 2;
771constexpr void ada::url_aggregator::reserve(uint32_t capacity) {
772 buffer.reserve(capacity);
776 ada_log(
"url_aggregator::has_non_empty_username");
781 ada_log(
"url_aggregator::has_non_empty_password");
786 ada_log(
"url_aggregator::has_password");
806 return has_authority();
810 ada_log(
"url_aggregator::has_port");
816[[nodiscard]]
constexpr bool url_aggregator::has_dash_dot() const noexcept {
820 ada_log(
"url_aggregator::has_dash_dot");
821#if ADA_DEVELOPMENT_CHECKS
827 buffer[components.
host_end + 1] ==
'.') ||
828 (buffer[components.
host_end] ==
':' &&
832 buffer[components.
host_end] ==
'/' &&
833 buffer[components.
host_end + 1] ==
'.') {
845 buffer[components.
host_end + 1] ==
'.';
850 ada_log(
"url_aggregator::get_href");
855 std::string_view view,
bool check_trailing_content)
noexcept {
856 ada_log(
"url_aggregator::parse_port('", view,
"') ", view.size());
857 if (!view.empty() && view[0] ==
'-') {
858 ada_log(
"parse_port: view[0] == '0' && view.size() > 1");
862 uint16_t parsed_port{};
863 auto r = std::from_chars(view.data(), view.data() + view.size(), parsed_port);
864 if (r.ec == std::errc::result_out_of_range) {
865 ada_log(
"parse_port: r.ec == std::errc::result_out_of_range");
869 ada_log(
"parse_port: ", parsed_port);
870 const size_t consumed = size_t(r.ptr - view.data());
871 ada_log(
"parse_port: consumed ", consumed);
872 if (check_trailing_content) {
874 (consumed == view.size() || view[consumed] ==
'/' ||
875 view[consumed] ==
'?' || (is_special() && view[consumed] ==
'\\'));
877 ada_log(
"parse_port: is_valid = ", is_valid);
879 ada_log(
"parse_port", r.ec == std::errc());
881 auto default_port = scheme_default_port();
882 bool is_port_valid = (default_port == 0 && parsed_port == 0) ||
883 (default_port != parsed_port);
884 if (r.ec == std::errc() && is_port_valid) {
885 update_base_port(parsed_port);
893constexpr void url_aggregator::set_protocol_as_file() {
894 ada_log(
"url_aggregator::set_protocol_as_file ");
901 if (buffer.empty()) {
902 buffer.append(
"file:");
905 buffer.insert(0,
"file:");
912 components.
host_end += new_difference;
928 ada_log(
"url_aggregator::validate inconsistent components \n",
947 ada_log(
"url_aggregator::validate omitted protocol_end \n",
to_diagram());
951 ada_log(
"url_aggregator::validate omitted username_end \n",
to_diagram());
955 ada_log(
"url_aggregator::validate omitted host_start \n",
to_diagram());
959 ada_log(
"url_aggregator::validate omitted host_end \n",
to_diagram());
963 ada_log(
"url_aggregator::validate omitted pathname_start \n",
to_diagram());
968 ada_log(
"url_aggregator::validate protocol_end overflow \n",
to_diagram());
972 ada_log(
"url_aggregator::validate username_end overflow \n",
to_diagram());
976 ada_log(
"url_aggregator::validate host_start overflow \n",
to_diagram());
979 if (components.
host_end > buffer.size()) {
980 ada_log(
"url_aggregator::validate host_end overflow \n",
to_diagram());
984 ada_log(
"url_aggregator::validate pathname_start overflow \n",
992 "url_aggregator::validate missing : at the end of the protocol \n",
1003 "url_aggregator::validate missing : or @ at the end of the username "
1010 if (components.
host_start != buffer.size()) {
1014 "url_aggregator::validate missing @ at the end of the password \n",
1024 "url_aggregator::validate missing // between protocol and host "
1033 "url_aggregator::validate missing @ at the end of the username "
1041 ada_log(
"url_aggregator::validate expected omitted host \n",
1047 if (components.
host_end != buffer.size() &&
1050 buffer[components.
host_end] ==
'/' &&
1051 buffer[components.
host_end + 1] ==
'.') {
1056 "url_aggregator::validate expected the path to begin with // \n",
1060 }
else if (buffer[components.
host_end] !=
':') {
1061 ada_log(
"url_aggregator::validate missing : at the port \n",
1070 ada_log(
"url_aggregator::validate missing / at the path \n",
1077 ada_log(
"url_aggregator::validate missing ? at the search \n",
1084 ada_log(
"url_aggregator::validate missing # at the hash \n",
1095 ada_log(
"url_aggregator::get_pathname pathname_start = ",
1097 " components.search_start = ", components.
search_start,
1098 " components.hash_start = ", components.
hash_start);
1099 auto ending_index = uint32_t(buffer.size());
1105 return helpers::substring(buffer, components.
pathname_start, ending_index);
1113void url_aggregator::update_host_to_base_host(
1114 const std::string_view input)
noexcept {
1115 ada_log(
"url_aggregator::update_host_to_base_host ", input);
1120 if (input.empty() && !is_special()) {
1121 if (has_hostname()) {
1123 }
else if (has_dash_dot()) {
1124 add_authority_slashes_if_needed();
1130 update_base_hostname(input);
Definitions of the character sets used by unicode functions.
Declaration of the character sets used by unicode functions.
Definitions for URL specific checkers used within Ada.
#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[])
std::ostream & operator<<(std::ostream &out, const ada::url &u)
Declarations for the URL scheme.
ada_really_inline const ada::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
URL Component representations using offsets.
std::string to_string() const
static constexpr uint32_t omitted
constexpr bool check_offset_consistency() const noexcept
Definitions for unicode operations.
Definitions for all unicode specific functions.
Declaration for the basic URL definitions.
Declaration for the URL Components.