Merge branch 'feature/get_chunk_length' into 'master'

esp_http_client: Add API to get chunk length

Closes IDFGH-3319

See merge request espressif/esp-idf!9074
pull/5452/head
Mahavir Jain 2020-06-05 15:06:26 +08:00
commit 790aecfc6a
2 zmienionych plików z 37 dodań i 0 usunięć

Wyświetl plik

@ -52,6 +52,7 @@ typedef struct {
esp_http_buffer_t *buffer; /*!< data buffer as linked list */
int status_code; /*!< status code (integer) */
int content_length; /*!< data length */
int chunk_length; /*!< chunk length */
int data_offset; /*!< offset to http data (Skip header) */
int data_process; /*!< data processed */
int method; /*!< http method */
@ -269,6 +270,14 @@ static int http_on_chunk_complete(http_parser *parser)
return 0;
}
static int http_on_chunk_header(http_parser *parser)
{
esp_http_client_handle_t client = parser->data;
client->response->chunk_length = parser->content_length;
ESP_LOGD(TAG, "http_on_chunk_header, chunk_length");
return 0;
}
esp_err_t esp_http_client_set_header(esp_http_client_handle_t client, const char *key, const char *value)
{
return http_header_set(client->request->headers, key, value);
@ -604,6 +613,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
client->parser_settings->on_body = http_on_body;
client->parser_settings->on_message_complete = http_on_message_complete;
client->parser_settings->on_chunk_complete = http_on_chunk_complete;
client->parser_settings->on_chunk_header = http_on_chunk_header;
client->parser->data = client;
client->event.client = client;
@ -1357,3 +1367,17 @@ esp_err_t esp_http_client_get_url(esp_http_client_handle_t client, char *url, co
}
return ESP_FAIL;
}
esp_err_t esp_http_client_get_chunk_length(esp_http_client_handle_t client, int *len)
{
if (client == NULL || len == NULL) {
return ESP_ERR_INVALID_ARG;
}
if (esp_http_client_is_chunked_response(client)) {
*len = client->response->chunk_length;
} else {
ESP_LOGE(TAG, "Response is not chunked");
return ESP_FAIL;
}
return ESP_OK;
}

Wyświetl plik

@ -525,6 +525,19 @@ int esp_http_client_read_response(esp_http_client_handle_t client, char *buffer,
esp_err_t esp_http_client_get_url(esp_http_client_handle_t client, char *url, const int len);
/**
* @brief Get Chunk-Length from client
*
* @param[in] client The esp_http_client handle
* @param[out] len Variable to store length
*
* @return
* - ESP_OK If successful, len will have length of current chunk
* - ESP_FAIL If the server is not a chunked server
* - ESP_ERR_INVALID_ARG If the client or len are NULL
*/
esp_err_t esp_http_client_get_chunk_length(esp_http_client_handle_t client, int *len);
#ifdef __cplusplus
}
#endif