41 using const_iterator = std::string_view::const_iterator;
62 [[
nodiscard]]
constexpr std::optional<std::string>
const& userinfo()
const noexcept
67 constexpr authority_type& set_userinfo(std::optional<std::string>
const& rhs)
noexcept
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)
108 size += rhs._userinfo->size() + 1;
110 size += rhs._host.size();
112 size += rhs._port->size() + 1;
122 r += URI::encode<HI_SUB_DELIM, ':'>(*rhs._userinfo);
126 if (rhs._host.empty()
or rhs._host.front() !=
'[') {
127 r += URI::encode<HI_SUB_DELIM>(rhs._host);
129 r += URI::encode<HI_SUB_DELIM, '[', ']', ':'>(rhs._host);
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;
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
311 [[
nodiscard]]
constexpr bool is_root()
const noexcept
316 [[
nodiscard]]
constexpr std::optional<std::string> filename()
const noexcept
341 hi_axiom(holds_invariant());
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());
441 [[
nodiscard]]
constexpr friend size_t to_string_size(path_type
const& rhs)
noexcept
460 r.
reserve(to_string_size(rhs));
467 r += URI::encode<HI_SUB_DELIM, '@'>(*
it);
469 r += URI::encode<HI_PCHAR>(*
it);
513 constexpr explicit URI(
const char *str) :
URI(
std::string_view{str}) {}
540 validate_scheme(*rhs);
557 constexpr URI& set_authority(std::optional<authority_type>
const& rhs)
noexcept
568 constexpr URI& set_path(path_type
const& rhs)
570 validate_path(rhs, to_bool(_authority));
577 return _path.filename();
600 constexpr URI& set_query(std::optional<std::string>
const& rhs)
noexcept
615 constexpr URI& set_fragment(std::optional<std::string>
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);
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 ==
'~'
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 == '.';
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.+-].");
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 ==
'#') {
899 set_authority(authority_type{std::string_view{first,
it}});
904 set_authority(authority_type{std::string_view{first, last}});
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);
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);