Skip to content

Commit 211cbef

Browse files
committed
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
2 parents ca84628 + d36f5cf commit 211cbef

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

components/esp_http_client/esp_http_client.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ typedef enum {
9191
struct esp_http_client {
9292
int redirect_counter;
9393
int max_redirection_count;
94+
int max_authorization_retries;
9495
int process_again;
9596
struct http_parser *parser;
9697
struct http_parser_settings *parser_settings;
@@ -138,8 +139,9 @@ static esp_err_t _clear_connection_info(esp_http_client_handle_t client);
138139
static const char *DEFAULT_HTTP_USER_AGENT = "ESP32 HTTP Client/1.0";
139140
static const char *DEFAULT_HTTP_PROTOCOL = "HTTP/1.1";
140141
static const char *DEFAULT_HTTP_PATH = "/";
141-
static int DEFAULT_MAX_REDIRECT = 10;
142-
static int DEFAULT_TIMEOUT_MS = 5000;
142+
static const int DEFAULT_MAX_REDIRECT = 10;
143+
static const int DEFAULT_MAX_AUTH_RETRIES = 10;
144+
static const int DEFAULT_TIMEOUT_MS = 5000;
143145

144146
static const char *HTTP_METHOD_MAPPING[] = {
145147
"GET",
@@ -151,7 +153,14 @@ static const char *HTTP_METHOD_MAPPING[] = {
151153
"NOTIFY",
152154
"SUBSCRIBE",
153155
"UNSUBSCRIBE",
154-
"OPTIONS"
156+
"OPTIONS",
157+
"COPY",
158+
"MOVE",
159+
"LOCK",
160+
"UNLOCK",
161+
"PROPFIND",
162+
"PROPPATCH",
163+
"MKCOL"
155164
};
156165

157166
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
358367
client->event_handler = config->event_handler;
359368
client->timeout_ms = config->timeout_ms;
360369
client->max_redirection_count = config->max_redirection_count;
370+
client->max_authorization_retries = config->max_authorization_retries;
361371
client->user_data = config->user_data;
362372
client->buffer_size_rx = config->buffer_size;
363373
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
375385
client->max_redirection_count = DEFAULT_MAX_REDIRECT;
376386
}
377387

388+
if (client->max_authorization_retries == 0) {
389+
client->max_authorization_retries = DEFAULT_MAX_AUTH_RETRIES;
390+
} else if (client->max_authorization_retries == -1) {
391+
client->max_authorization_retries = 0;
392+
}
393+
378394
if (config->path) {
379395
client->connection_info.path = strdup(config->path);
380396
} else {
@@ -1304,6 +1320,10 @@ void esp_http_client_add_auth(esp_http_client_handle_t client)
13041320
if (client->state != HTTP_STATE_RES_COMPLETE_HEADER) {
13051321
return;
13061322
}
1323+
if (client->redirect_counter >= client->max_authorization_retries) {
1324+
ESP_LOGE(TAG, "Error, reached max_authorization_retries count=%d", client->redirect_counter);
1325+
return;
1326+
}
13071327

13081328
char *auth_header = client->auth_header;
13091329
if (auth_header) {

components/esp_http_client/include/esp_http_client.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ typedef enum {
8383
HTTP_METHOD_SUBSCRIBE, /*!< HTTP SUBSCRIBE Method */
8484
HTTP_METHOD_UNSUBSCRIBE,/*!< HTTP UNSUBSCRIBE Method */
8585
HTTP_METHOD_OPTIONS, /*!< HTTP OPTIONS Method */
86+
HTTP_METHOD_COPY, /*!< HTTP COPY Method */
87+
HTTP_METHOD_MOVE, /*!< HTTP MOVE Method */
88+
HTTP_METHOD_LOCK, /*!< HTTP LOCK Method */
89+
HTTP_METHOD_UNLOCK, /*!< HTTP UNLOCK Method */
90+
HTTP_METHOD_PROPFIND, /*!< HTTP PROPFIND Method */
91+
HTTP_METHOD_PROPPATCH, /*!< HTTP PROPPATCH Method */
92+
HTTP_METHOD_MKCOL, /*!< HTTP MKCOL Method */
8693
HTTP_METHOD_MAX,
8794
} esp_http_client_method_t;
8895

@@ -113,7 +120,8 @@ typedef struct {
113120
esp_http_client_method_t method; /*!< HTTP Method */
114121
int timeout_ms; /*!< Network timeout in milliseconds */
115122
bool disable_auto_redirect; /*!< Disable HTTP automatic redirects */
116-
int max_redirection_count; /*!< Max redirection number, using default value if zero*/
123+
int max_redirection_count; /*!< Max number of redirections on receiving HTTP redirect status code, using default value if zero*/
124+
int max_authorization_retries; /*!< Max connection retries on receiving HTTP unauthorized status code, using default value if zero. Disables authorization retry if -1*/
117125
http_event_handle_cb event_handler; /*!< HTTP Event Handle */
118126
esp_http_client_transport_t transport_type; /*!< HTTP transport type, see `esp_http_client_transport_t` */
119127
int buffer_size; /*!< HTTP receive buffer size */

examples/protocols/esp_http_client/main/esp_http_client_example.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
8787
// ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
8888
free(output_buffer);
8989
output_buffer = NULL;
90-
output_len = 0;
9190
}
91+
output_len = 0;
9292
break;
9393
case HTTP_EVENT_DISCONNECTED:
9494
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
@@ -98,8 +98,8 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
9898
if (output_buffer != NULL) {
9999
free(output_buffer);
100100
output_buffer = NULL;
101-
output_len = 0;
102101
}
102+
output_len = 0;
103103
ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
104104
ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
105105
}
@@ -294,10 +294,18 @@ static void http_rest_with_hostname_path(void)
294294
#if CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH
295295
static void http_auth_basic(void)
296296
{
297+
/**
298+
* Note: `max_authorization_retries` in esp_http_client_config_t
299+
* can be used to configure number of retry attempts to be performed
300+
* in case unauthorized status code is received.
301+
*
302+
* To disable authorization retries, set max_authorization_retries to -1.
303+
*/
297304
esp_http_client_config_t config = {
298305
.url = "http://user:[email protected]/basic-auth/user/passwd",
299306
.event_handler = _http_event_handler,
300307
.auth_type = HTTP_AUTH_TYPE_BASIC,
308+
.max_authorization_retries = -1,
301309
};
302310
esp_http_client_handle_t client = esp_http_client_init(&config);
303311
esp_err_t err = esp_http_client_perform(client);

0 commit comments

Comments
 (0)