summaryrefslogtreecommitdiffhomepage
path: root/dev/lib/net
diff options
context:
space:
mode:
Diffstat (limited to 'dev/lib/net')
-rw-r--r--dev/lib/net/modem.hpp9
-rw-r--r--dev/lib/net/url.hpp52
2 files changed, 46 insertions, 15 deletions
diff --git a/dev/lib/net/modem.hpp b/dev/lib/net/modem.hpp
index 450aee1..80b3b7a 100644
--- a/dev/lib/net/modem.hpp
+++ b/dev/lib/net/modem.hpp
@@ -129,12 +129,13 @@ namespace ocl::net
return ret == 0L;
}
- ::bind(fd_, (struct sockaddr*)&addr_, sizeof(addr_));
- ::listen(fd_, basic_modem::backlog_count);
+ int ret = ::bind(fd_, (struct sockaddr*)&addr_, sizeof(addr_));
- bad_ = false;
+ bad_ = ret == -1;
+
+ ::listen(fd_, basic_modem::backlog_count);
- return true;
+ return bad_ == false;
}
bool destroy() noexcept
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