python-hyper / wsproto

Sans-IO WebSocket protocol implementation
https://wsproto.readthedocs.io/
MIT License
266 stars 39 forks source link

Can you send RejectConnection without RejectData? #121

Closed mehaase closed 5 years ago

mehaase commented 5 years ago

Generally, the library follows a principle that if one endpoint sends an event, the other endpoint receives a substantially similar event. Therefore I expect that if I send RejectConnection(has_body=False) from the server, the client should get a RejectConnection(has_body=False) event. Instead, the client gets a RejectConnection(has_body=True) followed by a RejectData(body=b'').

I assume that this is intentional behavior because this is just how h11 works. Perhaps the API docs should clarify that has_body should not be used when sending events, only when receiving events?

Kriechi commented 5 years ago

Your proposal sounds reasonable.

I'm wondering if we shouldn't parse the body data with this in mind - an empty bytestring is essentially no body, right?

pgjones commented 5 years ago

The ability to send a RejectConnection(has_body=False) was meant as a shortcut for the server code and it is the default (False is the default for has_body). I'd ideally have the received event also be RejectConnection(has_body=False) (rather than RejectConnection(has_body=True) followed by RejectData(body_finished=True)) but it isn't that easy to achieve. This follows h11 which returns a Response then a EndOfMessage.

I'm ok with this asymmetry, however if we decide we aren't then I think we should instead remove the has_body argument from the RejectConnection and insist the server follows the client with a RejectConnection() followed by RejectData(body_finished=True)