Files
moloapp/src/clients/url.cpp
T
retoor 3c4334cec9 feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security,
data, app, realtime, supervisor, clients, CLI, example plus
enterprise tenancy, audit, operability, governance,
supplychain. Postgres via docker compose for tests.
2026-10-23 18:20:04 +02:00

291 lines
8.0 KiB
C++

#include "clients/url.h"
#include <cctype>
#include "clients/caps.h"
namespace molo::clients {
namespace {
bool is_unreserved(char c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' || c == '~';
}
int hex_val(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
std::string to_lower(std::string s) {
for (auto& c : s) c = static_cast<char>(std::tolower((unsigned char)c));
return s;
}
std::uint16_t default_port(const std::string& scheme) {
if (scheme == "http") return 80;
if (scheme == "https") return 443;
if (scheme == "ws") return 80;
if (scheme == "wss") return 443;
return 0;
}
// Merges base path with a relative-path reference and removes dot segments
// (RFC 3986 section 5.2.3, simplified for non-opaque paths).
std::string merge_paths(const std::string& base, const std::string& rel) {
std::string merged;
if (rel.empty()) {
merged = base.empty() ? "/" : base;
} else if (rel[0] == '/') {
merged = rel;
} else {
auto slash = base.rfind('/');
merged = (slash == std::string::npos) ? "/" + rel
: base.substr(0, slash + 1) + rel;
}
std::vector<std::string> out;
std::string cur;
for (std::size_t i = 0; i <= merged.size(); ++i) {
if (i == merged.size() || merged[i] == '/') {
if (cur == "..") {
if (!out.empty()) out.pop_back();
} else if (!cur.empty() && cur != ".") {
out.push_back(cur);
}
cur.clear();
} else {
cur.push_back(merged[i]);
}
}
std::string path = "/";
for (std::size_t i = 0; i < out.size(); ++i) {
if (i) path.push_back('/');
path += out[i];
}
if (merged.size() > 1 && merged.back() == '/' && path.back() != '/')
path.push_back('/');
return path;
}
} // namespace
std::string Url::encode_component(const std::string& text) {
static const char* kHex = "0123456789ABCDEF";
std::string out;
out.reserve(text.size());
for (char c : text) {
if (is_unreserved(c)) {
out.push_back(c);
} else {
out.push_back('%');
out.push_back(kHex[(c >> 4) & 0xF]);
out.push_back(kHex[c & 0xF]);
}
}
return out;
}
std::string Url::decode_component(const std::string& text) {
std::string out;
out.reserve(text.size());
for (std::size_t i = 0; i < text.size(); ++i) {
char c = text[i];
if (c == '%' && i + 2 < text.size()) {
int hi = hex_val(text[i + 1]);
int lo = hex_val(text[i + 2]);
if (hi < 0 || lo < 0) throw UrlError("bad percent-encoding");
out.push_back(static_cast<char>((hi << 4) | lo));
i += 2;
} else if (c == '+') {
out.push_back(' ');
} else {
out.push_back(c);
}
}
return out;
}
Url Url::parse(const std::string& text) {
if (text.size() > Caps::kMaxUrlLength) throw UrlError("URL too long");
if (text.empty()) throw UrlError("empty URL");
Url url;
std::string rest = text;
auto scheme_end = rest.find("://");
if (scheme_end != std::string::npos) {
url.scheme_ = to_lower(rest.substr(0, scheme_end));
if (url.scheme_.empty()) throw UrlError("empty scheme");
rest = rest.substr(scheme_end + 3);
auto path_at = rest.find_first_of("/?#");
std::string authority =
path_at == std::string::npos ? rest : rest.substr(0, path_at);
rest = path_at == std::string::npos ? "" : rest.substr(path_at);
auto at = authority.rfind('@');
if (at != std::string::npos) {
url.userinfo_ = authority.substr(0, at);
authority = authority.substr(at + 1);
}
if (!authority.empty() && authority[0] == '[') {
auto close = authority.find(']');
if (close == std::string::npos) throw UrlError("bad IPv6 literal");
url.host_ = to_lower(authority.substr(0, close + 1));
if (close + 1 < authority.size() && authority[close + 1] == ':')
url.port_ = static_cast<std::uint16_t>(
std::stoul(authority.substr(close + 2)));
} else {
auto colon = authority.rfind(':');
if (colon != std::string::npos && authority.find(':') == colon) {
url.host_ = to_lower(authority.substr(0, colon));
url.port_ = static_cast<std::uint16_t>(
std::stoul(authority.substr(colon + 1)));
} else {
url.host_ = to_lower(authority);
}
}
if (url.host_.empty()) throw UrlError("empty host");
if (url.port_ == 0) url.port_ = default_port(url.scheme_);
}
auto hash = rest.find('#');
if (hash != std::string::npos) {
url.fragment_ = rest.substr(hash + 1);
rest = rest.substr(0, hash);
}
auto qmark = rest.find('?');
std::string query_text;
if (qmark != std::string::npos) {
query_text = rest.substr(qmark + 1);
rest = rest.substr(0, qmark);
}
url.path_ = rest.empty() ? (url.has_host() ? "/" : "") : rest;
while (!query_text.empty()) {
auto amp = query_text.find('&');
std::string pair =
amp == std::string::npos ? query_text : query_text.substr(0, amp);
query_text = amp == std::string::npos ? "" : query_text.substr(amp + 1);
if (pair.empty()) continue;
auto eq = pair.find('=');
if (eq == std::string::npos) {
url.query_.emplace_back(decode_component(pair), "");
} else {
url.query_.emplace_back(decode_component(pair.substr(0, eq)),
decode_component(pair.substr(eq + 1)));
}
}
return url;
}
Url Url::join(const std::string& ref) const {
if (ref.empty()) return *this;
if (ref.find("://") != std::string::npos) return Url::parse(ref);
Url out = *this;
std::string work = ref;
auto hash = work.find('#');
if (hash != std::string::npos) {
out.fragment_ = work.substr(hash + 1);
work = work.substr(0, hash);
} else {
out.fragment_.clear();
}
if (work.empty()) return out;
if (work[0] == '?') {
out.query_.clear();
Url tmp = Url::parse("http://x" + work);
out.query_ = tmp.query_;
return out;
}
auto qmark = work.find('?');
std::string path_part = work;
std::string query_part;
if (qmark != std::string::npos) {
path_part = work.substr(0, qmark);
query_part = work.substr(qmark + 1);
}
out.path_ = merge_paths(path_, path_part);
out.query_.clear();
if (!query_part.empty()) {
Url tmp = Url::parse("http://x/?" + query_part);
out.query_ = tmp.query_;
}
return out;
}
std::string Url::str() const {
std::string out;
if (!scheme_.empty()) {
out += scheme_;
out += "://";
if (!userinfo_.empty()) {
out += userinfo_;
out += '@';
}
out += host_;
if (port_ != 0 && port_ != default_port(scheme_)) {
out += ':';
out += std::to_string(port_);
}
}
out += path_.empty() ? "/" : path_;
std::string qs = query_string();
if (!qs.empty()) {
out += '?';
out += qs;
}
return out;
}
std::string Url::origin() const {
std::string out = scheme_ + "://" + host_;
if (port_ != 0 && port_ != default_port(scheme_)) {
out += ':';
out += std::to_string(port_);
}
return out;
}
std::string Url::target() const {
std::string out = path_.empty() ? "/" : path_;
std::string qs = query_string();
if (!qs.empty()) {
out += '?';
out += qs;
}
return out;
}
std::string Url::host_port() const {
return host_ + ":" + std::to_string(port_);
}
std::string Url::pool_key() const { return scheme_ + "://" + host_port(); }
std::optional<std::string> Url::query_value(const std::string& name) const {
for (const auto& kv : query_) {
if (kv.first == name) return kv.second;
}
return std::nullopt;
}
std::string Url::query_string() const {
std::string out;
for (const auto& kv : query_) {
if (!out.empty()) out.push_back('&');
out += encode_component(kv.first);
out.push_back('=');
out += encode_component(kv.second);
}
return out;
}
void Url::set_query_arg(const std::string& name, const std::string& value) {
for (auto& kv : query_) {
if (kv.first == name) {
kv.second = value;
return;
}
}
query_.emplace_back(name, value);
}
} // namespace molo::clients