diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-05 21:16:28 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-05 21:16:36 +0100 |
| commit | 964f0d8ca36d9333efcfd3136a5cae77dc5dded4 (patch) | |
| tree | 57eb44165e6008a3561970f5f5d86cb686fc2cbe /dev/lib/net | |
| parent | 01c6071ac4a64cbee4270a73f0fe88b10b31097d (diff) | |
feat: lib: implement basic_url and fix httptest header.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/lib/net')
| -rw-r--r-- | dev/lib/net/url.hpp | 59 |
1 files changed, 41 insertions, 18 deletions
diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp index 6772ac4..52991f2 100644 --- a/dev/lib/net/url.hpp +++ b/dev/lib/net/url.hpp @@ -11,42 +11,67 @@ #include <sstream> /// @author Amlal El Mahrouss (amlal@nekernel.org) +/// @brief Parse URLs (in a non-standard way). namespace scl::net { template <typename char_type> class basic_url; - template <typename char_type> - class basic_url_traits; + enum class url_protocol + { + invalid = 0, + http, + https, + mailto, + bad = 0xff, + }; /// @brief Basic URL parser container. template <typename char_type> class basic_url final { - friend basic_url_traits<char_type>; - - std::basic_stringstream<char_type> ss_{}; + url_protocol m_protocol_{url_protocol::invalid}; + std::basic_stringstream<char_type> m_ss_{}; public: - explicit basic_url() = default; - ~basic_url() = default; + explicit basic_url(const std::basic_string<char_type>& protocol) + { + if (protocol.starts_with("https://")) + { + m_protocol_ = url_protocol::https; + this->operator/=(protocol.substr(strlen("https://"))); + } + else if (protocol.starts_with("http://")) + { + m_protocol_ = url_protocol::http; + this->operator/=(protocol.substr(strlen("http://"))); + } + else if (protocol.starts_with("mailto:")) + { + m_protocol_ = url_protocol::mailto; + this->operator/=(protocol.substr(strlen("mailto:"))); + } + } + + ~basic_url() = default; basic_url& operator=(const basic_url&) = default; basic_url(const basic_url&) = default; + private: basic_url& operator/=(const std::basic_string<char_type>& in) { if (in.empty()) return *this; - ss_ += in; + m_ss_ += in; return *this; } basic_url& operator/=(const char_type& in) { - ss_ += in; + m_ss_ += in; return *this; } @@ -55,17 +80,15 @@ namespace scl::net return this->is_valid(); } - bool is_valid() + public: + bool protocol_exists() { - return ss_.size() > 0; + return this->m_protocol_ != url_protocol::bad || this->m_protocol_ != url_protocol::invalid; } - }; - enum class protocol - { - http, - https, - mailto, - bad + bool is_valid() + { + return m_ss_.size() > 0 && this->protocol_exists(); + } }; } // namespace scl::net |
