Skip to content

Commit 856a299

Browse files
committed
fix: reset redirect counter for using same handler
Closes espressif#13633
1 parent be9c714 commit 856a299

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

components/esp_http_client/esp_http_client.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -957,9 +957,19 @@ esp_err_t esp_http_client_set_redirection(esp_http_client_handle_t client)
957957
return err;
958958
}
959959

960+
esp_err_t esp_http_client_reset_redirect_counter(esp_http_client_handle_t client)
961+
{
962+
if (client == NULL) {
963+
return ESP_ERR_INVALID_ARG;
964+
}
965+
client->redirect_counter = 0;
966+
return ESP_OK;
967+
}
968+
960969
static esp_err_t esp_http_check_response(esp_http_client_handle_t client)
961970
{
962971
if (client->response->status_code >= HttpStatus_Ok && client->response->status_code < HttpStatus_MultipleChoices) {
972+
client->redirect_counter = 0;
963973
return ESP_OK;
964974
}
965975
if (client->redirect_counter >= client->max_redirection_count) {

components/esp_http_client/include/esp_http_client.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -36,7 +36,7 @@ typedef enum {
3636
HTTP_EVENT_ERROR = 0, /*!< This event occurs when there are any errors during execution */
3737
HTTP_EVENT_ON_CONNECTED, /*!< Once the HTTP has been connected to the server, no data exchange has been performed */
3838
HTTP_EVENT_HEADERS_SENT, /*!< After sending all the headers to the server */
39-
HTTP_EVENT_HEADER_SENT = HTTP_EVENT_HEADERS_SENT, /*!< This header has been kept for backward compatability
39+
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 */
4242
HTTP_EVENT_ON_DATA, /*!< Occurs when receiving data from the server, possibly multiple portions of the packet */
@@ -125,7 +125,7 @@ typedef enum {
125125
typedef enum {
126126
HTTP_AUTH_TYPE_NONE = 0, /*!< No authention */
127127
HTTP_AUTH_TYPE_BASIC, /*!< HTTP Basic authentication */
128-
HTTP_AUTH_TYPE_DIGEST, /*!< HTTP Disgest authentication */
128+
HTTP_AUTH_TYPE_DIGEST, /*!< HTTP Digest authentication */
129129
} esp_http_client_auth_type_t;
130130

131131
/**
@@ -249,7 +249,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
249249
* must be set while making a call to esp_http_client_init() API.
250250
* You can do any amount of calls to esp_http_client_perform while using the same esp_http_client_handle_t. The underlying connection may be kept open if the server allows it.
251251
* If you intend to transfer more than one file, you are even encouraged to do so.
252-
* esp_http_client will then attempt to re-use the same connection for the following transfers, thus making the operations faster, less CPU intense and using less network resources.
252+
* esp_http_client will then attempt to reuse the same connection for the following transfers, thus making the operations faster, less CPU intense and using less network resources.
253253
* Just note that you will have to use `esp_http_client_set_**` between the invokes to set options for the following esp_http_client_perform.
254254
*
255255
* @note You must never call this function simultaneously from two places using the same client handle.
@@ -612,7 +612,7 @@ esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_h
612612
* @brief Set redirection URL.
613613
* When received the 30x code from the server, the client stores the redirect URL provided by the server.
614614
* This function will set the current URL to redirect to enable client to execute the redirection request.
615-
* When `disable_auto_redirect` is set, the client will not call this function but the event `HTTP_EVENT_REDIRECT` will be dispatched giving the user contol over the redirection event.
615+
* When `disable_auto_redirect` is set, the client will not call this function but the event `HTTP_EVENT_REDIRECT` will be dispatched giving the user control over the redirection event.
616616
*
617617
* @param[in] client The esp_http_client handle
618618
*
@@ -622,6 +622,18 @@ esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_h
622622
*/
623623
esp_err_t esp_http_client_set_redirection(esp_http_client_handle_t client);
624624

625+
/**
626+
* @brief Reset the redirection counter.
627+
* This is useful to reset redirect counter in cases where the same handle is used for multiple requests.
628+
*
629+
* @param[in] client The esp_http_client handle
630+
*
631+
* @return
632+
* - ESP_OK
633+
* - ESP_ERR_INVALID_ARG
634+
*/
635+
esp_err_t esp_http_client_reset_redirect_counter(esp_http_client_handle_t client);
636+
625637
/**
626638
* @brief On receiving a custom authentication header, this API can be invoked to set the
627639
* authentication information from the header. This API can be called from the event
@@ -676,7 +688,7 @@ int esp_http_client_read_response(esp_http_client_handle_t client, char *buffer,
676688
/**
677689
* @brief Process all remaining response data
678690
* This uses an internal buffer to repeatedly receive, parse, and discard response data until complete data is processed.
679-
* As no additional user-supplied buffer is required, this may be preferrable to `esp_http_client_read_response` in situations where the content of the response may be ignored.
691+
* As no additional user-supplied buffer is required, this may be preferable to `esp_http_client_read_response` in situations where the content of the response may be ignored.
680692
*
681693
* @param[in] client The esp_http_client handle
682694
* @param len Length of data discarded

0 commit comments

Comments
 (0)