Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_resetting_redirect_counter_v5.1' into 'relea…
Browse files Browse the repository at this point in the history
…se/v5.1'

fix: reset redirect counter for using same handler (v5.1)

See merge request espressif/esp-idf!30937
  • Loading branch information
AdityaHPatwardhan committed May 21, 2024
2 parents 980a568 + a7f29df commit 3982d55
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
10 changes: 10 additions & 0 deletions components/esp_http_client/esp_http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,19 @@ esp_err_t esp_http_client_set_redirection(esp_http_client_handle_t client)
return err;
}

esp_err_t esp_http_client_reset_redirect_counter(esp_http_client_handle_t client)
{
if (client == NULL) {
return ESP_ERR_INVALID_ARG;
}
client->redirect_counter = 0;
return ESP_OK;
}

static esp_err_t esp_http_check_response(esp_http_client_handle_t client)
{
if (client->response->status_code >= HttpStatus_Ok && client->response->status_code < HttpStatus_MultipleChoices) {
client->redirect_counter = 0;
return ESP_OK;
}
if (client->redirect_counter >= client->max_redirection_count) {
Expand Down
22 changes: 17 additions & 5 deletions components/esp_http_client/include/esp_http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ typedef enum {
HTTP_EVENT_ERROR = 0, /*!< This event occurs when there are any errors during execution */
HTTP_EVENT_ON_CONNECTED, /*!< Once the HTTP has been connected to the server, no data exchange has been performed */
HTTP_EVENT_HEADERS_SENT, /*!< After sending all the headers to the server */
HTTP_EVENT_HEADER_SENT = HTTP_EVENT_HEADERS_SENT, /*!< This header has been kept for backward compatability
HTTP_EVENT_HEADER_SENT = HTTP_EVENT_HEADERS_SENT, /*!< This header has been kept for backward compatibility
and will be deprecated in future versions esp-idf */
HTTP_EVENT_ON_HEADER, /*!< Occurs when receiving each header sent from the server */
HTTP_EVENT_ON_DATA, /*!< Occurs when receiving data from the server, possibly multiple portions of the packet */
Expand Down Expand Up @@ -120,7 +120,7 @@ typedef enum {
typedef enum {
HTTP_AUTH_TYPE_NONE = 0, /*!< No authention */
HTTP_AUTH_TYPE_BASIC, /*!< HTTP Basic authentication */
HTTP_AUTH_TYPE_DIGEST, /*!< HTTP Disgest authentication */
HTTP_AUTH_TYPE_DIGEST, /*!< HTTP Digest authentication */
} esp_http_client_auth_type_t;

/**
Expand Down Expand Up @@ -238,7 +238,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
* must be set while making a call to esp_http_client_init() API.
* 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.
* If you intend to transfer more than one file, you are even encouraged to do so.
* 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.
* 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.
* 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.
*
* @note You must never call this function simultaneously from two places using the same client handle.
Expand Down Expand Up @@ -601,7 +601,7 @@ esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_h
* @brief Set redirection URL.
* When received the 30x code from the server, the client stores the redirect URL provided by the server.
* This function will set the current URL to redirect to enable client to execute the redirection request.
* 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.
* 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.
*
* @param[in] client The esp_http_client handle
*
Expand All @@ -611,6 +611,18 @@ esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_h
*/
esp_err_t esp_http_client_set_redirection(esp_http_client_handle_t client);

/**
* @brief Reset the redirection counter.
* This is useful to reset redirect counter in cases where the same handle is used for multiple requests.
*
* @param[in] client The esp_http_client handle
*
* @return
* - ESP_OK
* - ESP_ERR_INVALID_ARG
*/
esp_err_t esp_http_client_reset_redirect_counter(esp_http_client_handle_t client);

/**
* @brief On receiving HTTP Status code 401, this API can be invoked to add authorization
* information.
Expand Down Expand Up @@ -650,7 +662,7 @@ int esp_http_client_read_response(esp_http_client_handle_t client, char *buffer,
/**
* @brief Process all remaining response data
* This uses an internal buffer to repeatedly receive, parse, and discard response data until complete data is processed.
* 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.
* 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.
*
* @param[in] client The esp_http_client handle
* @param len Length of data discarded
Expand Down
13 changes: 11 additions & 2 deletions components/esp_http_server/src/util/ctrl_sock.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -11,7 +11,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include "sdkconfig.h"

#include "esp_log.h"
#include "ctrl_sock.h"

#if CONFIG_IDF_TARGET_LINUX
Expand All @@ -22,11 +22,20 @@
#define IPV6_ENABLED CONFIG_LWIP_IPV6
#endif // !CONFIG_IDF_TARGET_LINUX

#if !CONFIG_LWIP_NETIF_LOOPBACK
static const char *TAG = "esp_http_server";
#endif

/* Control socket, because in some network stacks select can't be woken up any
* other way
*/
int cs_create_ctrl_sock(int port)
{
#if !CONFIG_LWIP_NETIF_LOOPBACK
ESP_LOGE(TAG, "Please enable LWIP_NETIF_LOOPBACK for %s API", __func__);
return -1;
#endif

int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (fd < 0) {
return -1;
Expand Down

0 comments on commit 3982d55

Please sign in to comment.