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 (4.9.x) #7479

Closed
wants to merge 2 commits into from
Closed
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,8 +18,10 @@ package okhttp3.internal.http
import java.io.IOException
import java.net.ProtocolException
import okhttp3.Interceptor
import okhttp3.Protocol
import okhttp3.Response
import okhttp3.internal.EMPTY_RESPONSE
import okhttp3.internal.connection.Exchange
import okhttp3.internal.http2.ConnectionShutdownException
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 @@ -148,4 +149,15 @@ class CallServerInterceptor(private val forWebSocket: Boolean) : Interceptor {
throw e
}
}

private fun shouldIgnoreAndWaitForRealResponse(code: Int, exchange: Exchange): Boolean = when {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems to do the wrong thing for all 1xx codes except 100 and 103.

// 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
}
}
1 change: 1 addition & 0 deletions okhttp/src/main/kotlin/okhttp3/internal/http/StatusLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class StatusLine(
/** RFC 7540, Section 9.1.2. Retry these if the exchange used connection coalescing. */
const val HTTP_MISDIRECTED_REQUEST = 421
const val HTTP_CONTINUE = 100
const val HTTP_EARLY_HINTS = 103

fun get(response: Response): StatusLine {
return StatusLine(response.protocol, response.code, response.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import okhttp3.internal.http.ExchangeCodec
import okhttp3.internal.http.RequestLine
import okhttp3.internal.http.StatusLine
import okhttp3.internal.http.StatusLine.Companion.HTTP_CONTINUE
import okhttp3.internal.http.StatusLine.Companion.HTTP_EARLY_HINTS
import okhttp3.internal.http.promisesBody
import okhttp3.internal.http.receiveHeaders
import okhttp3.internal.skipAll
Expand Down Expand Up @@ -193,6 +194,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