Skip to content

Allow more control over handshake #83

Closed

Description

@mehaase

The current handshake is a bit inflexible:

  • Neither client nor server can send custom headers.
  • The server cannot see what extensions the client proposes, e.g. in ConnectionRequested event.[1]
  • The server cannot stipulate extensions in the accept() method. Extensions can only be set at instantiation.
  • The server cannot signal an HTTP error. (Already proposed in Support rejecting incoming connections with an HTTP response. #61)

I propose to add the following[2]:

  • Client can pass extra headers in the constructor. These headers are appended to the client's handshake.
  • Extensions are exposed in the ConnectionRequested event.
  • Add extensions argument to accept() method. This would take a list of extensions in the same format as the connection's current self._extensions list.
  • Add custom headers argument to the accept() method. These headers are appended to the server's handshake.
  • Add a reject() method that can be used to signal an HTTP error. (Already proposed in Support rejecting incoming connections with an HTTP response. #61)

These changes can all be done in a backwards compatible way. I'm willing to make a PR if I can get buy in on the concept. Let me know what you think.

[1] The event does include the Sec-WebSocket-Extensions headers, but these headers need to be parsed and that's best done within wsproto.
[2] These handshake features will be consumed downstream in python-trio/trio-websocket#4

Activity

  1. Kriechi commented on Oct 19, 2018

    @Kriechi
    Member

    Ideally, I would like to keep the handshake part as independent as possible from the actual WS communication.

    At least for most of my own use cases, I would like to instantiate a WSConn object without needing to touch HTTP requests, responses, or headers. So the WSConn ctor should be able to take all args that it ever needs to result in a fully working, fully initialized state.

    Extensions are exposed in the ConnectionRequested event.

    Sounds like a reasonable thing to do! Happy to merge a PR!
    (but also, see below)

    Add extensions argument to accept() method. This would take a list of extensions in the same format as the connection's current self._extensions list.

    Currently, wsproto keeps a list of "active" extensions, and then calls "accept" on each one to figure out if the header should be set. @mehaase: do you have a use case that does not fit in with this approach? Meaning, do you have extensions that DO NOT live as wsproto-extension object and are therefore not registered?

  2. mehaase commented on Oct 25, 2018

    @mehaase
    MemberAuthor

    Currently, wsproto keeps a list of "active" extensions, and then calls "accept" on each one to figure out if the header should be set. @mehaase: do you have a use case that does not fit in with this approach? Meaning, do you have extensions that DO NOT live as wsproto-extension object and are therefore not registered?

    No, actually I don't have any other extensions at all. I suggested this change because extensions can be negotiated in the handshake, and the caller might want fine-grained control over which extensions are enabled depending on handshake data. I don't have a specific use case in mind; just want to keep it flexible.

  3. pgjones commented on Jan 6, 2019

    @pgjones
    Member

    @mehaase I think all of this is now possible,

    Neither client nor server can send custom headers.

    # client
    connection.send(Request(extra_headers=[...]))
    # server
    connection.send(AcceptConnection(extra_headers=[...]))
    connection.send(RejectConnection(headers=[...]))

    The server cannot see what extensions the client proposes, e.g. in ConnectionRequested event.[1]

    event: Request
    event.extensions

    The server cannot stipulate extensions in the accept() method. Extensions can only be set at instantiation.

    connection.send(AcceptConnection(extensions=[...]))

    The server cannot signal an HTTP error. (Already proposed in #61)

    connection.send(RejectConnection())

    So can this be closed?

  4. mehaase commented on Jan 7, 2019

    @mehaase
    MemberAuthor

    This is great, @pgjones, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions