From 6a475393ca5409d09aca53677d82b3b7c4cfc599 Mon Sep 17 00:00:00 2001 From: glmfe Date: Wed, 2 Apr 2025 08:07:04 -0300 Subject: [PATCH] feat(websocket): Add ws get HTTP response headers --- .../esp_websocket_client.c | 21 +++++++++++++++++++ .../examples/target/main/websocket_example.c | 5 +++++ .../include/esp_websocket_client.h | 12 +++++++++++ 3 files changed, 38 insertions(+) diff --git a/components/esp_websocket_client/esp_websocket_client.c b/components/esp_websocket_client/esp_websocket_client.c index 3b6ec19934..cf5f2f8c84 100644 --- a/components/esp_websocket_client/esp_websocket_client.c +++ b/components/esp_websocket_client/esp_websocket_client.c @@ -25,6 +25,11 @@ static const char *TAG = "websocket_client"; +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0) +// Features supported in 5.5.0 +#define WS_TRANSPORT_GET_RESPONSE_HEADER 1 +#endif + #define WEBSOCKET_TCP_DEFAULT_PORT (80) #define WEBSOCKET_SSL_DEFAULT_PORT (443) #define WEBSOCKET_BUFFER_SIZE_BYTE (1024) @@ -85,6 +90,7 @@ typedef struct { char *subprotocol; char *user_agent; char *headers; + const char *response_headers; int pingpong_timeout_sec; size_t ping_interval_sec; const char *cert; @@ -1020,6 +1026,9 @@ static void esp_websocket_client_task(void *pv) client->config->host, client->config->port, client->config->network_timeout_ms); +#if WS_TRANSPORT_GET_RESPONSE_HEADER + client->config->response_headers = esp_transport_ws_get_response_header(client->transport); +#endif if (result < 0) { esp_tls_error_handle_t error_handle = esp_transport_get_error_handle(client->transport); 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 return client->wait_timeout_ms; } +#if WS_TRANSPORT_GET_RESPONSE_HEADER +const char* esp_websocket_client_get_response_header(esp_websocket_client_handle_t client) +{ + if (client == NULL) { + ESP_LOGW(TAG, "Client was not initialized"); + return NULL; + } + + return client->config->response_headers; +} +#endif + esp_err_t esp_websocket_client_set_reconnect_timeout(esp_websocket_client_handle_t client, int reconnect_timeout_ms) { if (client == NULL) { diff --git a/components/esp_websocket_client/examples/target/main/websocket_example.c b/components/esp_websocket_client/examples/target/main/websocket_example.c index 31998527b5..7e7611cbd0 100644 --- a/components/esp_websocket_client/examples/target/main/websocket_example.c +++ b/components/esp_websocket_client/examples/target/main/websocket_example.c @@ -195,6 +195,11 @@ static void websocket_app_start(void) } vTaskDelay(1000 / portTICK_PERIOD_MS); } + /* WebSocket handshake response headers if available */ + const char *headers = esp_websocket_client_get_response_header(client); + if (headers) { + ESP_LOGI(TAG, "WebSocket response headers:\n%s", headers); + } vTaskDelay(1000 / portTICK_PERIOD_MS); // Sending text data diff --git a/components/esp_websocket_client/include/esp_websocket_client.h b/components/esp_websocket_client/include/esp_websocket_client.h index 9923676aeb..6f0e085ce9 100644 --- a/components/esp_websocket_client/include/esp_websocket_client.h +++ b/components/esp_websocket_client/include/esp_websocket_client.h @@ -432,6 +432,18 @@ esp_err_t esp_websocket_client_set_ping_interval_sec(esp_websocket_client_handle */ int esp_websocket_client_get_reconnect_timeout(esp_websocket_client_handle_t client); +/** + * @brief Get the response header for client. + * + * Notes: + * - This API should be called after the connection atempt otherwise its result is meaningless + * + * @param[in] client The client + * + * @return The response header + */ +const char* esp_websocket_client_get_response_header(esp_websocket_client_handle_t client); + /** * @brief Set next reconnect timeout for client. *