From 62884dae3a2d7d19e91070fd0e959397778619e2 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Thu, 20 Oct 2022 12:18:47 +0530 Subject: [PATCH] esp_http_client: free cached data in case connection gets closed for redirection In case of `esp_http_client_read` based workflow, we cache data that is received during fetch header stage. In case, there is URL redirection and we have to close the connection on URL, port change then we must discard earlier cached data. Closes AUD-4158 --- components/esp_http_client/esp_http_client.c | 25 +++++++++++++------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index 4ccdc7af5a..bf22359ff5 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -765,6 +765,17 @@ error: return NULL; } +static void esp_http_client_cached_buf_cleanup(esp_http_buffer_t *res_buffer) +{ + /* Free cached data if any, that was received during fetch header stage */ + if (res_buffer && res_buffer->orig_raw_data) { + free(res_buffer->orig_raw_data); + res_buffer->orig_raw_data = NULL; + res_buffer->raw_data = NULL; + res_buffer->raw_len = 0; + } +} + esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client) { if (client == NULL) { @@ -786,11 +797,7 @@ esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client) http_header_destroy(client->response->headers); if (client->response->buffer) { free(client->response->buffer->data); - if (client->response->buffer->orig_raw_data) { - free(client->response->buffer->orig_raw_data); - client->response->buffer->orig_raw_data = NULL; - client->response->buffer->raw_data = NULL; - } + esp_http_client_cached_buf_cleanup(client->response->buffer); } free(client->response->buffer); free(client->response); @@ -886,6 +893,8 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u free(old_host); return ESP_ERR_NO_MEM; } + /* Free cached data if any, as we are closing this connection */ + esp_http_client_cached_buf_cleanup(client->response->buffer); esp_http_client_close(client); } @@ -910,6 +919,8 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u } if (old_port != client->connection_info.port) { + /* Free cached data if any, as we are closing this connection */ + esp_http_client_cached_buf_cleanup(client->response->buffer); esp_http_client_close(client); } @@ -1029,9 +1040,7 @@ int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len) res_buffer->raw_data += remain_len; ridx = remain_len; if (res_buffer->raw_len == 0) { - free(res_buffer->orig_raw_data); - res_buffer->orig_raw_data = NULL; - res_buffer->raw_data = NULL; + esp_http_client_cached_buf_cleanup(res_buffer); } } int need_read = len - ridx;