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
Add the possibility to add response header on WebSocket handshake request #692
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.
Out of curiosity... which headers are missing? Just adding headers might not be enough. Most headers negotiate features, which probably are not implemented yet.
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.
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.
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 ?
Activity
MichaelSB commentedon Jul 29, 2023
Out of curiosity... which headers are missing? Just adding headers might not be enough. Most headers negotiate features, which probably are not implemented yet.
KaSSaaaa commentedon Jul 29, 2023
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.
MichaelSB commentedon Jul 29, 2023
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.
KaSSaaaa commentedon Jul 29, 2023
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 ?
MichaelSB commentedon Jul 29, 2023
What you just described is likely what I would do. Sounds good to me :D
CrowCpp#692 Add possibility to specific Sec-WebSocket-Protocol
CrowCpp#692 Add documentation
CrowCpp#692 Not using find_first_of as it is a C++ 17 function
CrowCpp#692 Not using find_first_of as it is a C++ 17 function
CrowCpp#692 Added get_subprotocol getter
CrowCpp#692 Remove closing of the WebSocket if no subprotocol is matc…
CrowCpp#692 Fix missing include <algorithm>
CrowCpp#692 Add unit test for subprotocol
#692 Add the possibility to specify "Sec-WebSocket-Protocol" (#693)