Merge branch 'bugfix/redirection' into 'master'

esp_http_client: Skip check for redirection counter if status code is success, fix issue with digest auth, configurable user agent string.

Closes IDFGH-4009 and IDFGH-4184

See merge request espressif/esp-idf!10707
pull/6275/head
Mahavir Jain 2020-11-19 19:09:48 +08:00
commit 30bc5dec1a
4 zmienionych plików z 21 dodań i 4 usunięć

Wyświetl plik

@ -610,6 +610,8 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
goto error;
}
const char *user_agent = config->user_agent == NULL ? DEFAULT_HTTP_USER_AGENT : config->user_agent;
if (config->host != NULL && config->path != NULL) {
host_name = _get_host_header(client->connection_info.host, client->connection_info.port);
if (host_name == NULL) {
@ -617,7 +619,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
goto error;
}
_success = (
(esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) &&
(esp_http_client_set_header(client, "User-Agent", user_agent) == ESP_OK) &&
(esp_http_client_set_header(client, "Host", host_name) == ESP_OK)
);
free(host_name);
@ -637,7 +639,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
}
_success = (
(esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) &&
(esp_http_client_set_header(client, "User-Agent", user_agent) == ESP_OK) &&
(esp_http_client_set_header(client, "Host", host_name) == ESP_OK)
);
@ -721,6 +723,9 @@ esp_err_t esp_http_client_set_redirection(esp_http_client_handle_t client)
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) {
return ESP_OK;
}
if (client->redirect_counter >= client->max_redirection_count || client->disable_auto_redirect) {
ESP_LOGE(TAG, "Error, reach max_redirection_count count=%d", client->redirect_counter);
return ESP_ERR_HTTP_MAX_REDIRECT;

Wyświetl plik

@ -117,6 +117,7 @@ typedef struct {
const char *cert_pem; /*!< SSL server certification, PEM format as string, if the client requires to verify server */
const char *client_cert_pem; /*!< SSL client certification, PEM format as string, if the server requires to verify client */
const char *client_key_pem; /*!< SSL client key, PEM format as string, if the server requires to verify client */
const char *user_agent; /*!< The User Agent string to send with HTTP requests */
esp_http_client_method_t method; /*!< HTTP Method */
int timeout_ms; /*!< Network timeout in milliseconds */
bool disable_auto_redirect; /*!< Disable HTTP automatic redirects */
@ -136,7 +137,11 @@ typedef struct {
* Enum for the HTTP status codes.
*/
typedef enum {
/* 2xx - Success */
HttpStatus_Ok = 200,
/* 3xx - Redirection */
HttpStatus_MultipleChoices = 300,
HttpStatus_MovedPermanently = 301,
HttpStatus_Found = 302,
HttpStatus_TemporaryRedirect = 307,

Wyświetl plik

@ -72,6 +72,7 @@ char *http_auth_digest(const char *username, const char *password, esp_http_auth
char *ha1, *ha2 = NULL;
char *digest = NULL;
char *auth_str = NULL;
char *temp_auth_str = NULL;
if (username == NULL ||
password == NULL ||
@ -123,8 +124,13 @@ char *http_auth_digest(const char *username, const char *password, esp_http_auth
}
}
asprintf(&auth_str, "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", algorithm=\"MD5\", "
"response=\"%s\", opaque=\"%s\", qop=%s, nc=%08x, cnonce=\"%016llx\"",
username, auth_data->realm, auth_data->nonce, auth_data->uri, digest, auth_data->opaque, auth_data->qop, auth_data->nc, auth_data->cnonce);
"response=\"%s\", qop=%s, nc=%08x, cnonce=\"%016llx\"",
username, auth_data->realm, auth_data->nonce, auth_data->uri, digest, auth_data->qop, auth_data->nc, auth_data->cnonce);
if (auth_data->opaque) {
asprintf(&temp_auth_str, "%s, opaque=\"%s\"", auth_str, auth_data->opaque);
free(auth_str);
auth_str = temp_auth_str;
}
_digest_exit:
free(ha1);
free(ha2);

Wyświetl plik

@ -124,6 +124,7 @@ static void http_rest_with_url(void)
.query = "esp",
.event_handler = _http_event_handler,
.user_data = local_response_buffer, // Pass address of local buffer to get response
.disable_auto_redirect = true,
};
esp_http_client_handle_t client = esp_http_client_init(&config);