mirror of
https://github.com/espressif/esp-idf.git
synced 2025-12-05 16:50:18 +01:00
esp_http_client: Fix config member path's incorrect setting issue
This commit is contained in:
committed by
Ivan Grokhotkov
parent
5d7d4ba4f3
commit
4a667ee12c
@@ -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)
|
||||
|
||||
6
components/esp_http_client/test/CMakeLists.txt
Normal file
6
components/esp_http_client/test/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
set(COMPONENT_SRCDIRS ".")
|
||||
set(COMPONENT_ADD_INCLUDEDIRS ".")
|
||||
|
||||
set(COMPONENT_REQUIRES unity test_utils esp_http_client)
|
||||
|
||||
register_component()
|
||||
1
components/esp_http_client/test/component.mk
Normal file
1
components/esp_http_client/test/component.mk
Normal file
@@ -0,0 +1 @@
|
||||
COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive
|
||||
47
components/esp_http_client/test/test_http_client.c
Normal file
47
components/esp_http_client/test/test_http_client.c
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user