Skip to content

Commit

Permalink
Ignore early hints for 4.9.x (#7444)
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke committed Oct 16, 2022
1 parent 6e5dfe7 commit b565eec
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
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 @@ -2744,6 +2744,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

0 comments on commit b565eec

Please sign in to comment.