Skip to content

Commit

Permalink
docs(websocket): improve API's header notes
Browse files Browse the repository at this point in the history
  • Loading branch information
suren-gabrielyan-espressif committed Oct 6, 2023
1 parent af1f39e commit c3bd531
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
35 changes: 34 additions & 1 deletion components/esp_websocket_client/esp_websocket_client.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -358,6 +358,7 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c
}
if (config->headers) {
free(cfg->headers);
ESP_LOGI(TAG, "line = %d", __LINE__);
cfg->headers = strdup(config->headers);
ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->headers, return ESP_ERR_NO_MEM);
}
Expand Down Expand Up @@ -756,6 +757,38 @@ esp_err_t esp_websocket_client_set_headers(esp_websocket_client_handle_t client,
return ret;
}

esp_err_t esp_websocket_client_append_header(esp_websocket_client_handle_t client, const char *key, const char *value)
{
if (client == NULL || key == NULL || value == NULL) {
return ESP_ERR_INVALID_ARG;
}
websocket_config_storage_t *cfg = client->config;
size_t len = strlen(key) + strlen(value) + 3 + 2; // Length for "key: value\r\n"

if (cfg->headers == NULL) {
cfg->headers = (char *)malloc(len);
if (cfg->headers == NULL) {
return ESP_ERR_NO_MEM;
}
snprintf(cfg->headers, len, "%s: %s\r\n", key, value);
return ESP_OK;
}

char *new_headers;
size_t current_len = strlen(cfg->headers);
size_t new_len = current_len + len;

new_headers = (char *)realloc(cfg->headers, new_len);
if (new_headers == NULL) {
return ESP_ERR_NO_MEM;
}

snprintf(new_headers + current_len, len, "%s: %s\r\n", key, value);
cfg->headers = new_headers;

return ESP_OK;
}

static esp_err_t esp_websocket_client_recv(esp_websocket_client_handle_t client)
{
int rlen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,15 @@ static void websocket_app_start(void)

#else
websocket_cfg.uri = CONFIG_WEBSOCKET_URI;
//websocket_cfg.headers = "Sec-WebSocket-Key: my_key\r\nPassword: my_pass\r\n";
#endif /* CONFIG_WEBSOCKET_URI_FROM_STDIN */

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

esp_websocket_client_handle_t client = esp_websocket_client_init(&websocket_cfg);
esp_websocket_register_events(client, WEBSOCKET_EVENT_ANY, websocket_event_handler, (void *)client);
esp_websocket_client_append_header(client, "Sec-WebSocket-Key", "my_key");
esp_websocket_client_append_header(client, "Password", "my_pass");

esp_websocket_client_start(client);
xTimerStart(shutdown_signal_timer, portMAX_DELAY);
Expand Down
19 changes: 18 additions & 1 deletion components/esp_websocket_client/include/esp_websocket_client.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -158,13 +158,30 @@ esp_err_t esp_websocket_client_set_uri(esp_websocket_client_handle_t client, con
* @brief Set additional websocket headers for the client, when performing this behavior, the headers will replace the old ones
* @pre Must stop the WebSocket client before set headers if the client has been connected
*
* Notes:
* - To set multiple headers, you can concatenate them in a single line using the "\r\n" symbol.
* Example: websocket_cfg.headers="Sec-WebSocket-Key: my_key\r\nPassword: my_pass\r\n";
*
* @param[in] client The client
* @param headers additional header strings each terminated with \r\n
*
* @return esp_err_t
*/
esp_err_t esp_websocket_client_set_headers(esp_websocket_client_handle_t client, const char *headers);

/**
* @brief Appends new key value for to the headers of websocket client
* Notes:
* - This API should be called before websocket client start
*
* @param[in] client The client
* @param[in] key key which will be appended
* @param[in] value Value of the key
*
* @return esp_err_t
*/
esp_err_t esp_websocket_client_append_header(esp_websocket_client_handle_t client, const char *key, const char *value);

/**
* @brief Open the WebSocket connection
*
Expand Down

0 comments on commit c3bd531

Please sign in to comment.