Skip to content

feat(websocket): Add ws get HTTP response headers #794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions components/esp_websocket_client/esp_websocket_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -85,6 +90,7 @@
char *subprotocol;
char *user_agent;
char *headers;
const char *response_headers;
int pingpong_timeout_sec;
size_t ping_interval_sec;
const char *cert;
Expand Down Expand Up @@ -1016,10 +1022,13 @@
break;
}
esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_BEFORE_CONNECT, NULL, 0);
int result = esp_transport_connect(client->transport,
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);
Expand Down Expand Up @@ -1341,6 +1350,18 @@
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions components/esp_websocket_client/include/esp_websocket_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading