esp_http_client: Set user configurable authorization retries
Closes: https://github.com/espressif/esp-idf/issues/5407
This commit is contained in:
parent
bcfd2f07ab
commit
1bbafb817d
3 changed files with 25 additions and 3 deletions
|
@ -91,6 +91,7 @@ typedef enum {
|
||||||
struct esp_http_client {
|
struct esp_http_client {
|
||||||
int redirect_counter;
|
int redirect_counter;
|
||||||
int max_redirection_count;
|
int max_redirection_count;
|
||||||
|
int max_authorization_retries;
|
||||||
int process_again;
|
int process_again;
|
||||||
struct http_parser *parser;
|
struct http_parser *parser;
|
||||||
struct http_parser_settings *parser_settings;
|
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_USER_AGENT = "ESP32 HTTP Client/1.0";
|
||||||
static const char *DEFAULT_HTTP_PROTOCOL = "HTTP/1.1";
|
static const char *DEFAULT_HTTP_PROTOCOL = "HTTP/1.1";
|
||||||
static const char *DEFAULT_HTTP_PATH = "/";
|
static const char *DEFAULT_HTTP_PATH = "/";
|
||||||
static int DEFAULT_MAX_REDIRECT = 10;
|
static const int DEFAULT_MAX_REDIRECT = 10;
|
||||||
static int DEFAULT_TIMEOUT_MS = 5000;
|
static const int DEFAULT_MAX_AUTH_RETRIES = 10;
|
||||||
|
static const int DEFAULT_TIMEOUT_MS = 5000;
|
||||||
|
|
||||||
static const char *HTTP_METHOD_MAPPING[] = {
|
static const char *HTTP_METHOD_MAPPING[] = {
|
||||||
"GET",
|
"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->event_handler = config->event_handler;
|
||||||
client->timeout_ms = config->timeout_ms;
|
client->timeout_ms = config->timeout_ms;
|
||||||
client->max_redirection_count = config->max_redirection_count;
|
client->max_redirection_count = config->max_redirection_count;
|
||||||
|
client->max_authorization_retries = config->max_authorization_retries;
|
||||||
client->user_data = config->user_data;
|
client->user_data = config->user_data;
|
||||||
client->buffer_size_rx = config->buffer_size;
|
client->buffer_size_rx = config->buffer_size;
|
||||||
client->buffer_size_tx = config->buffer_size_tx;
|
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;
|
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) {
|
if (config->path) {
|
||||||
client->connection_info.path = strdup(config->path);
|
client->connection_info.path = strdup(config->path);
|
||||||
} else {
|
} 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) {
|
if (client->state != HTTP_STATE_RES_COMPLETE_HEADER) {
|
||||||
return;
|
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;
|
char *auth_header = client->auth_header;
|
||||||
if (auth_header) {
|
if (auth_header) {
|
||||||
|
|
|
@ -120,7 +120,8 @@ typedef struct {
|
||||||
esp_http_client_method_t method; /*!< HTTP Method */
|
esp_http_client_method_t method; /*!< HTTP Method */
|
||||||
int timeout_ms; /*!< Network timeout in milliseconds */
|
int timeout_ms; /*!< Network timeout in milliseconds */
|
||||||
bool disable_auto_redirect; /*!< Disable HTTP automatic redirects */
|
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 */
|
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` */
|
esp_http_client_transport_t transport_type; /*!< HTTP transport type, see `esp_http_client_transport_t` */
|
||||||
int buffer_size; /*!< HTTP receive buffer size */
|
int buffer_size; /*!< HTTP receive buffer size */
|
||||||
|
|
|
@ -294,10 +294,18 @@ static void http_rest_with_hostname_path(void)
|
||||||
#if CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH
|
#if CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH
|
||||||
static void http_auth_basic(void)
|
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 = {
|
esp_http_client_config_t config = {
|
||||||
.url = "http://user:passwd@httpbin.org/basic-auth/user/passwd",
|
.url = "http://user:passwd@httpbin.org/basic-auth/user/passwd",
|
||||||
.event_handler = _http_event_handler,
|
.event_handler = _http_event_handler,
|
||||||
.auth_type = HTTP_AUTH_TYPE_BASIC,
|
.auth_type = HTTP_AUTH_TYPE_BASIC,
|
||||||
|
.max_authorization_retries = -1,
|
||||||
};
|
};
|
||||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||||
esp_err_t err = esp_http_client_perform(client);
|
esp_err_t err = esp_http_client_perform(client);
|
||||||
|
|
Loading…
Reference in a new issue