From 1bbafb817d660436c172675526375ebdacd219dc Mon Sep 17 00:00:00 2001 From: Shubham Kulkarni Date: Tue, 9 Jun 2020 15:56:12 +0530 Subject: [PATCH] esp_http_client: Set user configurable authorization retries Closes: https://github.com/espressif/esp-idf/issues/5407 --- components/esp_http_client/esp_http_client.c | 17 +++++++++++++++-- .../esp_http_client/include/esp_http_client.h | 3 ++- .../main/esp_http_client_example.c | 8 ++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index e44cc7e5a..d355b509b 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -91,6 +91,7 @@ typedef enum { struct esp_http_client { int redirect_counter; int max_redirection_count; + int max_authorization_retries; int process_again; struct http_parser *parser; struct http_parser_settings *parser_settings; @@ -138,8 +139,9 @@ static esp_err_t _clear_connection_info(esp_http_client_handle_t client); static const char *DEFAULT_HTTP_USER_AGENT = "ESP32 HTTP Client/1.0"; static const char *DEFAULT_HTTP_PROTOCOL = "HTTP/1.1"; static const char *DEFAULT_HTTP_PATH = "/"; -static int DEFAULT_MAX_REDIRECT = 10; -static int DEFAULT_TIMEOUT_MS = 5000; +static const int DEFAULT_MAX_REDIRECT = 10; +static const int DEFAULT_MAX_AUTH_RETRIES = 10; +static const int DEFAULT_TIMEOUT_MS = 5000; static const char *HTTP_METHOD_MAPPING[] = { "GET", @@ -365,6 +367,7 @@ static esp_err_t _set_config(esp_http_client_handle_t client, const esp_http_cli client->event_handler = config->event_handler; client->timeout_ms = config->timeout_ms; client->max_redirection_count = config->max_redirection_count; + client->max_authorization_retries = config->max_authorization_retries; client->user_data = config->user_data; client->buffer_size_rx = config->buffer_size; client->buffer_size_tx = config->buffer_size_tx; @@ -382,6 +385,12 @@ static esp_err_t _set_config(esp_http_client_handle_t client, const esp_http_cli client->max_redirection_count = DEFAULT_MAX_REDIRECT; } + if (client->max_authorization_retries == 0) { + client->max_authorization_retries = DEFAULT_MAX_AUTH_RETRIES; + } else if (client->max_authorization_retries == -1) { + client->max_authorization_retries = 0; + } + if (config->path) { client->connection_info.path = strdup(config->path); } else { @@ -1311,6 +1320,10 @@ void esp_http_client_add_auth(esp_http_client_handle_t client) if (client->state != HTTP_STATE_RES_COMPLETE_HEADER) { return; } + if (client->redirect_counter >= client->max_authorization_retries) { + ESP_LOGE(TAG, "Error, reached max_authorization_retries count=%d", client->redirect_counter); + return; + } char *auth_header = client->auth_header; if (auth_header) { diff --git a/components/esp_http_client/include/esp_http_client.h b/components/esp_http_client/include/esp_http_client.h index 0aba3d268..2857ba919 100644 --- a/components/esp_http_client/include/esp_http_client.h +++ b/components/esp_http_client/include/esp_http_client.h @@ -120,7 +120,8 @@ typedef struct { esp_http_client_method_t method; /*!< HTTP Method */ int timeout_ms; /*!< Network timeout in milliseconds */ bool disable_auto_redirect; /*!< Disable HTTP automatic redirects */ - int max_redirection_count; /*!< Max redirection number, using default value if zero*/ + int max_redirection_count; /*!< Max number of redirections on receiving HTTP redirect status code, using default value if zero*/ + int max_authorization_retries; /*!< Max connection retries on receiving HTTP unauthorized status code, using default value if zero. Disables authorization retry if -1*/ http_event_handle_cb event_handler; /*!< HTTP Event Handle */ esp_http_client_transport_t transport_type; /*!< HTTP transport type, see `esp_http_client_transport_t` */ int buffer_size; /*!< HTTP receive buffer size */ diff --git a/examples/protocols/esp_http_client/main/esp_http_client_example.c b/examples/protocols/esp_http_client/main/esp_http_client_example.c index 7cc3eb1d9..0c15c3e05 100644 --- a/examples/protocols/esp_http_client/main/esp_http_client_example.c +++ b/examples/protocols/esp_http_client/main/esp_http_client_example.c @@ -294,10 +294,18 @@ static void http_rest_with_hostname_path(void) #if CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH static void http_auth_basic(void) { + /** + * Note: `max_authorization_retries` in esp_http_client_config_t + * can be used to configure number of retry attempts to be performed + * in case unauthorized status code is received. + * + * To disable authorization retries, set max_authorization_retries to -1. + */ esp_http_client_config_t config = { .url = "http://user:passwd@httpbin.org/basic-auth/user/passwd", .event_handler = _http_event_handler, .auth_type = HTTP_AUTH_TYPE_BASIC, + .max_authorization_retries = -1, }; esp_http_client_handle_t client = esp_http_client_init(&config); esp_err_t err = esp_http_client_perform(client);