2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_ARDUINO
|
|
|
|
|
2019-11-09 18:37:52 +01:00
|
|
|
#include "http_request.h"
|
2022-01-24 10:34:34 +01:00
|
|
|
#include "esphome/core/defines.h"
|
2019-11-09 18:37:52 +01:00
|
|
|
#include "esphome/core/log.h"
|
2021-10-12 00:11:04 +02:00
|
|
|
#include "esphome/components/network/util.h"
|
2019-11-09 18:37:52 +01:00
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace http_request {
|
|
|
|
|
2021-06-10 22:19:44 +02:00
|
|
|
static const char *const TAG = "http_request";
|
2019-11-09 18:37:52 +01:00
|
|
|
|
|
|
|
void HttpRequestComponent::dump_config() {
|
|
|
|
ESP_LOGCONFIG(TAG, "HTTP Request:");
|
|
|
|
ESP_LOGCONFIG(TAG, " Timeout: %ums", this->timeout_);
|
|
|
|
ESP_LOGCONFIG(TAG, " User-Agent: %s", this->useragent_);
|
2022-01-25 09:53:22 +01:00
|
|
|
ESP_LOGCONFIG(TAG, " Follow Redirects: %d", this->follow_redirects_);
|
|
|
|
ESP_LOGCONFIG(TAG, " Redirect limit: %d", this->redirect_limit_);
|
2019-11-09 18:37:52 +01:00
|
|
|
}
|
|
|
|
|
2020-12-03 19:37:00 +01:00
|
|
|
void HttpRequestComponent::set_url(std::string url) {
|
2021-06-10 13:04:40 +02:00
|
|
|
this->url_ = std::move(url);
|
|
|
|
this->secure_ = this->url_.compare(0, 6, "https:") == 0;
|
2020-12-03 19:37:00 +01:00
|
|
|
|
|
|
|
if (!this->last_url_.empty() && this->url_ != this->last_url_) {
|
|
|
|
// Close connection if url has been changed
|
|
|
|
this->client_.setReuse(false);
|
|
|
|
this->client_.end();
|
|
|
|
}
|
|
|
|
this->client_.setReuse(true);
|
|
|
|
}
|
|
|
|
|
2021-03-22 04:26:10 +01:00
|
|
|
void HttpRequestComponent::send(const std::vector<HttpRequestResponseTrigger *> &response_triggers) {
|
2021-10-12 00:11:04 +02:00
|
|
|
if (!network::is_connected()) {
|
|
|
|
this->client_.end();
|
|
|
|
this->status_set_warning();
|
|
|
|
ESP_LOGW(TAG, "HTTP Request failed; Not connected to network");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-09 18:37:52 +01:00
|
|
|
bool begin_status = false;
|
2020-07-21 22:26:21 +02:00
|
|
|
const String url = this->url_.c_str();
|
2022-01-25 09:53:22 +01:00
|
|
|
#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_);
|
2021-09-13 18:55:04 +02:00
|
|
|
#endif
|
2022-01-25 09:53:22 +01:00
|
|
|
this->client_.setRedirectLimit(this->redirect_limit_);
|
2019-11-09 18:37:52 +01:00
|
|
|
#endif
|
2022-01-25 09:53:22 +01:00
|
|
|
#if defined(USE_ESP32)
|
|
|
|
begin_status = this->client_.begin(url);
|
|
|
|
#elif defined(USE_ESP8266)
|
2021-09-13 18:55:04 +02:00
|
|
|
begin_status = this->client_.begin(*this->get_wifi_client_(), url);
|
2019-11-09 18:37:52 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!begin_status) {
|
|
|
|
this->client_.end();
|
|
|
|
this->status_set_warning();
|
|
|
|
ESP_LOGW(TAG, "HTTP Request failed at the begin phase. Please check the configuration");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->client_.setTimeout(this->timeout_);
|
2023-01-10 05:40:42 +01:00
|
|
|
#if defined(USE_ESP32)
|
|
|
|
this->client_.setConnectTimeout(this->timeout_);
|
|
|
|
#endif
|
2019-11-09 18:37:52 +01:00
|
|
|
if (this->useragent_ != nullptr) {
|
|
|
|
this->client_.setUserAgent(this->useragent_);
|
|
|
|
}
|
|
|
|
for (const auto &header : this->headers_) {
|
|
|
|
this->client_.addHeader(header.name, header.value, false, true);
|
|
|
|
}
|
|
|
|
|
2023-01-10 05:40:42 +01:00
|
|
|
uint32_t start_time = millis();
|
2019-11-09 18:37:52 +01:00
|
|
|
int http_code = this->client_.sendRequest(this->method_, this->body_.c_str());
|
2023-01-10 05:40:42 +01:00
|
|
|
uint32_t duration = millis() - start_time;
|
2021-03-22 04:26:10 +01:00
|
|
|
for (auto *trigger : response_triggers)
|
2023-01-10 05:40:42 +01:00
|
|
|
trigger->process(http_code, duration);
|
2021-03-22 04:26:10 +01:00
|
|
|
|
2019-11-09 18:37:52 +01:00
|
|
|
if (http_code < 0) {
|
2023-01-10 05:40:42 +01:00
|
|
|
ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s; Duration: %u ms", this->url_.c_str(),
|
|
|
|
HTTPClient::errorToString(http_code).c_str(), duration);
|
2019-11-09 18:37:52 +01:00
|
|
|
this->status_set_warning();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (http_code < 200 || http_code >= 300) {
|
2023-01-10 05:40:42 +01:00
|
|
|
ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Code: %d; Duration: %u ms", this->url_.c_str(), http_code, duration);
|
2019-11-09 18:37:52 +01:00
|
|
|
this->status_set_warning();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->status_clear_warning();
|
2023-01-10 05:40:42 +01:00
|
|
|
ESP_LOGD(TAG, "HTTP Request completed; URL: %s; Code: %d; Duration: %u ms", this->url_.c_str(), http_code, duration);
|
2019-11-09 18:37:52 +01:00
|
|
|
}
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_ESP8266
|
2021-09-13 11:31:02 +02:00
|
|
|
std::shared_ptr<WiFiClient> HttpRequestComponent::get_wifi_client_() {
|
2021-09-19 18:46:17 +02:00
|
|
|
#ifdef USE_HTTP_REQUEST_ESP8266_HTTPS
|
2020-03-12 01:27:05 +01:00
|
|
|
if (this->secure_) {
|
|
|
|
if (this->wifi_client_secure_ == nullptr) {
|
2021-09-13 11:31:02 +02:00
|
|
|
this->wifi_client_secure_ = std::make_shared<BearSSL::WiFiClientSecure>();
|
2020-03-12 01:27:05 +01:00
|
|
|
this->wifi_client_secure_->setInsecure();
|
|
|
|
this->wifi_client_secure_->setBufferSizes(512, 512);
|
|
|
|
}
|
|
|
|
return this->wifi_client_secure_;
|
|
|
|
}
|
2021-09-19 18:46:17 +02:00
|
|
|
#endif
|
2020-03-12 01:27:05 +01:00
|
|
|
|
|
|
|
if (this->wifi_client_ == nullptr) {
|
2021-09-13 11:31:02 +02:00
|
|
|
this->wifi_client_ = std::make_shared<WiFiClient>();
|
2020-03-12 01:27:05 +01:00
|
|
|
}
|
|
|
|
return this->wifi_client_;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-03 19:37:00 +01:00
|
|
|
void HttpRequestComponent::close() {
|
|
|
|
this->last_url_ = this->url_;
|
|
|
|
this->client_.end();
|
|
|
|
}
|
2020-03-12 01:27:05 +01:00
|
|
|
|
2020-05-01 04:05:11 +02:00
|
|
|
const char *HttpRequestComponent::get_string() {
|
2022-02-17 05:03:54 +01:00
|
|
|
#if defined(ESP32)
|
|
|
|
// The static variable is here because HTTPClient::getString() returns a String on ESP32,
|
|
|
|
// and we need something to keep a buffer alive.
|
|
|
|
static String str;
|
|
|
|
#else
|
|
|
|
// However on ESP8266, HTTPClient::getString() returns a String& to a member variable.
|
|
|
|
// Leaving this the default so that any new platform either doesn't copy, or encounters a compilation error.
|
|
|
|
auto &
|
|
|
|
#endif
|
|
|
|
str = this->client_.getString();
|
2022-01-03 19:40:05 +01:00
|
|
|
return str.c_str();
|
2020-05-01 04:05:11 +02:00
|
|
|
}
|
2020-03-12 01:27:05 +01:00
|
|
|
|
2019-11-09 18:37:52 +01:00
|
|
|
} // namespace http_request
|
|
|
|
} // namespace esphome
|
2021-09-20 11:47:51 +02:00
|
|
|
|
|
|
|
#endif // USE_ARDUINO
|