Skip to content

Commit 30758d9

Browse files
feat(esp_http_client): Event to signal last header download
When parsing ND-JSON streams, this is needed to indicate the point when the ND-JSON stream is considered open, which occurs just after the last HTTP_EVENT_ON_HEADER. ND-JSON stream clients cannot rely on the first HTTP_EVENT_ON_DATA, since that is only triggered by an event, which are optional and may never be sent, or only sent well after the last HTTP_EVENT_ON_HEADER. Closes #15952
1 parent a74725a commit 30758d9

File tree

9 files changed

+36
-16
lines changed

9 files changed

+36
-16
lines changed

components/esp_eth/test_apps/main/esp_eth_test_apps.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,9 @@ esp_err_t http_event_handle(esp_http_client_event_t *evt)
623623
case HTTP_EVENT_ON_HEADER:
624624
ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER");
625625
break;
626+
case HTTP_EVENT_ON_HEADERS_COMPLETE:
627+
ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADERS_COMPLETE");
628+
break;
626629
case HTTP_EVENT_ON_DATA:
627630
esp_rom_md5_update(&md5_context, evt->data, evt->data_len);
628631
break;

components/esp_http_client/esp_http_client.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ static int http_on_headers_complete(http_parser *parser)
289289
client->response->data_process = 0;
290290
ESP_LOGD(TAG, "http_on_headers_complete, status=%d, offset=%d, nread=%" PRId32, parser->status_code, client->response->data_offset, parser->nread);
291291
client->state = HTTP_STATE_RES_COMPLETE_HEADER;
292+
http_dispatch_event(client, HTTP_EVENT_ON_HEADERS_COMPLETE, NULL, 0);
293+
http_dispatch_event_to_event_loop(HTTP_EVENT_ON_HEADERS_COMPLETE, &client, sizeof(esp_http_client_handle_t));
292294
if (client->connection_info.method == HTTP_METHOD_HEAD) {
293295
/* In a HTTP_RESPONSE parser returning '1' from on_headers_complete will tell the
294296
parser that it should not expect a body. This is used when receiving a response

components/esp_http_client/include/esp_http_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef enum {
3939
HTTP_EVENT_HEADER_SENT = HTTP_EVENT_HEADERS_SENT, /*!< This header has been kept for backward compatibility
4040
and will be deprecated in future versions esp-idf */
4141
HTTP_EVENT_ON_HEADER, /*!< Occurs when receiving each header sent from the server */
42+
HTTP_EVENT_ON_HEADERS_COMPLETE, /*!< Occurs when all headers are received on the client side */
4243
HTTP_EVENT_ON_DATA, /*!< Occurs when receiving data from the server, possibly multiple portions of the packet */
4344
HTTP_EVENT_ON_FINISH, /*!< Occurs when finish a HTTP session */
4445
HTTP_EVENT_DISCONNECTED, /*!< The connection has been disconnected */

docs/en/api-reference/protocols/esp_http_client.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,15 @@ Diagnostic information could be helpful to gain insights into a problem. In the
126126

127127
Expected data types for different HTTP Client events in the event loop are as follows:
128128

129-
- HTTP_EVENT_ERROR : ``esp_http_client_handle_t``
130-
- HTTP_EVENT_ON_CONNECTED : ``esp_http_client_handle_t``
131-
- HTTP_EVENT_HEADERS_SENT : ``esp_http_client_handle_t``
132-
- HTTP_EVENT_ON_HEADER : ``esp_http_client_handle_t``
133-
- HTTP_EVENT_ON_DATA : ``esp_http_client_on_data_t``
134-
- HTTP_EVENT_ON_FINISH : ``esp_http_client_handle_t``
135-
- HTTP_EVENT_DISCONNECTED : ``esp_http_client_handle_t``
136-
- HTTP_EVENT_REDIRECT : ``esp_http_client_redirect_event_data_t``
129+
- HTTP_EVENT_ERROR : ``esp_http_client_handle_t``
130+
- HTTP_EVENT_ON_CONNECTED : ``esp_http_client_handle_t``
131+
- HTTP_EVENT_HEADERS_SENT : ``esp_http_client_handle_t``
132+
- HTTP_EVENT_ON_HEADER : ``esp_http_client_handle_t``
133+
- HTTP_EVENT_ON_HEADERS_COMPLETE: ``esp_http_client_handle_t``
134+
- HTTP_EVENT_ON_DATA : ``esp_http_client_on_data_t``
135+
- HTTP_EVENT_ON_FINISH : ``esp_http_client_handle_t``
136+
- HTTP_EVENT_DISCONNECTED : ``esp_http_client_handle_t``
137+
- HTTP_EVENT_REDIRECT : ``esp_http_client_redirect_event_data_t``
137138

138139
The :cpp:type:`esp_http_client_handle_t` received along with the event data will be valid until :cpp:enumerator:`HTTP_EVENT_DISCONNECTED <esp_http_client_event_id_t::HTTP_EVENT_DISCONNECTED>` is not received. This handle has been sent primarily to differentiate between different client connections and must not be used for any other purpose, as it may change based on client connection state.
139140

docs/zh_CN/api-reference/protocols/esp_http_client.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,15 @@ ESP HTTP 客户端诊断信息
126126

127127
事件循环中不同 HTTP 客户端事件的预期数据类型如下所示:
128128

129-
- HTTP_EVENT_ERROR : ``esp_http_client_handle_t``
130-
- HTTP_EVENT_ON_CONNECTED : ``esp_http_client_handle_t``
131-
- HTTP_EVENT_HEADERS_SENT : ``esp_http_client_handle_t``
132-
- HTTP_EVENT_ON_HEADER : ``esp_http_client_handle_t``
133-
- HTTP_EVENT_ON_DATA : ``esp_http_client_on_data_t``
134-
- HTTP_EVENT_ON_FINISH : ``esp_http_client_handle_t``
135-
- HTTP_EVENT_DISCONNECTED : ``esp_http_client_handle_t``
136-
- HTTP_EVENT_REDIRECT : ``esp_http_client_redirect_event_data_t``
129+
- HTTP_EVENT_ERROR : ``esp_http_client_handle_t``
130+
- HTTP_EVENT_ON_CONNECTED : ``esp_http_client_handle_t``
131+
- HTTP_EVENT_HEADERS_SENT : ``esp_http_client_handle_t``
132+
- HTTP_EVENT_ON_HEADER : ``esp_http_client_handle_t``
133+
- HTTP_EVENT_ON_HEADERS_COMPLETE: ``esp_http_client_handle_t``
134+
- HTTP_EVENT_ON_DATA : ``esp_http_client_on_data_t``
135+
- HTTP_EVENT_ON_FINISH : ``esp_http_client_handle_t``
136+
- HTTP_EVENT_DISCONNECTED : ``esp_http_client_handle_t``
137+
- HTTP_EVENT_REDIRECT : ``esp_http_client_redirect_event_data_t``
137138

138139
在无法接收到 :cpp:enumerator:`HTTP_EVENT_DISCONNECTED <esp_http_client_event_id_t::HTTP_EVENT_DISCONNECTED>` 之前,与事件数据一起接收到的 :cpp:type:`esp_http_client_handle_t` 将始终有效。这个句柄主要是为了区分不同的客户端连接,无法用于其他目的,因为它可能会随着客户端连接状态的变化而改变。
139140

examples/protocols/dns_over_https/components/dns_over_https/dns_over_https.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
7474
case HTTP_EVENT_ON_HEADER:
7575
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
7676
break;
77+
case HTTP_EVENT_ON_HEADERS_COMPLETE:
78+
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADERS_COMPLETE");
79+
break;
7780
case HTTP_EVENT_ON_DATA:
7881
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
7982
/* Check if buffer is null, if yes, initialize it */

examples/protocols/esp_http_client/main/esp_http_client_example.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
6565
case HTTP_EVENT_ON_HEADER:
6666
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
6767
break;
68+
case HTTP_EVENT_ON_HEADERS_COMPLETE:
69+
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADERS_COMPLETE");
70+
break;
6871
case HTTP_EVENT_ON_DATA:
6972
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
7073
// Clean the buffer in case of a new request

examples/system/ota/partitions_ota/main/app_main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
6868
case HTTP_EVENT_ON_HEADER:
6969
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
7070
break;
71+
case HTTP_EVENT_ON_HEADERS_COMPLETE:
72+
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADERS_COMPLETE");
73+
break;
7174
case HTTP_EVENT_ON_DATA:
7275
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
7376
break;

examples/system/ota/simple_ota_example/main/simple_ota_example.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
6060
case HTTP_EVENT_ON_HEADER:
6161
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
6262
break;
63+
case HTTP_EVENT_ON_HEADERS_COMPLETE:
64+
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADERS_COMPLETE");
65+
break;
6366
case HTTP_EVENT_ON_DATA:
6467
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
6568
break;

0 commit comments

Comments
 (0)