From 05cde47281a658041e7791e53225659736015307 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 15 Aug 2023 07:39:28 +0200 Subject: [PATCH] fix(http_client): Set common tcp config to both TCP and SSL transport 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 5778a7c7 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 https://github.com/espressif/esp-protocols/issues/322 --- components/esp_http_client/esp_http_client.c | 40 +++++++++++++------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index bf22359ff5..a1836d35b0 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -569,6 +569,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)); + HTTP_MEM_CHECK(TAG, client->if_name, return false); + 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) { @@ -606,19 +625,9 @@ 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); - HTTP_MEM_CHECK(TAG, client->if_name, goto error); - memcpy(client->if_name, config->if_name, sizeof(struct ifreq)); - esp_transport_tcp_set_interface_name(tcp, client->if_name); + if (!init_common_tcp_transport(client, config, tcp)) { + ESP_LOGE(TAG, "Failed to set TCP config"); + goto error; } #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS @@ -634,6 +643,11 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co goto error; } + if (!init_common_tcp_transport(client, config, ssl)) { + ESP_LOGE(TAG, "Failed to set SSL config"); + goto error; + } + if (config->crt_bundle_attach != NULL) { #ifdef CONFIG_MBEDTLS_CERTIFICATE_BUNDLE esp_transport_ssl_crt_bundle_attach(ssl, config->crt_bundle_attach);