]> granicus.if.org Git - esp-idf/commitdiff
Fix url redirection issue.
authorVikram Dattu <vikram.dattu@espressif.com>
Tue, 26 Mar 2019 08:45:46 +0000 (16:45 +0800)
committerbot <bot@espressif.com>
Wed, 10 Apr 2019 05:40:16 +0000 (05:40 +0000)
Operation:
In `esp_http_client_set_url`, we check for if old_host is same as new_host.
Delete and open new connection if host is different.

Issue:
We just pointed `client->connection_info.host` to `old_host` and reassigned it.
This made old_host and new_host always point to same location and hence, using old_host with new request.

Fix:
Made a separate copy for old_host using strdup.

Closes https://github.com/espressif/esp-idf/issues/2631

Signed-off-by: Vikram Dattu <vikram.dattu@espressif.com>
components/esp_http_client/esp_http_client.c

index 400bfa625944966383fdfd50f654a60fc95a8335..faf0c20fd5b44d9507d4d3c654ce636896bab2fd 100644 (file)
@@ -661,7 +661,9 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u
         ESP_LOGE(TAG, "Error parse url %s", url);
         return ESP_ERR_INVALID_ARG;
     }
-    old_host = client->connection_info.host;
+    if (client->connection_info.host) {
+        old_host = strdup(client->connection_info.host);
+    }
     old_port = client->connection_info.port;
 
     if (purl.field_data[UF_HOST].len) {
@@ -673,11 +675,17 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u
             && strcasecmp(old_host, (const void *)client->connection_info.host) != 0) {
         ESP_LOGD(TAG, "New host assign = %s", client->connection_info.host);
         if (esp_http_client_set_header(client, "Host", client->connection_info.host) != ESP_OK) {
+            free(old_host);
             return ESP_ERR_NO_MEM;
         }
         esp_http_client_close(client);
     }
 
+    if (old_host) {
+        free(old_host);
+        old_host = NULL;
+    }
+
     if (purl.field_data[UF_SCHEMA].len) {
         http_utils_assign_string(&client->connection_info.scheme, url + purl.field_data[UF_SCHEMA].off, purl.field_data[UF_SCHEMA].len);
         HTTP_MEM_CHECK(TAG, client->connection_info.scheme, return ESP_ERR_NO_MEM);