Skip to content

SocketsHttpHandler: GOAWAY on connection handshake #63715

Closed

Description

@JamesNK

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.

// 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 thrown

Expected 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

  1. ghost added
    untriaged
    on Jan 13, 2022
  2. karelz commented on Jan 13, 2022

    @karelz
    Member

    Triage: We should throw better exception. Impact on gRPC -> 7.0.

  3. added and removed
    untriaged
    on Jan 13, 2022
  4. added this to the 7.0.0 milestone on Jan 13, 2022
  5. MihaZupan commented on Feb 25, 2022

    @MihaZupan
    Member

    If we receive an HTTP_1_1_REQUIRED reset, 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 the VersionPolicy permits it.

  6. geoffkizer commented on Mar 1, 2022

    @geoffkizer
    Contributor

    If we receive an HTTP_1_1_REQUIRED reset, 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 the VersionPolicy permits it.

    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.

  7. MihaZupan commented on Mar 2, 2022

    @MihaZupan
    Member

    By "reset" do you mean RST_STREAM?

    Yes

  8. JamesNK commented on Apr 27, 2022

    @JamesNK
    MemberAuthor

    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.

  9. ghost added
    in-pr
    on Jul 2, 2022
  10. ghost removed
    in-pr
    on Jul 5, 2022
  11. ghost locked as resolved and limited conversation to collaborators on Aug 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions