Skip to content

Commit

Permalink
Merge branch 'bugfix/http_cached_data_in_redirection' into 'master'
Browse files Browse the repository at this point in the history
esp_http_client: free cached data in case connection gets closed for redirection

Closes AUD-4158

See merge request espressif/esp-idf!20694
  • Loading branch information
mahavirj committed Oct 21, 2022
2 parents 2cbc83a + 67e0804 commit d539b2d
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions components/esp_http_client/esp_http_client.c
Expand Up @@ -770,6 +770,17 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
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) {
Expand All @@ -791,11 +802,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);
Expand Down Expand Up @@ -892,6 +899,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);
}

Expand All @@ -916,6 +925,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);
}

Expand Down Expand Up @@ -1039,9 +1050,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;
Expand Down

0 comments on commit d539b2d

Please sign in to comment.