Skip to content

Commit 1b2f578

Browse files
authored
Merge pull request #4595 from dramforever/fetcher-http-redirect
libfetchers/tarball: Lock on effectiveUrl
2 parents e64cf8e + fc6bfb2 commit 1b2f578

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

src/libfetchers/fetchers.hh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,13 @@ DownloadFileResult downloadFile(
145145
bool immutable,
146146
const Headers & headers = {});
147147

148-
std::pair<Tree, time_t> downloadTarball(
148+
struct DownloadTarballMeta
149+
{
150+
time_t lastModified;
151+
std::string effectiveUrl;
152+
};
153+
154+
std::pair<Tree, DownloadTarballMeta> downloadTarball(
149155
ref<Store> store,
150156
const std::string & url,
151157
const std::string & name,

src/libfetchers/github.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,16 @@ struct GitArchiveInputScheme : InputScheme
207207

208208
auto url = getDownloadUrl(input);
209209

210-
auto [tree, lastModified] = downloadTarball(store, url.url, "source", true, url.headers);
210+
auto [tree, meta] = downloadTarball(store, url.url, "source", true, url.headers);
211211

212-
input.attrs.insert_or_assign("lastModified", uint64_t(lastModified));
212+
input.attrs.insert_or_assign("lastModified", uint64_t(meta.lastModified));
213213

214214
getCache()->add(
215215
store,
216216
immutableAttrs,
217217
{
218218
{"rev", rev->gitRev()},
219-
{"lastModified", uint64_t(lastModified)}
219+
{"lastModified", uint64_t(meta.lastModified)}
220220
},
221221
tree.storePath,
222222
true);

src/libfetchers/tarball.cc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ DownloadFileResult downloadFile(
109109
};
110110
}
111111

112-
std::pair<Tree, time_t> downloadTarball(
112+
std::pair<Tree, DownloadTarballMeta> downloadTarball(
113113
ref<Store> store,
114114
const std::string & url,
115115
const std::string & name,
@@ -127,7 +127,10 @@ std::pair<Tree, time_t> downloadTarball(
127127
if (cached && !cached->expired)
128128
return {
129129
Tree(store->toRealPath(cached->storePath), std::move(cached->storePath)),
130-
getIntAttr(cached->infoAttrs, "lastModified")
130+
{
131+
.lastModified = time_t(getIntAttr(cached->infoAttrs, "lastModified")),
132+
.effectiveUrl = maybeGetStrAttr(cached->infoAttrs, "effectiveUrl").value_or(url),
133+
},
131134
};
132135

133136
auto res = downloadFile(store, url, name, immutable, headers);
@@ -152,6 +155,7 @@ std::pair<Tree, time_t> downloadTarball(
152155

153156
Attrs infoAttrs({
154157
{"lastModified", uint64_t(lastModified)},
158+
{"effectiveUrl", res.effectiveUrl},
155159
{"etag", res.etag},
156160
});
157161

@@ -164,7 +168,10 @@ std::pair<Tree, time_t> downloadTarball(
164168

165169
return {
166170
Tree(store->toRealPath(*unpackedStorePath), std::move(*unpackedStorePath)),
167-
lastModified,
171+
{
172+
.lastModified = lastModified,
173+
.effectiveUrl = res.effectiveUrl,
174+
},
168175
};
169176
}
170177

@@ -223,9 +230,11 @@ struct TarballInputScheme : InputScheme
223230
return true;
224231
}
225232

226-
std::pair<Tree, Input> fetch(ref<Store> store, const Input & input) override
233+
std::pair<Tree, Input> fetch(ref<Store> store, const Input & _input) override
227234
{
228-
auto tree = downloadTarball(store, getStrAttr(input.attrs, "url"), "source", false).first;
235+
Input input(_input);
236+
auto [tree, meta] = downloadTarball(store, getStrAttr(input.attrs, "url"), "source", false);
237+
input.attrs.insert_or_assign("url", meta.effectiveUrl);
229238
return {std::move(tree), input};
230239
}
231240
};

0 commit comments

Comments
 (0)