Ada 4.1.1
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 <cstdint>
18#include <cstring>
19#include <ostream>
20#include <string>
21#include <string_view>
22#include <system_error>
23
24namespace ada {
25
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(
29 base.protocol_end, base.host_start - base.protocol_end);
30 ada_log("url_aggregator::update_base_authority ", input);
31
32 bool input_starts_with_dash = input.starts_with("//");
33 uint32_t diff = components.host_start - components.protocol_end;
34
35 buffer.erase(components.protocol_end,
36 components.host_start - components.protocol_end);
37 components.username_end = components.protocol_end;
38
39 if (input_starts_with_dash) {
40 input.remove_prefix(2);
41 diff += 2; // add "//"
42 buffer.insert(components.protocol_end, "//");
43 components.username_end += 2;
44 }
45
46 size_t password_delimiter = input.find(':');
47
48 // Check if input contains both username and password by checking the
49 // delimiter: ":" A typical input that contains authority would be "user:pass"
50 if (password_delimiter != std::string_view::npos) {
51 // Insert both username and password
52 std::string_view username = input.substr(0, password_delimiter);
53 std::string_view password = input.substr(password_delimiter + 1);
54
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()) {
62 // Insert only username
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());
67 }
68
69 components.host_start += diff;
70
71 if (buffer.size() > base.host_start && buffer[base.host_start] != '@') {
72 buffer.insert(components.host_start, "@");
73 diff++;
74 }
75 components.host_end += diff;
76 components.pathname_start += diff;
77 if (components.search_start != url_components::omitted) {
78 components.search_start += diff;
79 }
80 if (components.hash_start != url_components::omitted) {
81 components.hash_start += diff;
82 }
83}
84
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);
90 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
91 if (components.hash_start != url_components::omitted) {
92 buffer.resize(components.hash_start);
93 }
94 components.hash_start = uint32_t(buffer.size());
95 buffer += "#";
96 bool encoding_required = unicode::percent_encode<true>(
98 // When encoding_required is false, then buffer is left unchanged, and percent
99 // encoding was not deemed required.
100 if (!encoding_required) {
101 buffer.append(input);
102 }
103 ada_log("url_aggregator::update_unencoded_base_hash final buffer is '",
104 buffer, "' [", buffer.size(), " bytes]");
106}
107
108ada_really_inline uint32_t url_aggregator::replace_and_resize(
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;
113
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);
119 }
120 } else if (input_size < current_length) {
121 buffer.erase(start, current_length - input_size);
122 buffer.replace(start, input_size, input);
123 } else {
124 buffer.replace(start, current_length, input.substr(0, current_length));
125 buffer.insert(start + current_length, input.substr(current_length));
126 }
127
128 return new_difference;
129}
130
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]");
135 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
136
137 // This next line is required for when parsing a URL like `foo://`
138 add_authority_slashes_if_needed();
139
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);
143
144 if (has_credentials) {
145 buffer.insert(components.host_start, "@");
146 new_difference++;
147 }
148 components.host_end += new_difference;
149 components.pathname_start += new_difference;
150 if (components.search_start != url_components::omitted) {
151 components.search_start += new_difference;
152 }
153 if (components.hash_start != url_components::omitted) {
154 components.hash_start += new_difference;
155 }
157}
158
159[[nodiscard]] ada_really_inline uint32_t
161 ada_log("url_aggregator::get_pathname_length");
162 uint32_t ending_index = uint32_t(buffer.size());
163 if (components.search_start != url_components::omitted) {
164 ending_index = components.search_start;
165 } else if (components.hash_start != url_components::omitted) {
166 ending_index = components.hash_start;
167 }
168 return ending_index - components.pathname_start;
169}
170
171[[nodiscard]] ada_really_inline bool url_aggregator::is_at_path()
172 const noexcept {
173 return buffer.size() == components.pathname_start;
174}
175
176inline void url_aggregator::update_base_search(std::string_view input) {
177 ada_log("url_aggregator::update_base_search ", input);
179 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
180 if (input.empty()) {
181 clear_search();
182 return;
183 }
184
185 if (input[0] == '?') {
186 input.remove_prefix(1);
187 }
188
189 if (components.hash_start == url_components::omitted) {
190 if (components.search_start == url_components::omitted) {
191 components.search_start = uint32_t(buffer.size());
192 buffer += "?";
193 } else {
194 buffer.resize(components.search_start + 1);
195 }
196
197 buffer.append(input);
198 } else if (components.search_start != url_components::omitted) {
199 const uint32_t difference = replace_and_resize(
200 components.search_start + 1, components.hash_start, input);
201 components.hash_start += difference;
202 } else {
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(),
207 input.size());
208 }
209 components.hash_start += uint32_t(input.size() + 1); // Do not forget `?`
210 }
211
213}
214
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,
218 " with encoding parameter ", to_string(), "\n", to_diagram());
220 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
221
222 if (components.hash_start == url_components::omitted) {
223 if (components.search_start == url_components::omitted) {
224 components.search_start = uint32_t(buffer.size());
225 buffer += "?";
226 } else {
227 buffer.resize(components.search_start + 1);
228 }
229
230 bool encoding_required =
231 unicode::percent_encode<true>(input, query_percent_encode_set, buffer);
232 // When encoding_required is false, then buffer is left unchanged, and
233 // percent encoding was not deemed required.
234 if (!encoding_required) {
235 buffer.append(input);
236 }
237 } else {
238 const size_t idx =
239 ada::unicode::percent_encode_index(input, query_percent_encode_set);
240 std::string encoded;
241 std::string_view replacement = input;
242 if (idx != input.size()) {
243 encoded =
244 ada::unicode::percent_encode(input, query_percent_encode_set, idx);
245 replacement = encoded;
246 }
247
248 if (components.search_start != url_components::omitted) {
249 const uint32_t difference = replace_and_resize(
250 components.search_start + 1, components.hash_start, replacement);
251 components.hash_start += difference;
252 } else {
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());
258 }
259 components.hash_start +=
260 uint32_t(replacement.size() + 1); // Do not forget `?`
261 }
262 }
263
265}
266
267inline void url_aggregator::update_base_pathname(const std::string_view input) {
268 ada_log("url_aggregator::update_base_pathname '", input, "' [", input.size(),
269 " bytes] \n", to_diagram());
270 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
272
273 const bool begins_with_dashdash = input.starts_with("//");
274 if (!begins_with_dashdash && has_dash_dot()) {
275 // We must delete the ./
276 delete_dash_dot();
277 }
278
279 if (begins_with_dashdash && !has_opaque_path && !has_authority() &&
280 !has_dash_dot()) {
281 // If url's host is null, url does not have an opaque path, url's path's
282 // size is greater than 1, then append U+002F (/) followed by U+002E (.) to
283 // output.
284 buffer.insert(components.pathname_start, "/.");
285 components.pathname_start += 2;
286 if (components.search_start != url_components::omitted) {
287 components.search_start += 2;
288 }
289 if (components.hash_start != url_components::omitted) {
290 components.hash_start += 2;
291 }
292 }
293
294 uint32_t difference = replace_and_resize(
295 components.pathname_start,
296 components.pathname_start + get_pathname_length(), input);
297 if (components.search_start != url_components::omitted) {
298 components.search_start += difference;
299 }
300 if (components.hash_start != url_components::omitted) {
301 components.hash_start += difference;
302 }
304}
305
306inline void url_aggregator::append_base_pathname(const std::string_view input) {
307 ada_log("url_aggregator::append_base_pathname ", input, " ", to_string(),
308 "\n", to_diagram());
310 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
311#if ADA_DEVELOPMENT_CHECKS
312 // computing the expected password.
313 std::string path_expected(get_pathname());
314 path_expected.append(input);
315#endif // ADA_DEVELOPMENT_CHECKS
316 uint32_t ending_index = uint32_t(buffer.size());
317 if (components.search_start != url_components::omitted) {
318 ending_index = components.search_start;
319 } else if (components.hash_start != url_components::omitted) {
320 ending_index = components.hash_start;
321 }
322 buffer.insert(ending_index, input);
323
324 if (components.search_start != url_components::omitted) {
325 components.search_start += uint32_t(input.size());
326 }
327 if (components.hash_start != url_components::omitted) {
328 components.hash_start += uint32_t(input.size());
329 }
330#if ADA_DEVELOPMENT_CHECKS
331 std::string path_after = std::string(get_pathname());
333 path_expected, path_after,
334 "append_base_pathname problem after inserting " + std::string(input));
335#endif // ADA_DEVELOPMENT_CHECKS
337}
338
339inline void url_aggregator::update_base_username(const std::string_view input) {
340 ada_log("url_aggregator::update_base_username '", input, "' ", to_string(),
341 "\n", to_diagram());
343 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
344
345 add_authority_slashes_if_needed();
346
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);
352
353 components.username_end += diff;
354 components.host_start += diff;
355
356 if (!input.empty() && !host_starts_with_at) {
357 buffer.insert(components.host_start, "@");
358 diff++;
359 } else if (input.empty() && host_starts_with_at && !has_password) {
360 // Input is empty, there is no password, and we need to remove "@" from
361 // hostname
362 buffer.erase(components.host_start, 1);
363 diff--;
364 }
365
366 components.host_end += diff;
367 components.pathname_start += diff;
368 if (components.search_start != url_components::omitted) {
369 components.search_start += diff;
370 }
371 if (components.hash_start != url_components::omitted) {
372 components.hash_start += diff;
373 }
375}
376
377inline void url_aggregator::append_base_username(const std::string_view input) {
378 ada_log("url_aggregator::append_base_username ", input);
380 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
381#if ADA_DEVELOPMENT_CHECKS
382 // computing the expected password.
383 std::string username_expected(get_username());
384 username_expected.append(input);
385#endif // ADA_DEVELOPMENT_CHECKS
386 add_authority_slashes_if_needed();
387
388 // If input is empty, do nothing.
389 if (input.empty()) {
390 return;
391 }
392
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;
397
398 if (buffer[components.host_start] != '@' &&
399 components.host_start != components.host_end) {
400 buffer.insert(components.host_start, "@");
401 difference++;
402 }
403
404 components.host_end += difference;
405 components.pathname_start += difference;
406 if (components.search_start != url_components::omitted) {
407 components.search_start += difference;
408 }
409 if (components.hash_start != url_components::omitted) {
410 components.hash_start += difference;
411 }
412#if ADA_DEVELOPMENT_CHECKS
413 std::string username_after(get_username());
415 username_expected, username_after,
416 "append_base_username problem after inserting " + std::string(input));
417#endif // ADA_DEVELOPMENT_CHECKS
419}
420
421constexpr void url_aggregator::clear_password() {
422 ada_log("url_aggregator::clear_password ", to_string());
424 if (!has_password()) {
425 return;
426 }
427
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;
433 if (components.search_start != url_components::omitted) {
434 components.search_start -= diff;
435 }
436 if (components.hash_start != url_components::omitted) {
437 components.hash_start -= diff;
438 }
439}
440
441inline void url_aggregator::update_base_password(const std::string_view input) {
442 ada_log("url_aggregator::update_base_password ", input);
444 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
445
446 add_authority_slashes_if_needed();
447
448 // TODO: Optimization opportunity. Merge the following removal functions.
449 if (input.empty()) {
450 clear_password();
451
452 // Remove username too, if it is empty.
453 if (!has_non_empty_username()) {
454 update_base_username("");
455 }
456
457 return;
458 }
459
460 bool password_exists = has_password();
461 uint32_t difference = uint32_t(input.size());
462
463 if (password_exists) {
464 difference = replace_and_resize(components.username_end + 1,
465 components.host_start, input);
466 } else {
467 buffer.insert(components.username_end, input.size() + 1, ':');
468 std::memmove(buffer.data() + components.username_end + 1, input.data(),
469 input.size());
470 difference++;
471 }
472 components.host_start += difference;
473
474 // The following line is required to add "@" to hostname. When updating
475 // password if hostname does not start with "@", it is "update_base_password"s
476 // responsibility to set it.
477 if (buffer[components.host_start] != '@') {
478 buffer.insert(components.host_start, "@");
479 difference++;
480 }
481
482 components.host_end += difference;
483 components.pathname_start += difference;
484 if (components.search_start != url_components::omitted) {
485 components.search_start += difference;
486 }
487 if (components.hash_start != url_components::omitted) {
488 components.hash_start += difference;
489 }
491}
492
493inline void url_aggregator::append_base_password(const std::string_view input) {
494 ada_log("url_aggregator::append_base_password ", input, " ", to_string(),
495 "\n", to_diagram());
497 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
498#if ADA_DEVELOPMENT_CHECKS
499 // computing the expected password.
500 std::string password_expected = std::string(get_password());
501 password_expected.append(input);
502#endif // ADA_DEVELOPMENT_CHECKS
503 add_authority_slashes_if_needed();
504
505 // If input is empty, do nothing.
506 if (input.empty()) {
507 return;
508 }
509
510 uint32_t difference = uint32_t(input.size());
511 if (has_password()) {
512 buffer.insert(components.host_start, input);
513 } else {
514 difference++; // Increment for ":"
515 buffer.insert(components.username_end, ":");
516 buffer.insert(components.username_end + 1, input);
517 }
518 components.host_start += difference;
519
520 // The following line is required to add "@" to hostname. When updating
521 // password if hostname does not start with "@", it is "append_base_password"s
522 // responsibility to set it.
523 if (buffer[components.host_start] != '@') {
524 buffer.insert(components.host_start, "@");
525 difference++;
526 }
527
528 components.host_end += difference;
529 components.pathname_start += difference;
530 if (components.search_start != url_components::omitted) {
531 components.search_start += difference;
532 }
533 if (components.hash_start != url_components::omitted) {
534 components.hash_start += difference;
535 }
536#if ADA_DEVELOPMENT_CHECKS
537 std::string password_after(get_password());
539 password_expected, password_after,
540 "append_base_password problem after inserting " + std::string(input));
541#endif // ADA_DEVELOPMENT_CHECKS
543}
544
545inline void url_aggregator::update_base_port(uint32_t input) {
546 ada_log("url_aggregator::update_base_port");
548 if (input == url_components::omitted) {
549 clear_port();
550 return;
551 }
552 // calling std::to_string(input.value()) is unfortunate given that the port
553 // value is probably already available as a string.
554 std::string value = helpers::concat(":", std::to_string(input));
555 uint32_t difference = uint32_t(value.size());
556
557 if (components.port != url_components::omitted) {
558 difference -= components.pathname_start - components.host_end;
559 buffer.erase(components.host_end,
560 components.pathname_start - components.host_end);
561 }
562
563 buffer.insert(components.host_end, value);
564 components.pathname_start += difference;
565 if (components.search_start != url_components::omitted) {
566 components.search_start += difference;
567 }
568 if (components.hash_start != url_components::omitted) {
569 components.hash_start += difference;
570 }
571 components.port = input;
573}
574
576 ada_log("url_aggregator::clear_port");
578 if (components.port == url_components::omitted) {
579 return;
580 }
581 uint32_t length = components.pathname_start - components.host_end;
582 buffer.erase(components.host_end, length);
583 components.pathname_start -= length;
584 if (components.search_start != url_components::omitted) {
585 components.search_start -= length;
586 }
587 if (components.hash_start != url_components::omitted) {
588 components.hash_start -= length;
589 }
590 components.port = url_components::omitted;
592}
593
594[[nodiscard]] inline uint32_t url_aggregator::retrieve_base_port() const {
595 ada_log("url_aggregator::retrieve_base_port");
596 return components.port;
597}
598
600 ada_log("url_aggregator::clear_search");
602 if (components.search_start == url_components::omitted) {
603 return;
604 }
605
606 if (components.hash_start == url_components::omitted) {
607 buffer.resize(components.search_start);
608 } else {
609 buffer.erase(components.search_start,
610 components.hash_start - components.search_start);
611 components.hash_start = components.search_start;
612 }
613
614 components.search_start = url_components::omitted;
615
616#if ADA_DEVELOPMENT_CHECKS
618 "search should have been cleared on buffer=" + buffer +
619 " with " + components.to_string() + "\n" + to_diagram());
620#endif
622}
623
625 ada_log("url_aggregator::clear_hash");
627 if (components.hash_start == url_components::omitted) {
628 return;
629 }
630 buffer.resize(components.hash_start);
631 components.hash_start = url_components::omitted;
632
633#if ADA_DEVELOPMENT_CHECKS
635 "hash should have been cleared on buffer=" + buffer +
636 " with " + components.to_string() + "\n" + to_diagram());
637#endif
639}
640
641constexpr void url_aggregator::clear_pathname() {
642 ada_log("url_aggregator::clear_pathname");
644 uint32_t ending_index = uint32_t(buffer.size());
645 if (components.search_start != url_components::omitted) {
646 ending_index = components.search_start;
647 } else if (components.hash_start != url_components::omitted) {
648 ending_index = components.hash_start;
649 }
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);
658 difference += 2;
659 }
660 if (components.search_start != url_components::omitted) {
661 components.search_start -= difference;
662 }
663 if (components.hash_start != url_components::omitted) {
664 components.hash_start -= difference;
665 }
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());
671#endif
673 ada_log("url_aggregator::clear_pathname completed, running checks... ok");
674}
675
676constexpr void url_aggregator::clear_hostname() {
677 ada_log("url_aggregator::clear_hostname");
679 if (!has_authority()) {
680 return;
681 }
682 ADA_ASSERT_TRUE(has_authority());
683
684 uint32_t hostname_length = components.host_end - components.host_start;
685 uint32_t start = components.host_start;
686
687 // If hostname starts with "@", we should not remove that character.
688 if (hostname_length > 0 && buffer[start] == '@') {
689 start++;
690 hostname_length--;
691 }
692 buffer.erase(start, hostname_length);
693 components.host_end = start;
694 components.pathname_start -= hostname_length;
695 if (components.search_start != url_components::omitted) {
696 components.search_start -= hostname_length;
697 }
698 if (components.hash_start != url_components::omitted) {
699 components.hash_start -= hostname_length;
700 }
701#if ADA_DEVELOPMENT_CHECKS
703 "hostname should have been cleared on buffer=" + buffer +
704 " with " + components.to_string() + "\n" + to_diagram());
705#endif
706 ADA_ASSERT_TRUE(has_authority());
708 "hostname should have been cleared on buffer=" + buffer +
709 " with " + components.to_string() + "\n" + to_diagram());
711}
712
713[[nodiscard]] constexpr bool url_aggregator::has_hash() const noexcept {
714 ada_log("url_aggregator::has_hash");
715 return components.hash_start != url_components::omitted;
716}
717
718[[nodiscard]] constexpr bool url_aggregator::has_search() const noexcept {
719 ada_log("url_aggregator::has_search");
720 return components.search_start != url_components::omitted;
721}
722
723constexpr bool url_aggregator::has_credentials() const noexcept {
724 ada_log("url_aggregator::has_credentials");
726}
727
728constexpr bool url_aggregator::cannot_have_credentials_or_port() const {
729 ada_log("url_aggregator::cannot_have_credentials_or_port");
730 return type == ada::scheme::type::FILE ||
731 components.host_start == components.host_end;
732}
733
734[[nodiscard]] ada_really_inline const ada::url_components&
736 return components;
737}
738
739[[nodiscard]] constexpr bool ada::url_aggregator::has_authority()
740 const noexcept {
741 ada_log("url_aggregator::has_authority");
742 // Performance: instead of doing this potentially expensive check, we could
743 // have a boolean in the struct.
744 return components.protocol_end + 2 <= components.host_start &&
745 buffer[components.protocol_end] == '/' &&
746 buffer[components.protocol_end + 1] == '/';
747}
748
749inline void ada::url_aggregator::add_authority_slashes_if_needed() {
750 ada_log("url_aggregator::add_authority_slashes_if_needed");
751 ADA_ASSERT_TRUE(validate());
752 // Protocol setter will insert `http:` to the URL. It is up to hostname setter
753 // to insert
754 // `//` initially to the buffer, since it depends on the hostname existence.
755 if (has_authority()) {
756 return;
757 }
758 // Performance: the common case is components.protocol_end == buffer.size()
759 // Optimization opportunity: in many cases, the "//" is part of the input and
760 // the insert could be fused with another insert.
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;
766 if (components.search_start != url_components::omitted) {
767 components.search_start += 2;
768 }
769 if (components.hash_start != url_components::omitted) {
770 components.hash_start += 2;
771 }
772 ADA_ASSERT_TRUE(validate());
773}
774
775constexpr void ada::url_aggregator::reserve(uint32_t capacity) {
776 buffer.reserve(capacity);
777}
778
779constexpr bool url_aggregator::has_non_empty_username() const noexcept {
780 ada_log("url_aggregator::has_non_empty_username");
781 return components.protocol_end + 2 < components.username_end;
782}
783
784constexpr bool url_aggregator::has_non_empty_password() const noexcept {
785 ada_log("url_aggregator::has_non_empty_password");
786 return components.host_start > components.username_end;
787}
788
789constexpr bool url_aggregator::has_password() const noexcept {
790 ada_log("url_aggregator::has_password");
791 // This function does not care about the length of the password
792 return components.host_start > components.username_end &&
793 buffer[components.username_end] == ':';
794}
795
796constexpr bool url_aggregator::has_empty_hostname() const noexcept {
797 if (!has_hostname()) {
798 return false;
799 }
800 if (components.host_start == components.host_end) {
801 return true;
802 }
803 if (components.host_end > components.host_start + 1) {
804 return false;
805 }
806 return components.username_end != components.host_start;
807}
808
809constexpr bool url_aggregator::has_hostname() const noexcept {
810 return has_authority();
811}
812
813constexpr bool url_aggregator::has_port() const noexcept {
814 ada_log("url_aggregator::has_port");
815 // A URL cannot have a username/password/port if its host is null or the empty
816 // string, or its scheme is "file".
817 return has_hostname() && components.pathname_start != components.host_end;
818}
819
820[[nodiscard]] constexpr bool url_aggregator::has_dash_dot() const noexcept {
821 // If url's host is null, url does not have an opaque path, url's path's size
822 // is greater than 1, and url's path[0] is the empty string, then append
823 // U+002F (/) followed by U+002E (.) to output.
824 ada_log("url_aggregator::has_dash_dot");
825#if ADA_DEVELOPMENT_CHECKS
826 // If pathname_start and host_end are exactly two characters apart, then we
827 // either have a one-digit port such as http://test.com:5?param=1 or else we
828 // have a /.: sequence such as "non-spec:/.//". We test that this is the case.
829 if (components.pathname_start == components.host_end + 2) {
830 ADA_ASSERT_TRUE((buffer[components.host_end] == '/' &&
831 buffer[components.host_end + 1] == '.') ||
832 (buffer[components.host_end] == ':' &&
833 checkers::is_digit(buffer[components.host_end + 1])));
834 }
835 if (components.pathname_start == components.host_end + 2 &&
836 buffer[components.host_end] == '/' &&
837 buffer[components.host_end + 1] == '.') {
838 ADA_ASSERT_TRUE(components.pathname_start + 1 < buffer.size());
839 ADA_ASSERT_TRUE(buffer[components.pathname_start] == '/');
840 ADA_ASSERT_TRUE(buffer[components.pathname_start + 1] == '/');
841 }
842#endif
843 // Performance: it should be uncommon for components.pathname_start ==
844 // components.host_end + 2 to be true. So we put this check first in the
845 // sequence. Most times, we do not have an opaque path. Checking for '/.' is
846 // more expensive, but should be uncommon.
847 return components.pathname_start == components.host_end + 2 &&
848 !has_opaque_path && buffer[components.host_end] == '/' &&
849 buffer[components.host_end + 1] == '.';
850}
851
852[[nodiscard]] constexpr std::string_view url_aggregator::get_href()
853 const noexcept ada_lifetime_bound {
854 ada_log("url_aggregator::get_href");
855 return buffer;
856}
857
858[[nodiscard]] constexpr size_t url_aggregator::get_href_size() const noexcept {
859 return buffer.size();
860}
861
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");
867 is_valid = false;
868 return 0;
869 }
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");
874 is_valid = false;
875 return 0;
876 }
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) {
881 is_valid &=
882 (consumed == view.size() || view[consumed] == '/' ||
883 view[consumed] == '?' || (is_special() && view[consumed] == '\\'));
884 }
885 ada_log("parse_port: is_valid = ", is_valid);
886 if (is_valid) {
887 ada_log("parse_port", r.ec == std::errc());
888 // scheme_default_port can return 0, and we should allow 0 as a base port.
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);
894 } else {
895 clear_port();
896 }
897 }
898 return consumed;
899}
900
901constexpr void url_aggregator::set_protocol_as_file() {
902 ada_log("url_aggregator::set_protocol_as_file ");
905 // next line could overflow but unsigned arithmetic has well-defined
906 // overflows.
907 uint32_t new_difference = 5 - components.protocol_end;
908
909 if (buffer.empty()) {
910 buffer.append("file:");
911 } else {
912 buffer.erase(0, components.protocol_end);
913 buffer.insert(0, "file:");
914 }
915 components.protocol_end = 5;
916
917 // Update the rest of the components.
918 components.username_end += new_difference;
919 components.host_start += new_difference;
920 components.host_end += new_difference;
921 components.pathname_start += new_difference;
922 if (components.search_start != url_components::omitted) {
923 components.search_start += new_difference;
924 }
925 if (components.hash_start != url_components::omitted) {
926 components.hash_start += new_difference;
927 }
929}
930
931[[nodiscard]] constexpr bool url_aggregator::validate() const noexcept {
932 if (!is_valid) {
933 return true;
934 }
935 if (!components.check_offset_consistency()) {
936 ada_log("url_aggregator::validate inconsistent components \n",
937 to_diagram());
938 return false;
939 }
940 // We have a credible components struct, but let us investivate more
941 // carefully:
954 if (components.protocol_end == url_components::omitted) {
955 ada_log("url_aggregator::validate omitted protocol_end \n", to_diagram());
956 return false;
957 }
958 if (components.username_end == url_components::omitted) {
959 ada_log("url_aggregator::validate omitted username_end \n", to_diagram());
960 return false;
961 }
962 if (components.host_start == url_components::omitted) {
963 ada_log("url_aggregator::validate omitted host_start \n", to_diagram());
964 return false;
965 }
966 if (components.host_end == url_components::omitted) {
967 ada_log("url_aggregator::validate omitted host_end \n", to_diagram());
968 return false;
969 }
970 if (components.pathname_start == url_components::omitted) {
971 ada_log("url_aggregator::validate omitted pathname_start \n", to_diagram());
972 return false;
973 }
974
975 if (components.protocol_end > buffer.size()) {
976 ada_log("url_aggregator::validate protocol_end overflow \n", to_diagram());
977 return false;
978 }
979 if (components.username_end > buffer.size()) {
980 ada_log("url_aggregator::validate username_end overflow \n", to_diagram());
981 return false;
982 }
983 if (components.host_start > buffer.size()) {
984 ada_log("url_aggregator::validate host_start overflow \n", to_diagram());
985 return false;
986 }
987 if (components.host_end > buffer.size()) {
988 ada_log("url_aggregator::validate host_end overflow \n", to_diagram());
989 return false;
990 }
991 if (components.pathname_start > buffer.size()) {
992 ada_log("url_aggregator::validate pathname_start overflow \n",
993 to_diagram());
994 return false;
995 }
996
997 if (components.protocol_end > 0) {
998 if (buffer[components.protocol_end - 1] != ':') {
999 ada_log(
1000 "url_aggregator::validate missing : at the end of the protocol \n",
1001 to_diagram());
1002 return false;
1003 }
1004 }
1005
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] != '@') {
1010 ada_log(
1011 "url_aggregator::validate missing : or @ at the end of the username "
1012 "\n",
1013 to_diagram());
1014 return false;
1015 }
1016 }
1017
1018 if (components.host_start != buffer.size()) {
1019 if (components.host_start > components.username_end) {
1020 if (buffer[components.host_start] != '@') {
1021 ada_log(
1022 "url_aggregator::validate missing @ at the end of the password \n",
1023 to_diagram());
1024 return false;
1025 }
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] != '/') {
1031 ada_log(
1032 "url_aggregator::validate missing // between protocol and host "
1033 "\n",
1034 to_diagram());
1035 return false;
1036 }
1037 } else {
1038 if (components.host_start > components.protocol_end &&
1039 buffer[components.host_start] != '@') {
1040 ada_log(
1041 "url_aggregator::validate missing @ at the end of the username "
1042 "\n",
1043 to_diagram());
1044 return false;
1045 }
1046 }
1047 } else {
1048 if (components.host_end != components.host_start) {
1049 ada_log("url_aggregator::validate expected omitted host \n",
1050 to_diagram());
1051 return false;
1052 }
1053 }
1054 }
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] != '/') {
1063 ada_log(
1064 "url_aggregator::validate expected the path to begin with // \n",
1065 to_diagram());
1066 return false;
1067 }
1068 } else if (buffer[components.host_end] != ':') {
1069 ada_log("url_aggregator::validate missing : at the port \n",
1070 to_diagram());
1071 return false;
1072 }
1073 }
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",
1079 to_diagram());
1080 return false;
1081 }
1082 }
1083 if (components.search_start != url_components::omitted) {
1084 if (buffer[components.search_start] != '?') {
1085 ada_log("url_aggregator::validate missing ? at the search \n",
1086 to_diagram());
1087 return false;
1088 }
1089 }
1090 if (components.hash_start != url_components::omitted) {
1091 if (buffer[components.hash_start] != '#') {
1092 ada_log("url_aggregator::validate missing # at the hash \n",
1093 to_diagram());
1094 return false;
1095 }
1096 }
1097
1098 return true;
1099}
1100
1101[[nodiscard]] constexpr std::string_view url_aggregator::get_pathname() const
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());
1108 if (components.search_start != url_components::omitted) {
1109 ending_index = components.search_start;
1110 } else if (components.hash_start != url_components::omitted) {
1111 ending_index = components.hash_start;
1112 }
1113 return helpers::substring(buffer, components.pathname_start, ending_index);
1114}
1115
1116inline std::ostream& operator<<(std::ostream& out,
1117 const ada::url_aggregator& u) {
1118 return out << u.to_string();
1119}
1120
1121void url_aggregator::update_host_to_base_host(const std::string_view input) {
1122 ada_log("url_aggregator::update_host_to_base_host ", input);
1124 ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
1125 if (type != ada::scheme::type::FILE) {
1126 // Let host be the result of host parsing host_view with url is not special.
1127 if (input.empty() && !is_special()) {
1128 if (has_hostname()) {
1129 clear_hostname();
1130 } else if (has_dash_dot()) {
1131 add_authority_slashes_if_needed();
1132 delete_dash_dot();
1133 }
1134 return;
1135 }
1136 }
1137 update_base_hostname(input);
1139 return;
1140}
1141} // namespace ada
1142
1143#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:85
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:24
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)
Definition url-inl.h:43
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
bool is_valid
Definition url_base.h:58
bool has_opaque_path
Definition url_base.h:64
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.