Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: websocket: pass request context to callback #83597

Conversation

mrodgers-witekio
Copy link
Collaborator

When upgrading an HTTP1 connection to a websocket, pass the HTTP request context associated with the upgrade request to the user callback.

This allows the user callback to access URL query parameters and HTTP headers. The main motivation behind this is to allow for authentication via the headers before the application decides whether to accept or reject the websocket connection, but it could also be used to provide any other context needed on opening the connection (e.g. Sec-WebSocket-Protocol header).

@zephyrbot zephyrbot added area: HTTP HTTP client/server support area: Networking area: Samples Samples area: Shell Shell subsystem Release Notes To be mentioned in the release notes area: Sockets Networking sockets labels Jan 6, 2025
@mrodgers-witekio mrodgers-witekio force-pushed the websocket_pass_request_headers_to_callback branch from e0df52b to 2229c90 Compare January 8, 2025 14:48
rlubos
rlubos previously approved these changes Jan 8, 2025
Copy link
Contributor

@rlubos rlubos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems shell_websocket.h needs an update too (shell_websocket_setup declaration), looks good otherwise, thanks.

Passing HTTP upgrade request context to the user callback allows the
user to decide whether to accept or reject the websocket connection
based on the HTTP headers in the request. The primary reason for this is
to enable authentication of the websocket connection (e.g. via cookies
or Authorization header).

Signed-off-by: Matt Rodgers <[email protected]>
Update HTTP server documentation and migration guide to account for
added request_ctx parameter to the http_resource_websocket_cb_t
callback.

Signed-off-by: Matt Rodgers <[email protected]>
@mrodgers-witekio mrodgers-witekio force-pushed the websocket_pass_request_headers_to_callback branch from 2229c90 to 15f48a3 Compare January 8, 2025 15:44
@mrodgers-witekio
Copy link
Collaborator Author

It seems shell_websocket.h needs an update too (shell_websocket_setup declaration), looks good otherwise, thanks.

Good spot, updated this and also removed the extern from the function declarations in the header file (don't think this is needed unless I missed something?)

@rlubos
Copy link
Contributor

rlubos commented Jan 8, 2025

removed the extern from the function declarations in the header file (don't think this is needed unless I missed something?)

I don'tt think they're needed either, probably just some copy-paste leftover.

@kartben kartben merged commit 579e586 into zephyrproject-rtos:main Jan 8, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: HTTP HTTP client/server support area: Networking area: Samples Samples area: Shell Shell subsystem area: Sockets Networking sockets Release Notes To be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants