Skip to content

Commit 3a9529b

Browse files
feat(websocket): add WEBSOCKET_EVENT_HEADER_RECEIVED
Send a new event for each HTTP header-line received. Depends on espressif/esp-idf#16119 Closes #715
1 parent 5ab7e83 commit 3a9529b

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

components/esp_websocket_client/esp_websocket_client.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,14 @@ static esp_err_t stop_wait_task(esp_websocket_client_handle_t client)
484484
return ESP_OK;
485485
}
486486

487+
#if WS_TRANSPORT_HEADER_CALLBACK_SUPPORT
488+
static void websocket_header_hook(void * client, const char * line, int line_len)
489+
{
490+
ESP_LOGD(TAG, "%s header:%.*s", __func__, line_len, line);
491+
esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_HEADER_RECEIVED, line, line_len);
492+
}
493+
#endif
494+
487495
static esp_err_t set_websocket_transport_optional_settings(esp_websocket_client_handle_t client, const char *scheme)
488496
{
489497
esp_transport_handle_t trans = esp_transport_list_get_transport(client->transport_list, scheme);
@@ -493,6 +501,10 @@ static esp_err_t set_websocket_transport_optional_settings(esp_websocket_client_
493501
.sub_protocol = client->config->subprotocol,
494502
.user_agent = client->config->user_agent,
495503
.headers = client->config->headers,
504+
#if WS_TRANSPORT_HEADER_CALLBACK_SUPPORT
505+
.header_hook = websocket_header_hook,
506+
.header_user_context = client,
507+
#endif
496508
.auth = client->config->auth,
497509
.propagate_control_frames = true
498510
};

components/esp_websocket_client/examples/linux/main/websocket_linux.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ static void websocket_event_handler(void *handler_args, esp_event_base_t base, i
2828
case WEBSOCKET_EVENT_BEGIN:
2929
ESP_LOGI(TAG, "WEBSOCKET_EVENT_BEGIN");
3030
break;
31+
#if WS_TRANSPORT_HEADER_CALLBACK_SUPPORT
32+
case WEBSOCKET_EVENT_HEADER_RECEIVED:
33+
ESP_LOGI(TAG, "WEBSOCKET_EVENT_HEADER_RECEIVED: %.*s", data->data_len, data->data_ptr);
34+
break;
35+
#endif
3136
case WEBSOCKET_EVENT_CONNECTED:
3237
ESP_LOGI(TAG, "WEBSOCKET_EVENT_CONNECTED");
3338
break;

components/esp_websocket_client/examples/target/main/websocket_example.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ static void websocket_event_handler(void *handler_args, esp_event_base_t base, i
7777
case WEBSOCKET_EVENT_BEGIN:
7878
ESP_LOGI(TAG, "WEBSOCKET_EVENT_BEGIN");
7979
break;
80+
#if WS_TRANSPORT_HEADER_CALLBACK_SUPPORT
81+
case WEBSOCKET_EVENT_HEADER_RECEIVED:
82+
ESP_LOGI(TAG, "WEBSOCKET_EVENT_HEADER_RECEIVED: %.*s", data->data_len, data->data_ptr);
83+
break;
84+
#endif
8085
case WEBSOCKET_EVENT_CONNECTED:
8186
ESP_LOGI(TAG, "WEBSOCKET_EVENT_CONNECTED");
8287
break;

components/esp_websocket_client/include/esp_websocket_client.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -21,6 +21,13 @@
2121
extern "C" {
2222
#endif
2323

24+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
25+
// Features supported in 6.0.0
26+
#define WS_TRANSPORT_HEADER_CALLBACK_SUPPORT 1
27+
#else
28+
#define WS_TRANSPORT_HEADER_CALLBACK_SUPPORT 0
29+
#endif
30+
2431
typedef struct esp_websocket_client *esp_websocket_client_handle_t;
2532

2633
ESP_EVENT_DECLARE_BASE(WEBSOCKET_EVENTS); // declaration of the task events family
@@ -31,6 +38,9 @@ ESP_EVENT_DECLARE_BASE(WEBSOCKET_EVENTS); // declaration of the task eve
3138
typedef enum {
3239
WEBSOCKET_EVENT_ANY = -1,
3340
WEBSOCKET_EVENT_ERROR = 0, /*!< This event occurs when there are any errors during execution */
41+
#if WS_TRANSPORT_HEADER_CALLBACK_SUPPORT
42+
WEBSOCKET_EVENT_HEADER_RECEIVED,/*!< This event occurs for each pre-upgrade HTTP header */
43+
#endif
3444
WEBSOCKET_EVENT_CONNECTED, /*!< Once the Websocket has been connected to the server, no data exchange has been performed */
3545
WEBSOCKET_EVENT_DISCONNECTED, /*!< The connection has been disconnected */
3646
WEBSOCKET_EVENT_DATA, /*!< When receiving data from the server, possibly multiple portions of the packet */

0 commit comments

Comments
 (0)