Skip to content

Support dual-protocol HTTP/Websocket servers #27

Closed

Description

@njsmith

Right now WSConnection creates its own internal h11 object to manage the whole handshake. This makes it tricky to use in a dual-protocol server that wants to read in HTTP request headers, maybe do some routing, etc., and then decide whether to treat the request as a HTTP or Websocket connection.

The simplest way to do this (and sufficient for my use case) would be to assume that the server is also using h11, and accept a hand-off from an existing h11.Connection + h11.Request.

I'll probably get around to this at some point; mostly wanted to file this as a reminder to myself + a heads-up that we might need to break the handshake API so we shouldn't declare a 1.0 quite yet :-)

Activity

  1. theelous3 commented on Jun 3, 2017

    @theelous3

    Oh! That's interesting.

    As I hinted at here I compartmentalised the handshake away from the rest of the ws protocol, partly for this exact reason.

    (In my lib) currently the Handshake class is purely an addon for h11, leaving the rest of the protocol (from the point at which the ws connection can be considered 'open') completely agnostic of anything that took place before hand. It presumes in good faith that you've taken care of the handshake either by means of the Handshake + h11, or some other method. (Or not at all, if that's your thing.)

  2. theelous3 commented on Jun 3, 2017

    @theelous3

    Actually, upon reflection I changed my Handshake's methods so they'll accept any object with a dict/dictable iterator under the .headers attrib, and int/intable .status_code attrib. This way it's nicely compatiable with h11, and any other http lib's objects can be easily coerced in to working. How do you feel about that approach?

    I have no idea if it's bad form to be rambling on about my lib in here or if there's a more appropriate forum for this. If there is, please direct me :)

  3. njsmith commented on Jun 4, 2017

    @njsmith
    MemberAuthor

    (Context for anyone else reading this: I stumbled on @theelous3's noio_ws yesterday and filed theelous3/noio_ws#1)

    I think I do like wsproto's integration of the handshake into the Connection object in general; after all, it's part of the protocol :-). (And extensions kind of need to be integrated into both the handshake code and the framing code.)

    Note that blindly calling dict on an h11.Request.headers is not safe – it's legal for some header values to be split over multiple entries, like sec-websocket-protocol: option1 / sec-websocket-protocol: option2, which is equivalent to sec-websocket-protocol: option1, option 2.

  4. Kriechi commented on Jun 14, 2017

    @Kriechi
    Member

    In the case of @mitmproxy, we would like to instantiate a wsproto connection without going through the handshake phase, because we already handle that ourself in the proxy-related code.

    Ideally we could just create a new wsproto connection by supplying the following arguments / state:

    • ConnectionState = OPEN
    • ConnectionType,
    • host, resource, extensions, subprotocols
      and we can immediately send & receive WS frames.
  5. Lukasa commented on Jun 14, 2017

    @Lukasa
    Member

    In the case of @mitmproxy, we would like to instantiate a wsproto connection without going through the handshake phase, because we already handle that ourself in the proxy-related code.

    When you say "the handshake", you mean the HTTP-based Upgrade->101 dance?

  6. Kriechi commented on Jun 14, 2017

    @Kriechi
    Member

    Yes, correct.

    For our usecase this includes everything until the server 101-response headers are fully transmitted and read. The first bytes after this should be the beginning of a WS frame. This is where we want to start ideally.

    If you think this doesn't really fit into wsproto with the current architecture, we can probably replay / simulate another handshake in-memory or something...

  7. Lukasa commented on Jun 14, 2017

    @Lukasa
    Member

    I think wsproto should absolutely support this use-case: there's no reason it should be tied to you processing your HTTP data using h11. The integration there should be good, but it should absolutely be possible to use wsproto separately.

  8. njsmith commented on Jun 14, 2017

    @njsmith
    MemberAuthor

    Technically, wsproto doesn't process any HTTP data, it just implements the websocket protocol, and the websocket protocol's initial handshake happens to look suspiciously similar to HTTP...

    ...that said, I don't object to having a mitmproxy-friendly mode, though it may be tricky to make extensions play nicely. (What if the handshake you observed uses an extension we don't recognize, or configuration values we don't support? There's a certain amount of stuff you're guaranteed to be allowed to do when sniffing a websocket stream with unrecognized extensions – they did think about this when designing the protocol – but my guess is that supporting that would be complicated enough to be out-of-scope for wsproto.)

  9. Kriechi commented on Jun 14, 2017

    @Kriechi
    Member

    In that case I'm happy with wsproto.Connection just raising an UnsupportedExtensionException or something similar.

  10. pgjones commented on Feb 23, 2019

    @pgjones
    Member

    I believe this is complete with the Handshake class.

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