Closed foolip closed 3 years ago
It looks like "websockets" is much better for writing readable clients, and "pywebsocket3" is much better for writing servers that do evil things. Personally I don't see a problem with having two libraries as they have quite different purposes. I haven't used "websockets" but I would guess that making it do evil things would mess up the clean API. It would be possible to add a nice client API to pywebsocket3, but there isn't much motivation to do that since "websockets" does it better :-)
So is pywebsocket3 only ever used as a server? If we only use websockets as a client that would be an explainable split.
How deep does the "do evil things" go with pywebsocket3? Is it the ability to send arbitrary bytes in the network packets, that don't even resemble the WebSockets framing?
A few examples:
https://github.com/web-platform-tests/wpt/blob/master/websockets/handlers/sleep_10_v13_wsh.py trickles bytes in the handshake phase with 2 second pauses.
https://github.com/web-platform-tests/wpt/blob/master/websockets/handlers/invalid_wsh.py writes invalid bytes to the stream before the handshake.
https://github.com/web-platform-tests/wpt/blob/master/websockets/handlers/echo_raw_wsh.py writes whatever is in the incoming message data as bytes.
Thanks @zcorpan!
Messing with the handshake in websockets doesn't look straightforward, but from a quick look it seem like the size of the hack to allow sending or receiving arbitrary messages should be pretty small. But if most of what pywebsocket3 does is evil stuff and there's very little exchange of well-formed WebSocket frames, then there doesn't seem to be any overlap except for establishing the raw socket.
It would be nice to document this seeming redundancy somewhere, but right now there is no place where both libraries appear. I'll add a TODO in https://github.com/web-platform-tests/wpt/pull/28796.
We currently use two different Python libraries for WebSockets:
Both are vendored into tools/third_party/.
Note that even with pywebsocket3 we're using "private" APIs: https://github.com/web-platform-tests/wpt/blob/6a96f5c7fe3c2fd837f1814fef4fac93ade8344e/tools/wptserve/wptserve/ws_h2_handshake.py#L15-L26
And note that with WebDriver BiDi, we'll also want to have the ability to send some invalid messages, such as text messages that aren't valid UTF-8.
Has anyone looked into these libraries in detail? Is there a possible path where we can have just one library which is patchable enough for all of our needs?
cc @web-platform-tests/wpt-core-team + websockets/ folks @jdm @ricea @zqzhang