#include "url.h" #include "http/protobufs/url.pb.h" #include #include namespace bts::http { Url::Url(const protobufs::Url &url) : protocol_(url.protocol()), host_(url.host()), port_(url.port()), path_(url.path()) { for (const auto &qp : url.query_params()) { query_params_[qp.key()] = qp.value(); } } std::vector> Url::well_known_ports_() { return std::vector>{ {"http", 80}, {"https", 443}, {"ws", 80}, {"wss", 443}, {"ssh", 22}, {"sftp", 22}, }; } std::uint16_t Url::get_known_port_for_protocol(const std::string &protocol) { auto wkps = well_known_ports_(); for (const auto &p : wkps) { if (p.first == protocol) { return p.second; } } return 0; } std::string Url::get_known_protocol_for_port(std::uint16_t port) { auto wkps = well_known_ports_(); for (const auto &p : wkps) { if (p.second == port) { return p.first; } } return ""; } std::string &Url::protocol() { return protocol_; } const std::string &Url::protocol() const { return protocol_; } std::string &Url::host() { return host_; } const std::string &Url::host() const { return host_; } std::uint16_t &Url::port() { return port_; } std::uint16_t Url::port() const { return port_; } std::string &Url::path() { return path_; } const std::string &Url::path() const { return path_; } QueryParams &Url::query_params() { return query_params_; } const QueryParams &Url::query_params() const { return query_params_; } std::string Url::to_string() const { auto rv = protocol() + "://"; if (host().empty()) { rv.append("localhost"); } else { rv.append(host()); } if (port_ != 0) { rv.append(":" + std::to_string(port_)); } if (!path_.empty()) { rv.append("/" + path_); } if (!query_params_.empty()) { rv.append("?"); rv.append(query_params_.to_string()); } return rv; } std::ostream &operator<<(std::ostream &out, const Url &url) noexcept { out << url.to_string(); return out; } void Url::fill_protocol(const std::string &protocol) { if (protocol_.empty()) { protocol_ = protocol; } } void Url::fill_host(const std::string &host) { if (host_.empty()) { host_ = host; } } void Url::fill_port(std::uint16_t port) { if (port_ == 0) { port_ = port; } } void Url::fill_path(const std::string &path) { if (path_.empty()) { path_ = path; } } void Url::fill_query_params( const QueryParams &query_params, bool add_missing_params) { if (query_params_.empty()) { query_params_ = query_params; } else if (add_missing_params) { for (const auto &[k, v] : query_params) { query_params_.try_emplace(k, v); } } } void Url::fill_with(const Url &url) { fill_path(url.path()); fill_host(url.host()); fill_protocol(url.protocol()); fill_port(url.port()); fill_query_params(url.query_params()); } bool operator==(const Url &lhs, const Url &rhs) noexcept { bool rv = lhs.protocol() == rhs.protocol() && lhs.host() == rhs.host() && lhs.path() == rhs.path() && lhs.port() == rhs.port(); if (rv) { rv = std::equal( lhs.query_params().begin(), lhs.query_params().end(), rhs.query_params().begin(), rhs.query_params().end()); } return rv; } bool operator!=(const Url &lhs, const Url &rhs) noexcept { return !(lhs == rhs); } } // namespace bts::http