feat(esp_http_client): added new HTTP state HTTP_STATE_CONNECTING and change state flow

This commit added new http state HTTP_STATE_CONNECTING, and made states public.
Also added public API to get current state.
Updated state flow in esp_http_client_perform()

Closes https://github.com/espressif/esp-idf/issues/16019
This commit is contained in:
nilesh.kale
2025-07-10 15:56:06 +05:30
parent c3960c810e
commit 9984cc4aab
2 changed files with 37 additions and 11 deletions

View File

@@ -88,17 +88,7 @@ typedef struct {
int max_store_header_size;
} connection_info_t;
typedef enum {
HTTP_STATE_UNINIT = 0,
HTTP_STATE_INIT,
HTTP_STATE_CONNECTED,
HTTP_STATE_REQ_COMPLETE_HEADER,
HTTP_STATE_REQ_COMPLETE_DATA,
HTTP_STATE_RES_COMPLETE_HEADER,
HTTP_STATE_RES_ON_DATA_START,
HTTP_STATE_RES_COMPLETE_DATA,
HTTP_STATE_CLOSE
} esp_http_state_t;
typedef enum {
SESSION_TICKET_UNUSED = 0,
@@ -1429,6 +1419,8 @@ esp_err_t esp_http_client_perform(esp_http_client_handle_t client)
then the esp_http_client_perform() API will return ESP_ERR_HTTP_EAGAIN error. The user may call
esp_http_client_perform API again, and for this reason, we maintain the states */
case HTTP_STATE_INIT:
/* falls through */
case HTTP_STATE_CONNECTING:
if ((err = esp_http_client_connect(client)) != ESP_OK) {
if (client->is_async && err == ESP_ERR_HTTP_CONNECTING) {
return ESP_ERR_HTTP_EAGAIN;
@@ -1576,6 +1568,7 @@ static esp_err_t esp_http_client_connect(esp_http_client_handle_t client)
esp_http_client_close(client);
return err;
}
client->state = HTTP_STATE_CONNECTING;
if (client->state < HTTP_STATE_CONNECTED) {
#ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_CUSTOM_TRANSPORT
@@ -2025,3 +2018,11 @@ esp_err_t esp_http_client_get_chunk_length(esp_http_client_handle_t client, int
}
return ESP_OK;
}
esp_http_state_t esp_http_client_get_state(esp_http_client_handle_t client)
{
if (client == NULL) {
return HTTP_STATE_UNINIT;
}
return client->state;
}

View File

@@ -145,6 +145,22 @@ typedef enum {
HTTP_TLS_DYN_BUF_STRATEGY_MAX, /*!< to indicate max */
} esp_http_client_tls_dyn_buf_strategy_t;
/**
* @brief HTTP Client states
*/
typedef enum {
HTTP_STATE_UNINIT = 0, /*!< HTTP client uninitialized */
HTTP_STATE_INIT, /*!< HTTP client initialized */
HTTP_STATE_CONNECTING, /*!< HTTP client connecting to server */
HTTP_STATE_CONNECTED, /*!< HTTP client connected to server */
HTTP_STATE_REQ_COMPLETE_HEADER, /*!< HTTP request headers sent */
HTTP_STATE_REQ_COMPLETE_DATA, /*!< HTTP request data sent */
HTTP_STATE_RES_COMPLETE_HEADER, /*!< HTTP response headers received */
HTTP_STATE_RES_ON_DATA_START, /*!< HTTP response data started */
HTTP_STATE_RES_COMPLETE_DATA, /*!< HTTP response data completed */
HTTP_STATE_CLOSE /*!< HTTP client connection closed */
} esp_http_state_t;
/**
* @brief HTTP configuration
*/
@@ -822,6 +838,15 @@ esp_err_t esp_http_client_get_url(esp_http_client_handle_t client, char *url, co
*/
esp_err_t esp_http_client_get_chunk_length(esp_http_client_handle_t client, int *len);
/**
* @brief Get the current state of the HTTP client
*
* @param[in] client The HTTP client handle
*
* @return Current state of the HTTP client
*/
esp_http_state_t esp_http_client_get_state(esp_http_client_handle_t client);
#ifdef __cplusplus
}
#endif