http_request ESP32 insecure requests fix (#1041)

* Fixed #1175

* CI

Co-authored-by: Nikolay Vasilchuk <nikolay.vasilchuk@corp.mail.ru>
This commit is contained in:
Nikolay Vasilchuk 2020-05-01 05:05:11 +03:00 committed by GitHub
parent 14e9375262
commit 8b92456ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 8 deletions

View File

@ -15,14 +15,15 @@ void HttpRequestComponent::dump_config() {
void HttpRequestComponent::send() { void HttpRequestComponent::send() {
bool begin_status = false; bool begin_status = false;
this->client_.setReuse(true); this->client_.setReuse(true);
static const String URL = this->url_.c_str();
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
begin_status = this->client_.begin(this->url_); begin_status = this->client_.begin(URL);
#endif #endif
#ifdef ARDUINO_ARCH_ESP8266 #ifdef ARDUINO_ARCH_ESP8266
#ifndef CLANG_TIDY #ifndef CLANG_TIDY
this->client_.setFollowRedirects(true); this->client_.setFollowRedirects(true);
this->client_.setRedirectLimit(3); 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
#endif #endif
@ -43,19 +44,20 @@ void HttpRequestComponent::send() {
int http_code = this->client_.sendRequest(this->method_, this->body_.c_str()); int http_code = this->client_.sendRequest(this->method_, this->body_.c_str());
if (http_code < 0) { 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(); this->status_set_warning();
return; return;
} }
if (http_code < 200 || http_code >= 300) { 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(); this->status_set_warning();
return; return;
} }
this->status_clear_warning(); 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 #ifdef ARDUINO_ARCH_ESP8266
@ -78,7 +80,10 @@ WiFiClient *HttpRequestComponent::get_wifi_client_() {
void HttpRequestComponent::close() { this->client_.end(); } 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 http_request
} // namespace esphome } // namespace esphome

View File

@ -28,7 +28,7 @@ class HttpRequestComponent : public Component {
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
void set_url(std::string url) { void set_url(std::string url) {
this->url_ = url.c_str(); this->url_ = url;
this->secure_ = url.compare(0, 6, "https:") == 0; this->secure_ = url.compare(0, 6, "https:") == 0;
} }
void set_method(const char *method) { this->method_ = method; } void set_method(const char *method) { this->method_ = method; }
@ -42,7 +42,7 @@ class HttpRequestComponent : public Component {
protected: protected:
HTTPClient client_{}; HTTPClient client_{};
const char *url_; std::string url_;
const char *method_; const char *method_;
const char *useragent_{nullptr}; const char *useragent_{nullptr};
bool secure_; bool secure_;