Jelajahi Sumber

Merge branch 'bugfix/redirection_v4.0' into 'release/v4.0'

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

See merge request espressif/esp-idf!11599
Mahavir Jain 5 tahun lalu
induk
melakukan
db603854a9

+ 7 - 2
components/esp_http_client/esp_http_client.c

@@ -563,9 +563,11 @@ 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) {
         _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", client->connection_info.host) == ESP_OK)
         );
 
@@ -576,7 +578,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
     } else if (config->url != NULL) {
         _success = (
                     (esp_http_client_set_url(client, config->url) == ESP_OK) &&
-                    (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", client->connection_info.host) == ESP_OK)
                 );
 
@@ -650,6 +652,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;

+ 5 - 0
components/esp_http_client/include/esp_http_client.h

@@ -110,6 +110,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 */
@@ -128,7 +129,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,
 

+ 8 - 2
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);

+ 1 - 0
examples/protocols/esp_http_client/main/esp_http_client_example.c

@@ -81,6 +81,7 @@ static void http_rest_with_url()
     esp_http_client_config_t config = {
         .url = "http://httpbin.org/get",
         .event_handler = _http_event_handler,
+        .disable_auto_redirect = true,
     };
     esp_http_client_handle_t client = esp_http_client_init(&config);