From d857a4be5022711b91133a16dceb39b7869637a3 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Costa Date: Thu, 30 Oct 2025 02:36:28 +0000 Subject: [PATCH] refactor(libstore): add `HttpBinaryCacheStore::upload` method Introduce protected `upload` method overloads in `HttpBinaryCacheStore` that handle the actual upload after compression has been applied. This separates compression concerns (in `upsertFile`) from upload mechanics (in `upload`). Two overloads are provided: 1. `upload(path, RestartableSource &, sizeHint, mimeType, contentEncoding)` 2. `upload(path, CompressedSource &, mimeType)` --- src/libstore/http-binary-cache-store.cc | 38 +++++++++++++------ .../nix/store/http-binary-cache-store.hh | 31 +++++++++++++++ 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/libstore/http-binary-cache-store.cc b/src/libstore/http-binary-cache-store.cc index 8f37bba3e..fdb7c74fc 100644 --- a/src/libstore/http-binary-cache-store.cc +++ b/src/libstore/http-binary-cache-store.cc @@ -134,27 +134,41 @@ bool HttpBinaryCacheStore::fileExists(const std::string & path) } } -void HttpBinaryCacheStore::upsertFile( - const std::string & path, RestartableSource & source, const std::string & mimeType, uint64_t sizeHint) +void HttpBinaryCacheStore::upload( + std::string_view path, + RestartableSource & source, + uint64_t sizeHint, + std::string_view mimeType, + std::optional contentEncoding) { auto req = makeRequest(path); req.method = HttpMethod::PUT; - auto compressionMethod = getCompressionMethod(path); - std::optional compressed; - - if (compressionMethod) { - compressed = CompressedSource(source, *compressionMethod); - req.headers.emplace_back("Content-Encoding", *compressionMethod); - req.data = {compressed->size(), *compressed}; - } else { - req.data = {sizeHint, source}; + if (contentEncoding) { + req.headers.emplace_back("Content-Encoding", *contentEncoding); } + req.data = {sizeHint, source}; req.mimeType = mimeType; + getFileTransfer()->upload(req); +} + +void HttpBinaryCacheStore::upload(std::string_view path, CompressedSource & source, std::string_view mimeType) +{ + upload(path, static_cast(source), source.size(), mimeType, source.getCompressionMethod()); +} + +void HttpBinaryCacheStore::upsertFile( + const std::string & path, RestartableSource & source, const std::string & mimeType, uint64_t sizeHint) +{ try { - getFileTransfer()->upload(req); + if (auto compressionMethod = getCompressionMethod(path)) { + CompressedSource compressed(source, *compressionMethod); + upload(path, compressed, mimeType); + } else { + upload(path, source, sizeHint, mimeType, std::nullopt); + } } catch (FileTransferError & e) { UploadToHTTP err(e.message()); err.addTrace({}, "while uploading to HTTP binary cache at '%s'", config->cacheUri.to_string()); diff --git a/src/libstore/include/nix/store/http-binary-cache-store.hh b/src/libstore/include/nix/store/http-binary-cache-store.hh index 70ba36feb..b092b5b5e 100644 --- a/src/libstore/include/nix/store/http-binary-cache-store.hh +++ b/src/libstore/include/nix/store/http-binary-cache-store.hh @@ -85,6 +85,37 @@ protected: FileTransferRequest makeRequest(std::string_view path); + /** + * Uploads data to the binary cache. + * + * This is a lower-level method that handles the actual upload after + * compression has been applied. It does not handle compression or + * error wrapping - those are the caller's responsibility. + * + * @param path The path in the binary cache to upload to + * @param source The data source (should already be compressed if needed) + * @param sizeHint Size hint for the data + * @param mimeType The MIME type of the content + * @param contentEncoding Optional Content-Encoding header value (e.g., "xz", "br") + */ + void upload( + std::string_view path, + RestartableSource & source, + uint64_t sizeHint, + std::string_view mimeType, + std::optional contentEncoding); + + /** + * Uploads data to the binary cache (CompressedSource overload). + * + * This overload infers both the size and compression method from the CompressedSource. + * + * @param path The path in the binary cache to upload to + * @param source The compressed source (knows size and compression method) + * @param mimeType The MIME type of the content + */ + void upload(std::string_view path, CompressedSource & source, std::string_view mimeType); + void getFile(const std::string & path, Sink & sink) override; void getFile(const std::string & path, Callback> callback) noexcept override;