Skip to content

Add the possibility to add response header on WebSocket handshake request #692

Closed

Description

@KaSSaaaa

Hi,

I'm working on a small project with HTTP & WS and I chose Crow for that matter. The only thing is, I'm using a JS library for testing purposes but the WebSocket keeps closing because some headers are not available on the handshake response.

Is there any possibility to add headers to the handshake query's response ?

Otherwise, I've looked a bit into the source code and it shouldn't be too complicated to add something like an "onhandshake" method (like you have "onopen",...) which could allow the user to add the response headers on the handshake response. I would be glad to create a Pull Request with that feature if necessary.

Best regards,
Pierre

Activity

  1. MichaelSB commented on Jul 29, 2023

    @MichaelSB
    Contributor

    Out of curiosity... which headers are missing? Just adding headers might not be enough. Most headers negotiate features, which probably are not implemented yet.

  2. KaSSaaaa commented on Jul 29, 2023

    @KaSSaaaa
    ContributorAuthor

    I'm missing the "Sec-WebSocket-Protocol" header. I've tried editing the code of the latest release version with the "onhandshake" callback and it works pretty well.

  3. MichaelSB commented on Jul 29, 2023

    @MichaelSB
    Contributor

    What now follows is just my opinion on the matter...
    There's more to "Sec-WebSocket-Protocol" than just adding a header to make it work correctly. It may be the case in your case, but it is not generally speaking. Adding an onhandshake-hook to crow, that allows adding random headers to the handshake imo is not the way to go here. If you need "Sec-WebSocket-Protocol" support, one should add "Sec-WebSocket-Protocol" support and not something that works in singular cases and does nothing in others. That said... as far as I understand the spec by shortly looking at it, adding support for Sec-WebSocket-Protocol-support wouldn't be that hard to do. You get a list of protocols, have a list of protocols and if there's at least one match, continue normally with one of the matches in the response to the client, otherwise fail the connection.

  4. KaSSaaaa commented on Jul 29, 2023

    @KaSSaaaa
    ContributorAuthor

    You're right ! I can add something like that just for "Sec-WebSocket-Protocol" then. Like a hook on the WebSocketRule that takes a list of strings, check that in the Connection and then add them to the response body. What do you think ?

  5. MichaelSB commented on Jul 29, 2023

    @MichaelSB
    Contributor

    What you just described is likely what I would do. Sounds good to me :D

  6. added 5 commits that reference this issue on Jul 29, 2023
  7. added
    feature
    on Jan 3, 2024
  8. added 2 commits that reference this issue on Jan 14, 2024
  9. added a commit that references this issue on Sep 7, 2024
  10. added a commit that references this issue on Sep 10, 2024
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

    feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions