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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/connectivity/networking/api/http_server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ release it when done.
static int ws_socket;
static uint8_t ws_recv_buffer[1024];

int ws_setup(int sock, void *user_data)
int ws_setup(int sock, struct http_request_ctx *request_ctx, void *user_data)
{
ws_socket = sock;
return 0;
Expand Down
5 changes: 5 additions & 0 deletions doc/releases/migration-guide-4.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,11 @@ Networking
rather than directly in the :c:struct:`http_client_ctx` to correctly handle concurrent requests
on different HTTP/2 streams.

* The HTTP server public API function signature for the :c:type:`http_resource_websocket_cb_t` has
changed, a :c:struct:`http_request_ctx` parameter has been added. The application may use this to
access the request headers of the HTTP upgrade request, which may be useful in deciding whether
to accept or reject a websocket connection.

* The :kconfig:option:`CONFIG_NET_L2_OPENTHREAD` symbol no longer implies the
:kconfig:option:`CONFIG_NVS` Kconfig option. Platforms using OpenThread must explicitly enable
either the :kconfig:option:`CONFIG_NVS` or :kconfig:option:`CONFIG_ZMS` Kconfig option.
Expand Down
3 changes: 2 additions & 1 deletion include/zephyr/net/http/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,15 @@ BUILD_ASSERT(offsetof(struct http_resource_detail_dynamic, common) == 0);
* reading and writing websocket data, and closing the connection.
*
* @param ws_socket A socket for the Websocket data.
* @param request_ctx Request context structure associated with HTTP upgrade request
* @param user_data User specified data.
*
* @return 0 Accepting the connection, HTTP server library will no longer
* handle data to/from the socket and it is application responsibility
* to send and receive data to/from the supplied socket.
* <0 error, close the connection.
*/
typedef int (*http_resource_websocket_cb_t)(int ws_socket,
typedef int (*http_resource_websocket_cb_t)(int ws_socket, struct http_request_ctx *request_ctx,
void *user_data);

/** @brief Representation of a websocket server resource */
Expand Down
4 changes: 2 additions & 2 deletions include/zephyr/shell/shell_websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ struct shell_websocket {
};

extern const struct shell_transport_api shell_websocket_transport_api;
extern int shell_websocket_setup(int ws_socket, void *user_data);
extern int shell_websocket_enable(const struct shell *sh);
int shell_websocket_setup(int ws_socket, struct http_request_ctx *request_ctx, void *user_data);
int shell_websocket_enable(const struct shell *sh);

#define GET_WS_NAME(_service) ws_ctx_##_service
#define GET_WS_SHELL_NAME(_name) shell_websocket_##_name
Expand Down
4 changes: 2 additions & 2 deletions samples/net/sockets/http_server/src/ws.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ int ws_netstats_init(void)
}
SYS_INIT(ws_netstats_init, APPLICATION, 0);

int ws_echo_setup(int ws_socket, void *user_data)
int ws_echo_setup(int ws_socket, struct http_request_ctx *request_ctx, void *user_data)
{
int slot;

Expand Down Expand Up @@ -331,7 +331,7 @@ int ws_echo_setup(int ws_socket, void *user_data)
return 0;
}

int ws_netstats_setup(int ws_socket, void *user_data)
int ws_netstats_setup(int ws_socket, struct http_request_ctx *request_ctx, void *user_data)
{
int ret;
int slot;
Expand Down
8 changes: 6 additions & 2 deletions samples/net/sockets/http_server/src/ws.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/net/http/server.h>

/**
* @brief Setup websocket for echoing data back to client
*
* @param ws_socket Socket file descriptor associated with websocket
* @param request_ctx Request context associated with websocket HTTP upgrade request
* @param user_data User data pointer
*
* @return 0 on success
*/
int ws_echo_setup(int ws_socket, void *user_data);
int ws_echo_setup(int ws_socket, struct http_request_ctx *request_ctx, void *user_data);

/**
* @brief Setup websocket for sending net statistics to client
*
* @param ws_socket Socket file descriptor associated with websocket
* @param request_ctx Request context associated with websocket HTTP upgrade request
* @param user_data User data pointer
*
* @return 0 on success
*/
int ws_netstats_setup(int ws_socket, void *user_data);
int ws_netstats_setup(int ws_socket, struct http_request_ctx *request_ctx, void *user_data);
1 change: 1 addition & 0 deletions subsys/net/lib/http/http_server_http1.c
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ int handle_http1_request(struct http_client_ctx *client)
goto not_found;
}

detail->path_len = path_len;
client->current_detail = detail;
return handle_http1_to_websocket_upgrade(client);
}
Expand Down
10 changes: 9 additions & 1 deletion subsys/net/lib/http/http_server_ws.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ int handle_http1_to_websocket_upgrade(struct http_client_ctx *client)
*/
if (client->parser_state == HTTP1_MESSAGE_COMPLETE_STATE) {
struct http_resource_detail_websocket *ws_detail;
struct http_request_ctx request_ctx;
int ws_sock;
char *params;
size_t params_len;

ws_detail = (struct http_resource_detail_websocket *)client->current_detail;

Expand All @@ -105,9 +108,14 @@ int handle_http1_to_websocket_upgrade(struct http_client_ctx *client)
goto error;
}

memset(&request_ctx, 0, sizeof(request_ctx));
params = &client->url_buffer[client->current_detail->path_len];
params_len = strlen(params);
populate_request_ctx(&request_ctx, params, params_len, &client->header_capture_ctx);

ret = ws_detail->cb(ws_sock, &request_ctx, ws_detail->user_data);
http_server_release_client(client);

ret = ws_detail->cb(ws_sock, ws_detail->user_data);
if (ret < 0) {
NET_DBG("WS connection failed (%d)", ret);
websocket_unregister(ws_sock);
Expand Down
2 changes: 1 addition & 1 deletion subsys/shell/backends/shell_websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ const struct shell_transport_api shell_websocket_transport_api = {
.read = sh_read
};

int shell_websocket_setup(int ws_socket, void *user_data)
int shell_websocket_setup(int ws_socket, struct http_request_ctx *request_ctx, void *user_data)
{
struct shell_websocket *ws = user_data;

Expand Down
Loading