ESPHome  2024.7.1
http_request_arduino.cpp
Go to the documentation of this file.
1 #include "http_request_arduino.h"
2 
3 #ifdef USE_ARDUINO
4 
7 #include "esphome/core/defines.h"
8 #include "esphome/core/log.h"
9 
10 #include "watchdog.h"
11 
12 namespace esphome {
13 namespace http_request {
14 
15 static const char *const TAG = "http_request.arduino";
16 
17 std::shared_ptr<HttpContainer> HttpRequestArduino::start(std::string url, std::string method, std::string body,
18  std::list<Header> headers) {
19  if (!network::is_connected()) {
20  this->status_momentary_error("failed", 1000);
21  ESP_LOGW(TAG, "HTTP Request failed; Not connected to network");
22  return nullptr;
23  }
24 
25  std::shared_ptr<HttpContainerArduino> container = std::make_shared<HttpContainerArduino>();
26  container->set_parent(this);
27 
28  const uint32_t start = millis();
29 
30  bool secure = url.find("https:") != std::string::npos;
31  container->set_secure(secure);
32 
34 
35  if (this->follow_redirects_) {
36  container->client_.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
37  container->client_.setRedirectLimit(this->redirect_limit_);
38  } else {
39  container->client_.setFollowRedirects(HTTPC_DISABLE_FOLLOW_REDIRECTS);
40  }
41 
42 #if defined(USE_ESP8266)
43  std::unique_ptr<WiFiClient> stream_ptr;
44 #ifdef USE_HTTP_REQUEST_ESP8266_HTTPS
45  if (secure) {
46  ESP_LOGV(TAG, "ESP8266 HTTPS connection with WiFiClientSecure");
47  stream_ptr = std::make_unique<WiFiClientSecure>();
48  WiFiClientSecure *secure_client = static_cast<WiFiClientSecure *>(stream_ptr.get());
49  secure_client->setBufferSizes(512, 512);
50  secure_client->setInsecure();
51  } else {
52  stream_ptr = std::make_unique<WiFiClient>();
53  }
54 #else
55  ESP_LOGV(TAG, "ESP8266 HTTP connection with WiFiClient");
56  if (secure) {
57  ESP_LOGE(TAG, "Can't use HTTPS connection with esp8266_disable_ssl_support");
58  return nullptr;
59  }
60  stream_ptr = std::make_unique<WiFiClient>();
61 #endif // USE_HTTP_REQUEST_ESP8266_HTTPS
62 
63 #if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(3, 1, 0) // && USE_ARDUINO_VERSION_CODE < VERSION_CODE(?, ?, ?)
64  if (!secure) {
65  ESP_LOGW(TAG, "Using HTTP on Arduino version >= 3.1 is **very** slow. Consider setting framework version to 3.0.2 "
66  "in your YAML, or use HTTPS");
67  }
68 #endif // USE_ARDUINO_VERSION_CODE
69  bool status = container->client_.begin(*stream_ptr, url.c_str());
70 
71 #elif defined(USE_RP2040)
72  if (secure) {
73  container->client_.setInsecure();
74  }
75  bool status = container->client_.begin(url.c_str());
76 #elif defined(USE_ESP32)
77  bool status = container->client_.begin(url.c_str());
78 #endif
79 
80  App.feed_wdt();
81 
82  if (!status) {
83  ESP_LOGW(TAG, "HTTP Request failed; URL: %s", url.c_str());
84  container->end();
85  this->status_momentary_error("failed", 1000);
86  return nullptr;
87  }
88 
89  container->client_.setReuse(true);
90  container->client_.setTimeout(this->timeout_);
91 #if defined(USE_ESP32)
92  container->client_.setConnectTimeout(this->timeout_);
93 #endif
94 
95  if (this->useragent_ != nullptr) {
96  container->client_.setUserAgent(this->useragent_);
97  }
98  for (const auto &header : headers) {
99  container->client_.addHeader(header.name, header.value, false, true);
100  }
101 
102  // returned needed headers must be collected before the requests
103  static const char *header_keys[] = {"Content-Length", "Content-Type"};
104  static const size_t HEADER_COUNT = sizeof(header_keys) / sizeof(header_keys[0]);
105  container->client_.collectHeaders(header_keys, HEADER_COUNT);
106 
107  container->status_code = container->client_.sendRequest(method.c_str(), body.c_str());
108  if (container->status_code < 0) {
109  ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s", url.c_str(),
110  HTTPClient::errorToString(container->status_code).c_str());
111  this->status_momentary_error("failed", 1000);
112  container->end();
113  return nullptr;
114  }
115 
116  if (container->status_code < 200 || container->status_code >= 300) {
117  ESP_LOGE(TAG, "HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
118  this->status_momentary_error("failed", 1000);
119  container->end();
120  return nullptr;
121  }
122 
123  int content_length = container->client_.getSize();
124  ESP_LOGD(TAG, "Content-Length: %d", content_length);
125  container->content_length = (size_t) content_length;
126  container->duration_ms = millis() - start;
127 
128  return container;
129 }
130 
131 int HttpContainerArduino::read(uint8_t *buf, size_t max_len) {
132  const uint32_t start = millis();
133  watchdog::WatchdogManager wdm(this->parent_->get_watchdog_timeout());
134 
135  WiFiClient *stream_ptr = this->client_.getStreamPtr();
136  if (stream_ptr == nullptr) {
137  ESP_LOGE(TAG, "Stream pointer vanished!");
138  return -1;
139  }
140 
141  int available_data = stream_ptr->available();
142  int bufsize = std::min(max_len, std::min(this->content_length - this->bytes_read_, (size_t) available_data));
143 
144  if (bufsize == 0) {
145  this->duration_ms += (millis() - start);
146  return 0;
147  }
148 
149  App.feed_wdt();
150  int read_len = stream_ptr->readBytes(buf, bufsize);
151  this->bytes_read_ += read_len;
152 
153  this->duration_ms += (millis() - start);
154 
155  return read_len;
156 }
157 
159  watchdog::WatchdogManager wdm(this->parent_->get_watchdog_timeout());
160  this->client_.end();
161 }
162 
163 } // namespace http_request
164 } // namespace esphome
165 
166 #endif // USE_ARDUINO
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition: util.cpp:15
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
void status_momentary_error(const std::string &name, uint32_t length=5000)
Definition: component.cpp:182
int read(uint8_t *buf, size_t max_len) override
Application App
Global storage of Application pointer - only one Application can exist.
std::shared_ptr< HttpContainer > start(std::string url, std::string method, std::string body, std::list< Header > headers) override
uint8_t status
Definition: bl0942.h:23
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7