diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index 65ec5fcb47..f9fb3c148f 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -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; diff --git a/components/esp_http_client/include/esp_http_client.h b/components/esp_http_client/include/esp_http_client.h index 3304cfd73f..017ee5d5ac 100644 --- a/components/esp_http_client/include/esp_http_client.h +++ b/components/esp_http_client/include/esp_http_client.h @@ -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, diff --git a/components/esp_http_client/lib/http_auth.c b/components/esp_http_client/lib/http_auth.c index 2ed724e16d..0eb19651f4 100644 --- a/components/esp_http_client/lib/http_auth.c +++ b/components/esp_http_client/lib/http_auth.c @@ -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); 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 9c7fb68c95..1932320198 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 @@ -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);