Skip to content

Commit 7085a00

Browse files
committed
fix(http): Fixed the issue where AT HTTP requests returned OK even when the server's HTTP body was incomplete
- Closes ESPAT-2411 and ESPAT-1718
1 parent 5e69319 commit 7085a00

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

patches/http.patch

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
diff --git a/components/esp_common/src/esp_err_to_name.c b/components/esp_common/src/esp_err_to_name.c
2+
index 3fe660c9294..116a51c7ecb 100644
3+
--- a/components/esp_common/src/esp_err_to_name.c
4+
+++ b/components/esp_common/src/esp_err_to_name.c
5+
@@ -657,7 +657,14 @@ static const esp_err_msg_t esp_err_msg_table[] = {
6+
# ifdef ESP_ERR_HTTP_CONNECTION_CLOSED
7+
ERR_TBL_IT(ESP_ERR_HTTP_CONNECTION_CLOSED), /* 28680 0x7008 Read FIN from peer and the connection closed */
8+
# endif
9+
- // components/esp-tls/esp_tls_errors.h
10+
+# ifdef ESP_ERR_HTTP_READ_TIMEOUT
11+
+ ERR_TBL_IT(ESP_ERR_HTTP_READ_TIMEOUT), /* 28683 0x700b HTTP data read timeout */
12+
+# endif
13+
+# ifdef ESP_ERR_HTTP_INCOMPLETE_DATA
14+
+ ERR_TBL_IT(ESP_ERR_HTTP_INCOMPLETE_DATA), /* 28684 0x700c Incomplete data received, less than
15+
+ Content-Length or last chunk */
16+
+# endif
17+
+// components/esp-tls/esp_tls_errors.h
18+
# ifdef ESP_ERR_ESP_TLS_BASE
19+
ERR_TBL_IT(ESP_ERR_ESP_TLS_BASE), /* 32768 0x8000 Starting number of ESP-TLS error codes */
20+
# endif
21+
diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c
22+
index 96fb635fccf..c85dad298ae 100644
23+
--- a/components/esp_http_client/esp_http_client.c
24+
+++ b/components/esp_http_client/esp_http_client.c
25+
@@ -1314,7 +1314,7 @@ int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len)
26+
27+
esp_err_t esp_http_client_perform(esp_http_client_handle_t client)
28+
{
29+
- esp_err_t err;
30+
+ esp_err_t err = ESP_FAIL;
31+
do {
32+
if (client->process_again) {
33+
esp_http_client_prepare(client);
34+
@@ -1389,23 +1389,53 @@ esp_err_t esp_http_client_perform(esp_http_client_handle_t client)
35+
return err;
36+
}
37+
while (client->response->is_chunked && !client->is_chunk_complete) {
38+
- if (esp_http_client_get_data(client) <= 0) {
39+
+ int ret = esp_http_client_get_data(client);
40+
+ if (ret <= 0) {
41+
if (client->is_async && errno == EAGAIN) {
42+
return ESP_ERR_HTTP_EAGAIN;
43+
}
44+
+ if (client->connection_info.method != HTTP_METHOD_HEAD && !client->is_chunk_complete) {
45+
+ ESP_LOGE(TAG, "Incomplete chunked data received %d", ret);
46+
+
47+
+ if (ret == ESP_ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT) {
48+
+ err = ESP_ERR_HTTP_READ_TIMEOUT;
49+
+ } else if (ret == ESP_ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN) {
50+
+ err = ESP_ERR_HTTP_CONNECTION_CLOSED;
51+
+ } else {
52+
+ err = ESP_ERR_HTTP_INCOMPLETE_DATA;
53+
+ }
54+
+ }
55+
ESP_LOGD(TAG, "Read finish or server requests close");
56+
break;
57+
}
58+
}
59+
while (client->response->data_process < client->response->content_length) {
60+
- if (esp_http_client_get_data(client) <= 0) {
61+
+ int ret = esp_http_client_get_data(client);
62+
+ if (ret <= 0) {
63+
if (client->is_async && errno == EAGAIN) {
64+
return ESP_ERR_HTTP_EAGAIN;
65+
}
66+
+ if (client->connection_info.method != HTTP_METHOD_HEAD && client->response->data_process < client->response->content_length) {
67+
+ ESP_LOGE(TAG, "Incomlete data received, ret=%d, %"PRId64"/%"PRId64" bytes", ret, client->response->data_process, client->response->content_length);
68+
+
69+
+ if (ret == ESP_ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT) {
70+
+ err = ESP_ERR_HTTP_READ_TIMEOUT;
71+
+ } else if (ret == ESP_ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN) {
72+
+ err = ESP_ERR_HTTP_CONNECTION_CLOSED;
73+
+ } else {
74+
+ err = ESP_ERR_HTTP_INCOMPLETE_DATA;
75+
+ }
76+
+ }
77+
ESP_LOGD(TAG, "Read finish or server requests close");
78+
break;
79+
}
80+
}
81+
+
82+
+ if (err != ESP_OK) {
83+
+ http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
84+
+ http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t));
85+
+ }
86+
+
87+
http_dispatch_event(client, HTTP_EVENT_ON_FINISH, NULL, 0);
88+
http_dispatch_event_to_event_loop(HTTP_EVENT_ON_FINISH, &client, sizeof(esp_http_client_handle_t));
89+
90+
@@ -1423,8 +1453,9 @@ esp_err_t esp_http_client_perform(esp_http_client_handle_t client)
91+
default:
92+
break;
93+
}
94+
- } while (client->process_again);
95+
- return ESP_OK;
96+
+ } while (client->process_again && err == ESP_OK);
97+
+
98+
+ return err;
99+
}
100+
101+
int64_t esp_http_client_fetch_headers(esp_http_client_handle_t client)
102+
diff --git a/components/esp_http_client/include/esp_http_client.h b/components/esp_http_client/include/esp_http_client.h
103+
index 93c4db97834..8ae8947fa3e 100644
104+
--- a/components/esp_http_client/include/esp_http_client.h
105+
+++ b/components/esp_http_client/include/esp_http_client.h
106+
@@ -236,6 +236,8 @@ typedef enum {
107+
#define ESP_ERR_HTTP_CONNECTING (ESP_ERR_HTTP_BASE + 6) /*!< HTTP connection hasn't been established yet */
108+
#define ESP_ERR_HTTP_EAGAIN (ESP_ERR_HTTP_BASE + 7) /*!< Mapping of errno EAGAIN to esp_err_t */
109+
#define ESP_ERR_HTTP_CONNECTION_CLOSED (ESP_ERR_HTTP_BASE + 8) /*!< Read FIN from peer and the connection closed */
110+
+#define ESP_ERR_HTTP_READ_TIMEOUT (ESP_ERR_HTTP_BASE + 11) /*!< HTTP data read timeout */
111+
+#define ESP_ERR_HTTP_INCOMPLETE_DATA (ESP_ERR_HTTP_BASE + 12) /*!< Incomplete data received, less than Content-Length or last chunk */
112+
113+
/**
114+
* @brief Start a HTTP session
115+
@@ -273,6 +275,9 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
116+
* @return
117+
* - ESP_OK on successful
118+
* - ESP_FAIL on error
119+
+ * - ESP_ERR_HTTP_READ_TIMEOUT if read operation times out
120+
+ * - ESP_ERR_HTTP_INCOMPLETE_DATA if read operation returns less data than expected
121+
+ * - ESP_ERR_HTTP_CONNECTION_CLOSED if server closes the connection
122+
*/
123+
esp_err_t esp_http_client_perform(esp_http_client_handle_t client);
124+

patches/patch_list.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
when = after_sdkconfig
4848
dependency = CONFIG_AT_MQTT_COMMAND_SUPPORT
4949

50+
[http.patch]
51+
description = "[IDF-13454] Fixed the issue where AT HTTP requests returned OK even when the server's HTTP body was incomplete"
52+
path = esp-idf
53+
when = after_sdkconfig
54+
dependency = CONFIG_AT_HTTP_COMMAND_SUPPORT
55+
5056
[fatfs.patch]
5157
description = "[IDF-12532] Fixed some fatfs issues"
5258
path = esp-idf

0 commit comments

Comments
 (0)