Merge branch 'bugfix/http_client_empty_headers' into 'master'

esp_http_client: Fix issue when response headers have empty value

Closes IDFGH-3613, IDFGH-3921, and IDFGH-3959

See merge request espressif/esp-idf!10035
This commit is contained in:
Mahavir Jain
2020-09-11 23:47:32 +08:00
4 changed files with 43 additions and 8 deletions

View File

@@ -434,12 +434,12 @@ static esp_err_t _set_config(esp_http_client_handle_t client, const esp_http_cli
} }
if (config->transport_type == HTTP_TRANSPORT_OVER_SSL) { if (config->transport_type == HTTP_TRANSPORT_OVER_SSL) {
http_utils_assign_string(&client->connection_info.scheme, "https", 0); http_utils_assign_string(&client->connection_info.scheme, "https", -1);
if (client->connection_info.port == 0) { if (client->connection_info.port == 0) {
client->connection_info.port = DEFAULT_HTTPS_PORT; client->connection_info.port = DEFAULT_HTTPS_PORT;
} }
} else { } else {
http_utils_assign_string(&client->connection_info.scheme, "http", 0); http_utils_assign_string(&client->connection_info.scheme, "http", -1);
if (client->connection_info.port == 0) { if (client->connection_info.port == 0) {
client->connection_info.port = DEFAULT_HTTP_PORT; client->connection_info.port = DEFAULT_HTTP_PORT;
} }
@@ -783,10 +783,10 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u
if (password) { if (password) {
*password = 0; *password = 0;
password ++; password ++;
http_utils_assign_string(&client->connection_info.password, password, 0); http_utils_assign_string(&client->connection_info.password, password, -1);
HTTP_MEM_CHECK(TAG, client->connection_info.password, return ESP_ERR_NO_MEM); HTTP_MEM_CHECK(TAG, client->connection_info.password, return ESP_ERR_NO_MEM);
} }
http_utils_assign_string(&client->connection_info.username, username, 0); http_utils_assign_string(&client->connection_info.username, username, -1);
HTTP_MEM_CHECK(TAG, client->connection_info.username, return ESP_ERR_NO_MEM); HTTP_MEM_CHECK(TAG, client->connection_info.username, return ESP_ERR_NO_MEM);
free(user_info); free(user_info);
} else { } else {
@@ -798,7 +798,7 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u
if (purl.field_data[UF_PATH].len) { if (purl.field_data[UF_PATH].len) {
http_utils_assign_string(&client->connection_info.path, url + purl.field_data[UF_PATH].off, purl.field_data[UF_PATH].len); http_utils_assign_string(&client->connection_info.path, url + purl.field_data[UF_PATH].off, purl.field_data[UF_PATH].len);
} else { } else {
http_utils_assign_string(&client->connection_info.path, "/", 0); http_utils_assign_string(&client->connection_info.path, "/", -1);
} }
HTTP_MEM_CHECK(TAG, client->connection_info.path, return ESP_ERR_NO_MEM); HTTP_MEM_CHECK(TAG, client->connection_info.path, return ESP_ERR_NO_MEM);
@@ -1377,6 +1377,26 @@ int esp_http_client_read_response(esp_http_client_handle_t client, char *buffer,
return read_len; return read_len;
} }
esp_err_t esp_http_client_flush_response(esp_http_client_handle_t client, int *len)
{
if (client == NULL) {
ESP_LOGE(TAG, "client must not be NULL");
return ESP_ERR_INVALID_ARG;
}
int read_len = 0;
while (!esp_http_client_is_complete_data_received(client)) {
int data_read = esp_http_client_get_data(client);
if (data_read < 0) {
return ESP_FAIL;
}
read_len += data_read;
}
if (len) {
*len = read_len;
}
return ESP_OK;
}
esp_err_t esp_http_client_get_url(esp_http_client_handle_t client, char *url, const int len) esp_err_t esp_http_client_get_url(esp_http_client_handle_t client, char *url, const int len)
{ {
if (client == NULL || url == NULL) { if (client == NULL || url == NULL) {

View File

@@ -524,6 +524,21 @@ bool esp_http_client_is_complete_data_received(esp_http_client_handle_t client);
int esp_http_client_read_response(esp_http_client_handle_t client, char *buffer, int len); int esp_http_client_read_response(esp_http_client_handle_t client, char *buffer, int len);
/**
* @brief Process all remaining response data
* This uses an internal buffer to repeatedly receive, parse, and discard response data until complete data is processed.
* As no additional user-supplied buffer is required, this may be preferrable to `esp_http_client_read_response` in situations where the content of the response may be ignored.
*
* @param[in] client The esp_http_client handle
* @param len Length of data discarded
*
* @return
* - ESP_OK If successful, len will have discarded length
* - ESP_FAIL If failed to read response
* - ESP_ERR_INVALID_ARG If the client is NULL
*/
int esp_http_client_flush_response(esp_http_client_handle_t client, int *len);
/** /**
* @brief Get URL from client * @brief Get URL from client
* *

View File

@@ -86,10 +86,10 @@ static esp_err_t http_header_new_item(http_header_handle_t header, const char *k
item = calloc(1, sizeof(http_header_item_t)); item = calloc(1, sizeof(http_header_item_t));
HTTP_MEM_CHECK(TAG, item, return ESP_ERR_NO_MEM); HTTP_MEM_CHECK(TAG, item, return ESP_ERR_NO_MEM);
http_utils_assign_string(&item->key, key, 0); http_utils_assign_string(&item->key, key, -1);
HTTP_MEM_CHECK(TAG, item->key, goto _header_new_item_exit); HTTP_MEM_CHECK(TAG, item->key, goto _header_new_item_exit);
http_utils_trim_whitespace(&item->key); http_utils_trim_whitespace(&item->key);
http_utils_assign_string(&item->value, value, 0); http_utils_assign_string(&item->value, value, -1);
HTTP_MEM_CHECK(TAG, item->value, goto _header_new_item_exit); HTTP_MEM_CHECK(TAG, item->value, goto _header_new_item_exit);
http_utils_trim_whitespace(&item->value); http_utils_trim_whitespace(&item->value);
STAILQ_INSERT_TAIL(header, item, next); STAILQ_INSERT_TAIL(header, item, next);

View File

@@ -45,7 +45,7 @@ char *http_utils_assign_string(char **str, const char *new_str, int len)
return NULL; return NULL;
} }
char *old_str = *str; char *old_str = *str;
if (l <= 0) { if (l < 0) {
l = strlen(new_str); l = strlen(new_str);
} }
if (old_str) { if (old_str) {