Skip to content

Commit

Permalink
Cache temporary redirections in the original location
Browse files Browse the repository at this point in the history
  • Loading branch information
kysmith-csg committed Dec 20, 2024
1 parent edb9113 commit df64a9b
Showing 1 changed file with 34 additions and 16 deletions.
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

0 comments on commit df64a9b

Please sign in to comment.