feat(registry,cli): RFC 7232 conditional requests; require trusted verify keys
This commit is contained in:
1 parent
27d909fdad
commit
ea6ac852e7
9 files changed
+138
-6
No files matched your search
@@ -46,6 +46,36 @@ std::optional<HttpResponse> not_modified_for(const HttpRequest &req, const std::
|
||||
return resp;
|
||||
}
|
||||
|
||||
// Weak comparison of an If-None-Match field value against a strong ETag per RFC 7232
|
||||
// section 3.2: `*` matches any current representation, list members compare by opaque tag,
|
||||
// and the W/ weakness prefix is ignored on both sides.
|
||||
bool if_none_match_matches(std::string_view field, std::string_view etag) {
|
||||
const auto trim = [](std::string_view text) {
|
||||
while (!text.empty() && (text.front() == ' ' || text.front() == '\t')) {
|
||||
text.remove_prefix(1);
|
||||
}
|
||||
while (!text.empty() && (text.back() == ' ' || text.back() == '\t')) {
|
||||
text.remove_suffix(1);
|
||||
}
|
||||
return text;
|
||||
};
|
||||
const auto opaque_tag = [&](std::string_view tag) {
|
||||
tag = trim(tag);
|
||||
if (tag.starts_with("W/")) {
|
||||
tag.remove_prefix(2);
|
||||
}
|
||||
return tag;
|
||||
};
|
||||
const auto expected = opaque_tag(etag);
|
||||
for (const auto part : Void::Str::split(field, ',')) {
|
||||
const std::string_view candidate = trim(part);
|
||||
if (candidate == "*" || opaque_tag(candidate) == expected) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Adds a strong ETag to a cacheable 200 response and honours If-None-Match with 304.
|
||||
HttpResponse with_etag(HttpResponse resp, const HttpRequest &req) {
|
||||
if (resp.status != 200) {
|
||||
@@ -54,10 +84,11 @@ HttpResponse with_etag(HttpResponse resp, const HttpRequest &req) {
|
||||
std::string etag = "\"" + Util::sha256_hex(resp.body).substr(0, 32) + "\"";
|
||||
resp.headers["ETag"] = etag;
|
||||
resp.headers["Cache-Control"] = "no-cache";
|
||||
if (const auto inm = req.header("if-none-match"); inm && *inm == etag) {
|
||||
if (const auto inm = req.header("if-none-match"); inm && if_none_match_matches(*inm, etag)) {
|
||||
HttpResponse not_modified;
|
||||
not_modified.status = 304;
|
||||
not_modified.headers["ETag"] = etag;
|
||||
not_modified.headers["Cache-Control"] = "no-cache";
|
||||
return not_modified;
|
||||
}
|
||||
return resp;
|
||||
|
||||
Reference in new issue
Block a user