diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-17 10:45:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-17 10:45:15 +0100 |
| commit | 5abec5514a0745b8032d8c60f5752529e4a13eaa (patch) | |
| tree | c91152ab4cc3a56fbb74d44dc801be5cb8ad3787 /dev/lib/net/url.hpp | |
| parent | c6b7510cdb9350c4e129bfcd7959efc4daee9df6 (diff) | |
| parent | 4700f4d91c342276cc2663f149ed7a8b4e8f3498 (diff) | |
Merge pull request #8 from amlel-el-mahrouss/developv1.0.45
release: Mojave
Diffstat (limited to 'dev/lib/net/url.hpp')
| -rw-r--r-- | dev/lib/net/url.hpp | 52 |
1 files changed, 41 insertions, 11 deletions
diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp index a337538..b77e790 100644 --- a/dev/lib/net/url.hpp +++ b/dev/lib/net/url.hpp @@ -8,46 +8,70 @@ #pragma once #include <string> -#include <iostream> #include <sstream> /// @author Amlal El Mahrouss (amlal@nekernel.org) +/// @brief Parse URLs (in a non-standard way). namespace ocl::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; } @@ -56,9 +80,15 @@ namespace ocl::net return this->is_valid(); } + public: + bool protocol_exists() + { + return this->m_protocol_ != url_protocol::bad || this->m_protocol_ != url_protocol::invalid; + } + bool is_valid() { - return ss_.size() > 0; + return m_ss_.size() > 0 && this->protocol_exists(); } }; } // namespace ocl::net |
