Merge branch 'bugfix/authorization_retries' into 'master'
http_client: Add http methods required for WebDAV and fix for authorization retries Closes IDFGH-3441 and IDFGH-3445 See merge request espressif/esp-idf!9122
This commit is contained in:
commit
211cbef276
3 changed files with 42 additions and 6 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",
|
||||||
|
@ -151,7 +153,14 @@ static const char *HTTP_METHOD_MAPPING[] = {
|
||||||
"NOTIFY",
|
"NOTIFY",
|
||||||
"SUBSCRIBE",
|
"SUBSCRIBE",
|
||||||
"UNSUBSCRIBE",
|
"UNSUBSCRIBE",
|
||||||
"OPTIONS"
|
"OPTIONS",
|
||||||
|
"COPY",
|
||||||
|
"MOVE",
|
||||||
|
"LOCK",
|
||||||
|
"UNLOCK",
|
||||||
|
"PROPFIND",
|
||||||
|
"PROPPATCH",
|
||||||
|
"MKCOL"
|
||||||
};
|
};
|
||||||
|
|
||||||
static esp_err_t esp_http_client_request_send(esp_http_client_handle_t client, int write_len);
|
static esp_err_t esp_http_client_request_send(esp_http_client_handle_t client, int write_len);
|
||||||
|
@ -358,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;
|
||||||
|
@ -375,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 {
|
||||||
|
@ -1304,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) {
|
||||||
|
|
|
@ -83,6 +83,13 @@ typedef enum {
|
||||||
HTTP_METHOD_SUBSCRIBE, /*!< HTTP SUBSCRIBE Method */
|
HTTP_METHOD_SUBSCRIBE, /*!< HTTP SUBSCRIBE Method */
|
||||||
HTTP_METHOD_UNSUBSCRIBE,/*!< HTTP UNSUBSCRIBE Method */
|
HTTP_METHOD_UNSUBSCRIBE,/*!< HTTP UNSUBSCRIBE Method */
|
||||||
HTTP_METHOD_OPTIONS, /*!< HTTP OPTIONS Method */
|
HTTP_METHOD_OPTIONS, /*!< HTTP OPTIONS Method */
|
||||||
|
HTTP_METHOD_COPY, /*!< HTTP COPY Method */
|
||||||
|
HTTP_METHOD_MOVE, /*!< HTTP MOVE Method */
|
||||||
|
HTTP_METHOD_LOCK, /*!< HTTP LOCK Method */
|
||||||
|
HTTP_METHOD_UNLOCK, /*!< HTTP UNLOCK Method */
|
||||||
|
HTTP_METHOD_PROPFIND, /*!< HTTP PROPFIND Method */
|
||||||
|
HTTP_METHOD_PROPPATCH, /*!< HTTP PROPPATCH Method */
|
||||||
|
HTTP_METHOD_MKCOL, /*!< HTTP MKCOL Method */
|
||||||
HTTP_METHOD_MAX,
|
HTTP_METHOD_MAX,
|
||||||
} esp_http_client_method_t;
|
} esp_http_client_method_t;
|
||||||
|
|
||||||
|
@ -113,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 */
|
||||||
|
|
|
@ -87,8 +87,8 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
||||||
// ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
|
// ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
|
||||||
free(output_buffer);
|
free(output_buffer);
|
||||||
output_buffer = NULL;
|
output_buffer = NULL;
|
||||||
output_len = 0;
|
|
||||||
}
|
}
|
||||||
|
output_len = 0;
|
||||||
break;
|
break;
|
||||||
case HTTP_EVENT_DISCONNECTED:
|
case HTTP_EVENT_DISCONNECTED:
|
||||||
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
|
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
|
||||||
|
@ -98,8 +98,8 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
||||||
if (output_buffer != NULL) {
|
if (output_buffer != NULL) {
|
||||||
free(output_buffer);
|
free(output_buffer);
|
||||||
output_buffer = NULL;
|
output_buffer = NULL;
|
||||||
output_len = 0;
|
|
||||||
}
|
}
|
||||||
|
output_len = 0;
|
||||||
ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
|
ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
|
||||||
ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
|
ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
|
||||||
}
|
}
|
||||||
|
@ -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