Repository navigation
Description
Description
In Kestrel we're trying to make it easier to debug a client making a request with the wrong HTTP protocol to an endpoint when TLS isn't used.
e.g.
- HTTP/1.1 request sent to a port that expects pre-negotiated HTTP/2
- Pre-negotiated HTTP/2 request sent to a port that expects HTTP/1.1
To make these mistakes easier to understand, we are detecting when a wrong HTTP version is received by Kestrel. This allows:
- Logging that the wrong HTTP version was received by the server
- Sending back an error in the HTTP protocol the request was made with. That means:
- An HTTP/1.1 request sent to an HTTP/2 port gets back an HTTP/1.1 400 status response.
- An HTTP/2 connection made to an HTTP/1.1 port gets back a GOAWAY frame with an HTTP_1_1_REQUIRED error code in response to connection handshake.
I think there is a slight bug with how SocketsHttpHandler handles getting a GOAWAY during the connection handshake. It treats anything other than a SETTINGS frame from the server as a protocol error.
runtime/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs
Lines 471 to 476 in dfbae37
| // Read the initial settings frame. | |
| frameHeader = await ReadFrameAsync(initialFrame: true).ConfigureAwait(false); | |
| if (frameHeader.Type != FrameType.Settings || frameHeader.AckFlag) | |
| { | |
| ThrowProtocolError(); | |
| } |
Today a GOAWAY results in an exception with a PROTOCOL_ERROR, regardless of what the GOAWAY's error code was.
I think the client should also accept reading a GOAWAY frame here, and then throw Http2ConnectionErrorException with the error code returned from the server. That would allow HTTP_1_1_REQUIRED to be surfaced to the user so they have a clue to what is wrong.
Reproduction Steps
// pre-negotiate HTTP/2. However, server only supports HTTP/1.1.
HttpRequest request = new HttpRequest(HttpMethod.Get, "http://localhost");
request.Version = HttpVersion.Version20;
request.VersionPolicy = HttpVersionPolicy.Exact;
var response = await client.SendAsync(request); // error thrownExpected behavior
Server GOAWAY error code - HTTP_1_1_REQUIRED - is thrown by HttpClient.
Actual behavior
PROTOCOL_ERROR is always thrown.
Regression?
No
Known Workarounds
No response
Configuration
No response
Other information
No response
Activity
karelz commentedon Jan 13, 2022
Triage: We should throw better exception. Impact on gRPC -> 7.0.
MihaZupan commentedon Feb 25, 2022
If we receive an
HTTP_1_1_REQUIREDreset, we shouldn't just throw a better exception, but a retryable one so that the connection pool may retry the request on HTTP/1.1 if theVersionPolicypermits it.geoffkizer commentedon Mar 1, 2022
By "reset" do you mean RST_STREAM? I agree that we should probably retry in this case.
That said, if we receive a connection-level HTTP_1_1_REQUIRED error, I'm not sure we should retry. It seems like this can only happen if (a) version policy was set to assume HTTP2, in which case that's a user error and they should fix their version policy; or (b) the server is misbehaving.
MihaZupan commentedon Mar 2, 2022
Yes
JamesNK commentedon Apr 27, 2022
I agree that retrying isn't the right thing to do here.
An HTTP/2 request is only sent over plaintext if the client has been configured that way. I don't think we should go against that configuration. There isn't a way to know whether the client was misconfigured or the server.