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
This commit is contained in:
Mahavir Jain 2022-10-20 12:18:47 +05:30
parent dd76328927
commit 67e080471f
No known key found for this signature in database
GPG Key ID: 99324EF4A00734E0

View File

@ -770,6 +770,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) {
@ -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);
@ -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);
}
@ -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);
}
@ -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;