Files
moloapp/src/app/negotiate/accept.h
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

99 lines
3.1 KiB
C++

#pragma once
// Spec: molo::app::negotiate Accept parser. Parses an Accept header into
// media ranges (type, subtype, q, params). Missing or empty header means
// */*. Invalid q values are treated as weight 0 (entry ignored), documented
// leniency choice. Range params other than q are parsed and ignored for
// matching (documented gap vs RFC 9110 section 12).
#include <cctype>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace molo::app::negotiate {
struct MediaRange {
std::string type;
std::string subtype;
double q = 1.0;
std::vector<std::pair<std::string, std::string>> params;
std::size_t order = 0;
};
inline std::string trim_ws(std::string_view s) {
std::size_t a = 0;
while (a < s.size() && (s[a] == ' ' || s[a] == '\t')) ++a;
std::size_t b = s.size();
while (b > a && (s[b - 1] == ' ' || s[b - 1] == '\t')) --b;
std::string out(s.substr(a, b - a));
for (char& c : out) c = (char)std::tolower((unsigned char)c);
return out;
}
inline double parse_q(std::string_view s) {
if (s.empty() || s.size() > 5) return 0.0;
try {
std::size_t used = 0;
double v = std::stod(std::string(s), &used);
if (used != s.size() || v < 0.0 || v > 1.0) return 0.0;
return v;
} catch (...) {
return 0.0;
}
}
inline std::vector<MediaRange> parse_accept(std::string_view header) {
std::vector<MediaRange> out;
if (trim_ws(header).empty()) {
out.push_back(MediaRange{"*", "*", 1.0, {}, 0});
return out;
}
std::size_t i = 0;
std::size_t order = 0;
while (i <= header.size()) {
std::size_t comma = header.find(',', i);
if (comma == std::string_view::npos) comma = header.size();
std::string_view entry = header.substr(i, comma - i);
std::size_t semi = entry.find(';');
std::string_view full = (semi == std::string_view::npos) ? entry : entry.substr(0, semi);
std::size_t slash = full.find('/');
if (slash != std::string_view::npos) {
MediaRange range;
range.type = trim_ws(full.substr(0, slash));
range.subtype = trim_ws(full.substr(slash + 1));
range.order = order++;
if (!range.type.empty() && !range.subtype.empty()) {
if (semi != std::string_view::npos) {
std::string_view rest = entry.substr(semi + 1);
std::size_t j = 0;
while (j <= rest.size()) {
std::size_t s2 = rest.find(';', j);
if (s2 == std::string_view::npos) s2 = rest.size();
std::string_view kv = rest.substr(j, s2 - j);
std::size_t eq = kv.find('=');
if (eq != std::string_view::npos) {
std::string k = trim_ws(kv.substr(0, eq));
std::string v = trim_ws(kv.substr(eq + 1));
if (k == "q") {
range.q = parse_q(v);
} else {
range.params.emplace_back(k, v);
}
}
if (s2 == rest.size()) break;
j = s2 + 1;
}
}
out.push_back(std::move(range));
}
}
if (comma == header.size()) break;
i = comma + 1;
}
if (out.empty()) out.push_back(MediaRange{"*", "*", 1.0, {}, 0});
return out;
}
} // namespace molo::app::negotiate