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

Ignore Early Hints (5.x) #7444

Merged
merged 1 commit into from
Oct 16, 2022
Merged
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 @@ -18,7 +18,9 @@ package okhttp3.internal.http
import java.io.IOException
import java.net.ProtocolException
import okhttp3.Interceptor
import okhttp3.Protocol
import okhttp3.Response
import okhttp3.internal.connection.Exchange
import okhttp3.internal.http2.ConnectionShutdownException
import okhttp3.internal.stripBody
import okio.buffer
Expand Down Expand Up @@ -103,9 +105,8 @@ class CallServerInterceptor(private val forWebSocket: Boolean) : Interceptor {
.receivedResponseAtMillis(System.currentTimeMillis())
.build()
var code = response.code
if (code == 100) {
// Server sent a 100-continue even though we did not request one. Try again to read the
// actual response status.

if (shouldIgnoreAndWaitForRealResponse(code, exchange)) {
responseBuilder = exchange.readResponseHeaders(expectContinue = false)!!
if (invokeStartEvent) {
exchange.responseHeadersStart()
Expand Down Expand Up @@ -146,4 +147,15 @@ class CallServerInterceptor(private val forWebSocket: Boolean) : Interceptor {
throw e
}
}

private fun shouldIgnoreAndWaitForRealResponse(code: Int, exchange: Exchange): Boolean = when {
// Server sent a 100-continue even though we did not request one. Try again to read the
// actual response status.
code == 100 -> true

// Early Hints (103) but not supported yet in OkHttp
code == 103 -> true

else -> false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import okhttp3.internal.http.StatusLine
import okhttp3.internal.http.promisesBody
import okhttp3.internal.http.receiveHeaders
import okhttp3.internal.http.HTTP_CONTINUE
import okhttp3.internal.http.HTTP_EARLY_HINTS
import okhttp3.internal.skipAll
import okio.Buffer
import okio.BufferedSink
Expand Down Expand Up @@ -192,6 +193,11 @@ class Http1ExchangeCodec(
state = STATE_READ_RESPONSE_HEADERS
responseBuilder
}
statusLine.code == HTTP_EARLY_HINTS -> {
// Early Hints will mean a second header are coming.
state = STATE_READ_RESPONSE_HEADERS
responseBuilder
}
else -> {
state = STATE_OPEN_RESPONSE_BODY
responseBuilder
Expand Down
28 changes: 28 additions & 0 deletions okhttp/src/jvmTest/java/okhttp3/CallTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2741,6 +2741,34 @@ open class CallTest {
assertThat(recordedRequest.body.readUtf8()).isEqualTo("abc")
}

@Test fun serverRespondsWithEarlyHintsHttp2() {
enableProtocol(Protocol.HTTP_2)
serverRespondsWithEarlyHints()
}

@Test fun serverRespondsWithEarlyHints() {
val mockResponse = MockResponse()
server.enqueue(
mockResponse.apply {
addInformationalResponse(
MockResponse()
.setResponseCode(103)
.setHeaders(headersOf("Link", "</style.css>; rel=preload; as=style"))
)
}
)
val request = Request(
url = server.url("/"),
body = "abc".toRequestBody("text/plain".toMediaType()),
)
executeSynchronously(request)
.assertCode(200)
.assertSuccessful()
val recordedRequest = server.takeRequest()
assertThat(recordedRequest.body.readUtf8()).isEqualTo("abc")
assertThat(recordedRequest.headers["Link"]).isNull()
}

@Test fun serverRespondsWithUnsolicited100Continue_HTTP2() {
enableProtocol(Protocol.HTTP_2)
serverRespondsWithUnsolicited100Continue()
Expand Down