Merge branch 'bugfix/fix_http_client_error' into 'master'

esp_http_client: Fix reset errno to 0 before call esp_transport_read

Closes IDFGH-7444

See merge request espressif/esp-idf!26539
This commit is contained in:
Mahavir Jain 2024-01-08 11:36:52 +08:00
commit dd49da5f4f
3 changed files with 29 additions and 1 deletions

View File

@ -1141,7 +1141,7 @@ static int esp_http_client_get_data(esp_http_client_handle_t client)
esp_http_buffer_t *res_buffer = client->response->buffer;
ESP_LOGD(TAG, "data_process=%"PRId64", content_length=%"PRId64, client->response->data_process, client->response->content_length);
errno = 0;
int rlen = esp_transport_read(client->transport, res_buffer->data, client->buffer_size_rx, client->timeout_ms);
if (rlen >= 0) {
// When tls error is ESP_TLS_ERR_SSL_WANT_READ (-0x6900), esp_trasnport_read returns ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT (0x0).
@ -1380,6 +1380,7 @@ int64_t esp_http_client_fetch_headers(esp_http_client_handle_t client)
client->response->status_code = -1;
while (client->state < HTTP_STATE_RES_COMPLETE_HEADER) {
errno = 0;
buffer->len = esp_transport_read(client->transport, buffer->data, client->buffer_size_rx, client->timeout_ms);
if (buffer->len <= 0) {
if (buffer->len == ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT) {

View File

@ -651,6 +651,31 @@ static void https_async(void)
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
}
esp_http_client_cleanup(client);
// Test HTTP_METHOD_HEAD with is_async enabled
config.url = "https://"CONFIG_EXAMPLE_HTTP_ENDPOINT"/get";
config.event_handler = _http_event_handler;
config.crt_bundle_attach = esp_crt_bundle_attach;
config.is_async = true;
config.timeout_ms = 5000;
client = esp_http_client_init(&config);
esp_http_client_set_method(client, HTTP_METHOD_HEAD);
while (1) {
err = esp_http_client_perform(client);
if (err != ESP_ERR_HTTP_EAGAIN) {
break;
}
}
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRId64,
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 https_with_invalid_url(void)

View File

@ -46,6 +46,7 @@ def test_examples_protocol_esp_http_client(dut: Dut) -> None:
# content-len for chunked encoding is typically -1, could be a positive length in some cases
dut.expect(r'HTTP Stream reader Status = 200, content_length = (\d)')
dut.expect(r'HTTPS Status = 200, content_length = (\d)')
dut.expect(r'HTTPS Status = 200, content_length = (\d)')
dut.expect(r'Last esp error code: 0x8001')
dut.expect(r'HTTP GET Status = 200, content_length = (\d)')
dut.expect(r'HTTP POST Status = 200, content_length = (\d)')
@ -92,6 +93,7 @@ def test_examples_protocol_esp_http_client_dynamic_buffer(dut: Dut) -> None:
# content-len for chunked encoding is typically -1, could be a positive length in some cases
dut.expect(r'HTTP Stream reader Status = 200, content_length = (\d)')
dut.expect(r'HTTPS Status = 200, content_length = (\d)')
dut.expect(r'HTTPS Status = 200, content_length = (\d)')
dut.expect(r'Last esp error code: 0x8001')
dut.expect(r'HTTP GET Status = 200, content_length = (\d)')
dut.expect(r'HTTP POST Status = 200, content_length = (\d)')