cleanup: Tidy up URL.h/cpp

This commit is contained in:
John Colvin committed 2023-10-29 23:24:00 -04:00
1 parent bfc3106b00
commit fd9ed211b4
2 files changed
+109 -119

No files matched your search

+22 -106
View File
@@ -1,49 +1,44 @@
#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();
// }
// }
URL URL::from_string(const std::string &url_str) {
// Protocol
using std::pair;
using std::size_t;
using std::string;
using std::uint16_t;
using std::vector;
URL URL::from_string(const string &url_str) {
URL url;
const char *protocol_separator{"://"};
std::size_t pos = 0;
std::size_t pos_end = url_str.find(protocol_separator);
if (pos_end != std::string::npos) {
size_t pos = 0;
size_t pos_end = url_str.find(protocol_separator);
if (pos_end != string::npos) {
url.fill_protocol(url_str.substr(pos, pos_end));
}
pos = pos_end + strlen(protocol_separator) + 1;
pos_end = url_str.find(':', pos);
if (pos_end != std::string::npos) {
if (pos_end != string::npos) {
// This chunk is <host>:<port>
url.fill_host(url_str.substr(pos, pos_end));
pos = pos_end + 1;
pos_end = url_str.find('/', pos);
if (pos_end == std::string::npos) {
if (pos_end == string::npos) {
pos_end = url_str.size();
}
std::int32_t port = std::stoi(url_str.substr(pos, pos_end - pos));
url.fill_port(static_cast<std::uint16_t>(port));
int32_t port = std::stoi(url_str.substr(pos, pos_end - pos));
url.fill_port(static_cast<uint16_t>(port));
} else {
// This chunk is <host>
pos_end = url_str.find('/', pos);
if (pos_end == std::string::npos) {
if (pos_end == string::npos) {
pos_end = url_str.size();
}
url.fill_host(url_str.substr(pos, pos_end - pos));
}
// Path
if (pos_end != std::string::npos && pos_end < url_str.size()) {
if (pos_end != string::npos && pos_end < url_str.size()) {
url.fill_host(url_str.substr(pos, pos_end - pos));
pos = pos_end + 1;
if (pos < url_str.size()) {
@@ -55,14 +50,14 @@ URL URL::from_string(const std::string &url_str) {
return url;
}
std::vector<std::pair<std::string, std::uint16_t>> URL::well_known_ports_() {
return std::vector<std::pair<std::string, std::uint16_t>>{
vector<pair<string, uint16_t>> URL::well_known_ports_() {
return vector<pair<string, 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) {
uint16_t URL::get_known_port_for_protocol(const string &protocol) {
auto wkps = well_known_ports_();
for (const auto &p : wkps) {
if (p.first == protocol) {
@@ -72,7 +67,7 @@ std::uint16_t URL::get_known_port_for_protocol(const std::string &protocol) {
return 0;
}
std::string URL::get_known_protocol_for_port(std::uint16_t port) {
string URL::get_known_protocol_for_port(uint16_t port) {
auto wkps = well_known_ports_();
for (const auto &p : wkps) {
if (p.second == port) {
@@ -82,23 +77,7 @@ std::string URL::get_known_protocol_for_port(std::uint16_t port) {
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 {
string URL::to_string() const {
auto rv = protocol() + "://";
if (host().empty()) {
rv.append("localhost");
@@ -118,67 +97,4 @@ std::string URL::to_string() const {
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
} // namespace bts::io
+87 -13
View File
@@ -19,15 +19,7 @@ namespace bts::api::io {
namespace bts::io {
class URL {
public:
URL() = default;
// explicit URL(const bts::api::io::URL &url);
URL(const URL &) = default;
URL(URL &&) noexcept = default;
URL &operator=(const URL &) = default;
URL &operator=(URL &&) noexcept = default;
public:
std::string &protocol();
const std::string &protocol() const;
@@ -58,7 +50,7 @@ class URL {
static std::string get_known_protocol_for_port(std::uint16_t port);
static URL from_string(const std::string &str);
private:
private:
std::string protocol_;
std::string host_;
std::uint16_t port_{0};
@@ -74,7 +66,89 @@ bool operator!=(const URL &lhs, const URL &rhs) noexcept;
std::ostream &operator<<(std::ostream &out, const URL &url) noexcept;
} // namespace bts::io
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline std::string &URL::protocol() { return protocol_; }
inline const std::string &URL::protocol() const { return protocol_; }
inline std::string &URL::host() { return host_; }
inline const std::string &URL::host() const { return host_; }
inline uint16_t &URL::port() { return port_; }
inline uint16_t URL::port() const { return port_; }
inline std::string &URL::path() { return path_; }
inline const std::string &URL::path() const { return path_; }
inline QueryParams &URL::query_params() { return query_params_; }
inline const QueryParams &URL::query_params() const { return query_params_; }
inline void URL::fill_protocol(const std::string &protocol) {
if (protocol_.empty()) {
protocol_ = protocol;
}
}
inline void URL::fill_host(const std::string &host) {
if (host_.empty()) {
host_ = host;
}
}
inline void URL::fill_port(uint16_t port) {
if (port_ == 0) {
port_ = port;
}
}
inline void URL::fill_path(const std::string &path) {
if (path_.empty()) {
path_ = path;
}
}
inline 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);
}
}
}
inline 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());
}
inline std::ostream &operator<<(std::ostream &out, const URL &url) noexcept {
out << url.to_string();
return out;
}
inline 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;
}
inline bool operator!=(const URL &lhs, const URL &rhs) noexcept {
return !(lhs == rhs);
}
} // namespace bts::io
namespace std {
@@ -83,7 +157,7 @@ struct hash<bts::io::URL> {
std::size_t operator()(const bts::io::URL &url) const;
};
} // namespace std
} // namespace std
//////////////////////////////////////////////////
// Definition
@@ -98,6 +172,6 @@ inline std::size_t hash<bts::io::URL>::operator()(
url.query_params().size());
}
} // namespace std
} // namespace std
FMT_IMPL(bts::io::URL, u, "{}", u.to_string());