Skip to content

Commit

Permalink
Support maxRedirects in URLConnections.checkFollowRedirect(...), re #11
Browse files Browse the repository at this point in the history
  • Loading branch information
safris committed Jun 11, 2023
1 parent 107a983 commit a8d99d7
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/libj/net/URLConnections.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ public static URLConnection checkFollowRedirect(URLConnection connection, final
beforeConnect.accept(httpURLConnection);

status = httpURLConnection.getResponseCode();
if (status < HttpURLConnection.HTTP_MOVED_PERM || HttpURLConnection.HTTP_SEE_OTHER < status)
if (status < HttpURLConnection.HTTP_MOVED_PERM || HttpURLConnection.HTTP_SEE_OTHER < status || 1 == maxRedirects)
return connection;

final LinkedHashSet<String> visited = new LinkedHashSet<>();
visited.add(location0);
visited.add(location);
do {
int i = 1; do {
location = httpURLConnection.getHeaderField("Location");
if (!visited.add(location))
throw new IOException("Infinite redirection loop: " + visited.stream().collect(Collectors.joining(" -> ")) + " -> " + location);
Expand All @@ -126,7 +126,7 @@ public static URLConnection checkFollowRedirect(URLConnection connection, final
beforeConnect.accept(httpURLConnection);

status = httpURLConnection.getResponseCode();
if (status < HttpURLConnection.HTTP_MOVED_PERM || HttpURLConnection.HTTP_SEE_OTHER < status)
if (status < HttpURLConnection.HTTP_MOVED_PERM || HttpURLConnection.HTTP_SEE_OTHER < status || ++i == maxRedirects)
return connection;
}
while (true);
Expand Down

0 comments on commit a8d99d7

Please sign in to comment.