You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 :-)
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.)
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 :)
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.
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.
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?
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...
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.
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.)
Activity
theelous3 commentedon Jun 3, 2017
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
Handshakeclass 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 theHandshake+ h11, or some other method. (Or not at all, if that's your thing.)theelous3 commentedon Jun 3, 2017
Actually, upon reflection I changed my
Handshake's methods so they'll accept any object with a dict/dictable iterator under the.headersattrib, and int/intable.status_codeattrib. 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 :)
njsmith commentedon Jun 4, 2017
(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
Connectionobject 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
dicton anh11.Request.headersis not safe – it's legal for some header values to be split over multiple entries, likesec-websocket-protocol: option1 / sec-websocket-protocol: option2, which is equivalent tosec-websocket-protocol: option1, option 2.Kriechi commentedon Jun 14, 2017
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 = OPENConnectionType,host,resource,extensions,subprotocolsand we can immediately send & receive WS frames.
Lukasa commentedon Jun 14, 2017
When you say "the handshake", you mean the HTTP-based
Upgrade->101dance?Kriechi commentedon Jun 14, 2017
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...
Lukasa commentedon Jun 14, 2017
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.
njsmith commentedon Jun 14, 2017
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.)
Kriechi commentedon Jun 14, 2017
In that case I'm happy with wsproto.Connection just raising an
UnsupportedExtensionExceptionor something similar.pgjones commentedon Feb 23, 2019
I believe this is complete with the Handshake class.