ESPHome 2026.3.0
Loading...
Searching...
No Matches
http_request_arduino.cpp
Go to the documentation of this file.
2
3#if defined(USE_ARDUINO) && !defined(USE_ESP32)
4
7
10#include "esphome/core/log.h"
11
12// Include BearSSL error constants for TLS failure diagnostics
13#ifdef USE_ESP8266
14#include <bearssl/bearssl_ssl.h>
15#endif
16
17namespace esphome::http_request {
18
19static const char *const TAG = "http_request.arduino";
20#ifdef USE_ESP8266
21// ESP8266 Arduino core (WiFiClientSecureBearSSL.cpp) returns -1000 on OOM
22static constexpr int ESP8266_SSL_ERR_OOM = -1000;
23#endif
24
25std::shared_ptr<HttpContainer> HttpRequestArduino::perform(const std::string &url, const std::string &method,
26 const std::string &body,
27 const std::vector<Header> &request_headers,
28 const std::vector<std::string> &lower_case_collect_headers) {
29 if (!network::is_connected()) {
30 this->status_momentary_error("failed", 1000);
31 ESP_LOGW(TAG, "HTTP Request failed; Not connected to network");
32 return nullptr;
33 }
34
35 std::shared_ptr<HttpContainerArduino> container = std::make_shared<HttpContainerArduino>();
36 container->set_parent(this);
37
38 const uint32_t start = millis();
39
40 bool secure = url.find("https:") != std::string::npos;
41 container->set_secure(secure);
42
44
45 if (this->follow_redirects_) {
46 container->client_.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
47 container->client_.setRedirectLimit(this->redirect_limit_);
48 } else {
49 container->client_.setFollowRedirects(HTTPC_DISABLE_FOLLOW_REDIRECTS);
50 }
51
52#if defined(USE_ESP8266)
53 std::unique_ptr<WiFiClient> stream_ptr;
54#ifdef USE_HTTP_REQUEST_ESP8266_HTTPS
55 if (secure) {
56 ESP_LOGV(TAG, "ESP8266 HTTPS connection with WiFiClientSecure");
57 stream_ptr = std::make_unique<WiFiClientSecure>();
58 WiFiClientSecure *secure_client = static_cast<WiFiClientSecure *>(stream_ptr.get());
59 secure_client->setBufferSizes(this->tls_buffer_size_rx_, this->tls_buffer_size_tx_);
60 secure_client->setInsecure();
61 } else {
62 stream_ptr = std::make_unique<WiFiClient>();
63 }
64#else
65 ESP_LOGV(TAG, "ESP8266 HTTP connection with WiFiClient");
66 if (secure) {
67 ESP_LOGE(TAG, "Can't use HTTPS connection with esp8266_disable_ssl_support");
68 return nullptr;
69 }
70 stream_ptr = std::make_unique<WiFiClient>();
71#endif // USE_HTTP_REQUEST_ESP8266_HTTPS
72
73#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(3, 1, 0) // && USE_ARDUINO_VERSION_CODE < VERSION_CODE(?, ?, ?)
74 if (!secure) {
75 ESP_LOGW(TAG, "Using HTTP on Arduino version >= 3.1 is **very** slow. Consider setting framework version to 3.0.2 "
76 "in your YAML, or use HTTPS");
77 }
78#endif // USE_ARDUINO_VERSION_CODE
79 bool status = container->client_.begin(*stream_ptr, url.c_str());
80
81#elif defined(USE_RP2040)
82 if (secure) {
83 container->client_.setInsecure();
84 }
85 bool status = container->client_.begin(url.c_str());
86#endif
87
88 App.feed_wdt();
89
90 if (!status) {
91 ESP_LOGW(TAG, "HTTP Request failed; URL: %s", url.c_str());
92 container->end();
93 this->status_momentary_error("failed", 1000);
94 return nullptr;
95 }
96
97 container->client_.setReuse(true);
98 container->client_.setTimeout(this->timeout_);
99
100 if (this->useragent_ != nullptr) {
101 container->client_.setUserAgent(this->useragent_);
102 }
103 for (const auto &header : request_headers) {
104 container->client_.addHeader(header.name.c_str(), header.value.c_str(), false, true);
105 }
106
107 // returned needed headers must be collected before the requests
108 const char *header_keys[lower_case_collect_headers.size()];
109 int index = 0;
110 for (auto const &header_name : lower_case_collect_headers) {
111 header_keys[index++] = header_name.c_str();
112 }
113 container->client_.collectHeaders(header_keys, index);
114
115 App.feed_wdt();
116 container->status_code = container->client_.sendRequest(method.c_str(), body.c_str());
117 App.feed_wdt();
118 if (container->status_code < 0) {
119#if defined(USE_ESP8266) && defined(USE_HTTP_REQUEST_ESP8266_HTTPS)
120 if (secure) {
121 WiFiClientSecure *secure_client = static_cast<WiFiClientSecure *>(stream_ptr.get());
122 int last_error = secure_client->getLastSSLError();
123
124 if (last_error != 0) {
125 const LogString *error_msg;
126 switch (last_error) {
127 case ESP8266_SSL_ERR_OOM:
128 error_msg = LOG_STR("Unable to allocate buffer memory");
129 break;
130 case BR_ERR_TOO_LARGE:
131 error_msg = LOG_STR("Incoming TLS record does not fit in receive buffer (BR_ERR_TOO_LARGE)");
132 break;
133 default:
134 error_msg = LOG_STR("Unknown SSL error");
135 break;
136 }
137 ESP_LOGW(TAG, "SSL failure: %s (Code: %d)", LOG_STR_ARG(error_msg), last_error);
138 if (last_error == ESP8266_SSL_ERR_OOM) {
139 ESP_LOGW(TAG, "Configured TLS buffer sizes: %u/%u bytes, check max free heap block using the debug component",
140 (unsigned int) this->tls_buffer_size_rx_, (unsigned int) this->tls_buffer_size_tx_);
141 }
142 } else {
143 ESP_LOGW(TAG, "Connection failure with no error code");
144 }
145 }
146#endif
147
148 ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s", url.c_str(),
149 HTTPClient::errorToString(container->status_code).c_str());
150
151 this->status_momentary_error("failed", 1000);
152 container->end();
153 return nullptr;
154 }
155 if (!is_success(container->status_code)) {
156 ESP_LOGE(TAG, "HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
157 this->status_momentary_error("failed", 1000);
158 // Still return the container, so it can be used to get the status code and error message
159 }
160
161 container->response_headers_.clear();
162 auto header_count = container->client_.headers();
163 for (int i = 0; i < header_count; i++) {
164 const std::string header_name = str_lower_case(container->client_.headerName(i).c_str());
165 if (should_collect_header(lower_case_collect_headers, header_name)) {
166 std::string header_value = container->client_.header(i).c_str();
167 ESP_LOGD(TAG, "Received response header, name: %s, value: %s", header_name.c_str(), header_value.c_str());
168 container->response_headers_.push_back({header_name, header_value});
169 }
170 }
171
172 // HTTPClient::getSize() returns -1 for chunked transfer encoding (no Content-Length).
173 // When cast to size_t, -1 becomes SIZE_MAX (4294967295 on 32-bit).
174 // The read() method uses a chunked transfer encoding decoder (read_chunked_) to strip
175 // chunk framing and deliver only decoded content. When the final 0-size chunk is received,
176 // is_chunked_ is cleared and content_length is set to the actual decoded size, so
177 // is_read_complete() returns true and callers exit their read loops correctly.
178 int content_length = container->client_.getSize();
179 ESP_LOGD(TAG, "Content-Length: %d", content_length);
180 container->content_length = (size_t) content_length;
181 // -1 (SIZE_MAX when cast to size_t) means chunked transfer encoding
182 container->set_chunked(content_length == -1);
183 container->duration_ms = millis() - start;
184
185 return container;
186}
187
188// Arduino HTTP read implementation
189//
190// WARNING: Return values differ from BSD sockets! See http_request.h for full documentation.
191//
192// Arduino's WiFiClient is inherently non-blocking - available() returns 0 when
193// no data is ready. We use connected() to distinguish "no data yet" from
194// "connection closed".
195//
196// WiFiClient behavior:
197// available() > 0: data ready to read
198// available() == 0 && connected(): no data yet, still connected
199// available() == 0 && !connected(): connection closed
200//
201// We normalize to HttpContainer::read() contract (NOT BSD socket semantics!):
202// > 0: bytes read
203// 0: no data yet, retry <-- NOTE: 0 means retry, NOT EOF!
204// < 0: error/connection closed <-- connection closed returns -1, not 0
205//
206// For chunked transfer encoding, read_chunked_() decodes chunk framing and delivers
207// only the payload data. When the final 0-size chunk is received, it clears is_chunked_
208// and sets content_length = bytes_read_ so is_read_complete() returns true.
209int HttpContainerArduino::read(uint8_t *buf, size_t max_len) {
210 const uint32_t start = millis();
211 watchdog::WatchdogManager wdm(this->parent_->get_watchdog_timeout());
212
213 WiFiClient *stream_ptr = this->client_.getStreamPtr();
214 if (stream_ptr == nullptr) {
215 ESP_LOGE(TAG, "Stream pointer vanished!");
216 return HTTP_ERROR_CONNECTION_CLOSED;
217 }
218
219 if (this->is_chunked_) {
220 int result = this->read_chunked_(buf, max_len, stream_ptr);
221 this->duration_ms += (millis() - start);
222 if (result > 0) {
223 return result;
224 }
225 // result <= 0: check for completion or errors
226 if (this->is_read_complete()) {
227 return 0; // Chunked transfer complete (final 0-size chunk received)
228 }
229 if (result < 0) {
230 return result; // Stream error during chunk decoding
231 }
232 // read_chunked_ returned 0: no data was available (available() was 0).
233 // This happens when the TCP buffer is empty - either more data is in flight,
234 // or the connection dropped. Arduino's connected() returns false only when
235 // both the remote has closed AND the receive buffer is empty, so any buffered
236 // data is fully drained before we report the drop.
237 if (!stream_ptr->connected()) {
238 return HTTP_ERROR_CONNECTION_CLOSED;
239 }
240 return 0; // No data yet, caller should retry
241 }
242
243 // Non-chunked path
244 int available_data = stream_ptr->available();
245 size_t remaining = (this->content_length > 0) ? (this->content_length - this->bytes_read_) : max_len;
246 int bufsize = std::min(max_len, std::min(remaining, (size_t) available_data));
247
248 if (bufsize == 0) {
249 this->duration_ms += (millis() - start);
250 if (this->is_read_complete()) {
251 return 0; // All content read successfully
252 }
253 if (!stream_ptr->connected()) {
254 return HTTP_ERROR_CONNECTION_CLOSED;
255 }
256 return 0; // No data yet, caller should retry
257 }
258
259 App.feed_wdt();
260 int read_len = stream_ptr->readBytes(buf, bufsize);
261 this->bytes_read_ += read_len;
262
263 this->duration_ms += (millis() - start);
264
265 return read_len;
266}
267
269 if (this->chunk_remaining_ == 0) {
271 this->chunk_remaining_ = 1; // repurpose as at-start-of-line flag
272 } else {
274 }
275}
276
277// Chunked transfer encoding decoder
278//
279// On Arduino, getStreamPtr() returns raw TCP data. For chunked responses, this includes
280// chunk framing (size headers, CRLF delimiters) mixed with payload data. This decoder
281// strips the framing and delivers only decoded content to the caller.
282//
283// Chunk format (RFC 9112 Section 7.1):
284// <hex-size>[;extension]\r\n
285// <data bytes>\r\n
286// ...
287// 0\r\n
288// [trailer-field\r\n]*
289// \r\n
290//
291// Non-blocking: only processes bytes already in the TCP receive buffer.
292// State (chunk_state_, chunk_remaining_) is preserved between calls, so partial
293// chunk headers or split \r\n sequences resume correctly on the next call.
294// Framing bytes (hex sizes, \r\n) may be consumed without producing output;
295// the caller sees 0 and retries via the normal read timeout logic.
296//
297// WiFiClient::read() returns -1 on error despite available() > 0 (connection reset
298// between check and read). On any stream error (c < 0 or readBytes <= 0), we return
299// already-decoded data if any; otherwise HTTP_ERROR_CONNECTION_CLOSED. The error
300// will surface again on the next call since the stream stays broken.
301//
302// Returns: > 0 decoded bytes, 0 no data available, < 0 error
303int HttpContainerArduino::read_chunked_(uint8_t *buf, size_t max_len, WiFiClient *stream) {
304 int total_decoded = 0;
305
306 while (total_decoded < (int) max_len && this->chunk_state_ != ChunkedState::COMPLETE) {
307 // Non-blocking: only process what's already buffered
308 if (stream->available() == 0)
309 break;
310
311 // CHUNK_DATA reads multiple bytes; handle before the single-byte switch
313 // Only read what's available, what fits in buf, and what remains in this chunk
314 size_t to_read =
315 std::min({max_len - (size_t) total_decoded, this->chunk_remaining_, (size_t) stream->available()});
316 if (to_read == 0)
317 break;
318 App.feed_wdt();
319 int read_len = stream->readBytes(buf + total_decoded, to_read);
320 if (read_len <= 0)
321 return total_decoded > 0 ? total_decoded : HTTP_ERROR_CONNECTION_CLOSED;
322 total_decoded += read_len;
323 this->chunk_remaining_ -= read_len;
324 this->bytes_read_ += read_len;
325 if (this->chunk_remaining_ == 0)
327 continue;
328 }
329
330 // All other states consume a single byte
331 int c = stream->read();
332 if (c < 0)
333 return total_decoded > 0 ? total_decoded : HTTP_ERROR_CONNECTION_CLOSED;
334
335 switch (this->chunk_state_) {
336 // Parse hex chunk size, one byte at a time: "<hex>[;ext]\r\n"
337 // Note: if no hex digits are parsed (e.g., bare \r\n), chunk_remaining_ stays 0
338 // and is treated as the final chunk. This is intentionally lenient — on embedded
339 // devices, rejecting malformed framing is less useful than terminating cleanly.
340 // Overflow of chunk_remaining_ from extremely long hex strings (>8 digits on
341 // 32-bit) is not checked; >4GB chunks are unrealistic on embedded targets and
342 // would simply cause fewer bytes to be read from that chunk.
344 if (c == '\n') {
345 // \n terminates the size line; chunk_remaining_ == 0 means last chunk
347 } else {
348 uint8_t hex = parse_hex_char(c);
349 if (hex != INVALID_HEX_CHAR) {
350 this->chunk_remaining_ = (this->chunk_remaining_ << 4) | hex;
351 } else if (c != '\r') {
352 this->chunk_state_ = ChunkedState::CHUNK_HEADER_EXT; // ';' starts extension, skip to \n
353 }
354 }
355 break;
356
357 // Skip chunk extension bytes until \n (e.g., ";name=value\r\n")
359 if (c == '\n') {
361 }
362 break;
363
364 // Consume \r\n trailing each chunk's data
366 if (c == '\n') {
368 this->chunk_remaining_ = 0; // reset for next chunk's hex accumulation
369 }
370 // else: \r is consumed silently, next iteration gets \n
371 break;
372
373 // Consume optional trailer headers and terminating empty line after final chunk.
374 // Per RFC 9112 Section 7.1: "0\r\n" is followed by optional "field\r\n" lines
375 // and a final "\r\n". chunk_remaining_ is repurposed as a flag: 1 = at start
376 // of line (may be the empty terminator), 0 = mid-line (reading a trailer field).
378 if (c == '\n') {
379 if (this->chunk_remaining_ != 0) {
380 this->chunk_state_ = ChunkedState::COMPLETE; // Empty line terminates trailers
381 } else {
382 this->chunk_remaining_ = 1; // End of trailer field, at start of next line
383 }
384 } else if (c != '\r') {
385 this->chunk_remaining_ = 0; // Non-CRLF char: reading a trailer field
386 }
387 // \r doesn't change the flag — it's part of \r\n line endings
388 break;
389
390 default:
391 break;
392 }
393
395 // Clear chunked flag and set content_length to actual decoded size so
396 // is_read_complete() returns true and callers exit their read loops
397 this->is_chunked_ = false;
398 this->content_length = this->bytes_read_;
399 }
400 }
401
402 return total_decoded;
403}
404
406 watchdog::WatchdogManager wdm(this->parent_->get_watchdog_timeout());
407 this->client_.end();
408}
409
410} // namespace esphome::http_request
411
412#endif // USE_ARDUINO && !USE_ESP32
uint8_t status
Definition bl0942.h:8
void feed_wdt(uint32_t time=0)
void status_momentary_error(const char *name, uint32_t length=5000)
Set error status flag and automatically clear it after a timeout.
int read_chunked_(uint8_t *buf, size_t max_len, WiFiClient *stream)
Decode chunked transfer encoding from the raw stream.
size_t chunk_remaining_
Bytes remaining in current chunk.
int read(uint8_t *buf, size_t max_len) override
void chunk_header_complete_()
Transition from chunk header to data or trailer based on parsed size.
virtual bool is_read_complete() const
Check if all expected content has been read.
bool is_chunked_
True if response uses chunked transfer encoding.
std::shared_ptr< HttpContainer > perform(const std::string &url, const std::string &method, const std::string &body, const std::vector< Header > &request_headers, const std::vector< std::string > &lower_case_collect_headers) override
std::shared_ptr< HttpContainer > start(const std::string &url, const std::string &method, const std::string &body, const std::vector< Header > &request_headers)
bool should_collect_header(const std::vector< std::string > &lower_case_collect_headers, const std::string &lower_header_name)
Check if a header name should be collected (linear scan, fine for small lists)
bool is_success(int const status)
Checks if the given HTTP status code indicates a successful request.
@ CHUNK_DATA
Reading chunk data bytes.
@ COMPLETE
Finished: final chunk and trailers consumed.
@ CHUNK_HEADER
Reading hex digits of chunk size.
@ CHUNK_HEADER_EXT
Skipping chunk extensions until .
@ CHUNK_DATA_TRAIL
Skipping \r after chunk data.
@ CHUNK_TRAILER
Consuming trailer headers after final 0-size chunk.
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.h:27
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
Definition helpers.cpp:201
constexpr uint8_t parse_hex_char(char c)
Definition helpers.h:1102
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t