From 02367b44ef66420532d20815f97a75406ea37f38 Mon Sep 17 00:00:00 2001 From: yuanjm Date: Fri, 2 Jul 2021 15:25:06 +0800 Subject: [PATCH] esp_http_client: Fix when receive FIN, close the connection in internal. Closes https://github.com/espressif/esp-idf/issues/7036 --- components/esp_common/src/esp_err_to_name.c | 3 +++ components/esp_http_client/esp_http_client.c | 5 +++++ components/esp_http_client/include/esp_http_client.h | 1 + 3 files changed, 9 insertions(+) diff --git a/components/esp_common/src/esp_err_to_name.c b/components/esp_common/src/esp_err_to_name.c index 460eafcf00..304b02aef3 100644 --- a/components/esp_common/src/esp_err_to_name.c +++ b/components/esp_common/src/esp_err_to_name.c @@ -596,6 +596,9 @@ static const esp_err_msg_t esp_err_msg_table[] = { # endif # ifdef ESP_ERR_HTTP_EAGAIN ERR_TBL_IT(ESP_ERR_HTTP_EAGAIN), /* 28679 0x7007 Mapping of errno EAGAIN to esp_err_t */ +# endif +# ifdef ESP_ERR_HTTP_CONNECTION_CLOSED + ERR_TBL_IT(ESP_ERR_HTTP_CONNECTION_CLOSED), /* 28680 0x7008 Read FIN from peer and the connection closed */ # endif // components/esp-tls/esp_tls_errors.h # ifdef ESP_ERR_ESP_TLS_BASE diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index 07e67b106c..30cc3c9824 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -1052,6 +1052,11 @@ esp_err_t esp_http_client_perform(esp_http_client_handle_t client) if (client->is_async && errno == EAGAIN) { return ESP_ERR_HTTP_EAGAIN; } + if (esp_tls_get_and_clear_last_error(esp_transport_get_error_handle(client->transport), NULL, NULL) == ESP_ERR_ESP_TLS_TCP_CLOSED_FIN) { + ESP_LOGW(TAG, "Close connection due to FIN received"); + esp_http_client_close(client); + return ESP_ERR_HTTP_CONNECTION_CLOSED; + } return ESP_ERR_HTTP_FETCH_HEADER; } /* falls through */ diff --git a/components/esp_http_client/include/esp_http_client.h b/components/esp_http_client/include/esp_http_client.h index f6ef1da340..ab7e3c2e32 100644 --- a/components/esp_http_client/include/esp_http_client.h +++ b/components/esp_http_client/include/esp_http_client.h @@ -167,6 +167,7 @@ typedef enum { #define ESP_ERR_HTTP_INVALID_TRANSPORT (ESP_ERR_HTTP_BASE + 5) /*!< There are no transport support for the input scheme */ #define ESP_ERR_HTTP_CONNECTING (ESP_ERR_HTTP_BASE + 6) /*!< HTTP connection hasn't been established yet */ #define ESP_ERR_HTTP_EAGAIN (ESP_ERR_HTTP_BASE + 7) /*!< Mapping of errno EAGAIN to esp_err_t */ +#define ESP_ERR_HTTP_CONNECTION_CLOSED (ESP_ERR_HTTP_BASE + 8) /*!< Read FIN from peer and the connection closed */ /** * @brief Start a HTTP session