Skip to content

Commit

Permalink
Merge branch 'feature/http_request_cancel' into 'master'
Browse files Browse the repository at this point in the history
Cancel an HTTP request

Closes IDFGH-8426

See merge request espressif/esp-idf!21171
  • Loading branch information
mahavirj committed Dec 5, 2022
2 parents f27a95c + 6de9e42 commit 26e3f0b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
23 changes: 23 additions & 0 deletions components/esp_http_client/esp_http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions components/esp_http_client/include/esp_http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 26e3f0b

Please sign in to comment.