Skip to content

Commit

Permalink
SharedHttpCacheStorage doesn't resolve redirect correctly if the uri …
Browse files Browse the repository at this point in the history
…that is given isn't normalized #2666

make sure that the incoming uri param is normalized when used further
up. This way it is not an extra redirect (so there are 2 redirects to
the final source)
  • Loading branch information
jcompagner committed Jul 28, 2023
1 parent fbbc756 commit f426ca2
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ protected boolean removeEldestEntry(final Map.Entry<File, CacheLine> eldest) {
*/
@Override
public CacheEntry getCacheEntry(URI uri, Logger logger) throws FileNotFoundException {
CacheLine cacheLine = getCacheLine(uri);
URI normalized = uri.normalize();
CacheLine cacheLine = getCacheLine(normalized);
if (!cacheConfig.isUpdate()) { // if not updates are forced ...
int code = cacheLine.getResponseCode();
if (code == HttpURLConnection.HTTP_NOT_FOUND) {
throw new FileNotFoundException(uri.toASCIIString());
throw new FileNotFoundException(normalized.toASCIIString());
}
if (code == HttpURLConnection.HTTP_MOVED_PERM) {
return getCacheEntry(cacheLine.getRedirect(uri), logger);
return getCacheEntry(cacheLine.getRedirect(normalized), logger);
}
}
return new CacheEntry() {
Expand All @@ -106,19 +107,19 @@ public CacheEntry getCacheEntry(URI uri, Logger logger) throws FileNotFoundExcep
public long getLastModified(HttpTransportFactory transportFactory)
throws IOException {
if (cacheConfig.isOffline()) {
return cacheLine.getLastModified(uri, transportFactory,
return cacheLine.getLastModified(normalized, transportFactory,
SharedHttpCacheStorage::mavenIsOffline, logger);
}
try {
return cacheLine.fetchLastModified(uri, transportFactory, logger);
return cacheLine.fetchLastModified(normalized, transportFactory, logger);
} catch (FileNotFoundException | AuthenticationFailedException e) {
//for not found and failed authentication we can't do anything useful
throw e;
} catch (IOException e) {
if (!cacheConfig.isUpdate() && cacheLine.getResponseCode() > 0) {
//if we have something cached, use that ...
logger.warn("Request to " + uri + " failed, trying cache instead");
return cacheLine.getLastModified(uri, transportFactory, nil -> e, logger);
logger.warn("Request to " + normalized + " failed, trying cache instead");
return cacheLine.getLastModified(normalized, transportFactory, nil -> e, logger);
}
throw e;
}
Expand All @@ -128,19 +129,19 @@ public long getLastModified(HttpTransportFactory transportFactory)
public File getCacheFile(HttpTransportFactory transportFactory)
throws IOException {
if (cacheConfig.isOffline()) {
return cacheLine.getFile(uri, transportFactory,
return cacheLine.getFile(normalized, transportFactory,
SharedHttpCacheStorage::mavenIsOffline, logger);
}
try {
return cacheLine.fetchFile(uri, transportFactory, logger);
return cacheLine.fetchFile(normalized, transportFactory, logger);
} catch (FileNotFoundException | AuthenticationFailedException e) {
//for not found and failed authentication we can't do anything useful
throw e;
} catch (IOException e) {
if (!cacheConfig.isUpdate() && cacheLine.getResponseCode() > 0) {
//if we have something cached, use that ...
logger.warn("Request to " + uri + " failed, trying cache instead");
return cacheLine.getFile(uri, transportFactory, nil -> e, logger);
logger.warn("Request to " + normalized + " failed, trying cache instead");
return cacheLine.getFile(normalized, transportFactory, nil -> e, logger);
}
throw e;
}
Expand Down

0 comments on commit f426ca2

Please sign in to comment.