Add User-Agent and additional headers to esp_websocket_client

Merges https://github.com/espressif/esp-idf/pull/4345


* Original commit: espressif/esp-idf@9200250f51
This commit is contained in:
David N. Junod
2019-11-15 04:15:55 -07:00
committed by gabsuren
parent 1fcc001ae8
commit a48b0fafe8
2 changed files with 22 additions and 0 deletions

View File

@ -63,6 +63,8 @@ typedef struct {
void *user_context; void *user_context;
int network_timeout_ms; int network_timeout_ms;
char *subprotocol; char *subprotocol;
char *user_agent;
char *headers;
} websocket_config_storage_t; } websocket_config_storage_t;
typedef enum { typedef enum {
@ -179,6 +181,16 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c
cfg->subprotocol = strdup(config->subprotocol); cfg->subprotocol = strdup(config->subprotocol);
ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->subprotocol, return ESP_ERR_NO_MEM); ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->subprotocol, return ESP_ERR_NO_MEM);
} }
if (config->user_agent) {
free(cfg->user_agent);
cfg->user_agent = strdup(config->user_agent);
ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->user_agent, return ESP_ERR_NO_MEM);
}
if (config->headers) {
free(cfg->headers);
cfg->headers = strdup(config->headers);
ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->headers, return ESP_ERR_NO_MEM);
}
cfg->network_timeout_ms = WEBSOCKET_NETWORK_TIMEOUT_MS; cfg->network_timeout_ms = WEBSOCKET_NETWORK_TIMEOUT_MS;
cfg->user_context = config->user_context; cfg->user_context = config->user_context;
@ -207,6 +219,8 @@ static esp_err_t esp_websocket_client_destroy_config(esp_websocket_client_handle
free(cfg->username); free(cfg->username);
free(cfg->password); free(cfg->password);
free(cfg->subprotocol); free(cfg->subprotocol);
free(cfg->user_agent);
free(cfg->headers);
memset(cfg, 0, sizeof(websocket_config_storage_t)); memset(cfg, 0, sizeof(websocket_config_storage_t));
free(client->config); free(client->config);
client->config = NULL; client->config = NULL;
@ -221,6 +235,12 @@ static void set_websocket_transport_optional_settings(esp_websocket_client_handl
if (trans && client->config->subprotocol) { if (trans && client->config->subprotocol) {
esp_transport_ws_set_subprotocol(trans, client->config->subprotocol); esp_transport_ws_set_subprotocol(trans, client->config->subprotocol);
} }
if (trans && client->config->user_agent) {
esp_transport_ws_set_user_agent(trans, client->config->user_agent);
}
if (trans && client->config->headers) {
esp_transport_ws_set_headers(trans, client->config->headers);
}
} }
esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_client_config_t *config) esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_client_config_t *config)

View File

@ -92,6 +92,8 @@ typedef struct {
const char *cert_pem; /*!< SSL Certification, PEM format as string, if the client requires to verify server */ const char *cert_pem; /*!< SSL Certification, PEM format as string, if the client requires to verify server */
esp_websocket_transport_t transport; /*!< Websocket transport type, see `esp_websocket_transport_t */ esp_websocket_transport_t transport; /*!< Websocket transport type, see `esp_websocket_transport_t */
char *subprotocol; /*!< Websocket subprotocol */ char *subprotocol; /*!< Websocket subprotocol */
char *user_agent; /*!< Websocket user-agent */
char *headers; /*!< Websocket additional headers */
} esp_websocket_client_config_t; } esp_websocket_client_config_t;
/** /**