esp_http_client: Add support to check the binary length of the recieved stream and compare it with the size mentioned in the header.

While downloading OTA firmware, if their is a Origin Respnse Timeout or the binary is only partially downloaded, OTA failure is observed. Checking binary size can also be helpful for simple http client applications.

Closes https://github.com/espressif/esp-idf/issues/3004
This commit is contained in:
Hrudaynath Dhabe 2019-08-02 19:18:44 +08:00 committed by Shubham Kulkarni
parent b2a1de4d62
commit 012ff5775b
2 changed files with 32 additions and 1 deletions

View File

@ -790,6 +790,22 @@ static int esp_http_client_get_data(esp_http_client_handle_t client)
return rlen;
}
bool esp_http_client_is_complete_data_received(esp_http_client_handle_t client)
{
if (client->response->is_chunked) {
if (!client->is_chunk_complete) {
ESP_LOGI(TAG, "Chunks were not completely read");
return false;
}
} else {
if (client->response->data_process != client->response->content_length) {
ESP_LOGI(TAG, "Data processed %d != Data specified in content length %d", client->response->data_process, client->response->content_length);
return false;
}
}
return true;
}
int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len)
{
esp_http_buffer_t *res_buffer = client->response->buffer;
@ -813,7 +829,7 @@ int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len)
} else {
is_data_remain = client->response->data_process < client->response->content_length;
}
ESP_LOGD(TAG, "is_data_remain=%d, is_chunked=%d", is_data_remain, client->response->is_chunked);
ESP_LOGD(TAG, "is_data_remain=%d, is_chunked=%d, content_length=%d", is_data_remain, client->response->is_chunked, client->response->content_length);
if (!is_data_remain) {
break;
}
@ -821,10 +837,14 @@ int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len)
if (byte_to_read > client->buffer_size) {
byte_to_read = client->buffer_size;
}
errno = 0;
rlen = esp_transport_read(client->transport, res_buffer->data, byte_to_read, client->timeout_ms);
ESP_LOGD(TAG, "need_read=%d, byte_to_read=%d, rlen=%d, ridx=%d", need_read, byte_to_read, rlen, ridx);
if (rlen <= 0) {
if (errno != 0) {
ESP_LOGW(TAG, "esp_transport_read returned : %d and errno : %d ", rlen, errno);
}
return ridx;
}
res_buffer->output_ptr = buffer + ridx;

View File

@ -470,6 +470,17 @@ esp_err_t esp_http_client_set_redirection(esp_http_client_handle_t client);
*/
void esp_http_client_add_auth(esp_http_client_handle_t client);
/**
* @brief Checks if entire data in the response has been read without any error.
*
* @param[in] client The esp_http_client handle
*
* @return
* - true
* - false
*/
bool esp_http_client_is_complete_data_received(esp_http_client_handle_t client);
#ifdef __cplusplus
}
#endif