Skip to content

Commit

Permalink
fix(http_client): Set common tcp config to both TCP and SSL transport
Browse files Browse the repository at this point in the history
Foundation transport contained TCP properties for both TCP and SSL
transport, so it was enough to set the TCP connection properties
(keepalive, interface binding) to one transport only. After merging
5778a7c we have separate TCP properties for these transports and need
to set the same for both.
This commit also fixes unnecessary allocation of 1 more byte for if_name

Closes espressif/esp-protocols#322
  • Loading branch information
david-cermak authored and CommanderRedYT committed Sep 24, 2023
1 parent ac1f8f3 commit f015621
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions components/esp_http_client/esp_http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,25 @@ static char *_get_host_header(char *host, int port)
return host_name;
}

static bool init_common_tcp_transport(esp_http_client_handle_t client, const esp_http_client_config_t *config, esp_transport_handle_t transport)
{
if (config->keep_alive_enable == true) {
client->keep_alive_cfg.keep_alive_enable = true;
client->keep_alive_cfg.keep_alive_idle = (config->keep_alive_idle == 0) ? DEFAULT_KEEP_ALIVE_IDLE : config->keep_alive_idle;
client->keep_alive_cfg.keep_alive_interval = (config->keep_alive_interval == 0) ? DEFAULT_KEEP_ALIVE_INTERVAL : config->keep_alive_interval;
client->keep_alive_cfg.keep_alive_count = (config->keep_alive_count == 0) ? DEFAULT_KEEP_ALIVE_COUNT : config->keep_alive_count;
esp_transport_tcp_set_keep_alive(transport, &client->keep_alive_cfg);
}

if (config->if_name) {
client->if_name = calloc(1, sizeof(struct ifreq));
ESP_RETURN_ON_FALSE(client->if_name, false, TAG, "Memory exhausted");
memcpy(client->if_name, config->if_name, sizeof(struct ifreq));
esp_transport_tcp_set_interface_name(transport, client->if_name);
}
return true;
}

esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *config)
{

Expand Down Expand Up @@ -665,20 +684,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
goto error;
}

if (config->keep_alive_enable == true) {
client->keep_alive_cfg.keep_alive_enable = true;
client->keep_alive_cfg.keep_alive_idle = (config->keep_alive_idle == 0) ? DEFAULT_KEEP_ALIVE_IDLE : config->keep_alive_idle;
client->keep_alive_cfg.keep_alive_interval = (config->keep_alive_interval == 0) ? DEFAULT_KEEP_ALIVE_INTERVAL : config->keep_alive_interval;
client->keep_alive_cfg.keep_alive_count = (config->keep_alive_count == 0) ? DEFAULT_KEEP_ALIVE_COUNT : config->keep_alive_count;
esp_transport_tcp_set_keep_alive(tcp, &client->keep_alive_cfg);
}

if (config->if_name) {
client->if_name = calloc(1, sizeof(struct ifreq) + 1);
ESP_GOTO_ON_FALSE(client->if_name, ESP_FAIL, error, TAG, "Memory exhausted");
memcpy(client->if_name, config->if_name, sizeof(struct ifreq));
esp_transport_tcp_set_interface_name(tcp, client->if_name);
}
ESP_GOTO_ON_FALSE(init_common_tcp_transport(client, config, tcp), ESP_FAIL, error, TAG, "Failed to set TCP config");

#ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
esp_transport_handle_t ssl = NULL;
Expand All @@ -693,6 +699,8 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
goto error;
}

ESP_GOTO_ON_FALSE(init_common_tcp_transport(client, config, ssl), ESP_FAIL, error, TAG, "Failed to set SSL config");

if (config->crt_bundle_attach != NULL) {
#ifdef CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
esp_transport_ssl_crt_bundle_attach(ssl, config->crt_bundle_attach);
Expand Down

0 comments on commit f015621

Please sign in to comment.