Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complies with rfc7231 about statuses 307 and 308 #5990

Merged
merged 3 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Complies with rfc7231 about statuses 307 and 308
Author:    Vladimir Kravets <vova.kravets@gmail.com>
  • Loading branch information
vkravets authored and yschimke committed Apr 25, 2020
commit 9a7f5bce415fd0177262369d0cd3e65e0606fe3d
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,7 @@ class RetryAndFollowUpInterceptor(private val client: OkHttpClient) : Intercepto

HTTP_UNAUTHORIZED -> return client.authenticator.authenticate(route, userResponse)

HTTP_PERM_REDIRECT, HTTP_TEMP_REDIRECT -> {
// "If the 307 or 308 status code is received in response to a request other than GET
// or HEAD, the user agent MUST NOT automatically redirect the request"
if (method != "GET" && method != "HEAD") {
return null
}
return buildRedirectRequest(userResponse, method)
}

HTTP_MULT_CHOICE, HTTP_MOVED_PERM, HTTP_MOVED_TEMP, HTTP_SEE_OTHER -> {
HTTP_PERM_REDIRECT, HTTP_TEMP_REDIRECT, HTTP_MULT_CHOICE, HTTP_MOVED_PERM, HTTP_MOVED_TEMP, HTTP_SEE_OTHER -> {
return buildRedirectRequest(userResponse, method)
}

Expand Down Expand Up @@ -312,8 +303,11 @@ class RetryAndFollowUpInterceptor(private val client: OkHttpClient) : Intercepto
// Most redirects don't include a request body.
val requestBuilder = userResponse.request.newBuilder()
if (HttpMethod.permitsRequestBody(method)) {
val maintainBody = HttpMethod.redirectsWithBody(method)
if (HttpMethod.redirectsToGet(method)) {
val responseCode = userResponse.code
val maintainBody = HttpMethod.redirectsWithBody(method) ||
responseCode == HTTP_PERM_REDIRECT ||
responseCode == HTTP_TEMP_REDIRECT
if (HttpMethod.redirectsToGet(method) && responseCode != HTTP_PERM_REDIRECT && responseCode != HTTP_TEMP_REDIRECT) {
requestBuilder.method("GET", null)
} else {
val requestBody = if (maintainBody) userResponse.request.body else null
Expand Down
9 changes: 0 additions & 9 deletions okhttp/src/test/java/okhttp3/URLConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2396,17 +2396,8 @@ private void testRedirect(boolean temporary, String method) throws Exception {
assertThat(responseString).isEqualTo("Page 2");
} else if (method.equals("HEAD")) {
assertThat(responseString).isEqualTo("");
} else {
// Methods other than GET/HEAD shouldn't follow the redirect.
if (method.equals("POST")) {
assertThat(page1.getBody().readUtf8()).isEqualTo("ABCD");
}
assertThat(server.getRequestCount()).isEqualTo(1);
assertThat(responseString).isEqualTo("This page has moved!");
return;
}

// GET/HEAD requests should have followed the redirect with the same method.
assertThat(server.getRequestCount()).isEqualTo(2);
RecordedRequest page2 = server.takeRequest();
assertThat(page2.getRequestLine()).isEqualTo((method + " /page2 HTTP/1.1"));
Expand Down