Skip to content

Commit

Permalink
fix: now handling more HTTP redirect types
Browse files Browse the repository at this point in the history
We were handling only a limited set of redirect types, this has now been
extended to include all common and not so common 3xx redirect codes.
This includes the somewhat strange 303 See Other where the spec isn't
completely clear but the general concensus seems to be that it forces
the next request to be a GET request.

Fixes #1769
  • Loading branch information
quintesse committed Mar 14, 2024
1 parent 998d82a commit bc32cd3
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/main/java/dev/jbang/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -1063,16 +1063,16 @@ private static String extractFileName(URLConnection urlConnection) throws IOExce
// extracts file name from header field
fileName = getDispositionFilename(disposition);
}
if (isBlankString(fileName)) {
// extracts file name from URL if nothing found
int p = fileURL.indexOf("?");
// Strip parameters from the URL (if any)
String simpleUrl = (p > 0) ? fileURL.substring(0, p) : fileURL;
while (simpleUrl.endsWith("/")) {
simpleUrl = simpleUrl.substring(0, simpleUrl.length() - 1);
}
fileName = simpleUrl.substring(simpleUrl.lastIndexOf("/") + 1);
}
if (isBlankString(fileName)) {
// extracts file name from URL if nothing found
int p = fileURL.indexOf("?");
// Strip parameters from the URL (if any)
String simpleUrl = (p > 0) ? fileURL.substring(0, p) : fileURL;
while (simpleUrl.endsWith("/")) {
simpleUrl = simpleUrl.substring(0, simpleUrl.length() - 1);
}
fileName = simpleUrl.substring(simpleUrl.lastIndexOf("/") + 1);
}
} else {
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1);
Expand All @@ -1087,17 +1087,27 @@ private static HttpURLConnection handleRedirects(HttpURLConnection httpConn, Con
while (true) {
httpConn.setInstanceFollowRedirects(false);
responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_PERM ||
if (responseCode == HttpURLConnection.HTTP_MULT_CHOICE ||
responseCode == HttpURLConnection.HTTP_MOVED_PERM ||
responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
responseCode == 307 /* TEMP REDIRECT */) {
responseCode == HttpURLConnection.HTTP_SEE_OTHER ||
responseCode == 307 /* TEMP REDIRECT */ ||
responseCode == 308 /* PERM REDIRECT */) {
if (redirects++ > 8) {
throw new IOException("Too many redirects");
}
String location = httpConn.getHeaderField("Location");
if (location == null) {
throw new IOException("No 'Location' header in redirect");
}
URL url = new URL(httpConn.getURL(), location);
url = new URL(swizzleURL(url.toString()));
verboseMsg("Redirected to: " + url); // Should be debug info
httpConn = (HttpURLConnection) url.openConnection();
if (responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
// This response code forces the method to GET
httpConn.setRequestMethod("GET");
}
configurator.configure(httpConn);
continue;
}
Expand Down

0 comments on commit bc32cd3

Please sign in to comment.