1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-27 04:22:07 +03:00

Merge pull request #91 from square/jwilson/redirect_retry

Retry if a request fails after a redirect.
This commit is contained in:
Jake Wharton
2013-01-30 21:10:02 -08:00
2 changed files with 25 additions and 4 deletions

View File

@@ -326,6 +326,8 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
if (retry == Retry.DIFFERENT_CONNECTION) {
httpEngine.automaticallyReleaseConnectionToPool();
} else if (retry == Retry.SAME_CONNECTION && httpEngine.getConnection() != null) {
httpEngine.getConnection().setRecycled();
}
httpEngine.release(false);
@@ -349,15 +351,16 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
return true;
} catch (IOException e) {
RouteSelector routeSelector = httpEngine.routeSelector;
if (routeSelector == null) {
throw e; // Without a route selector, we can't retry.
} else if (httpEngine.connection != null) {
if (routeSelector != null && httpEngine.connection != null) {
routeSelector.connectFailed(httpEngine.connection, e);
}
if (routeSelector == null && httpEngine.connection == null) {
throw e; // If we failed before finding a route or a connection, give up.
}
// The connection failure isn't fatal if there's another route to attempt.
OutputStream requestBody = httpEngine.getRequestBody();
if (routeSelector.hasNext() && isRecoverable(e)
if ((routeSelector == null || routeSelector.hasNext()) && isRecoverable(e)
&& (requestBody == null || requestBody instanceof RetryableOutputStream)) {
httpEngine.release(true);
httpEngine = newHttpEngine(method, rawRequestHeaders, null,

View File

@@ -1794,6 +1794,24 @@ public final class URLConnectionTest {
0, server.takeRequest().getSequenceNumber());
}
/**
* Retry redirects if the socket is closed.
* https://code.google.com/p/android/issues/detail?id=41576
*/
@Test public void sameConnectionRedirectAndReuse() throws Exception {
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.setSocketPolicy(SHUTDOWN_INPUT_AT_END)
.addHeader("Location: /foo"));
server.enqueue(new MockResponse().setBody("This is the new page!"));
server.play();
assertContent("This is the new page!", client.open(server.getUrl("/")));
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(0, server.takeRequest().getSequenceNumber());
}
@Test public void responseCodeDisagreesWithHeaders() throws IOException, InterruptedException {
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)