esp_http_client: Add feature to cancel an HTTP request

Closes: https://github.com/espressif/esp-idf/issues/9892
This commit is contained in:
Harshit Malpani
2022-11-10 17:39:30 +05:30
parent f54df61486
commit 6de9e42122
2 changed files with 35 additions and 0 deletions

View File

@@ -365,6 +365,29 @@ esp_err_t esp_http_client_get_password(esp_http_client_handle_t client, char **v
return ESP_OK;
}
esp_err_t esp_http_client_cancel_request(esp_http_client_handle_t client)
{
if (client == NULL) {
ESP_LOGD(TAG, "Client handle is NULL");
return ESP_ERR_INVALID_ARG;
}
if (client->state < HTTP_STATE_CONNECTED) {
ESP_LOGD(TAG, "Invalid State: %d", client->state);
return ESP_ERR_INVALID_STATE;
}
if (esp_transport_close(client->transport) != 0) {
return ESP_FAIL;
}
esp_err_t err = esp_http_client_connect(client);
// esp_http_client_connect() will return ESP_ERR_HTTP_CONNECTING in case of non-blocking mode and if the connection has not been established.
if (err == ESP_OK || (client->is_async && err == ESP_ERR_HTTP_CONNECTING)) {
client->response->data_process = client->response->content_length;
return ESP_OK;
}
return ESP_FAIL;
}
esp_err_t esp_http_client_set_password(esp_http_client_handle_t client, const char *password)
{
if (client == NULL) {

View File

@@ -218,6 +218,18 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
*/
esp_err_t esp_http_client_perform(esp_http_client_handle_t client);
/**
* @brief Cancel an ongoing HTTP request. This API closes the current socket and opens a new socket with the same esp_http_client context.
*
* @param client The esp_http_client handle
* @return
* - ESP_OK on successful
* - ESP_FAIL on error
* - ESP_ERR_INVALID_ARG
* - ESP_ERR_INVALID_STATE
*/
esp_err_t esp_http_client_cancel_request(esp_http_client_handle_t client);
/**
* @brief Set URL for client, when performing this behavior, the options in the URL will replace the old ones
*