| |
| |
| |
| |
| |
|
|
| #ifndef MAMBA_UTIL_URL_HPP |
| #define MAMBA_UTIL_URL_HPP |
|
|
| #include <array> |
| #include <functional> |
| #include <string> |
| #include <string_view> |
|
|
| #include <tl/expected.hpp> |
|
|
| namespace mamba::util |
| { |
| namespace detail |
| { |
| |
|
|
| enum class StripScheme : bool |
| { |
| no, |
| yes |
| }; |
|
|
| enum class Credentials |
| { |
| Show, |
| Hide, |
| Remove, |
| }; |
|
|
| struct Encode |
| { |
| inline static constexpr struct yes_type |
| { |
| } yes = {}; |
|
|
| inline static constexpr struct no_type |
| { |
| } no = {}; |
| }; |
|
|
| struct Decode |
| { |
| inline static constexpr struct yes_type |
| { |
| } yes = {}; |
|
|
| inline static constexpr struct no_type |
| { |
| } no = {}; |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| class URL |
| { |
| public: |
|
|
| using StripScheme = detail::StripScheme; |
| using Credentials = detail::Credentials; |
| using Encode = detail::Encode; |
| using Decode = detail::Decode; |
|
|
| struct ParseError |
| { |
| std::string what; |
| }; |
|
|
| inline static constexpr std::string_view https = "https"; |
| inline static constexpr std::string_view localhost = "localhost"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| [[nodiscard]] static auto parse(std::string_view url) -> tl::expected<URL, ParseError>; |
|
|
| |
| URL() = default; |
|
|
| |
| [[nodiscard]] auto scheme_is_defaulted() const -> bool; |
|
|
| |
| [[nodiscard]] auto scheme() const -> std::string_view; |
|
|
| |
| void set_scheme(std::string_view scheme); |
|
|
| |
| auto clear_scheme() -> std::string; |
|
|
| |
| [[nodiscard]] auto has_user() const -> bool; |
|
|
| |
| [[nodiscard]] auto user(Decode::no_type) const -> const std::string&; |
|
|
| |
| [[nodiscard]] auto user(Decode::yes_type = Decode::yes) const -> std::string; |
|
|
| |
| void set_user(std::string_view user, Encode::yes_type = Encode::yes); |
|
|
| |
| void set_user(std::string user, Encode::no_type); |
|
|
| |
| auto clear_user() -> std::string; |
|
|
| |
| [[nodiscard]] auto has_password() const -> bool; |
|
|
| |
| [[nodiscard]] auto password(Decode::no_type) const -> const std::string&; |
|
|
| |
| [[nodiscard]] auto password(Decode::yes_type = Decode::yes) const -> std::string; |
|
|
| |
| void set_password(std::string_view password, Encode::yes_type = Encode::yes); |
|
|
| |
| void set_password(std::string password, Encode::no_type); |
|
|
| |
| auto clear_password() -> std::string; |
|
|
| |
| [[nodiscard]] auto authentication() const -> std::string; |
|
|
| |
| [[nodiscard]] auto host_is_defaulted() const -> bool; |
|
|
| |
| [[nodiscard]] auto host(Decode::no_type) const -> std::string_view; |
|
|
| |
| [[nodiscard]] auto host(Decode::yes_type = Decode::yes) const -> std::string; |
|
|
| |
| void set_host(std::string_view host, Encode::yes_type = Encode::yes); |
|
|
| |
| void set_host(std::string host, Encode::no_type); |
|
|
| |
| auto clear_host() -> std::string; |
|
|
| |
| [[nodiscard]] auto port() const -> const std::string&; |
|
|
| |
| void set_port(std::string_view port); |
|
|
| |
| auto clear_port() -> std::string; |
|
|
| |
| [[nodiscard]] auto authority(Credentials = Credentials::Hide) const -> std::string; |
|
|
| |
| [[nodiscard]] auto path(Decode::no_type) const -> const std::string&; |
|
|
| |
| [[nodiscard]] auto path(Decode::yes_type = Decode::yes) const -> std::string; |
|
|
| |
| |
| |
| |
| |
| |
| |
| void set_path(std::string_view path, Encode::yes_type = Encode::yes); |
|
|
| |
| void set_path(std::string path, Encode::no_type); |
|
|
| |
| auto clear_path() -> std::string; |
|
|
| |
| |
| |
| |
| |
| |
| [[nodiscard]] auto pretty_path() const -> std::string; |
|
|
| |
| |
| |
| |
| |
| |
| |
| void append_path(std::string_view path, Encode::yes_type = Encode::yes); |
|
|
| |
| |
| |
| |
| |
| |
| void append_path(std::string_view path, Encode::no_type); |
|
|
| |
| [[nodiscard]] auto query() const -> const std::string&; |
|
|
| |
| void set_query(std::string_view query); |
|
|
| |
| auto clear_query() -> std::string; |
|
|
| |
| [[nodiscard]] auto fragment() const -> const std::string&; |
|
|
| |
| void set_fragment(std::string_view fragment); |
|
|
| |
| auto clear_fragment() -> std::string; |
|
|
| |
| [[nodiscard]] auto str(Credentials credentials = Credentials::Hide) const -> std::string; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| [[nodiscard]] auto pretty_str( |
| StripScheme strip_scheme = StripScheme::no, |
| char rstrip_path = 0, |
| Credentials credentials = Credentials::Hide |
| ) const -> std::string; |
|
|
| protected: |
|
|
| [[nodiscard]] auto authentication_elems(Credentials, Decode::no_type) const |
| -> std::array<std::string_view, 3>; |
| [[nodiscard]] auto authentication_elems(Credentials, Decode::yes_type) const |
| -> std::array<std::string, 3>; |
|
|
| [[nodiscard]] auto authority_elems(Credentials, Decode::no_type) const |
| -> std::array<std::string_view, 7>; |
| [[nodiscard]] auto authority_elems(Credentials, Decode::yes_type) const |
| -> std::array<std::string, 7>; |
|
|
| [[nodiscard]] auto |
| pretty_str_path(StripScheme strip_scheme = StripScheme::no, char rstrip_path = 0) const |
| -> std::string; |
|
|
| private: |
|
|
| std::string m_scheme = {}; |
| std::string m_user = {}; |
| std::string m_password = {}; |
| std::string m_host = {}; |
| std::string m_path = "/"; |
| std::string m_port = {}; |
| std::string m_query = {}; |
| std::string m_fragment = {}; |
| }; |
|
|
| |
| auto operator==(URL const& a, URL const& b) -> bool; |
| auto operator!=(URL const& a, URL const& b) -> bool; |
|
|
| |
| auto operator/(URL const& url, std::string_view subpath) -> URL; |
| auto operator/(URL&& url, std::string_view subpath) -> URL; |
| } |
|
|
| template <> |
| struct std::hash<mamba::util::URL> |
| { |
| auto operator()(const mamba::util::URL& p) const -> std::size_t; |
| }; |
| #endif |
|
|