From 7a0827e3d0d91923eb4fcc875357754e364ae74d Mon Sep 17 00:00:00 2001 From: guillempages Date: Tue, 25 Jan 2022 09:53:22 +0100 Subject: [PATCH] Configurable HTTP redirect following (#3100) Co-authored-by: Oxan van Leeuwen --- esphome/components/http_request/__init__.py | 7 +++++ .../components/http_request/http_request.cpp | 27 +++++++++++-------- .../components/http_request/http_request.h | 4 +++ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/esphome/components/http_request/__init__.py b/esphome/components/http_request/__init__.py index e044e5fece..c8c0ca5369 100644 --- a/esphome/components/http_request/__init__.py +++ b/esphome/components/http_request/__init__.py @@ -31,6 +31,8 @@ CONF_BODY = "body" CONF_JSON = "json" CONF_VERIFY_SSL = "verify_ssl" CONF_ON_RESPONSE = "on_response" +CONF_FOLLOW_REDIRECTS = "follow_redirects" +CONF_REDIRECT_LIMIT = "redirect_limit" def validate_url(value): @@ -71,6 +73,8 @@ CONFIG_SCHEMA = cv.All( { cv.GenerateID(): cv.declare_id(HttpRequestComponent), cv.Optional(CONF_USERAGENT, "ESPHome"): cv.string, + cv.Optional(CONF_FOLLOW_REDIRECTS, True): cv.boolean, + cv.Optional(CONF_REDIRECT_LIMIT, 3): cv.int_, cv.Optional( CONF_TIMEOUT, default="5s" ): cv.positive_time_period_milliseconds, @@ -90,6 +94,9 @@ async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) cg.add(var.set_timeout(config[CONF_TIMEOUT])) cg.add(var.set_useragent(config[CONF_USERAGENT])) + cg.add(var.set_follow_redirects(config[CONF_FOLLOW_REDIRECTS])) + cg.add(var.set_redirect_limit(config[CONF_REDIRECT_LIMIT])) + if CORE.is_esp8266 and not config[CONF_ESP8266_DISABLE_SSL_SUPPORT]: cg.add_define("USE_HTTP_REQUEST_ESP8266_HTTPS") diff --git a/esphome/components/http_request/http_request.cpp b/esphome/components/http_request/http_request.cpp index 0fd9c6a4ae..64f3f97de9 100644 --- a/esphome/components/http_request/http_request.cpp +++ b/esphome/components/http_request/http_request.cpp @@ -14,6 +14,8 @@ void HttpRequestComponent::dump_config() { ESP_LOGCONFIG(TAG, "HTTP Request:"); ESP_LOGCONFIG(TAG, " Timeout: %ums", this->timeout_); ESP_LOGCONFIG(TAG, " User-Agent: %s", this->useragent_); + ESP_LOGCONFIG(TAG, " Follow Redirects: %d", this->follow_redirects_); + ESP_LOGCONFIG(TAG, " Redirect limit: %d", this->redirect_limit_); } void HttpRequestComponent::set_url(std::string url) { @@ -38,18 +40,21 @@ void HttpRequestComponent::send(const std::vector bool begin_status = false; const String url = this->url_.c_str(); -#ifdef USE_ESP32 +#if defined(USE_ESP32) || (defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)) +#if defined(USE_ESP32) || USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0) + if (this->follow_redirects_) { + this->client_.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); + } else { + this->client_.setFollowRedirects(HTTPC_DISABLE_FOLLOW_REDIRECTS); + } +#else + this->client_.setFollowRedirects(this->follow_redirects_); +#endif + this->client_.setRedirectLimit(this->redirect_limit_); +#endif +#if defined(USE_ESP32) begin_status = this->client_.begin(url); -#endif -#ifdef USE_ESP8266 -#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0) - this->client_.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); -#elif USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) - this->client_.setFollowRedirects(true); -#endif -#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) - this->client_.setRedirectLimit(3); -#endif +#elif defined(USE_ESP8266) begin_status = this->client_.begin(*this->get_wifi_client_(), url); #endif diff --git a/esphome/components/http_request/http_request.h b/esphome/components/http_request/http_request.h index a38bdf9c95..4590163f2c 100644 --- a/esphome/components/http_request/http_request.h +++ b/esphome/components/http_request/http_request.h @@ -40,6 +40,8 @@ class HttpRequestComponent : public Component { void set_method(const char *method) { this->method_ = method; } void set_useragent(const char *useragent) { this->useragent_ = useragent; } void set_timeout(uint16_t timeout) { this->timeout_ = timeout; } + void set_follow_redirects(bool follow_redirects) { this->follow_redirects_ = follow_redirects; } + void set_redirect_limit(uint16_t limit) { this->redirect_limit_ = limit; } void set_body(const std::string &body) { this->body_ = body; } void set_headers(std::list
headers) { this->headers_ = std::move(headers); } void send(const std::vector &response_triggers); @@ -53,6 +55,8 @@ class HttpRequestComponent : public Component { const char *method_; const char *useragent_{nullptr}; bool secure_; + bool follow_redirects_; + uint16_t redirect_limit_; uint16_t timeout_{5000}; std::string body_; std::list
headers_;