Skip to content

Allow more control over the handshake? #4

Closed

Description

@njsmith

The websocket protocol allows some options during the handshake that currently aren't exposed:

  • The client can propose a list of possible "subprotocols"

  • The client can send arbitrary HTTP headers (e.g., authorization headers)

  • The server can see the list of possible subprotocols and headers

  • The server can pick a subprotocol to use

  • The server can include arbitrary headers when accepting the connection

  • The server can reject the connection with some kind of HTTP 4xx response, including headers and body

Not all of these are actually exposed through wsproto currently (e.g. see python-hyper/wsproto#61), but it would be good to at least leave the door open to supporting them later. In particular, I'm a bit nervous about the server API, since it does the handshake automatically before the user's handler has a chance to do anything.

Activity

  1. belm0 commented on Sep 8, 2018

    @belm0
    Member

    related: trio-websocket is not exposing handshake info such as path (#17)

  2. self-assigned this
    on Oct 11, 2018
  3. mehaase commented on Oct 11, 2018

    @mehaase
    Contributor

    Overview

    I'm starting to plan for this issue. I read the WebSocket RFC so I can understand the handshake details better. I think wsproto already handles the following issues:

    • Subprotocols
    • Extensions

    And it does not handle the following issues:

    So I'll write up my thoughts here about how to add eventual support for all of these features...

    Subprotocols

    On the client side, subprotocols can be added to the API as a subprotocols=[…] argument to the client constructor/factories. We can pass this down to wsproto, which will send the list of subprotocols in its HTTP upgrade handshake. When the connection is established, the client can read the selected subprotocol and expose it as a property of the WebSocketConnection instance.

    The server side is a bit more complicated. The server gets a ConnectionRequested event that contains the list of subprotocols provided by the client. It needs to choose zero or one subprotocols and pass it to accept() to finish the handshake.

    The simple server implementation is to provide a list of subprotocols to the server constructor/factories. On each connection, the server can loop over the client's proposed subprotocols and pick the first one that it supports, or None if it doesn't support any of them.

    The simple server implementation is limited because once set, that server instance will never be able to offer other subprotocols. Maybe it wants to offer one subprotocol on path /chat and a different subprotocol on /hearbeat. So the more complicated and generalized server solution is to offer a callback that receives connection request metadata (headers, URL path, subprotocols, etc.) and returns zero or one subprotocols. (This same design might be preferred for the other aspects of this issue. See below.)

    Open questions for subprotocols:

    • If a client requires a certain subprotocol, should we have a way to express that? The client can specify a list of acceptable subprotocols but the server has the right to choose zero of them. The client can check the subprotocol property to see if it got what it was expecting, and it can close the connection if needed. Should we offer some shortcut way to demand a certain subprotocol?
    • Do we prefer the "simple" server subprotocol selection, the "complex", or something else? I'm leaning towards "complex" because it is more generalized and will help with the other parts of this issue.

    Extensions

    Extensions can be added to the API as an extensions=[…] argument to the client constructors/factories. wsproto exposes extensions as classes (e.g. wsproto.extensions.PerMessageDeflate) so we can either re-export those classes in trio-websocket, or we can allow the user to specify extensions as strings. Strings are more convenient but would preclude the user from providing their own extension implementation, so maybe we support both, i.e. you can pass a string like permessage-deflate or you can pass a subclass of wsproto.extensions.Extension.

    The negotiated extensions should be exposed as a read-only property of WebSocketConnection.

    As with subprotocols, the server side is a bit murkier. The most flexible approach is to have a callback that receives connection request metadata and chooses which extensions to enable. On the other hand, wsproto only implements one extension and I'm not sure how useful it would be to change extensions based on connection request metadata.

    Open questions for extensions:

    • Should we specify extensions as strings, classes, or allow both?
    • Should the server use a callback to decide which extensions to enable? (If so, it can be unified with the subprotocol's callback.)

    Arbitrary Headers

    Wsproto doesn't support this feature yet, but we can at least think about how it would work.

    On the client-side, we can offer a headers={…} argument. On the server side, for reasons similar to those discussed above with subprotocols and extensions, we probably want a callback that receives connection request metadata and can modify headers before the response is sent.

    Open questions for headers:

    • Should we block certain headers? E.g. we don't want the user to pass in a Sec-WebSocket-Extensions header because that would mess up wsproto's internal state machine. (Maybe this is something for wsproto to handle.)
    • Should headers be exposed as a read-only property of WebSocketConnection? Probably not, because headers are not negotiated and the caller already knows which headers they set.

    Rejecting with HTTP 4xx response

    Wsproto doesn't support this feature yet, but we can at least think about how it would work.

    This feature only applies to the server. The only reasonable way to implement this is by using a callback that receives connection request metadata (headers, URL path, etc.) and can signal when an HTTP error needs to be returned.

    Conclusions

    It seems like most of this can be accomplished by a combination of the following changes:

    • New client-side arguments like headers, extensions, and subprotocols that get passed down to wsproto.
    • New WebSocketConnection properties that expose the negotiated extensions and subprotocol.
    • A callback for server connections that receives connection metadata and is allowed to do things like selecting a subprotocol, enabling/disabling extensions, modifying headers, or raising an HTTP error.

    In order to simplify usage, a default callback should implement common options, like setting subprotocol to None and enabling all extensions that are supported by wsproto. If a user implements a callback, they should be able to ignore the parts they don't care about. I.e. if they don't care which extensions are enabled, then they should get all the built-in extensions automatically. Therefore, the callback requires some careful design to handle all 4 of these features (subprotocols, extensions, headers, http errors) without requiring each user to write lots of boilerplate.

  4. njsmith commented on Oct 11, 2018

    @njsmith
    MemberAuthor

    For extensions, I wouldn't get fancy at this stage. wsproto has all this "generic" extension machinery, but... Extensions are defined through RFCs and can make more-or-less arbitrary changes to the protocol. Websockets have been shipping in browsers since 2009, and in that time only 1 extension has been defined (permessage-deflate). So... there might never be another extension. Or if there is, it might turn out that wsproto's current machinery can't handle it, and needs to be redone anyway. So wsproto's whole extension API is kind of optimistic. For now, I'd probably just stick to a simple API for configuring permessage-deflate. (The simplest version would be a bool to enable/disable it. If you want to get fancier I guess you could expose the extension parameters.)

    For the server-side negotiation API, I wouldn't use a callback. Instead, I'd let the handler task start up, examine the situation, and possibly reconfigure things before accepting/rejecting the connection.

    One approach would be, server handlers don't receive a websocket object, they receive a WSConnectRequest and then they have to call .accept() to actually accept the connection and get a websocket object.

    Another approach would be, server handlers receive an "uninitialized" websocket object: it's a full-fledged websocket object but it hasn't done the handshake yet. If you start calling send/receive without doing the handshake, then it automatically performs the handshake with default values and then performs the requested operation. But, if you want, before you start using the object for real, you can examine it and then explicitly complete the handshake by calling a method like accept or reject. (This is how Trio's SSLStream handles the TLS handshake.)

    The "request object" approach is nicely explicit, but would mean that our getting started examples are slightly more complicated than other competing websocket libraries. I don't have a strong intuition for which is better.

  5. mehaase commented on Oct 17, 2018

    @mehaase
    Contributor

    I think I prefer the explicit approach (request object). A user only needs to add 1 line of code in the base case. I'll work on this approach first and see how it looks.

    The alternative approach sounds like it would be deceptively tricky for the user. The user can only call accept() or reject() during a window of time after the client has done its part of the handshake but before the server has done its part of the handshake. This seems like it would cause more confusion than it prevents. But I'll keep this in mind as an alternative to later, if needed.

  6. mehaase commented on Oct 18, 2018

    @mehaase
    Contributor

    For reference, Aaugustin's websocket library has some support for customizing the handshake. The server constructor has arguments for extensions, subprotocols, and extra headers, but the library performs all the underlying logic. The server has a process_request(path, request_headers) method that can signal an HTTP error, but the user must override this method in a subclass.

    The client constructor also has arguments for extensions, subprotocols, and extra headers.

    This approach leads to a simple API (all handshake options are passed into the constructor) but limits flexibility. For example, the default algorithm to select a subprotocol can only be customized by overriding in a subclass. Furthermore, all server connection instances will have the same extensions/protocols/headers configuration, because these options are set in the server's constructor.

  7. added a commit that references this issue on Oct 19, 2018
  8. mehaase commented on Oct 19, 2018

    @mehaase
    Contributor

    First commit is done. The scope is limited to subprotocols. I ended up not supporting extensions because wsproto's ConnectionRequested event doesn't expose the client's proposed extensions, and wsproto's accept() method doesn't have any argument to specify extensions. But I think the general design will work for extensions, custom headers, and rejecting connections.

  9. mehaase commented on Oct 23, 2018

    @mehaase
    Contributor

    Fixed in #62

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

Metadata

Metadata

Assignees

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