diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-29 09:57:50 -0500 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-29 09:57:50 -0500 |
| commit | 463a0c01f96d86c9c91f02903bc1d194c5e55b15 (patch) | |
| tree | 913c82e8c40c1963751dc4d5052d41d72dad6680 | |
| parent | 83bdb3bd0ce5ca6301aec047c1886c8d00e34085 (diff) | |
chore&feat: basic URL API.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | examples/url/CMakeLists.txt | 15 | ||||
| -rw-r--r-- | examples/url/url.cc | 24 | ||||
| -rw-r--r-- | include/ocl/core/config.hpp | 2 | ||||
| -rw-r--r-- | include/ocl/net/url.hpp | 117 |
4 files changed, 120 insertions, 38 deletions
diff --git a/examples/url/CMakeLists.txt b/examples/url/CMakeLists.txt new file mode 100644 index 0000000..7195d6c --- /dev/null +++ b/examples/url/CMakeLists.txt @@ -0,0 +1,15 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + Url + VERSION 1.0 + LANGUAGES CXX) + +find_package(Boost REQUIRED COMPONENTS container) + +add_executable(Url fix.cc) + +set_property(TARGET Url PROPERTY CXX_STANDARD 20) +target_include_directories(Url PUBLIC ../../include/ocl) +target_link_libraries(Url PRIVATE Boost::container) diff --git a/examples/url/url.cc b/examples/url/url.cc new file mode 100644 index 0000000..b4cc924 --- /dev/null +++ b/examples/url/url.cc @@ -0,0 +1,24 @@ +/* + fix example + Written by Amlal El Mahrouss. + Licensed under the Boost Software License + */ + +#include <core/error_handler.hpp> +#include <net/url.hpp> +#include <unistd.h> +#include <io/print.hpp> +#include <sys/socket.h> + +/* finally test it */ +int main(int argc, char** argv) +{ + ocl::net::url url_type(ocl::net::url::file); + + url_type /= "bin"; + url_type /= "ls"; + + ocl::io::println(url_type.assemble()); + + return EXIT_SUCCESS; +} diff --git a/include/ocl/core/config.hpp b/include/ocl/core/config.hpp index 4fdfc4e..15439d8 100644 --- a/include/ocl/core/config.hpp +++ b/include/ocl/core/config.hpp @@ -27,9 +27,11 @@ #define OCL_DEPRECATED_MSG(MSG) [[deprecated( MSG )]] #ifdef __cplusplus +#define OCL_DECL extern "C" /// DLL/Dylib/So specific macro. #define OCL_EXPORT_DECL extern "C" BOOST_SYMBOL_EXPORT #else +#define OCL_DECL #define OCL_EXPORT_DECL #endif diff --git a/include/ocl/net/url.hpp b/include/ocl/net/url.hpp index 361cd66..71c18d6 100644 --- a/include/ocl/net/url.hpp +++ b/include/ocl/net/url.hpp @@ -7,6 +7,8 @@ #pragma once +#include <core/config.hpp> + #include <string> #include <sstream> @@ -23,51 +25,69 @@ namespace ocl::net class basic_url final { public: - using ref_type = basic_url&; - enum { invalid = 0, - http, + http = 100, https, - mailto, + file, ftp, + mailto = 200, tel, bad = 0xff, }; + private: + auto to_string_() + { + if (m_protocol_ == https) + return "https://"; + + if (m_protocol_ == http) + return "http://"; + + if (m_protocol_ == file) + return "file://"; + + if (m_protocol_ == tel) + return "tel:"; + + if (m_protocol_ == mailto) + return "mailto:"; + + return "invalid:"; + } + + auto to_enum_(const std::basic_string<char_type>& protocol) + { + if (protocol == "https") + return https; + + if (protocol == "http") + return http; + + if (protocol == "file") + return file; + + if (protocol == "tel") + return tel; + + if (protocol == "mailto") + return mailto; + + return invalid; + } + uint32_t m_protocol_{basic_url::invalid}; std::basic_stringstream<char_type> m_ss_{}; std::basic_string<char_type> m_port_{""}; public: - explicit basic_url(const std::basic_string<char_type>& protocol) + using ref_type = basic_url&; + + explicit basic_url(const uint32_t& protocol) { - if (protocol.starts_with("https://")) - { - m_protocol_ = basic_url::https; - this->operator/=(protocol.substr(std::size("https://"))); - } - else if (protocol.starts_with("http://")) - { - m_protocol_ = basic_url::http; - this->operator/=(protocol.substr(std::size("http://"))); - } - else if (protocol.starts_with("mailto:")) - { - m_protocol_ = basic_url::mailto; - this->operator/=(protocol.substr(std::size("mailto:"))); - } - else if (protocol.starts_with("tel:")) - { - m_protocol_ = basic_url::tel; - this->operator/=(protocol.substr(std::size("tel:"))); - } - else if (protocol.starts_with("ftp:")) - { - m_protocol_ = basic_url::ftp; - this->operator/=(protocol.substr(std::size("ftp:"))); - } + m_protocol_ = protocol; } ~basic_url() = default; @@ -75,7 +95,7 @@ namespace ocl::net basic_url& operator=(const basic_url&) = default; basic_url(const basic_url&) = default; - private: + public: ref_type operator/=(const std::basic_string<char_type>& in) { if (in.empty()) @@ -83,17 +103,28 @@ namespace ocl::net if (in.starts_with(":")) { + if (m_protocol_ == tel || m_protocol_ == mailto) + return *this; + m_port_ = in.substr(1); - return *this; + } + else if (in.starts_with("+")) + { + if (m_protocol_ != tel) + return *this; + + for (auto& ch : in) + { + if (ch == ' ') + return *this; + } } - m_ss_ += in; - return *this; - } + m_ss_ << in; + + if (!in.ends_with("/")) + m_ss_ << "/"; - ref_type operator/=(const char_type& in) - { - m_ss_ += in; return *this; } @@ -113,6 +144,16 @@ namespace ocl::net return this->m_port_; } + std::basic_string<char_type> assemble() noexcept + { + std::basic_string<char_type> out = to_string_(); + out += this->m_ss_.str(); + + out.pop_back(); + + return out; + } + bool is_valid() const noexcept { return m_ss_.size() > 0 && this->m_protocol_ != basic_url::bad || this->m_protocol_ != basic_url::invalid; |
