Skip to content

Commit 6a47539

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

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

components/esp_websocket_client/esp_websocket_client.c

+21
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_GET_RESPONSE_HEADER 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)
@@ -85,6 +90,7 @@ typedef struct {
8590
char *subprotocol;
8691
char *user_agent;
8792
char *headers;
93+
const char *response_headers;
8894
int pingpong_timeout_sec;
8995
size_t ping_interval_sec;
9096
const char *cert;
@@ -1020,6 +1026,9 @@ static void esp_websocket_client_task(void *pv)
10201026
client->config->host,
10211027
client->config->port,
10221028
client->config->network_timeout_ms);
1029+
#if WS_TRANSPORT_GET_RESPONSE_HEADER
1030+
client->config->response_headers = esp_transport_ws_get_response_header(client->transport);
1031+
#endif
10231032
if (result < 0) {
10241033
esp_tls_error_handle_t error_handle = esp_transport_get_error_handle(client->transport);
10251034
client->error_handle.esp_ws_handshake_status_code = esp_transport_ws_get_upgrade_request_status(client->transport);
@@ -1341,6 +1350,18 @@ int esp_websocket_client_get_reconnect_timeout(esp_websocket_client_handle_t cli
13411350
return client->wait_timeout_ms;
13421351
}
13431352

1353+
#if WS_TRANSPORT_GET_RESPONSE_HEADER
1354+
const char* esp_websocket_client_get_response_header(esp_websocket_client_handle_t client)
1355+
{
1356+
if (client == NULL) {
1357+
ESP_LOGW(TAG, "Client was not initialized");
1358+
return NULL;
1359+
}
1360+
1361+
return client->config->response_headers;
1362+
}
1363+
#endif
1364+
13441365
esp_err_t esp_websocket_client_set_reconnect_timeout(esp_websocket_client_handle_t client, int reconnect_timeout_ms)
13451366
{
13461367
if (client == NULL) {

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

+5
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ static void websocket_app_start(void)
195195
}
196196
vTaskDelay(1000 / portTICK_PERIOD_MS);
197197
}
198+
/* WebSocket handshake response headers if available */
199+
const char *headers = esp_websocket_client_get_response_header(client);
200+
if (headers) {
201+
ESP_LOGI(TAG, "WebSocket response headers:\n%s", headers);
202+
}
198203

199204
vTaskDelay(1000 / portTICK_PERIOD_MS);
200205
// Sending text data

components/esp_websocket_client/include/esp_websocket_client.h

+12
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,18 @@ esp_err_t esp_websocket_client_set_ping_interval_sec(esp_websocket_client_handle
432432
*/
433433
int esp_websocket_client_get_reconnect_timeout(esp_websocket_client_handle_t client);
434434

435+
/**
436+
* @brief Get the response header for client.
437+
*
438+
* Notes:
439+
* - This API should be called after the connection atempt otherwise its result is meaningless
440+
*
441+
* @param[in] client The client
442+
*
443+
* @return The response header
444+
*/
445+
const char* esp_websocket_client_get_response_header(esp_websocket_client_handle_t client);
446+
435447
/**
436448
* @brief Set next reconnect timeout for client.
437449
*

0 commit comments

Comments
 (0)