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
11 changes: 9 additions & 2 deletions 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 @@ -474,7 +476,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 @@ -725,7 +731,8 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
client->config->cert_common_name = config->cert_common_name;
client->config->crt_bundle_attach = config->crt_bundle_attach;
client->config->ext_transport = config->ext_transport;

client->config->response_headers = config->response_headers;
client->config->response_headers_len = config->response_headers_len;
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 @@ -177,6 +178,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 = malloc(WS_HANDSHAKE_RESPONSE_HEADERS_MAX_SIZE);
websocket_cfg.response_headers_len = WS_HANDSHAKE_RESPONSE_HEADERS_MAX_SIZE;

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

Expand All @@ -189,6 +192,12 @@ static void websocket_app_start(void)
int i = 0;
while (i < 5) {
if (esp_websocket_client_is_connected(client)) {
if (i == 0) {
/* WebSocket handshake response headers if available */
if (websocket_cfg.response_headers) {
ESP_LOGI(TAG, "WebSocket response headers:\n%s", websocket_cfg.response_headers);
}
}
int len = sprintf(data, "hello %04d", i++);
ESP_LOGI(TAG, "Sending %s", data);
esp_websocket_client_send_text(client, data, len, portMAX_DELAY);
Expand Down Expand Up @@ -226,6 +235,7 @@ static void websocket_app_start(void)

xSemaphoreTake(shutdown_sema, portMAX_DELAY);
esp_websocket_client_close(client, portMAX_DELAY);
free(websocket_cfg.response_headers);
ESP_LOGI(TAG, "Websocket Stopped");
esp_websocket_unregister_events(client, WEBSOCKET_EVENT_ANY, websocket_event_handler);
esp_websocket_client_destroy(client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,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 */
size_t response_headers_len; /*!< WebSocket handshake response headers length */
} esp_websocket_client_config_t;

/**
Expand Down