use of okhttp3.internal.http.StatusLine.HTTP_TEMP_REDIRECT in project okhttp by square.
the class RetryAndFollowUpInterceptor method followUpRequest.
/**
* Figures out the HTTP request to make in response to receiving {@code userResponse}. This will
* either add authentication headers, follow redirects or handle a client request timeout. If a
* follow-up is either unnecessary or not applicable, this returns null.
*/
private Request followUpRequest(Response userResponse) throws IOException {
if (userResponse == null)
throw new IllegalStateException();
Connection connection = streamAllocation.connection();
Route route = connection != null ? connection.route() : null;
int responseCode = userResponse.code();
final String method = userResponse.request().method();
switch(responseCode) {
case HTTP_PROXY_AUTH:
Proxy selectedProxy = route != null ? route.proxy() : client.proxy();
if (selectedProxy.type() != Proxy.Type.HTTP) {
throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
}
return client.proxyAuthenticator().authenticate(route, userResponse);
case HTTP_UNAUTHORIZED:
return client.authenticator().authenticate(route, userResponse);
case HTTP_PERM_REDIRECT:
case HTTP_TEMP_REDIRECT:
// or HEAD, the user agent MUST NOT automatically redirect the request"
if (!method.equals("GET") && !method.equals("HEAD")) {
return null;
}
// fall-through
case HTTP_MULT_CHOICE:
case HTTP_MOVED_PERM:
case HTTP_MOVED_TEMP:
case HTTP_SEE_OTHER:
// Does the client allow redirects?
if (!client.followRedirects())
return null;
String location = userResponse.header("Location");
if (location == null)
return null;
HttpUrl url = userResponse.request().url().resolve(location);
// Don't follow redirects to unsupported protocols.
if (url == null)
return null;
// If configured, don't follow redirects between SSL and non-SSL.
boolean sameScheme = url.scheme().equals(userResponse.request().url().scheme());
if (!sameScheme && !client.followSslRedirects())
return null;
// Most redirects don't include a request body.
Request.Builder requestBuilder = userResponse.request().newBuilder();
if (HttpMethod.permitsRequestBody(method)) {
final boolean maintainBody = HttpMethod.redirectsWithBody(method);
if (HttpMethod.redirectsToGet(method)) {
requestBuilder.method("GET", null);
} else {
RequestBody requestBody = maintainBody ? userResponse.request().body() : null;
requestBuilder.method(method, requestBody);
}
if (!maintainBody) {
requestBuilder.removeHeader("Transfer-Encoding");
requestBuilder.removeHeader("Content-Length");
requestBuilder.removeHeader("Content-Type");
}
}
// way to retain them.
if (!sameConnection(userResponse, url)) {
requestBuilder.removeHeader("Authorization");
}
return requestBuilder.url(url).build();
case HTTP_CLIENT_TIMEOUT:
// repeat the request (even non-idempotent ones.)
if (userResponse.request().body() instanceof UnrepeatableRequestBody) {
return null;
}
return userResponse.request();
default:
return null;
}
}
use of okhttp3.internal.http.StatusLine.HTTP_TEMP_REDIRECT in project okhttp by square.
the class URLConnectionTest method testRedirect.
private void testRedirect(boolean temporary, String method) throws Exception {
MockResponse response1 = new MockResponse().setResponseCode(temporary ? HTTP_TEMP_REDIRECT : HTTP_PERM_REDIRECT).addHeader("Location: /page2");
if (!method.equals("HEAD")) {
response1.setBody("This page has moved!");
}
server.enqueue(response1);
server.enqueue(new MockResponse().setBody("Page 2"));
connection = urlFactory.open(server.url("/page1").url());
connection.setRequestMethod(method);
byte[] requestBody = { 'A', 'B', 'C', 'D' };
if (method.equals("POST")) {
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBody);
outputStream.close();
}
String response = readAscii(connection.getInputStream(), Integer.MAX_VALUE);
RecordedRequest page1 = server.takeRequest();
assertEquals(method + " /page1 HTTP/1.1", page1.getRequestLine());
if (method.equals("GET")) {
assertEquals("Page 2", response);
} else if (method.equals("HEAD")) {
assertEquals("", response);
} else {
// Methods other than GET/HEAD shouldn't follow the redirect
if (method.equals("POST")) {
assertTrue(connection.getDoOutput());
assertEquals("ABCD", page1.getBody().readUtf8());
}
assertEquals(1, server.getRequestCount());
assertEquals("This page has moved!", response);
return;
}
// GET/HEAD requests should have followed the redirect with the same method
assertFalse(connection.getDoOutput());
assertEquals(2, server.getRequestCount());
RecordedRequest page2 = server.takeRequest();
assertEquals(method + " /page2 HTTP/1.1", page2.getRequestLine());
}
Aggregations