feat(daemon,registry): filter watch events by prefix; answer OPTIONS
This commit is contained in:
1 parent
e7698a411d
commit
e61eff7a8f
9 files changed
+208
-11
No files matched your search
@@ -204,6 +204,51 @@ std::vector<std::string> split_path(std::string_view path) {
|
||||
return segs;
|
||||
}
|
||||
|
||||
// OPTIONS is a CORS-style preflight: the advertised methods depend only on the route shape, so
|
||||
// this never authenticates or inspects package/release metadata.
|
||||
std::optional<std::string_view> allowed_methods_for(const std::vector<std::string> &segs) {
|
||||
if (segs.size() < 3 || segs[0] != "api" || segs[1] != "v1" || segs[2] != "packages") {
|
||||
return std::nullopt;
|
||||
}
|
||||
switch (segs.size()) {
|
||||
case 3:
|
||||
return "GET, HEAD, OPTIONS";
|
||||
case 4:
|
||||
return "GET, HEAD, PATCH, OPTIONS"; // <name>
|
||||
case 5:
|
||||
if (segs[4] == "owners") {
|
||||
return "GET, HEAD, PUT, POST, DELETE, OPTIONS";
|
||||
}
|
||||
return "GET, HEAD, PUT, OPTIONS"; // <version>
|
||||
case 6:
|
||||
if (segs[4] == "owners") {
|
||||
return "PUT, POST, DELETE, OPTIONS";
|
||||
}
|
||||
if (segs[5] == "download" || segs[5] == "binaries") {
|
||||
return "GET, HEAD, OPTIONS";
|
||||
}
|
||||
if (segs[5] == "yank") {
|
||||
return "POST, OPTIONS";
|
||||
}
|
||||
return std::nullopt;
|
||||
case 7:
|
||||
if (segs[5] == "binaries") {
|
||||
return "GET, HEAD, PUT, OPTIONS";
|
||||
}
|
||||
return std::nullopt;
|
||||
case 8:
|
||||
if (segs[5] == "binaries" && segs[7] == "download") {
|
||||
return "GET, HEAD, OPTIONS";
|
||||
}
|
||||
if (segs[5] == "binaries" && segs[7] == "yank") {
|
||||
return "POST, OPTIONS";
|
||||
}
|
||||
return std::nullopt;
|
||||
default:
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
const JsonVal *find_key(const JsonVal::Object &obj, const std::string &k) {
|
||||
const auto it = obj.find(k);
|
||||
return it == obj.end() ? nullptr : &it->second;
|
||||
@@ -723,9 +768,22 @@ HttpResponse RegistryService::handle_request(const HttpRequest &req) {
|
||||
return serve_static_web_asset(req.path);
|
||||
}
|
||||
|
||||
std::lock_guard lk{mu_};
|
||||
const auto segs = split_path(req.path);
|
||||
|
||||
// OPTIONS preflight: answer from the route shape alone, before authentication.
|
||||
if (req.method == "OPTIONS") {
|
||||
if (const auto allow = allowed_methods_for(segs)) {
|
||||
HttpResponse resp;
|
||||
resp.status = 204;
|
||||
resp.reason = "No Content";
|
||||
resp.headers["Allow"] = std::string{*allow};
|
||||
return resp;
|
||||
}
|
||||
return HttpResponse::error_json(404, "not_found", "unknown API endpoint");
|
||||
}
|
||||
|
||||
std::lock_guard lk{mu_};
|
||||
|
||||
// Health endpoint.
|
||||
if (req.method == "GET" && req.path == "/api/v1/health") {
|
||||
return HttpResponse::json(200, "{\"status\":\"ok\",\"schema_version\":1}\n");
|
||||
|
||||
Reference in new issue
Block a user