Bläddra i källkod

esp_http_client: Added event for HTTP redirect
- Allows users to manually intercept and process the HTTP redirection
when disable_auto_redirect (from the esp_http_client handle) is set to true

Closes https://github.com/espressif/esp-idf/issues/8029

Laukik Hase 4 år sedan
förälder
incheckning
ca84d2d6c9

+ 3 - 0
components/esp_eth/test/test_emac.c

@@ -461,6 +461,9 @@ esp_err_t http_event_handle(esp_http_client_event_t *evt)
     case HTTP_EVENT_DISCONNECTED:
         ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
         break;
+    case HTTP_EVENT_REDIRECT:
+        ESP_LOGI(TAG, "HTTP_EVENT_REDIRECT");
+        break;
     }
     return ESP_OK;
 }

+ 5 - 2
components/esp_http_client/esp_http_client.c

@@ -1,5 +1,5 @@
 /*
- * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  *
  * SPDX-License-Identifier: Apache-2.0
  */
@@ -787,7 +787,7 @@ 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) {
+    if (client->redirect_counter >= client->max_redirection_count) {
         ESP_LOGE(TAG, "Error, reach max_redirection_count count=%d", client->redirect_counter);
         return ESP_ERR_HTTP_MAX_REDIRECT;
     }
@@ -795,6 +795,9 @@ static esp_err_t esp_http_check_response(esp_http_client_handle_t client)
         case HttpStatus_MovedPermanently:
         case HttpStatus_Found:
         case HttpStatus_TemporaryRedirect:
+            if (client->disable_auto_redirect) {
+                http_dispatch_event(client, HTTP_EVENT_REDIRECT, NULL, 0);
+            }
             esp_http_client_set_redirection(client);
             client->redirect_counter ++;
             client->process_again = 1;

+ 2 - 1
components/esp_http_client/include/esp_http_client.h

@@ -1,5 +1,5 @@
 /*
- * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  *
  * SPDX-License-Identifier: Apache-2.0
  */
@@ -35,6 +35,7 @@ typedef enum {
     HTTP_EVENT_ON_DATA,         /*!< Occurs when receiving data from the server, possibly multiple portions of the packet */
     HTTP_EVENT_ON_FINISH,       /*!< Occurs when finish a HTTP session */
     HTTP_EVENT_DISCONNECTED,    /*!< The connection has been disconnected */
+    HTTP_EVENT_REDIRECT,        /*!< Intercepting HTTP redirects to handle them manually */
 } esp_http_client_event_id_t;
 
 /**

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

@@ -109,6 +109,11 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
                 ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
             }
             break;
+        case HTTP_EVENT_REDIRECT:
+            ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT");
+            esp_http_client_set_header(evt->client, "From", "user@example.com");
+            esp_http_client_set_header(evt->client, "Accept", "text/html");
+            break;
     }
     return ESP_OK;
 }
@@ -447,6 +452,26 @@ static void http_absolute_redirect(void)
     esp_http_client_cleanup(client);
 }
 
+static void http_absolute_redirect_manual(void)
+{
+    esp_http_client_config_t config = {
+        .url = "http://httpbin.org/absolute-redirect/3",
+        .event_handler = _http_event_handler,
+        .disable_auto_redirect = true,
+    };
+    esp_http_client_handle_t client = esp_http_client_init(&config);
+    esp_err_t err = esp_http_client_perform(client);
+
+    if (err == ESP_OK) {
+        ESP_LOGI(TAG, "HTTP Absolute path redirect (manual) Status = %d, content_length = %lld",
+                esp_http_client_get_status_code(client),
+                esp_http_client_get_content_length(client));
+    } else {
+        ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
+    }
+    esp_http_client_cleanup(client);
+}
+
 static void http_redirect_to_https(void)
 {
     esp_http_client_config_t config = {
@@ -699,6 +724,7 @@ static void http_test_task(void *pvParameters)
 #endif
     http_relative_redirect();
     http_absolute_redirect();
+    http_absolute_redirect_manual();
     https_with_url();
     https_with_hostname_path();
     http_redirect_to_https();

+ 3 - 0
examples/system/ota/simple_ota_example/main/simple_ota_example.c

@@ -66,6 +66,9 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
     case HTTP_EVENT_DISCONNECTED:
         ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
         break;
+    case HTTP_EVENT_REDIRECT:
+        ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT");
+        break;
     }
     return ESP_OK;
 }