Files
spark/src/lib/io/URL.cpp
T
John Colvin 3c97f2552b fix build: Don't use libfmt or ranges in win cpp
I can't deal with this anymore. My build is suddenly broken on Windows
(for spark_crypto.prod branch). I assume they updated the compiler
in a way that isn't backwards compatible. Thanks Windows.
2023-07-05 11:58:14 -04:00

144 lines
3.4 KiB
C++

#include "URL.h"
#include "lib/api/io/URL.pb.h"
#include <iostream>
#include <string>
namespace bts::io {
URL::URL(const bts::api::io::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<std::pair<std::string, std::uint16_t>> URL::well_known_ports_() {
return std::vector<std::pair<std::string, std::uint16_t>>{
{"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::io