Skip to content

Commit 9f1299b

Browse files
committed
feat(websocket): Add ws get HTTP response headers
1 parent d2e94e5 commit 9f1299b

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

components/esp_websocket_client/esp_websocket_client.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525

2626
static const char *TAG = "websocket_client";
2727

28+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
29+
// Features supported in 5.5.0
30+
#define WS_TRANSPORT_STORE_RESPONSE_HEADERS 1
31+
#endif
32+
2833
#define WEBSOCKET_TCP_DEFAULT_PORT (80)
2934
#define WEBSOCKET_SSL_DEFAULT_PORT (443)
3035
#define WEBSOCKET_BUFFER_SIZE_BYTE (1024)
@@ -101,6 +106,8 @@ typedef struct {
101106
const char *cert_common_name;
102107
esp_err_t (*crt_bundle_attach)(void *conf);
103108
esp_transport_handle_t ext_transport;
109+
char *response_headers;
110+
size_t response_headers_len;
104111
} websocket_config_storage_t;
105112

106113
typedef enum {
@@ -474,7 +481,11 @@ static esp_err_t set_websocket_transport_optional_settings(esp_websocket_client_
474481
.user_agent = client->config->user_agent,
475482
.headers = client->config->headers,
476483
.auth = client->config->auth,
477-
.propagate_control_frames = true
484+
.propagate_control_frames = true,
485+
#if WS_TRANSPORT_STORE_RESPONSE_HEADERS
486+
.response_headers = client->config->response_headers,
487+
.response_headers_len = client->config->response_headers_len
488+
#endif
478489
};
479490
return esp_transport_ws_set_config(trans, &config);
480491
}
@@ -725,7 +736,8 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
725736
client->config->cert_common_name = config->cert_common_name;
726737
client->config->crt_bundle_attach = config->crt_bundle_attach;
727738
client->config->ext_transport = config->ext_transport;
728-
739+
client->config->response_headers = config->response_headers;
740+
client->config->response_headers_len = config->response_headers_len;
729741
if (config->uri) {
730742
if (esp_websocket_client_set_uri(client, config->uri) != ESP_OK) {
731743
ESP_LOGE(TAG, "Invalid uri");

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

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <cJSON.h>
3333

3434
#define NO_DATA_TIMEOUT_SEC 5
35+
#define WS_HANDSHAKE_RESPONSE_HEADERS_MAX_SIZE 1024
3536

3637
static const char *TAG = "websocket";
3738

@@ -177,6 +178,8 @@ static void websocket_app_start(void)
177178
#if CONFIG_WS_OVER_TLS_SKIP_COMMON_NAME_CHECK
178179
websocket_cfg.skip_cert_common_name_check = true;
179180
#endif
181+
websocket_cfg.response_headers = malloc(WS_HANDSHAKE_RESPONSE_HEADERS_MAX_SIZE);
182+
websocket_cfg.response_headers_len = WS_HANDSHAKE_RESPONSE_HEADERS_MAX_SIZE;
180183

181184
ESP_LOGI(TAG, "Connecting to %s...", websocket_cfg.uri);
182185

@@ -189,6 +192,12 @@ static void websocket_app_start(void)
189192
int i = 0;
190193
while (i < 5) {
191194
if (esp_websocket_client_is_connected(client)) {
195+
if (i == 0) {
196+
/* WebSocket handshake response headers if available */
197+
if (websocket_cfg.response_headers) {
198+
ESP_LOGI(TAG, "WebSocket response headers:\n%s", websocket_cfg.response_headers);
199+
}
200+
}
192201
int len = sprintf(data, "hello %04d", i++);
193202
ESP_LOGI(TAG, "Sending %s", data);
194203
esp_websocket_client_send_text(client, data, len, portMAX_DELAY);

components/esp_websocket_client/include/esp_websocket_client.h

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ typedef struct {
134134
size_t ping_interval_sec; /*!< Websocket ping interval, defaults to 10 seconds if not set */
135135
struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
136136
esp_transport_handle_t ext_transport; /*!< External WebSocket tcp_transport handle to the client; or if null, the client will create its own transport handle. */
137+
char *response_headers; /*!< WebSocket handshake response headers */
138+
size_t response_headers_len; /*!< WebSocket handshake response headers length */
137139
} esp_websocket_client_config_t;
138140

139141
/**

0 commit comments

Comments
 (0)