feat(registry): support single-range downloads; extend install tests
This commit is contained in:
1 parent
3fa7ab086f
commit
c6436a8010
7 files changed
+345
-17
No files matched your search
@@ -30,6 +30,72 @@ Void::Result::Error make_error(Void::System::SystemErrorCode code, std::string m
|
||||
return Void::System::make_system_error(code, std::move(msg));
|
||||
}
|
||||
|
||||
// Applies a single-range `Range: bytes=start-end` request to a download response. Always
|
||||
// advertises Accept-Ranges; unsatisfiable or malformed ranges produce 416 with Content-Range.
|
||||
HttpResponse apply_range(HttpResponse resp, std::string_view range_header) {
|
||||
const auto total = resp.body.size();
|
||||
resp.headers["Accept-Ranges"] = "bytes";
|
||||
if (range_header.empty()) {
|
||||
return resp;
|
||||
}
|
||||
const auto reject = [&] {
|
||||
HttpResponse out;
|
||||
out.status = 416;
|
||||
out.headers["Accept-Ranges"] = "bytes";
|
||||
out.headers["Content-Range"] = std::format("bytes */{}", total);
|
||||
return out;
|
||||
};
|
||||
if (!range_header.starts_with("bytes=")) {
|
||||
return reject();
|
||||
}
|
||||
std::string_view spec = range_header.substr(6);
|
||||
if (spec.find(',') != std::string_view::npos) {
|
||||
return reject(); // Multipart ranges are not supported.
|
||||
}
|
||||
const auto dash = spec.find('-');
|
||||
if (dash == std::string_view::npos) {
|
||||
return reject();
|
||||
}
|
||||
const auto parse_index = [](std::string_view text, std::size_t &out) {
|
||||
if (text.empty()) {
|
||||
return false;
|
||||
}
|
||||
std::size_t value = 0;
|
||||
const auto [ptr, ec] = std::from_chars(text.data(), text.data() + text.size(), value);
|
||||
if (ec != std::errc{} || ptr != text.data() + text.size()) {
|
||||
return false;
|
||||
}
|
||||
out = value;
|
||||
return true;
|
||||
};
|
||||
const auto first = spec.substr(0, dash);
|
||||
const auto second = spec.substr(dash + 1);
|
||||
std::size_t start = 0;
|
||||
std::size_t end = total == 0 ? 0 : total - 1;
|
||||
if (first.empty()) {
|
||||
std::size_t suffix = 0;
|
||||
if (!parse_index(second, suffix) || suffix == 0) {
|
||||
return reject();
|
||||
}
|
||||
start = suffix >= total ? 0 : total - suffix;
|
||||
} else {
|
||||
if (!parse_index(first, start)) {
|
||||
return reject();
|
||||
}
|
||||
if (!second.empty() && !parse_index(second, end)) {
|
||||
return reject();
|
||||
}
|
||||
}
|
||||
if (total == 0 || start >= total || start > end) {
|
||||
return reject();
|
||||
}
|
||||
end = std::min(end, total - 1);
|
||||
resp.status = 206;
|
||||
resp.headers["Content-Range"] = std::format("bytes {}-{}/{}", start, end, total);
|
||||
resp.body = resp.body.substr(start, end - start + 1);
|
||||
return resp;
|
||||
}
|
||||
|
||||
std::int64_t now_epoch() {
|
||||
return std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
@@ -1162,7 +1228,8 @@ HttpResponse RegistryService::handle_request(const HttpRequest &req) {
|
||||
resp.headers["Content-Type"] = "application/x-tar";
|
||||
resp.headers["X-Content-SHA256"] = rit->second.sha256;
|
||||
resp.body = std::move(*bytes);
|
||||
return resp;
|
||||
const auto range = req.header("range");
|
||||
return apply_range(std::move(resp), range ? *range : std::string_view{});
|
||||
}
|
||||
|
||||
// Publish a prebuilt binary variant:
|
||||
@@ -1350,7 +1417,8 @@ HttpResponse RegistryService::handle_request(const HttpRequest &req) {
|
||||
resp.headers["Content-Type"] = "application/octet-stream";
|
||||
resp.headers["X-Content-SHA256"] = bit->second.sha256;
|
||||
resp.body = std::move(*bytes);
|
||||
return resp;
|
||||
const auto range = req.header("range");
|
||||
return apply_range(std::move(resp), range ? *range : std::string_view{});
|
||||
}
|
||||
|
||||
// List binary variants: GET /api/v1/packages/<name>/<version>/binaries
|
||||
|
||||
Reference in new issue
Block a user