summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-11-29 09:57:50 -0500
committerAmlal El Mahrouss <amlal@nekernel.org>2025-11-29 09:57:50 -0500
commit463a0c01f96d86c9c91f02903bc1d194c5e55b15 (patch)
tree913c82e8c40c1963751dc4d5052d41d72dad6680 /include
parent83bdb3bd0ce5ca6301aec047c1886c8d00e34085 (diff)
chore&feat: basic URL API.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'include')
-rw-r--r--include/ocl/core/config.hpp2
-rw-r--r--include/ocl/net/url.hpp117
2 files changed, 81 insertions, 38 deletions
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;