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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion components/esp_websocket_client/esp_websocket_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ typedef struct {
const char *cert_common_name;
esp_err_t (*crt_bundle_attach)(void *conf);
esp_transport_handle_t ext_transport;
char *response_headers;
size_t response_headers_len;
} websocket_config_storage_t;

typedef enum {
Expand Down Expand Up @@ -215,6 +217,9 @@ static esp_err_t esp_websocket_client_dispatch_event(esp_websocket_client_handle
event_data.error_handle.error_type = client->error_handle.error_type;
event_data.error_handle.esp_ws_handshake_status_code = client->error_handle.esp_ws_handshake_status_code;

// Add response headers to event data
event_data.response_headers = client->config->response_headers;
event_data.response_headers_len = client->config->response_headers_len;

if ((err = esp_event_post_to(client->event_handle,
WEBSOCKET_EVENTS, event,
Expand Down Expand Up @@ -420,6 +425,7 @@ static esp_err_t esp_websocket_client_destroy_config(esp_websocket_client_handle
free(cfg->subprotocol);
free(cfg->user_agent);
free(cfg->headers);
free(cfg->response_headers);
memset(cfg, 0, sizeof(websocket_config_storage_t));
free(client->config);
client->config = NULL;
Expand Down Expand Up @@ -474,7 +480,11 @@ static esp_err_t set_websocket_transport_optional_settings(esp_websocket_client_
.user_agent = client->config->user_agent,
.headers = client->config->headers,
.auth = client->config->auth,
.propagate_control_frames = true
.propagate_control_frames = true,
#if WS_TRANSPORT_STORE_RESPONSE_HEADERS
.response_headers = client->config->response_headers,
.response_headers_len = client->config->response_headers_len
#endif
};
return esp_transport_ws_set_config(trans, &config);
}
Expand Down Expand Up @@ -726,6 +736,17 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
client->config->crt_bundle_attach = config->crt_bundle_attach;
client->config->ext_transport = config->ext_transport;

// Allocate memory for response_headers if length is specified
if (config->response_headers_len > 0) {
client->config->response_headers = malloc(config->response_headers_len);
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->response_headers, goto _websocket_init_fail);
memset(client->config->response_headers, 0, config->response_headers_len);
client->config->response_headers_len = config->response_headers_len;
} else {
client->config->response_headers = NULL;
client->config->response_headers_len = 0;
}

if (config->uri) {
if (esp_websocket_client_set_uri(client, config->uri) != ESP_OK) {
ESP_LOGE(TAG, "Invalid uri");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <cJSON.h>

#define NO_DATA_TIMEOUT_SEC 5
#define WS_HANDSHAKE_RESPONSE_HEADERS_MAX_SIZE 1024

static const char *TAG = "websocket";

Expand Down Expand Up @@ -79,6 +80,10 @@ static void websocket_event_handler(void *handler_args, esp_event_base_t base, i
break;
case WEBSOCKET_EVENT_CONNECTED:
ESP_LOGI(TAG, "WEBSOCKET_EVENT_CONNECTED");
/* WebSocket handshake response headers if available */
if (data->response_headers && data->response_headers_len > 0) {
ESP_LOGI(TAG, "WebSocket response headers:\n%.*s", (int)data->response_headers_len, data->response_headers);
}
break;
case WEBSOCKET_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "WEBSOCKET_EVENT_DISCONNECTED");
Expand Down Expand Up @@ -177,6 +182,8 @@ static void websocket_app_start(void)
#if CONFIG_WS_OVER_TLS_SKIP_COMMON_NAME_CHECK
websocket_cfg.skip_cert_common_name_check = true;
#endif
websocket_cfg.response_headers = NULL;
websocket_cfg.response_headers_len = WS_HANDSHAKE_RESPONSE_HEADERS_MAX_SIZE;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ typedef struct {
int payload_len; /*!< Total payload length, payloads exceeding buffer will be posted through multiple events */
int payload_offset; /*!< Actual offset for the data associated with this event */
esp_websocket_error_codes_t error_handle; /*!< esp-websocket error handle including esp-tls errors as well as internal websocket errors */
const char *response_headers; /*!< WebSocket handshake response headers */
size_t response_headers_len; /*!< WebSocket handshake response headers length */
} esp_websocket_event_data_t;

/**
Expand Down Expand Up @@ -134,6 +136,8 @@ typedef struct {
size_t ping_interval_sec; /*!< Websocket ping interval, defaults to 10 seconds if not set */
struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
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. */
char *response_headers; /*!< WebSocket handshake response headers */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
char *response_headers; /*!< WebSocket handshake response headers */
char *response_headers; /*!< WebSocket handshake response headers buffer, user is responsible for manage lifetime. */

size_t response_headers_len; /*!< WebSocket handshake response headers length */
} esp_websocket_client_config_t;

/**
Expand Down