Skip to content

Commit

Permalink
Make Headers an optional argument
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Sep 29, 2020
1 parent de86abb commit 5999978
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/libexpr/common-eval-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Path lookupFileArg(EvalState & state, string s)
if (isUri(s)) {
return state.store->toRealPath(
fetchers::downloadTarball(
state.store, resolveUri(s), Headers {}, "source", false).first.storePath);
state.store, resolveUri(s), "source", false).first.storePath);
} else if (s.size() > 2 && s.at(0) == '<' && s.at(s.size() - 1) == '>') {
Path p = s.substr(1, s.size() - 2);
return state.findFile(p);
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ std::pair<bool, std::string> EvalState::resolveSearchPathElem(const SearchPathEl
if (isUri(elem.second)) {
try {
res = { true, store->toRealPath(fetchers::downloadTarball(
store, resolveUri(elem.second), Headers {}, "source", false).first.storePath) };
store, resolveUri(elem.second), "source", false).first.storePath) };
} catch (FileTransferError & e) {
logWarning({
.name = "Entry download",
Expand Down
4 changes: 2 additions & 2 deletions src/libexpr/primops/fetchTree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ static void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,

auto storePath =
unpack
? fetchers::downloadTarball(state.store, *url, Headers {}, name, (bool) expectedHash).first.storePath
: fetchers::downloadFile(state.store, *url, Headers{}, name, (bool) expectedHash).storePath;
? fetchers::downloadTarball(state.store, *url, name, (bool) expectedHash).first.storePath
: fetchers::downloadFile(state.store, *url, name, (bool) expectedHash).storePath;

auto path = state.store->toRealPath(storePath);

Expand Down
8 changes: 4 additions & 4 deletions src/libfetchers/fetchers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ struct DownloadFileResult
DownloadFileResult downloadFile(
ref<Store> store,
const std::string & url,
const Headers & headers,
const std::string & name,
bool immutable);
bool immutable,
const Headers & headers = {});

std::pair<Tree, time_t> downloadTarball(
ref<Store> store,
const std::string & url,
const Headers & headers,
const std::string & name,
bool immutable);
bool immutable,
const Headers & headers = {});

}
6 changes: 3 additions & 3 deletions src/libfetchers/github.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ struct GitArchiveInputScheme : InputScheme
headers.push_back(*url.access_token_header);
}

auto [tree, lastModified] = downloadTarball(store, url.url, headers, "source", true);
auto [tree, lastModified] = downloadTarball(store, url.url, "source", true, headers);

input.attrs.insert_or_assign("lastModified", lastModified);

Expand Down Expand Up @@ -215,7 +215,7 @@ struct GitHubInputScheme : GitArchiveInputScheme
auto json = nlohmann::json::parse(
readFile(
store->toRealPath(
downloadFile(store, url, headers, "source", false).storePath)));
downloadFile(store, url, "source", false, headers).storePath)));
auto rev = Hash::parseAny(std::string { json["sha"] }, htSHA1);
debug("HEAD revision for '%s' is %s", url, rev.gitRev());
return rev;
Expand Down Expand Up @@ -271,7 +271,7 @@ struct GitLabInputScheme : GitArchiveInputScheme
auto json = nlohmann::json::parse(
readFile(
store->toRealPath(
downloadFile(store, url, headers, "source", false).storePath)));
downloadFile(store, url, "source", false, headers).storePath)));
auto rev = Hash::parseAny(std::string(json[0]["id"]), htSHA1);
debug("HEAD revision for '%s' is %s", url, rev.gitRev());
return rev;
Expand Down
2 changes: 1 addition & 1 deletion src/libfetchers/registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static std::shared_ptr<Registry> getGlobalRegistry(ref<Store> store)
auto path = settings.flakeRegistry.get();

if (!hasPrefix(path, "/")) {
auto storePath = downloadFile(store, path, Headers {}, "flake-registry.json", false).storePath;
auto storePath = downloadFile(store, path, "flake-registry.json", false).storePath;
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>())
store2->addPermRoot(storePath, getCacheDir() + "/nix/flake-registry.json");
path = store->toRealPath(storePath);
Expand Down
15 changes: 8 additions & 7 deletions src/libfetchers/tarball.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace nix::fetchers {
DownloadFileResult downloadFile(
ref<Store> store,
const std::string & url,
const Headers & headers,
const std::string & name,
bool immutable)
bool immutable,
const Headers & headers)
{
// FIXME: check store

Expand All @@ -38,7 +38,8 @@ DownloadFileResult downloadFile(
if (cached && !cached->expired)
return useCached();

FileTransferRequest request(url, headers);
FileTransferRequest request(url);
request.headers = headers;
if (cached)
request.expectedETag = getStrAttr(cached->infoAttrs, "etag");
FileTransferResult res;
Expand Down Expand Up @@ -112,9 +113,9 @@ DownloadFileResult downloadFile(
std::pair<Tree, time_t> downloadTarball(
ref<Store> store,
const std::string & url,
const Headers & headers,
const std::string & name,
bool immutable)
bool immutable,
const Headers & headers)
{
Attrs inAttrs({
{"type", "tarball"},
Expand All @@ -130,7 +131,7 @@ std::pair<Tree, time_t> downloadTarball(
getIntAttr(cached->infoAttrs, "lastModified")
};

auto res = downloadFile(store, url, headers, name, immutable);
auto res = downloadFile(store, url, name, immutable, headers);

std::optional<StorePath> unpackedStorePath;
time_t lastModified;
Expand Down Expand Up @@ -225,7 +226,7 @@ struct TarballInputScheme : InputScheme

std::pair<Tree, Input> fetch(ref<Store> store, const Input & input) override
{
auto tree = downloadTarball(store, getStrAttr(input.attrs, "url"), Headers {}, "source", false).first;
auto tree = downloadTarball(store, getStrAttr(input.attrs, "url"), "source", false).first;
return {std::move(tree), input};
}
};
Expand Down
3 changes: 0 additions & 3 deletions src/libstore/filetransfer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ struct FileTransferRequest
FileTransferRequest(const std::string & uri)
: uri(uri), parentAct(getCurActivity()) { }

FileTransferRequest(const std::string & uri, Headers headers)
: uri(uri), headers(headers) { }

std::string verb()
{
return data ? "upload" : "download";
Expand Down
6 changes: 3 additions & 3 deletions src/nix-channel/nix-channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static void update(const StringSet & channelNames)
// We want to download the url to a file to see if it's a tarball while also checking if we
// got redirected in the process, so that we can grab the various parts of a nix channel
// definition from a consistent location if the redirect changes mid-download.
auto result = fetchers::downloadFile(store, url, Headers {}, std::string(baseNameOf(url)), false);
auto result = fetchers::downloadFile(store, url, std::string(baseNameOf(url)), false);
auto filename = store->toRealPath(result.storePath);
url = result.effectiveUrl;

Expand All @@ -119,9 +119,9 @@ static void update(const StringSet & channelNames)
if (!unpacked) {
// Download the channel tarball.
try {
filename = store->toRealPath(fetchers::downloadFile(store, url + "/nixexprs.tar.xz", Headers {}, "nixexprs.tar.xz", false).storePath);
filename = store->toRealPath(fetchers::downloadFile(store, url + "/nixexprs.tar.xz", "nixexprs.tar.xz", false).storePath);
} catch (FileTransferError & e) {
filename = store->toRealPath(fetchers::downloadFile(store, url + "/nixexprs.tar.bz2", Headers {}, "nixexprs.tar.bz2", false).storePath);
filename = store->toRealPath(fetchers::downloadFile(store, url + "/nixexprs.tar.bz2", "nixexprs.tar.bz2", false).storePath);
}
}

Expand Down

0 comments on commit 5999978

Please sign in to comment.