Merge branch 'bugfix/esp-http-client-incorrect-path' into 'master'

esp_http_client: Fix config member path's incorrect setting issue

See merge request idf/esp-idf!4078
This commit is contained in:
Ivan Grokhotkov 2019-01-21 19:08:24 +08:00
commit d9f0d83466
5 changed files with 202 additions and 23 deletions

View file

@ -461,8 +461,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
if (!_success) {
ESP_LOGE(TAG, "Error allocate memory");
esp_http_client_cleanup(client);
return NULL;
goto error;
}
_success = (
@ -473,8 +472,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
);
if (!_success) {
ESP_LOGE(TAG, "Error initialize transport");
esp_http_client_cleanup(client);
return NULL;
goto error;
}
#ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
esp_transport_handle_t ssl;
@ -486,8 +484,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
if (!_success) {
ESP_LOGE(TAG, "Error initialize SSL Transport");
esp_http_client_cleanup(client);
return NULL;
goto error;
}
if (config->cert_pem) {
@ -497,8 +494,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
if (_set_config(client, config) != ESP_OK) {
ESP_LOGE(TAG, "Error set configurations");
esp_http_client_cleanup(client);
return NULL;
goto error;
}
_success = (
(client->request->buffer->data = malloc(client->buffer_size)) &&
@ -507,20 +503,33 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
if (!_success) {
ESP_LOGE(TAG, "Allocation failed");
esp_http_client_cleanup(client);
return NULL;
goto error;
}
_success = (
(esp_http_client_set_url(client, config->url) == ESP_OK) &&
(esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) &&
(esp_http_client_set_header(client, "Host", client->connection_info.host) == ESP_OK)
);
if (config->host != NULL && config->path != NULL) {
_success = (
(esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) &&
(esp_http_client_set_header(client, "Host", client->connection_info.host) == ESP_OK)
);
if (!_success) {
ESP_LOGE(TAG, "Error set default configurations");
esp_http_client_cleanup(client);
return NULL;
if (!_success) {
ESP_LOGE(TAG, "Error while setting default configurations");
goto error;
}
} else if (config->url != NULL) {
_success = (
(esp_http_client_set_url(client, config->url) == ESP_OK) &&
(esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) &&
(esp_http_client_set_header(client, "Host", client->connection_info.host) == ESP_OK)
);
if (!_success) {
ESP_LOGE(TAG, "Error while setting default configurations");
goto error;
}
} else {
ESP_LOGE(TAG, "config should have either URL or host & path");
goto error;
}
client->parser_settings->on_message_begin = http_on_message_begin;
@ -537,6 +546,9 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
client->state = HTTP_STATE_INIT;
return client;
error:
esp_http_client_cleanup(client);
return NULL;
}
esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client)

View file

@ -0,0 +1,6 @@
set(COMPONENT_SRCDIRS ".")
set(COMPONENT_ADD_INCLUDEDIRS ".")
set(COMPONENT_REQUIRES unity test_utils esp_http_client)
register_component()

View file

@ -0,0 +1 @@
COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive

View file

@ -0,0 +1,47 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdlib.h>
#include <stdbool.h>
#include <esp_system.h>
#include <esp_http_client.h>
#include "unity.h"
#include "test_utils.h"
TEST_CASE("Input Param Tests", "[ESP HTTP CLIENT]")
{
esp_http_client_config_t config_incorrect = {0};
test_case_uses_tcpip();
esp_http_client_handle_t client = esp_http_client_init(&config_incorrect);
TEST_ASSERT(client == NULL);
esp_http_client_config_t config_with_url = {
.url = "http://httpbin.org/get",
};
client = esp_http_client_init(&config_with_url);
TEST_ASSERT(client != NULL);
TEST_ASSERT(esp_http_client_cleanup(client) == ESP_OK);
esp_http_client_config_t config_with_hostname_path = {
.host = "httpbin.org",
.path = "/get",
};
client = esp_http_client_init(&config_with_hostname_path);
TEST_ASSERT(client != NULL);
TEST_ASSERT(esp_http_client_cleanup(client) == ESP_OK);
}

View file

@ -67,7 +67,7 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
return ESP_OK;
}
static void http_rest()
static void http_rest_with_url()
{
esp_http_client_config_t config = {
.url = "http://httpbin.org/get",
@ -151,6 +151,93 @@ static void http_rest()
esp_http_client_cleanup(client);
}
static void http_rest_with_hostname_path()
{
esp_http_client_config_t config = {
.host = "httpbin.org",
.path = "/get",
.transport_type = HTTP_TRANSPORT_OVER_TCP,
.event_handler = _http_event_handler,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
// GET
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
}
// POST
const char *post_data = "field1=value1&field2=value2";
esp_http_client_set_url(client, "/post");
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_post_field(client, post_data, strlen(post_data));
err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
}
//PUT
esp_http_client_set_url(client, "/put");
esp_http_client_set_method(client, HTTP_METHOD_PUT);
err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "HTTP PUT request failed: %s", esp_err_to_name(err));
}
//PATCH
esp_http_client_set_url(client, "/patch");
esp_http_client_set_method(client, HTTP_METHOD_PATCH);
esp_http_client_set_post_field(client, NULL, 0);
err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "HTTP PATCH request failed: %s", esp_err_to_name(err));
}
//DELETE
esp_http_client_set_url(client, "/delete");
esp_http_client_set_method(client, HTTP_METHOD_DELETE);
err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "HTTP DELETE request failed: %s", esp_err_to_name(err));
}
//HEAD
esp_http_client_set_url(client, "/get");
esp_http_client_set_method(client, HTTP_METHOD_HEAD);
err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP HEAD Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "HTTP HEAD request failed: %s", esp_err_to_name(err));
}
esp_http_client_cleanup(client);
}
static void http_auth_basic()
{
esp_http_client_config_t config = {
@ -209,7 +296,7 @@ static void http_auth_digest()
esp_http_client_cleanup(client);
}
static void https()
static void https_with_url()
{
esp_http_client_config_t config = {
.url = "https://www.howsmyssl.com",
@ -229,6 +316,28 @@ static void https()
esp_http_client_cleanup(client);
}
static void https_with_hostname_path()
{
esp_http_client_config_t config = {
.host = "www.howsmyssl.com",
.path = "/",
.transport_type = HTTP_TRANSPORT_OVER_SSL,
.event_handler = _http_event_handler,
.cert_pem = howsmyssl_com_root_cert_pem_start,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
}
esp_http_client_cleanup(client);
}
static void http_relative_redirect()
{
esp_http_client_config_t config = {
@ -374,21 +483,25 @@ static void https_async()
esp_http_client_cleanup(client);
}
static void http_test_task(void *pvParameters)
{
app_wifi_wait_connected();
ESP_LOGI(TAG, "Connected to AP, begin http example");
http_rest();
http_rest_with_url();
http_rest_with_hostname_path();
http_auth_basic();
http_auth_basic_redirect();
http_auth_digest();
http_relative_redirect();
http_absolute_redirect();
https();
https_with_url();
https_with_hostname_path();
http_redirect_to_https();
http_download_chunk();
http_perform_as_stream_reader();
https_async();
ESP_LOGI(TAG, "Finish http example");
vTaskDelete(NULL);
}