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
{{ message }}
Repository navigation
Support rejecting incoming connections with an HTTP response. #61
In order for a dual-protocol client to send 4xx HTTP responses when it wishes to deny a websocket upgrade, it's necessary to use _upgrade_connection.
(Both of the uvicorn and hypercorn webservers use it for instance.)
Would it be reasonable to rename _upgrade_connection to upgrade_connection to promote it to public API? (with a synonym property of _upgrade_connection kept around for one version cycle)
Happy to go ahead and make the PR if that seems a reasonable approach.
My suggestion in that thread was that dual-protocol servers would use h11 directly to accept the request, and then when they decided they wanted to switch to websocket they'd do something like conn = WSConnection.from_request(h11_connection, h11_request) (which would use _process_connection_request)
Uvicorn (and Hypercorn too) pretty much take the approach in #27 - the HTTP protocol passes the handling over to the WebSocket protocol once it's determined that it has an websocket upgrade request. However at that point we still haven't determined if the application layer is going to accept the request or not (authentication and routing being application-layer concerns.), so the websocket implementation still ends up being responsible for returning 403, 404, or whatever on rejected connections.
It's possible that a bit of re-jigging might be able to ensure that the HTTP protocol strictly doesn't hand over to the WebSocket case until the application layer has accepted the request, so it's possible that it could be considered an implementation issue, tho either way around it's worth noting that there's at least a couple of implementations currently relying on that bit of private API.
Because the acceptance is an application-level concern, rather than a server-level concern. (As I say, it could be that there's some rejigging that'd make it possible, but it's a little awkward.)
Consider it a different way around. Suppose you have a setup where nginx is routing websocket requests to a websocket server (and http requests to a standard HTTP application). In that case your websocket server will be solely handling websocket requests, it's not a dual-protocol server, but it does still need to be able to close off sessions that fail authentication with a 403, rather than a websocket accept then close. It's not really a dual-protocol server, because it never handles regular HTTP requests, but it does still need to be able to perform "reject this connection with an HTTP status code".
the acceptance is an application-level concern, rather than a server-level concern
Sure, but... the application considers the request, makes its decision, and then tells the server to actually accept or reject, right?
your websocket server will be solely handling websocket requests, it's not a dual-protocol server, but it does still need to be able to close off sessions that fail authentication with a 403, rather than a websocket accept then close
Ah, that is an interesting case! I feel like just handing people the h11.Connection object and telling them to have fun is maybe not the right approach here though? Maybe we should have a method that's similar to WSConnection.accept, like reject(event, status_code, reason, headers)?
changed the title [-]Add a public API for `_upgrade_connection`.[/-][+]Support API for rejecting incoming connections with an HTTP response.[/+]on Aug 17, 2018
changed the title [-]Support API for rejecting incoming connections with an HTTP response.[/-][+]Support rejecting incoming connections with an HTTP response.[/+]on Aug 17, 2018
Activity
njsmith commentedon Aug 14, 2018
I think this is a dupe of #27?
My suggestion in that thread was that dual-protocol servers would use h11 directly to accept the request, and then when they decided they wanted to switch to websocket they'd do something like
conn = WSConnection.from_request(h11_connection, h11_request)(which would use_process_connection_request)lovelydinosaur commentedon Aug 17, 2018
Yes and no.
Uvicorn (and Hypercorn too) pretty much take the approach in #27 - the HTTP protocol passes the handling over to the WebSocket protocol once it's determined that it has an websocket upgrade request. However at that point we still haven't determined if the application layer is going to accept the request or not (authentication and routing being application-layer concerns.), so the websocket implementation still ends up being responsible for returning 403, 404, or whatever on rejected connections.
It's possible that a bit of re-jigging might be able to ensure that the HTTP protocol strictly doesn't hand over to the WebSocket case until the application layer has accepted the request, so it's possible that it could be considered an implementation issue, tho either way around it's worth noting that there's at least a couple of implementations currently relying on that bit of private API.
njsmith commentedon Aug 17, 2018
why are you switching over to wsproto before you know that you want to use the websocket protocol?
lovelydinosaur commentedon Aug 17, 2018
Because the acceptance is an application-level concern, rather than a server-level concern. (As I say, it could be that there's some rejigging that'd make it possible, but it's a little awkward.)
Consider it a different way around. Suppose you have a setup where nginx is routing websocket requests to a websocket server (and http requests to a standard HTTP application). In that case your websocket server will be solely handling websocket requests, it's not a dual-protocol server, but it does still need to be able to close off sessions that fail authentication with a 403, rather than a websocket accept then close. It's not really a dual-protocol server, because it never handles regular HTTP requests, but it does still need to be able to perform "reject this connection with an HTTP status code".
njsmith commentedon Aug 17, 2018
Sure, but... the application considers the request, makes its decision, and then tells the server to actually accept or reject, right?
Ah, that is an interesting case! I feel like just handing people the
h11.Connectionobject and telling them to have fun is maybe not the right approach here though? Maybe we should have a method that's similar toWSConnection.accept, likereject(event, status_code, reason, headers)?lovelydinosaur commentedon Aug 17, 2018
Something like that would be perfect, yup.
[-]Add a public API for `_upgrade_connection`.[/-][+]Support API for rejecting incoming connections with an HTTP response.[/+][-]Support API for rejecting incoming connections with an HTTP response.[/-][+]Support rejecting incoming connections with an HTTP response.[/+]lovelydinosaur commentedon Aug 17, 2018
Given your observation, I've retitle the issue to better reflect the intent.
pgjones commentedon Dec 30, 2018
#93 should implement what is required here.
pgjones commentedon Jan 6, 2019
As an example here is a chunked response (fully customized rejection response),