Possibly h11 in client mode should raise an exception if it receives data before sending a Request
This is related to: https://github.com/njsmith/h11/issues/23
Right now, there are two reasons this doesn't happen:
-
h11 doesn't look at the next request/response cycle at all until the current one finishes and someone calls
start_next_cycle -
the state machine actually allows a server to send a spontaneous
Response. This is because when acting as a server, we want to be able to send back a400response to malformed requests, which from the state machine's point of view is like having no request at all. When acting as a client, though, then this doesn't make much sense.
CC @Lukasa
Oh, meh, checking git logs reminded me of the other reason why the state machine allows for spontaneous Response. We used to only allow this if the client state was ERROR, but it turns out there's this thing called 408 Timed out...
Heh, so amusingly I bumped into the spontaneous response while working on CONNECT tunnelling for urllib3. We're doing that by using two h11 state machines, one for the original CONNECT request and then a new one for the "inner" connection. However, I typoed and sent the CONNECT request on the "inner" state machine, and didn't notice for a very long time (it just led to weird errors that were difficult to track down). A bit annoying.
However, as you point out it's not totally implausible to get responses without requests. Generally speaking though I'd say that any client that sees one is probably doing something well outside the norm.