39 using const_iterator = std::string_view::const_iterator;
60 [[nodiscard]]
constexpr std::optional<std::string>
const& userinfo()
const noexcept
65 constexpr authority_type& set_userinfo(std::optional<std::string>
const& rhs)
noexcept
71 [[nodiscard]]
constexpr std::string const& host()
const noexcept
79 _host = to_lower(rhs);
83 [[nodiscard]]
constexpr std::optional<std::string>
const& port()
const noexcept
88 constexpr authority_type& set_port(std::optional<std::string>
const& rhs)
102 [[nodiscard]]
constexpr friend size_t to_string_size(
authority_type const& rhs)
noexcept
106 size += rhs._userinfo->size() + 1;
108 size += rhs._host.size();
110 size += rhs._port->size() + 1;
120 r += URI::encode<HI_SUB_DELIM, ':'>(*rhs._userinfo);
124 if (rhs._host.empty() or rhs._host.front() !=
'[') {
125 r += URI::encode<HI_SUB_DELIM>(rhs._host);
127 r += URI::encode<HI_SUB_DELIM, '[', ']', ':'>(rhs._host);
139 std::optional<std::string> _userinfo = {};
141 std::optional<std::string> _port = {};
143 [[nodiscard]]
constexpr static void validate_host(std::string_view str)
145 if (str.starts_with(
'[') and not str.ends_with(
']')) {
146 throw uri_error(
"The host-component starts with '[' has missing ']' at end");
147 }
else if (str.ends_with(
']') and str.starts_with(
'[')) {
148 throw uri_error(
"The host-component ends with ']' has missing '[' at start");
152 [[nodiscard]]
constexpr static void validate_port(std::string_view str)
155 if (not(c >=
'0' and c <=
'9')) {
156 throw uri_error(
"The port-component contains a non-digit.");
168 [[nodiscard]]
constexpr const_iterator parse_userinfo(const_iterator first, const_iterator last)
noexcept
170 for (
auto it = first; it != last; ++it) {
171 if (
hilet c = *it; c ==
'@') {
187 [[nodiscard]]
constexpr const_iterator parse_host(const_iterator first, const_iterator last)
noexcept
205 for (; it != last; ++it) {
206 if (
hilet c = *it; c ==
':') {
219 [[nodiscard]]
constexpr const_iterator parse_port(const_iterator first, const_iterator last)
noexcept
225 constexpr void parse(std::string_view rhs)
noexcept
227 auto first = rhs.cbegin();
228 auto last = rhs.cend();
229 auto it = parse_userinfo(first, last);
230 it = parse_host(it, last);
232 if (it != last and *it ==
':') {
233 it = parse_port(++it, last);
270 constexpr path_type()
noexcept =
default;
278 auto r = make_vector<std::string>(std::views::transform(std::views::split(str, std::string_view{
"/"}), [](
auto&& x) {
279 return URI::decode(std::string_view{std::ranges::begin(x), std::ranges::end(x)});
282 if (r.size() == 1 and r.front().empty()) {
286 if (not r.empty() and (r.back() ==
"." or r.back() ==
".." or r.back() ==
"**")) {
299 [[nodiscard]]
constexpr bool absolute()
const noexcept
304 [[nodiscard]]
constexpr bool double_absolute()
const noexcept
306 return size() >= 3 and (*
this)[0].
empty() and (*
this)[1].empty();
309 [[nodiscard]]
constexpr bool is_root()
const noexcept
311 return size() == 2 and (*
this)[0].
empty() and (*
this)[1].empty();
314 [[nodiscard]]
constexpr std::optional<std::string> filename()
const noexcept
345 if (base_has_authority and base.empty()) {
351 if (not base.empty()) {
355 base.insert(base.cend(), ref.cbegin(), ref.cend());
357 if (base.size() == 1 and base.front().empty()) {
393 for (
auto it = path.cbegin(); it != path.cend();) {
398 }
else if (*it ==
"..") {
399 if (it == path.cbegin()) {
403 }
else if (it - 1 == path.cbegin() and (it - 1)->empty()) {
409 it = path.erase(it - 1, it + 1);
417 if (path.size() == 1 and path.front().empty()) {
426 [[nodiscard]]
constexpr bool holds_invariant() const noexcept
432 if (
back() ==
"." or
back() ==
".." or
back() ==
"**") {
439 [[nodiscard]]
constexpr friend size_t to_string_size(path_type
const& rhs)
noexcept
442 for (
hilet& segment : rhs) {
458 r.
reserve(to_string_size(rhs));
460 auto segment_is_empty =
false;
461 for (
auto it = rhs.
cbegin(); it != rhs.
cend(); ++it) {
462 segment_is_empty = it->empty();
464 if (it == rhs.
cbegin() and not has_scheme) {
465 r += URI::encode<HI_SUB_DELIM, '@'>(*it);
467 r += URI::encode<HI_PCHAR>(*it);
472 if (not r.empty() and not segment_is_empty) {
480 constexpr URI() noexcept = default;
481 constexpr
URI(
URI const&) noexcept = default;
482 constexpr
URI(
URI&&) noexcept = default;
483 constexpr
URI& operator=(
URI const&) noexcept = default;
484 constexpr
URI& operator=(
URI&&) noexcept = default;
492 constexpr explicit
URI(
std::string_view str)
511 constexpr explicit URI(
const char *str) :
URI(
std::string_view{str}) {}
513 [[nodiscard]]
constexpr bool empty() const noexcept
515 return not _scheme and not _authority and _path.
empty() and not _query and not _fragment;
518 constexpr operator bool() const noexcept
527 [[nodiscard]]
constexpr std::optional<std::string>
const&
scheme() const noexcept
538 validate_scheme(*rhs);
550 [[nodiscard]]
constexpr std::optional<authority_type>
const&
authority() const noexcept
555 constexpr URI& set_authority(std::optional<authority_type>
const& rhs)
noexcept
561 [[nodiscard]]
constexpr path_type
const& path() const noexcept
566 constexpr URI& set_path(path_type
const& rhs)
568 validate_path(rhs, to_bool(_authority));
573 [[nodiscard]]
constexpr std::optional<std::string> filename() const noexcept
575 return _path.filename();
593 [[nodiscard]]
constexpr std::optional<std::string>
const&
query() const noexcept
598 constexpr URI& set_query(std::optional<std::string>
const& rhs)
noexcept
608 [[nodiscard]]
constexpr std::optional<std::string>
const&
fragment() const noexcept
613 constexpr URI& set_fragment(std::optional<std::string>
const& rhs)
noexcept
619 [[nodiscard]]
constexpr friend std::string to_string(
URI const& rhs)
noexcept
622 r.
reserve(to_string_size(rhs));
629 if (rhs._authority) {
632 r += to_string(*rhs._authority);
635 r += to_string(rhs._path, to_bool(rhs._scheme));
644 r +=
URI::encode<HI_PCHAR,
'/',
'?'>(*rhs._fragment);
652 return lhs << to_string(rhs);
655 [[nodiscard]]
constexpr friend bool operator==(URI
const& lhs, URI
const& rhs)
noexcept =
default;
656 [[nodiscard]]
constexpr friend auto operator<=>(URI
const& lhs, URI
const& rhs)
noexcept =
default;
658 [[nodiscard]]
constexpr friend URI operator/(URI
const& base, URI
const& ref)
noexcept
663 target._scheme =
ref._scheme;
664 target._authority =
ref._authority;
665 target._path = remove_dot_segments(
ref._path);
666 target._query =
ref._query;
668 if (
ref._authority) {
669 target._authority =
ref._authority;
670 target._path = remove_dot_segments(
ref._path);
671 target._query =
ref._query;
674 if (
ref._path.empty()) {
675 target._path = base._path;
677 target._query =
ref._query;
679 target._query = base._query;
682 if (
ref._path.absolute()) {
683 target._path = remove_dot_segments(
ref._path);
685 target._path = remove_dot_segments(
merge(base._path,
ref._path, to_bool(base._authority)));
687 target._query =
ref._query;
689 target._authority = base._authority;
691 target._scheme = base._scheme;
694 target._fragment =
ref._fragment;
699 [[nodiscard]]
constexpr friend bool operator==(URI
const& lhs, std::string_view rhs)
noexcept
701 return lhs == URI(rhs);
704 [[nodiscard]]
constexpr friend auto operator<=>(URI
const& lhs, std::string_view rhs)
noexcept
706 return lhs == URI(rhs);
709 [[nodiscard]]
constexpr friend URI operator/(URI
const& base, std::string_view ref)
noexcept
711 return base / URI(ref);
723 for (
auto i = r.find(
'%'); i != std::string::npos; i = r.find(
'%', i)) {
725 auto c = from_string<uint8_t>(r.substr(i + 1, 2), 16);
728 r.replace(i, 3, 1, char_cast<char>(c));
745 return decode(std::string_view{first, last});
755 template<
char... Extras,
typename It,
typename ItEnd>
763 for (
auto it = first; it != last; ++it) {
767 (c >=
'a' and c <=
'z') or
768 (c >=
'A' and c <=
'Z') or
769 (c >=
'0' and c <=
'9') or
770 c ==
'-' or c ==
'.' or c ==
'_' or c ==
'~'
771 ) or ... or (c == Extras))) {
776 auto uc = char_cast<uint8_t>(c);
777 if (
auto nibble = narrow_cast<char>(uc >> 4); nibble <= 9) {
780 r +=
'A' + nibble - 10;
782 if (
auto nibble = narrow_cast<char>(uc & 0xf); nibble <= 9) {
785 r +=
'A' + nibble - 10;
800 template<
char... Extras,
typename Range>
803 return encode<Extras...>(std::ranges::begin(range), std::ranges::end(range));
807 std::optional<std::string> _scheme;
808 std::optional<authority_type> _authority;
810 std::optional<std::string> _query;
811 std::optional<std::string> _fragment;
813 [[nodiscard]]
constexpr friend size_t to_string_size(
URI const& rhs)
noexcept
817 size += rhs._scheme->
size() + 1;
819 if (rhs._authority) {
820 size += to_string_size(*rhs._authority) + 2;
822 size += to_string_size(rhs._path);
825 size += rhs._query->size() + 1;
828 size += rhs._fragment->size() + 1;
834 [[nodiscard]]
constexpr static bool is_scheme_start(
char c)
noexcept
836 return (c >=
'a' and c <=
'z') or (c >=
'A' and c <=
'Z');
839 [[nodiscard]]
constexpr static bool is_scheme(
char c)
noexcept
841 return is_scheme_start(c) or (c >=
'0' and c <=
'9') or c == '+' or c == '-' or c == '.';
844 [[nodiscard]] constexpr static
void validate_scheme(
std::string_view str)
847 throw uri_error(
"The scheme-component is not allowed to be empty (it is allowed to not exist).");
849 if (not is_scheme_start(str.front())) {
850 throw uri_error(
"The scheme-component does not start with [a-zA-Z].");
853 if (not is_scheme(c)) {
854 throw uri_error(
"The scheme-component contains a character outside [a-zA-Z0-9.+-].");
859 [[nodiscard]]
constexpr static void validate_path(path_type
const& path,
bool has_authority)
862 if (not(path.empty() or path.absolute())) {
863 throw uri_error(
"A path-component in a URI with an authority-component must be empty or absolute.");
865 }
else if (path.double_absolute()) {
866 throw uri_error(
"A path-component in a URI without an authority-component may not start with a double slash '//'.");
876 [[nodiscard]]
constexpr const_iterator parse_scheme(const_iterator first, const_iterator last)
878 for (
auto it = first; it != last; ++it) {
879 if (
hilet c = *it; c ==
':') {
883 }
else if (c ==
'/' or c ==
'?' or c ==
'#') {
893 [[nodiscard]]
constexpr const_iterator parse_authority(const_iterator first, const_iterator last)
895 for (
auto it = first; it != last; it++) {
896 if (
hilet c = *it; c ==
'/' or c ==
'?' or c ==
'#') {
897 set_authority(authority_type{std::string_view{first, it}});
902 set_authority(authority_type{std::string_view{first, last}});
906 [[nodiscard]]
constexpr const_iterator parse_path(const_iterator first, const_iterator last)
908 for (
auto it = first; it != last; it++) {
909 if (
hilet c = *it; c ==
'?' or c ==
'#') {
910 set_path(path_type{std::string_view{first, it}});
915 set_path(path_type{std::string_view{first, last}});
919 [[nodiscard]]
constexpr const_iterator parse_query(const_iterator first, const_iterator last)
921 for (
auto it = first; it != last; it++) {
922 if (
hilet c = *it; c ==
'#') {
932 [[nodiscard]]
constexpr const_iterator parse_fragment(const_iterator first, const_iterator last)
938 constexpr void parse(std::string_view str)
940 auto first = str.cbegin();
941 auto last = str.cend();
942 auto it = parse_scheme(first, last);
944 if (
std::distance(it, last) >= 2 and it[0] ==
'/' and it[1] ==
'/') {
946 it = parse_authority(it, last);
949 it = parse_path(it, last);
951 if (it != last and *it ==
'?') {
952 it = parse_query(++it, last);
955 if (it != last and *it ==
'#') {
956 it = parse_fragment(++it, last);