From 3af8001e0a75c25433bf4cabea7ee9600cc09a51 Mon Sep 17 00:00:00 2001 From: Matt Rodgers Date: Wed, 4 Sep 2024 13:52:26 +0100 Subject: [PATCH] doc: http_server: document capturing of request headers Add documentation on new feature allowing application to access HTTP request headers. Signed-off-by: Matt Rodgers --- .../networking/api/http_server.rst | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/doc/connectivity/networking/api/http_server.rst b/doc/connectivity/networking/api/http_server.rst index ac24bb84c05..2d4444e3478 100644 --- a/doc/connectivity/networking/api/http_server.rst +++ b/doc/connectivity/networking/api/http_server.rst @@ -346,6 +346,38 @@ processing of the Websocket connection is application-specific, hence outside of scope of this guide. See :zephyr:code-sample:`sockets-http-server` for an example Websocket-based echo service implementation. +Accessing request headers +========================= + +The application can register an interest in any specific HTTP request headers. +These headers are then stored for each incoming request, and can be accessed +from within a dynamic resource callback. + +This feature must first be enabled with +:kconfig:option:`CONFIG_HTTP_SERVER_CAPTURE_HEADERS` Kconfig option. + +Then the application can register headers to be captured, and read the values +from within the dynamic resource callback: + +.. code-block:: c + + HTTP_SERVER_REGISTER_HEADER_CAPTURE(capture_user_agent, "User-Agent"); + + static int dyn_handler(struct http_client_ctx *client, enum http_data_status status, + uint8_t *buffer, size_t len, void *user_data) + { + size_t header_count = client->header_capture_ctx.count; + const struct http_header *headers = client->header_capture_ctx.headers; + + LOG_INF("Captured %d headers with request", header_count); + + for (uint32_t i = 0; i < header_count; i++) { + LOG_INF("Header: '%s: %s'", headers[i].name, headers[i].value); + } + + return 0; + } + API Reference *************