From 8b92456ded97c236846e959fb3797f51b9dda4fc Mon Sep 17 00:00:00 2001 From: Nikolay Vasilchuk Date: Fri, 1 May 2020 05:05:11 +0300 Subject: [PATCH] http_request ESP32 insecure requests fix (#1041) * Fixed #1175 * CI Co-authored-by: Nikolay Vasilchuk --- .../components/http_request/http_request.cpp | 17 +++++++++++------ esphome/components/http_request/http_request.h | 4 ++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/esphome/components/http_request/http_request.cpp b/esphome/components/http_request/http_request.cpp index f2fe0cff54..390867c948 100644 --- a/esphome/components/http_request/http_request.cpp +++ b/esphome/components/http_request/http_request.cpp @@ -15,14 +15,15 @@ void HttpRequestComponent::dump_config() { void HttpRequestComponent::send() { bool begin_status = false; this->client_.setReuse(true); + static const String URL = this->url_.c_str(); #ifdef ARDUINO_ARCH_ESP32 - begin_status = this->client_.begin(this->url_); + begin_status = this->client_.begin(URL); #endif #ifdef ARDUINO_ARCH_ESP8266 #ifndef CLANG_TIDY this->client_.setFollowRedirects(true); this->client_.setRedirectLimit(3); - begin_status = this->client_.begin(*this->get_wifi_client_(), this->url_); + begin_status = this->client_.begin(*this->get_wifi_client_(), URL); #endif #endif @@ -43,19 +44,20 @@ void HttpRequestComponent::send() { int http_code = this->client_.sendRequest(this->method_, this->body_.c_str()); if (http_code < 0) { - ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s", this->url_, HTTPClient::errorToString(http_code).c_str()); + ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s", this->url_.c_str(), + HTTPClient::errorToString(http_code).c_str()); this->status_set_warning(); return; } if (http_code < 200 || http_code >= 300) { - ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Code: %d", this->url_, http_code); + ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Code: %d", this->url_.c_str(), http_code); this->status_set_warning(); return; } this->status_clear_warning(); - ESP_LOGD(TAG, "HTTP Request completed; URL: %s; Code: %d", this->url_, http_code); + ESP_LOGD(TAG, "HTTP Request completed; URL: %s; Code: %d", this->url_.c_str(), http_code); } #ifdef ARDUINO_ARCH_ESP8266 @@ -78,7 +80,10 @@ WiFiClient *HttpRequestComponent::get_wifi_client_() { void HttpRequestComponent::close() { this->client_.end(); } -const char *HttpRequestComponent::get_string() { return this->client_.getString().c_str(); } +const char *HttpRequestComponent::get_string() { + static const String STR = this->client_.getString(); + return STR.c_str(); +} } // namespace http_request } // namespace esphome diff --git a/esphome/components/http_request/http_request.h b/esphome/components/http_request/http_request.h index 0c849f2ab0..e6c0510b32 100644 --- a/esphome/components/http_request/http_request.h +++ b/esphome/components/http_request/http_request.h @@ -28,7 +28,7 @@ class HttpRequestComponent : public Component { float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } void set_url(std::string url) { - this->url_ = url.c_str(); + this->url_ = url; this->secure_ = url.compare(0, 6, "https:") == 0; } void set_method(const char *method) { this->method_ = method; } @@ -42,7 +42,7 @@ class HttpRequestComponent : public Component { protected: HTTPClient client_{}; - const char *url_; + std::string url_; const char *method_; const char *useragent_{nullptr}; bool secure_;