Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ object HttpMethod {
@JvmStatic // Despite being 'internal', this method is called by popular 3rd party SDKs.
fun permitsRequestBody(method: String): Boolean = !(method == "GET" || method == "HEAD")

fun redirectsWithBody(method: String): Boolean = method == "PROPFIND"
fun redirectsWithBody(method: String): Boolean = method == "PROPFIND" || method == "QUERY"

fun redirectsToGet(method: String): Boolean = method != "PROPFIND"
fun redirectsToGet(method: String): Boolean = method != "PROPFIND" && method != "QUERY"

fun isCacheable(requestMethod: String): Boolean = requestMethod == "GET" || requestMethod == "QUERY"
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,14 @@ class RetryAndFollowUpInterceptor : Interceptor {
val requestBuilder = userResponse.request.newBuilder()
if (HttpMethod.permitsRequestBody(method)) {
val responseCode = userResponse.code
val isQueryAndSeeOther = method == "QUERY" && responseCode == HTTP_SEE_OTHER
val maintainBody =
HttpMethod.redirectsWithBody(method) ||
(HttpMethod.redirectsWithBody(method) ||
responseCode == HTTP_PERM_REDIRECT ||
responseCode == HTTP_TEMP_REDIRECT
if (HttpMethod.redirectsToGet(method) && responseCode != HTTP_PERM_REDIRECT && responseCode != HTTP_TEMP_REDIRECT) {
responseCode == HTTP_TEMP_REDIRECT) && !isQueryAndSeeOther
if ((HttpMethod.redirectsToGet(method) || isQueryAndSeeOther) &&
responseCode != HTTP_PERM_REDIRECT && responseCode != HTTP_TEMP_REDIRECT
) {
requestBuilder.method("GET", null)
} else {
val requestBody = if (maintainBody) userResponse.request.body else null
Expand Down
90 changes: 90 additions & 0 deletions okhttp/src/jvmTest/kotlin/okhttp3/CallTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,96 @@ open class CallTest {
assertThat(page2.body?.utf8()).isEqualTo("Request Body")
}

@Test
fun queryRedirectsToQueryAndMaintainsRequestBodyOnMovedTemp() {
server.enqueue(
MockResponse(
code = HttpURLConnection.HTTP_MOVED_TEMP,
headers = headersOf("Location", "/page2"),
body = "This page has moved!",
),
)
server.enqueue(MockResponse(body = "Page 2"))

val response =
client
.newCall(
Request
.Builder()
.url(server.url("/page1"))
.query("Query Body".toRequestBody("text/plain".toMediaType()))
.build(),
).execute()

assertThat(response.body.string()).isEqualTo("Page 2")
val page1 = server.takeRequest()
assertThat(page1.requestLine).isEqualTo("QUERY /page1 HTTP/1.1")
assertThat(page1.body?.utf8()).isEqualTo("Query Body")
val page2 = server.takeRequest()
assertThat(page2.requestLine).isEqualTo("QUERY /page2 HTTP/1.1")
assertThat(page2.body?.utf8()).isEqualTo("Query Body")
}

@Test
fun queryRedirectsToQueryAndMaintainsRequestBodyOnMovedPerm() {
server.enqueue(
MockResponse(
code = HttpURLConnection.HTTP_MOVED_PERM,
headers = headersOf("Location", "/page2"),
body = "This page has moved!",
),
)
server.enqueue(MockResponse(body = "Page 2"))

val response =
client
.newCall(
Request
.Builder()
.url(server.url("/page1"))
.query("Query Body".toRequestBody("text/plain".toMediaType()))
.build(),
).execute()

assertThat(response.body.string()).isEqualTo("Page 2")
val page1 = server.takeRequest()
assertThat(page1.requestLine).isEqualTo("QUERY /page1 HTTP/1.1")
assertThat(page1.body?.utf8()).isEqualTo("Query Body")
val page2 = server.takeRequest()
assertThat(page2.requestLine).isEqualTo("QUERY /page2 HTTP/1.1")
assertThat(page2.body?.utf8()).isEqualTo("Query Body")
}

@Test
fun queryRedirectsToGetAndDropsRequestBodyOnSeeOther() {
server.enqueue(
MockResponse(
code = HttpURLConnection.HTTP_SEE_OTHER,
headers = headersOf("Location", "/page2"),
body = "See other page!",
),
)
server.enqueue(MockResponse(body = "Page 2"))

val response =
client
.newCall(
Request
.Builder()
.url(server.url("/page1"))
.query("Query Body".toRequestBody("text/plain".toMediaType()))
.build(),
).execute()

assertThat(response.body.string()).isEqualTo("Page 2")
val page1 = server.takeRequest()
assertThat(page1.requestLine).isEqualTo("QUERY /page1 HTTP/1.1")
assertThat(page1.body?.utf8()).isEqualTo("Query Body")
val page2 = server.takeRequest()
assertThat(page2.requestLine).isEqualTo("GET /page2 HTTP/1.1")
assertThat(page2.body).isNull()
}

@Test
fun responseCookies() {
server.enqueue(
Expand Down