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 maojianxin
parent 2e32cdb3b2
commit 97aeb75955
2 changed files with 28 additions and 1 deletions

View File

@ -808,6 +808,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;
@ -831,7 +847,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;
}

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