1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-25 16:01:38 +03:00

Merge pull request #93 from square/jwilson/redirects

Follow up to 20 redirects.
This commit is contained in:
Jake Wharton
2013-01-30 21:55:45 -08:00
2 changed files with 40 additions and 4 deletions

View File

@@ -57,10 +57,10 @@ import javax.net.ssl.SSLHandshakeException;
*/
public class HttpURLConnectionImpl extends HttpURLConnection {
/**
* HTTP 1.1 doesn't specify how many redirects to follow, but HTTP/1.0
* recommended 5. http://www.w3.org/Protocols/HTTP/1.0/spec.html#Code3xx
* How many redirects should we follow? Chrome follows 21; Firefox, curl,
* and wget follow 20; Safari follows 16; and HTTP/1.0 recommends 5.
*/
private static final int MAX_REDIRECTS = 5;
private static final int MAX_REDIRECTS = 20;
private final int defaultPort;
@@ -418,7 +418,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
return Retry.NONE;
}
if (++redirectionCount > MAX_REDIRECTS) {
throw new ProtocolException("Too many redirects");
throw new ProtocolException("Too many redirects: " + redirectionCount);
}
String location = getHeaderField("Location");
if (location == null) {

View File

@@ -1682,6 +1682,42 @@ public final class URLConnectionTest {
assertEquals(1, server.getRequestCount());
}
@Test public void follow20Redirects() throws Exception {
for (int i = 0; i < 20; i++) {
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.addHeader("Location: /" + (i + 1))
.setBody("Redirecting to /" + (i + 1)));
}
server.enqueue(new MockResponse().setBody("Success!"));
server.play();
HttpURLConnection connection = client.open(server.getUrl("/0"));
assertContent("Success!", connection);
assertEquals(server.getUrl("/20"), connection.getURL());
}
@Test public void doesNotFollow21Redirects() throws Exception {
for (int i = 0; i < 21; i++) {
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.addHeader("Location: /" + (i + 1))
.setBody("Redirecting to /" + (i + 1)));
}
server.play();
HttpURLConnection connection = client.open(server.getUrl("/0"));
try {
connection.getInputStream();
fail();
} catch (ProtocolException expected) {
assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, connection.getResponseCode());
assertEquals("Too many redirects: 21", expected.getMessage());
assertContent("Redirecting to /21", connection);
assertEquals(server.getUrl("/20"), connection.getURL());
}
}
@Test public void httpsWithCustomTrustManager() throws Exception {
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
RecordingTrustManager trustManager = new RecordingTrustManager();