Skip to content

Support rejecting incoming connections with an HTTP response. #61

Closed

Description

@lovelydinosaur

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.

Activity

  1. njsmith commented on Aug 14, 2018

    @njsmith
    Member

    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)

  2. lovelydinosaur commented on Aug 17, 2018

    @lovelydinosaur
    Author

    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.

  3. njsmith commented on Aug 17, 2018

    @njsmith
    Member

    why are you switching over to wsproto before you know that you want to use the websocket protocol?

  4. lovelydinosaur commented on Aug 17, 2018

    @lovelydinosaur
    Author

    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".

  5. njsmith commented on Aug 17, 2018

    @njsmith
    Member

    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)?

  6. lovelydinosaur commented on Aug 17, 2018

    @lovelydinosaur
    Author

    Maybe we should have a method that's similar to WSConnection.accept, like reject(event, status_code, reason, headers)?

    Something like that would be perfect, yup.

  7. changed the title [-]Add a public API for `_upgrade_connection`.[/-] [+]Support API for rejecting incoming connections with an HTTP response.[/+] on Aug 17, 2018
  8. 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
  9. lovelydinosaur commented on Aug 17, 2018

    @lovelydinosaur
    Author

    Given your observation, I've retitle the issue to better reflect the intent.

  10. pgjones commented on Dec 30, 2018

    @pgjones
    Member

    #93 should implement what is required here.

  11. pgjones commented on Jan 6, 2019

    @pgjones
    Member

    As an example here is a chunked response (fully customized rejection response),

    connection.send(RejectConnection(
        status_code=200, has_body=True, headers=[(b"Transfer-Encoding", b"chunked")],
    ))
    connection.send(RejectData(data=b"Some body part", body_finished=False))
    connection.send(RejectData(data=b"another body part", body_finished=False))
    connection.send(RejectData(data=b"final body part", body_finished=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions