Repository navigation
Description
In an effort to solve #27, #61, #83 I propose changing the WSConnection API following aspects from h11 and h2. In summary I propose a initiate_upgrade_connection method and a send method with the latter being a switch to event driven.
Initiating upgrades
Currently I think most implementations that support websockets and HTTP simply recreate the raw bytes for the upgrade request and call receive_bytes, as I do in Hypercorn for example.
I propose the following method,
class WSConnection:
def initiate_upgrade_connection(
self,
headers: List[Tuple[bytes, bytes]],
path: str,
) -> None:
...which would setup the connection as if it had received that request, thereby emitting a ConnectionRequested instance as the first event from the events method.
This should satisfy use cases that don't use h11 as easily as those that do.
Controlling the handshake & being event driven
Currently to accept a connection the accept method is called, to reject a connection requires some effort, such as using the private h11 _upgrade_connection. (As the _upgrade_connection is required it is clear wsproto can handle the full HTTP request/response cycle.)
I propose wsproto follows the h11 practice and infact accepts h11 event objects via a send method like,
class WSConnection:
def send(self, Union[h11.Event, wsproto.Event]):
...this would deprecate the existing accept, close, and send_data methods initially replacing them as so,
class WSConnection:
def accept(self):
return self.send(AcceptConnection())
def close(self, code):
return self.send(CloseConnection(code))
def send_data(self, data):
return self.send(Data(data))with the send method also accepting the h11.Response, h11.Data and h11.EndOfMessage events thereby allowing complete control over the HTTP acceptance/rejection responses.
As for the AcceptConnection, CloseConnection and Data events I'd follow the existing wsproto event structure.
A Note on h11 dependence
This would further tie wsproto to h11, which could be considered problematic. However I think h11 is great and also maintained by the same group (python-hyper) and hence cooperation would be expected and beneficial to both.
Next steps
Assuming @Kriechi, @mehaase are in some sought of agreement with me I'll make the changes, update the documentation and prepare a new release :). (Would be good to know what @njsmith thinks as well).
Activity
mehaase commentedon Oct 25, 2018
Thank you @pgjones for putting in the effort to think through this and to write it up. I'm not very familiar with h11, but I do generally like the approach you have described here. I am also not concerned about the increased dependency on h11.
I think it might be nice to include a
reject(status_code, headers, body=None)method to provide symmetry withaccept(…). This would be a convenience method that sends the corresponding h11 events for you.The proposed
send(…)method also deprecates the currentping()andpong()methods, right? Do you propose to update those methods or drop them?Regarding
initiate_upgrade_connection()…The
headersargument is extra headers to send, right? Andwsprotowill still take care of handshake headers likeSec-Websocket-Accept? I think this would also be the right place to putsubprotocolsandextensionsarguments, since those can both be negotiated as part of the handshake. (It wouldn't make sense to set those values in the constructor anymore, because the caller can negotiate the handshake separately from wsproto.)pgjones commentedon Oct 25, 2018
I would rather deprecate and remove
accept(..)so I don't think adding areject(status_code, headers, body=None)would make sense, however I think it would look like (to better illustrate my thoughts),You are right, I had forgotten about
pingandpong, which could be replaced bysend(Ping()). (Do we need a pong? It seems like something that should be dealt with automatically by the connection, as h2 does).Regarding
initiate_upgrade_connection(), the headers argument is in fact the full set of headers received by the server. This is only for a server side connection and would allow another HTTP parser to receive the initial HTTP request and then pass over to wsproto to handle thereafter (by passing the headers).Regarding the extra headers, I think the
initiate_connectionmethod should gain aextra_headersargument.Kriechi commentedon Oct 25, 2018
pong()should probably stay: https://github.com/python-hyper/wsproto/pull/36/files#diff-76076ffd5b77a8b4cfe658823ea73be1R456mehaase commentedon Oct 25, 2018
Oh okay, I misunderstood the part about deprecation when I first read it. Now I understand that
accept()et. al. would be deprecated (and could eventually be removed). As @Kriechi points out, there is value in having a pong feature.It also seems like the events need to be renamed if they can be both sent and received. I.e.
conn.send(PingReceived(…))is a bit confusing and should probably beconn.send(PingEvent(…))or justconn.send(Ping(…)).pgjones commentedon Oct 25, 2018
I agree with the naming suggestion, on this as well we could have a subclass with the old name initially (to help with the migration). I'm also happy to keep an ability to force send a pong.
Should I start work? I think we seem in agreement.
mehaase commentedon Oct 26, 2018
👍 from me. Let me know how I can help: code, docs, review, whatever you need.
pgjones commentedon Nov 15, 2018
For those following, I'm pushing the work in parts to https://github.com/pgjones/wsproto and then I'll open a pull request here.
njsmith commentedon Dec 3, 2018
I haven't had time to look into this in any detail, but just to raise a flag: if we're rethinking the handshake API we should also think about RFC 8441 handshakes (#90)
pgjones commentedon Dec 30, 2018
Looking at #92 and following on from the work on this I would like to propose removing the
Failevent and instead replacing it with raisedLocalProtocolandRemoteProtocolerrors (the former is roughly the same as #92). This would follow how h11 works, and I think be clearer as I'd expect an exception rather than having to check for a certain error type (more pythonic).Note that the
Failevent isn't really an event if #93 is merged as it is effectively replaced byRejectConnection.Note also that the equivalent of raising
LocalProtocolcurrently happens via a select fewValueErrorexceptions.pgjones commentedon Dec 30, 2018
I should add that regarding #91 I think the API would become:
With LocalProtocolErrors up to the user to decide how to deal with programming errors.
(Maybe also
_stateshould bestateto encourage usage?)Kriechi commentedon Dec 30, 2018
Regarding
_state:yes, if we make it a public API, it should become
state, but I don't like exposing it directly.We already have a
closedproperty, I would like to see anopenedproperty, as was recently proposed in https://github.com/python-hyper/wsproto/pull/92/files#diff-76076ffd5b77a8b4cfe658823ea73be1R193 by @touilleMan.pgjones commentedon Dec 30, 2018
I think you'd want to know if you were in the
REJECTINGstate to know whether you could send Rejection data, and theCONNECTINGstate to know if you can reject etc... as in you'd want to know about all the states. I prefer not to have a property for each possible state, as it is less helpful than exposing the attribute.Kriechi commentedon Dec 30, 2018
Fine with me - but currently we have a mix. If you want to expose
stateas public API (read-only, if possible?), I would suggest removing theclosedproperty. Either is fine with me.