Skip to content

Commit 549e5de

Browse files
mrodgers-witekiokartben
authored andcommitted
net: websocket: pass HTTP upgrade request context to user callback
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]>
1 parent 91c10f1 commit 549e5de

File tree

7 files changed

+23
-9
lines changed

7 files changed

+23
-9
lines changed

include/zephyr/net/http/server.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,15 @@ BUILD_ASSERT(offsetof(struct http_resource_detail_dynamic, common) == 0);
254254
* reading and writing websocket data, and closing the connection.
255255
*
256256
* @param ws_socket A socket for the Websocket data.
257+
* @param request_ctx Request context structure associated with HTTP upgrade request
257258
* @param user_data User specified data.
258259
*
259260
* @return 0 Accepting the connection, HTTP server library will no longer
260261
* handle data to/from the socket and it is application responsibility
261262
* to send and receive data to/from the supplied socket.
262263
* <0 error, close the connection.
263264
*/
264-
typedef int (*http_resource_websocket_cb_t)(int ws_socket,
265+
typedef int (*http_resource_websocket_cb_t)(int ws_socket, struct http_request_ctx *request_ctx,
265266
void *user_data);
266267

267268
/** @brief Representation of a websocket server resource */

include/zephyr/shell/shell_websocket.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ struct shell_websocket {
6262
};
6363

6464
extern const struct shell_transport_api shell_websocket_transport_api;
65-
extern int shell_websocket_setup(int ws_socket, void *user_data);
66-
extern int shell_websocket_enable(const struct shell *sh);
65+
int shell_websocket_setup(int ws_socket, struct http_request_ctx *request_ctx, void *user_data);
66+
int shell_websocket_enable(const struct shell *sh);
6767

6868
#define GET_WS_NAME(_service) ws_ctx_##_service
6969
#define GET_WS_SHELL_NAME(_name) shell_websocket_##_name

samples/net/sockets/http_server/src/ws.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ int ws_netstats_init(void)
295295
}
296296
SYS_INIT(ws_netstats_init, APPLICATION, 0);
297297

298-
int ws_echo_setup(int ws_socket, void *user_data)
298+
int ws_echo_setup(int ws_socket, struct http_request_ctx *request_ctx, void *user_data)
299299
{
300300
int slot;
301301

@@ -331,7 +331,7 @@ int ws_echo_setup(int ws_socket, void *user_data)
331331
return 0;
332332
}
333333

334-
int ws_netstats_setup(int ws_socket, void *user_data)
334+
int ws_netstats_setup(int ws_socket, struct http_request_ctx *request_ctx, void *user_data)
335335
{
336336
int ret;
337337
int slot;

samples/net/sockets/http_server/src/ws.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include <zephyr/net/http/server.h>
8+
79
/**
810
* @brief Setup websocket for echoing data back to client
911
*
1012
* @param ws_socket Socket file descriptor associated with websocket
13+
* @param request_ctx Request context associated with websocket HTTP upgrade request
1114
* @param user_data User data pointer
1215
*
1316
* @return 0 on success
1417
*/
15-
int ws_echo_setup(int ws_socket, void *user_data);
18+
int ws_echo_setup(int ws_socket, struct http_request_ctx *request_ctx, void *user_data);
1619

1720
/**
1821
* @brief Setup websocket for sending net statistics to client
1922
*
2023
* @param ws_socket Socket file descriptor associated with websocket
24+
* @param request_ctx Request context associated with websocket HTTP upgrade request
2125
* @param user_data User data pointer
2226
*
2327
* @return 0 on success
2428
*/
25-
int ws_netstats_setup(int ws_socket, void *user_data);
29+
int ws_netstats_setup(int ws_socket, struct http_request_ctx *request_ctx, void *user_data);

subsys/net/lib/http/http_server_http1.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ int handle_http1_request(struct http_client_ctx *client)
865865
goto not_found;
866866
}
867867

868+
detail->path_len = path_len;
868869
client->current_detail = detail;
869870
return handle_http1_to_websocket_upgrade(client);
870871
}

subsys/net/lib/http/http_server_ws.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ int handle_http1_to_websocket_upgrade(struct http_client_ctx *client)
9393
*/
9494
if (client->parser_state == HTTP1_MESSAGE_COMPLETE_STATE) {
9595
struct http_resource_detail_websocket *ws_detail;
96+
struct http_request_ctx request_ctx;
9697
int ws_sock;
98+
char *params;
99+
size_t params_len;
97100

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

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

111+
memset(&request_ctx, 0, sizeof(request_ctx));
112+
params = &client->url_buffer[client->current_detail->path_len];
113+
params_len = strlen(params);
114+
populate_request_ctx(&request_ctx, params, params_len, &client->header_capture_ctx);
115+
116+
ret = ws_detail->cb(ws_sock, &request_ctx, ws_detail->user_data);
108117
http_server_release_client(client);
109118

110-
ret = ws_detail->cb(ws_sock, ws_detail->user_data);
111119
if (ret < 0) {
112120
NET_DBG("WS connection failed (%d)", ret);
113121
websocket_unregister(ws_sock);

subsys/shell/backends/shell_websocket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ const struct shell_transport_api shell_websocket_transport_api = {
377377
.read = sh_read
378378
};
379379

380-
int shell_websocket_setup(int ws_socket, void *user_data)
380+
int shell_websocket_setup(int ws_socket, struct http_request_ctx *request_ctx, void *user_data)
381381
{
382382
struct shell_websocket *ws = user_data;
383383

0 commit comments

Comments
 (0)