Skip to content

Commit 4b8ff01

Browse files
kysmith-csglaeubi
authored andcommitted
Cache temporary redirection in the original location
1 parent 16e06cf commit 4b8ff01

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/SharedHttpCacheStorage.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,13 @@ public synchronized long getLastModified(URI uri, HttpTransportFactory transport
223223

224224
public synchronized File fetchFile(URI uri, HttpTransportFactory transportFactory, Logger logger)
225225
throws IOException {
226-
boolean exits = file.isFile();
227-
if (exits && !mustValidate()) {
226+
boolean exists = file.isFile();
227+
if (exists && !mustValidate()) {
228228
return file;
229229
}
230230
HttpTransport transport = transportFactory.createTransport(uri);
231231
Properties lastHeader = getHeader();
232-
if (exits) {
232+
if (exists) {
233233
if (lastHeader.containsKey(Headers.ETAG_HEADER.toLowerCase())) {
234234
transport.setHeader("If-None-Match", lastHeader.getProperty(Headers.ETAG_HEADER.toLowerCase()));
235235
}
@@ -242,7 +242,7 @@ public synchronized File fetchFile(URI uri, HttpTransportFactory transportFactor
242242
return transport.get(response -> {
243243
File tempFile;
244244
int code = response.statusCode();
245-
if (exits && code == HttpURLConnection.HTTP_NOT_MODIFIED) {
245+
if (exists && code == HttpURLConnection.HTTP_NOT_MODIFIED) {
246246
updateHeader(response, getResponseCode());
247247
return file;
248248
}
@@ -251,19 +251,37 @@ public synchronized File fetchFile(URI uri, HttpTransportFactory transportFactor
251251
}
252252
updateHeader(response, code);
253253
if (isRedirected(code)) {
254-
File cachedFile = SharedHttpCacheStorage.this.getCacheEntry(getRedirect(uri), logger)
255-
.getCacheFile(transportFactory);
256-
// https://github.com/eclipse-tycho/tycho/issues/2938
257-
// Redirect may change extension. P2's SimpleMetadataRepositoryFactory relies on
258-
// accurate file extension to be cached.
259-
// Copying file to accommodate original request and its file extension.
260-
// Once https://github.com/eclipse-equinox/p2/issues/355 is fixed, cachedFile
261-
// may be returned directly without copying.
262-
response.close(); // early close before doing unrelated file I/O
263-
FileUtils.copyFile(cachedFile, file);
264-
return file;
254+
URI redirect = getRedirect(uri);
255+
if (code == HttpURLConnection.HTTP_MOVED_TEMP) {
256+
// https://github.com/eclipse-tycho/tycho/issues/4459
257+
// Don't save temporary redirects since they might change later, rendering the
258+
// cache entry useless. Save them in the original request URI instead.
259+
logger.warn(
260+
String.format("%s was temporarily redirected to %s but cached in the original location",
261+
uri, redirect));
262+
HttpTransport redirectTransport = transportFactory.createTransport(redirect);
263+
redirectTransport.get(r -> {
264+
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
265+
r.transferTo(os);
266+
}
267+
return null;
268+
});
269+
return file;
270+
} else {
271+
File cachedFile = SharedHttpCacheStorage.this.getCacheEntry(redirect, logger)
272+
.getCacheFile(transportFactory);
273+
// https://github.com/eclipse-tycho/tycho/issues/2938
274+
// Redirect may change extension. P2's SimpleMetadataRepositoryFactory relies on
275+
// accurate file extension to be cached.
276+
// Copying file to accommodate original request and its file extension.
277+
// Once https://github.com/eclipse-equinox/p2/issues/355 is fixed, cachedFile
278+
// may be returned directly without copying.
279+
response.close(); // early close before doing unrelated file I/O
280+
FileUtils.copyFile(cachedFile, file);
281+
return file;
282+
}
265283
}
266-
if (exits) {
284+
if (exists) {
267285
FileUtils.forceDelete(file);
268286
}
269287
response.checkResponseCode();

tycho-its/src/test/java/org/eclipse/tycho/test/tycho2938/ContentJarTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void redirectToBadLocation() throws Exception {
8686
String redirectedUrl = server.addRedirect("repoB", originalPath -> mainRepoUrl + originalPath + "_invalid");
8787
configureRepositoryInTargetDefinition(redirectedUrl);
8888
Assert.assertThrows(VerificationException.class, () -> verifier.executeGoal("package"));
89-
verifier.verifyTextInLog("No repository found at " + redirectedUrl);
89+
verifier.verifyTextInLog("Unable to read repository at " + redirectedUrl);
9090
assertVisited("/content.jar_invalid");
9191
}
9292

0 commit comments

Comments
 (0)