Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache temporary redirections in the original location (#4459) #4547

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ public synchronized long getLastModified(URI uri, HttpTransportFactory transport

public synchronized File fetchFile(URI uri, HttpTransportFactory transportFactory, Logger logger)
throws IOException {
boolean exits = file.isFile();
if (exits && !mustValidate()) {
boolean exists = file.isFile();
if (exists && !mustValidate()) {
return file;
}
HttpTransport transport = transportFactory.createTransport(uri);
Properties lastHeader = getHeader();
if (exits) {
if (exists) {
if (lastHeader.containsKey(Headers.ETAG_HEADER.toLowerCase())) {
transport.setHeader("If-None-Match", lastHeader.getProperty(Headers.ETAG_HEADER.toLowerCase()));
}
Expand All @@ -242,7 +242,7 @@ public synchronized File fetchFile(URI uri, HttpTransportFactory transportFactor
return transport.get(response -> {
File tempFile;
int code = response.statusCode();
if (exits && code == HttpURLConnection.HTTP_NOT_MODIFIED) {
if (exists && code == HttpURLConnection.HTTP_NOT_MODIFIED) {
updateHeader(response, getResponseCode());
return file;
}
Expand All @@ -251,19 +251,37 @@ public synchronized File fetchFile(URI uri, HttpTransportFactory transportFactor
}
updateHeader(response, code);
if (isRedirected(code)) {
File cachedFile = SharedHttpCacheStorage.this.getCacheEntry(getRedirect(uri), logger)
.getCacheFile(transportFactory);
// https://github.com/eclipse-tycho/tycho/issues/2938
// Redirect may change extension. P2's SimpleMetadataRepositoryFactory relies on
// accurate file extension to be cached.
// Copying file to accommodate original request and its file extension.
// Once https://github.com/eclipse-equinox/p2/issues/355 is fixed, cachedFile
// may be returned directly without copying.
response.close(); // early close before doing unrelated file I/O
FileUtils.copyFile(cachedFile, file);
return file;
URI redirect = getRedirect(uri);
if (code == HttpURLConnection.HTTP_MOVED_TEMP) {
// https://github.com/eclipse-tycho/tycho/issues/4459
// Don't save temporary redirects since they might change later, rendering the
// cache entry useless. Save them in the original request URI instead.
logger.warn(
String.format("%s was temporarily redirected to %s but cached in the original location",
uri, redirect));
HttpTransport redirectTransport = transportFactory.createTransport(redirect);
redirectTransport.get(r -> {
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
r.transferTo(os);
}
return null;
});
return file;
} else {
File cachedFile = SharedHttpCacheStorage.this.getCacheEntry(redirect, logger)
.getCacheFile(transportFactory);
// https://github.com/eclipse-tycho/tycho/issues/2938
// Redirect may change extension. P2's SimpleMetadataRepositoryFactory relies on
// accurate file extension to be cached.
// Copying file to accommodate original request and its file extension.
// Once https://github.com/eclipse-equinox/p2/issues/355 is fixed, cachedFile
// may be returned directly without copying.
response.close(); // early close before doing unrelated file I/O
FileUtils.copyFile(cachedFile, file);
return file;
}
}
if (exits) {
if (exists) {
FileUtils.forceDelete(file);
}
response.checkResponseCode();
Expand Down
Loading