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 c2374dc
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/main/java/org/libj/net/URLConnections.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ public static URLConnection checkFollowRedirect(final URLConnection connection,
* @param beforeConnect The {@link Consumer} to be called before this method invokes {@link HttpURLConnection#getResponseCode()}
* on the provided {@link URLConnection}.
* @return An {@link InputStream} to the specified url that may or may not exist at a redirected location.
* @throws IOException If an I/O error has occurred, or if the redirects are found to loop.
* @throws IllegalArgumentException If {@code maxRedirects} is negative.
* @throws NullPointerException If the provided {@link URLConnection} is null.
* @throws IOException If an I/O error has occurred, or if the redirects are found to loop.
*/
public static URLConnection checkFollowRedirect(URLConnection connection, final int maxRedirects, final ThrowingConsumer<HttpURLConnection,IOException> beforeConnect) throws IOException {
assertPositive(maxRedirects);
assertNotNegative(maxRedirects);

if (!(connection instanceof HttpURLConnection))
return connection;
Expand All @@ -86,7 +87,7 @@ public static URLConnection checkFollowRedirect(URLConnection connection, final
beforeConnect.accept(httpURLConnection);

int 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 || 0 == maxRedirects)
return connection;

final String location0 = httpURLConnection.getURL().toString();
Expand All @@ -105,13 +106,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 +127,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 c2374dc

Please sign in to comment.