41 using const_iterator = std::string_view::const_iterator;
44 class authority_type {
46 constexpr authority_type()
noexcept =
default;
47 constexpr authority_type(authority_type
const&)
noexcept =
default;
48 constexpr authority_type(authority_type&&)
noexcept =
default;
49 constexpr authority_type& operator=(authority_type
const&)
noexcept =
default;
50 constexpr authority_type& operator=(authority_type&&)
noexcept =
default;
52 constexpr authority_type(std::string_view
const& rhs)
noexcept
62 [[nodiscard]]
constexpr std::optional<std::string>
const& userinfo()
const noexcept
67 constexpr authority_type& set_userinfo(std::optional<std::string>
const& rhs)
noexcept
73 [[nodiscard]]
constexpr std::string const& host()
const noexcept
78 constexpr authority_type& set_host(
std::string const& rhs)
81 _host = to_lower(rhs);
85 [[nodiscard]]
constexpr std::optional<std::string>
const& port()
const noexcept
90 constexpr authority_type& set_port(std::optional<std::string>
const& rhs)
101 [[nodiscard]]
constexpr friend bool operator==(authority_type
const&, authority_type
const&)
noexcept =
default;
102 [[nodiscard]]
constexpr friend auto operator<=>(authority_type
const&, authority_type
const&)
noexcept =
default;
104 [[nodiscard]]
constexpr friend size_t to_string_size(authority_type
const& rhs)
noexcept
108 size += rhs._userinfo->size() + 1;
110 size += rhs._host.size();
112 size += rhs._port->size() + 1;
117 [[nodiscard]]
constexpr friend std::string to_string(authority_type
const& rhs)
noexcept
126 if (rhs._host.empty() or rhs._host.front() !=
'[') {
141 std::optional<std::string> _userinfo = {};
143 std::optional<std::string> _port = {};
145 [[nodiscard]]
constexpr static void validate_host(std::string_view str)
147 if (str.starts_with(
'[') and not str.ends_with(
']')) {
148 throw uri_error(
"The host-component starts with '[' has missing ']' at end");
149 }
else if (str.ends_with(
']') and str.starts_with(
'[')) {
150 throw uri_error(
"The host-component ends with ']' has missing '[' at start");
154 [[nodiscard]]
constexpr static void validate_port(std::string_view str)
157 if (not(c >=
'0' and c <=
'9')) {
158 throw uri_error(
"The port-component contains a non-digit.");
170 [[nodiscard]]
constexpr const_iterator parse_userinfo(const_iterator first, const_iterator last)
noexcept
172 for (
auto it = first; it != last; ++it) {
173 if (hilet c = *it; c ==
'@') {
189 [[nodiscard]]
constexpr const_iterator parse_host(const_iterator first, const_iterator last)
noexcept
207 for (; it != last; ++it) {
208 if (hilet c = *it; c ==
':') {
221 [[nodiscard]]
constexpr const_iterator parse_port(const_iterator first, const_iterator last)
noexcept
227 constexpr void parse(std::string_view rhs)
noexcept
229 auto first = rhs.cbegin();
230 auto last = rhs.cend();
231 auto it = parse_userinfo(first, last);
232 it = parse_host(it, last);
234 if (it != last and *it ==
':') {
235 it = parse_port(++it, last);
272 constexpr path_type()
noexcept =
default;
273 constexpr path_type(path_type
const&)
noexcept =
default;
274 constexpr path_type(path_type&&)
noexcept =
default;
275 constexpr path_type& operator=(path_type
const&)
noexcept =
default;
276 constexpr path_type& operator=(path_type&&)
noexcept =
default;
280 auto r = make_vector<std::string>(std::views::transform(std::views::split(str, std::string_view{
"/"}), [](
auto&& x) {
281 return URI::decode(std::string_view{std::ranges::begin(x), std::ranges::end(x)});
284 if (r.size() == 1 and r.front().empty()) {
288 if (not r.empty() and (r.back() ==
"." or r.back() ==
".." or r.back() ==
"**")) {
298 hi_axiom(holds_invariant());
301 [[nodiscard]]
constexpr bool absolute()
const noexcept
306 [[nodiscard]]
constexpr bool double_absolute()
const noexcept
308 return size() >= 3 and (*
this)[0].
empty() and (*
this)[1].empty();
311 [[nodiscard]]
constexpr bool is_root()
const noexcept
313 return size() == 2 and (*
this)[0].
empty() and (*
this)[1].empty();
316 [[nodiscard]]
constexpr std::optional<std::string> filename()
const noexcept
341 hi_axiom(holds_invariant());
347 if (base_has_authority and base.empty()) {
353 if (not base.empty()) {
357 base.insert(base.cend(), ref.cbegin(), ref.cend());
359 if (base.size() == 1 and base.front().empty()) {
364 hi_axiom(base.holds_invariant());
395 for (
auto it = path.cbegin(); it != path.cend();) {
400 }
else if (*it ==
"..") {
401 if (it == path.cbegin()) {
405 }
else if (it - 1 == path.cbegin() and (it - 1)->
empty()) {
411 it = path.erase(it - 1, it + 1);
419 if (path.size() == 1 and path.front().empty()) {
424 hi_axiom(path.holds_invariant());
428 [[nodiscard]]
constexpr bool holds_invariant() const noexcept
434 if (
back() ==
"." or
back() ==
".." or
back() ==
"**") {
441 [[nodiscard]]
constexpr friend size_t to_string_size(path_type
const& rhs)
noexcept
444 for (hilet& segment : rhs) {
460 r.
reserve(to_string_size(rhs));
462 auto segment_is_empty =
false;
463 for (
auto it = rhs.
cbegin(); it != rhs.
cend(); ++it) {
464 segment_is_empty = it->empty();
466 if (it == rhs.
cbegin() and not has_scheme) {
474 if (not r.empty() and not segment_is_empty) {
482 constexpr URI() noexcept = default;
483 constexpr
URI(
URI const&) noexcept = default;
484 constexpr
URI(
URI&&) noexcept = default;
485 constexpr
URI& operator=(
URI const&) noexcept = default;
486 constexpr
URI& operator=(
URI&&) noexcept = default;
494 constexpr explicit URI(
std::string_view str)
513 constexpr explicit URI(
const char *str) : URI(
std::string_view{str}) {}
515 [[nodiscard]]
constexpr bool empty() const noexcept
517 return not _scheme and not _authority and _path.
empty() and not _query and not _fragment;
520 constexpr operator bool() const noexcept
529 [[nodiscard]]
constexpr std::optional<std::string>
const&
scheme() const noexcept
537 constexpr URI&
set_scheme(std::optional<std::string>
const& rhs)
540 validate_scheme(*rhs);
552 [[nodiscard]]
constexpr std::optional<authority_type>
const&
authority() const noexcept
557 constexpr URI& set_authority(std::optional<authority_type>
const& rhs)
noexcept
563 [[nodiscard]]
constexpr path_type
const& path() const noexcept
568 constexpr URI& set_path(path_type
const& rhs)
570 validate_path(rhs, to_bool(_authority));
575 [[nodiscard]]
constexpr std::optional<std::string> filename() const noexcept
577 return _path.filename();
595 [[nodiscard]]
constexpr std::optional<std::string>
const&
query() const noexcept
600 constexpr URI& set_query(std::optional<std::string>
const& rhs)
noexcept
610 [[nodiscard]]
constexpr std::optional<std::string>
const&
fragment() const noexcept
615 constexpr URI& set_fragment(std::optional<std::string>
const& rhs)
noexcept
621 [[nodiscard]]
constexpr friend std::string to_string(
URI const& rhs)
noexcept
624 r.
reserve(to_string_size(rhs));
631 if (rhs._authority) {
634 r += to_string(*rhs._authority);
637 r += to_string(rhs._path, to_bool(rhs._scheme));
646 r +=
URI::encode<HI_PCHAR,
'/',
'?'>(*rhs._fragment);
652 friend std::ostream& operator<<(std::ostream& lhs, URI
const& rhs)
noexcept
654 return lhs << to_string(rhs);
657 [[nodiscard]]
constexpr friend bool operator==(URI
const& lhs, URI
const& rhs)
noexcept =
default;
658 [[nodiscard]]
constexpr friend auto operator<=>(URI
const& lhs, URI
const& rhs)
noexcept =
default;
660 [[nodiscard]]
constexpr friend URI operator/(URI
const& base, URI
const& ref)
noexcept
665 target._scheme =
ref._scheme;
666 target._authority =
ref._authority;
667 target._path = remove_dot_segments(
ref._path);
668 target._query =
ref._query;
670 if (
ref._authority) {
671 target._authority =
ref._authority;
672 target._path = remove_dot_segments(
ref._path);
673 target._query =
ref._query;
676 if (
ref._path.empty()) {
677 target._path = base._path;
679 target._query =
ref._query;
681 target._query = base._query;
684 if (
ref._path.absolute()) {
685 target._path = remove_dot_segments(
ref._path);
687 target._path = remove_dot_segments(
merge(base._path,
ref._path, to_bool(base._authority)));
689 target._query =
ref._query;
691 target._authority = base._authority;
693 target._scheme = base._scheme;
696 target._fragment =
ref._fragment;
701 [[nodiscard]]
constexpr friend bool operator==(URI
const& lhs, std::string_view rhs)
noexcept
703 return lhs == URI(rhs);
706 [[nodiscard]]
constexpr friend auto operator<=>(URI
const& lhs, std::string_view rhs)
noexcept
708 return lhs == URI(rhs);
711 [[nodiscard]]
constexpr friend URI operator/(URI
const& base, std::string_view ref)
noexcept
713 return base / URI(ref);
725 for (
auto i = r.find(
'%'); i != std::string::npos; i = r.find(
'%', i)) {
747 return decode(std::string_view{first, last});
757 template<
char... Extras,
typename It,
typename ItEnd>
765 for (
auto it = first; it != last; ++it) {
769 (c >=
'a' and c <=
'z') or
770 (c >=
'A' and c <=
'Z') or
771 (c >=
'0' and c <=
'9') or
772 c ==
'-' or c ==
'.' or c ==
'_' or c ==
'~'
773 ) or ... or (c == Extras))) {
782 r +=
'A' + nibble - 10;
787 r +=
'A' + nibble - 10;
802 template<
char... Extras,
typename Range>
805 return encode<Extras...>(std::ranges::begin(range), std::ranges::end(range));
809 std::optional<std::string> _scheme;
810 std::optional<authority_type> _authority;
812 std::optional<std::string> _query;
813 std::optional<std::string> _fragment;
815 [[nodiscard]]
constexpr friend size_t to_string_size(
URI const& rhs)
noexcept
819 size += rhs._scheme->size() + 1;
821 if (rhs._authority) {
822 size += to_string_size(*rhs._authority) + 2;
824 size += to_string_size(rhs._path);
827 size += rhs._query->size() + 1;
830 size += rhs._fragment->size() + 1;
836 [[nodiscard]]
constexpr static bool is_scheme_start(
char c)
noexcept
838 return (c >=
'a' and c <=
'z') or (c >=
'A' and c <=
'Z');
841 [[nodiscard]]
constexpr static bool is_scheme(
char c)
noexcept
843 return is_scheme_start(c) or (c >=
'0' and c <=
'9') or c ==
'+' or c ==
'-' or c ==
'.';
846 [[nodiscard]] constexpr static
void validate_scheme(std::string_view str)
849 throw uri_error(
"The scheme-component is not allowed to be empty (it is allowed to not exist).");
851 if (not is_scheme_start(str.front())) {
852 throw uri_error(
"The scheme-component does not start with [a-zA-Z].");
855 if (not is_scheme(c)) {
856 throw uri_error(
"The scheme-component contains a character outside [a-zA-Z0-9.+-].");
861 [[nodiscard]]
constexpr static void validate_path(
path_type const& path,
bool has_authority)
864 if (not(path.empty() or path.absolute())) {
865 throw uri_error(
"A path-component in a URI with an authority-component must be empty or absolute.");
867 }
else if (path.double_absolute()) {
868 throw uri_error(
"A path-component in a URI without an authority-component may not start with a double slash '//'.");
878 [[nodiscard]]
constexpr const_iterator parse_scheme(const_iterator first, const_iterator last)
880 for (
auto it = first; it != last; ++it) {
881 if (hilet c = *it; c ==
':') {
885 }
else if (c ==
'/' or c ==
'?' or c ==
'#') {
895 [[nodiscard]]
constexpr const_iterator parse_authority(const_iterator first, const_iterator last)
897 for (
auto it = first; it != last; it++) {
898 if (hilet c = *it; c ==
'/' or c ==
'?' or c ==
'#') {
908 [[nodiscard]]
constexpr const_iterator parse_path(const_iterator first, const_iterator last)
910 for (
auto it = first; it != last; it++) {
911 if (hilet c = *it; c ==
'?' or c ==
'#') {
912 set_path(
path_type{std::string_view{first, it}});
917 set_path(
path_type{std::string_view{first, last}});
921 [[nodiscard]]
constexpr const_iterator parse_query(const_iterator first, const_iterator last)
923 for (
auto it = first; it != last; it++) {
924 if (hilet c = *it; c ==
'#') {
934 [[nodiscard]]
constexpr const_iterator parse_fragment(const_iterator first, const_iterator last)
940 constexpr void parse(std::string_view str)
942 auto first = str.cbegin();
943 auto last = str.cend();
944 auto it = parse_scheme(first, last);
946 if (
std::distance(it, last) >= 2 and it[0] ==
'/' and it[1] ==
'/') {
948 it = parse_authority(it, last);
951 it = parse_path(it, last);
953 if (it != last and *it ==
'?') {
954 it = parse_query(++it, last);
957 if (it != last and *it ==
'#') {
958 it = parse_fragment(++it, last);
962 friend struct std::hash<URI>;