From b06035443fbdcd66d1db463fc8a597ca031ad0b4 Mon Sep 17 00:00:00 2001 From: Shubham Kulkarni Date: Tue, 22 Sep 2020 14:21:23 +0530 Subject: [PATCH] esp_http_client: Include port in host field Closes: https://github.com/espressif/esp-idf/issues/3628 --- components/esp_http_client/esp_http_client.c | 39 ++++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index 650b65ed34..3d8fe4616c 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -513,11 +513,27 @@ static esp_err_t esp_http_client_prepare(esp_http_client_handle_t client) return ESP_OK; } +static char *_get_host_header(char *host, int port) +{ + int err = 0; + char *host_name; + if (port != DEFAULT_HTTP_PORT && port != DEFAULT_HTTPS_PORT) { + err = asprintf(&host_name, "%s:%d", host, port); + } else { + err = asprintf(&host_name, "%s", host); + } + if (err == -1) { + return NULL; + } + return host_name; +} + esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *config) { esp_http_client_handle_t client; esp_transport_handle_t tcp; + char *host_name; bool _success; _success = ( @@ -595,22 +611,37 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co } if (config->host != NULL && config->path != NULL) { + host_name = _get_host_header(client->connection_info.host, client->connection_info.port); + if (host_name == NULL) { + ESP_LOGE(TAG, "Failed to allocate memory for host header"); + goto error; + } _success = ( (esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) && - (esp_http_client_set_header(client, "Host", client->connection_info.host) == ESP_OK) + (esp_http_client_set_header(client, "Host", host_name) == ESP_OK) ); - + free(host_name); if (!_success) { ESP_LOGE(TAG, "Error while setting default configurations"); goto error; } } else if (config->url != NULL) { + if (esp_http_client_set_url(client, config->url) != ESP_OK) { + ESP_LOGE(TAG, "Failed to set URL"); + goto error; + } + host_name = _get_host_header(client->connection_info.host, client->connection_info.port); + if (host_name == NULL) { + ESP_LOGE(TAG, "Failed to allocate memory for host header"); + goto error; + } + _success = ( - (esp_http_client_set_url(client, config->url) == ESP_OK) && (esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) && - (esp_http_client_set_header(client, "Host", client->connection_info.host) == ESP_OK) + (esp_http_client_set_header(client, "Host", host_name) == ESP_OK) ); + free(host_name); if (!_success) { ESP_LOGE(TAG, "Error while setting default configurations"); goto error;