ESPHome 2026.6.0-dev
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 bool status = container->client_.begin(*stream_ptr, url.c_str());
74
75#elif defined(USE_RP2040)
76 if (secure) {
77 container->client_.setInsecure();
78 }
79 bool status = container->client_.begin(url.c_str());
80#endif
81
82 App.feed_wdt();
83
84 if (!status) {
85 ESP_LOGW(TAG, "HTTP Request failed; URL: %s", url.c_str());
86 container->end();
87 this->status_momentary_error("failed", 1000);
88 return nullptr;
89 }
90
91 container->client_.setReuse(true);
92 container->client_.setTimeout(this->timeout_);
93
94 if (this->useragent_ != nullptr) {
95 container->client_.setUserAgent(this->useragent_);
96 }
97 for (const auto &header : request_headers) {
98 container->client_.addHeader(header.name.c_str(), header.value.c_str(), false, true);
99 }
100
101 // returned needed headers must be collected before the requests
102 const char *header_keys[lower_case_collect_headers.size()];
103 int index = 0;
104 for (auto const &header_name : lower_case_collect_headers) {
105 header_keys[index++] = header_name.c_str();
106 }
107 container->client_.collectHeaders(header_keys, index);
108
109 App.feed_wdt();
110 container->status_code = container->client_.sendRequest(method.c_str(), body.c_str());
111 App.feed_wdt();
112 if (container->status_code < 0) {
113#if defined(USE_ESP8266) && defined(USE_HTTP_REQUEST_ESP8266_HTTPS)
114 if (secure) {
115 WiFiClientSecure *secure_client = static_cast<WiFiClientSecure *>(stream_ptr.get());
116 int last_error = secure_client->getLastSSLError();
117
118 if (last_error != 0) {
119 const LogString *error_msg;
120 switch (last_error) {
121 case ESP8266_SSL_ERR_OOM:
122 error_msg = LOG_STR("Unable to allocate buffer memory");
123 break;
124 case BR_ERR_TOO_LARGE:
125 error_msg = LOG_STR("Incoming TLS record does not fit in receive buffer (BR_ERR_TOO_LARGE)");
126 break;
127 default:
128 error_msg = LOG_STR("Unknown SSL error");
129 break;
130 }
131 ESP_LOGW(TAG, "SSL failure: %s (Code: %d)", LOG_STR_ARG(error_msg), last_error);
132 if (last_error == ESP8266_SSL_ERR_OOM) {
133 ESP_LOGW(TAG, "Configured TLS buffer sizes: %u/%u bytes, check max free heap block using the debug component",
134 (unsigned int) this->tls_buffer_size_rx_, (unsigned int) this->tls_buffer_size_tx_);
135 }
136 } else {
137 ESP_LOGW(TAG, "Connection failure with no error code");
138 }
139 }
140#endif
141
142 ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s", url.c_str(),
143 HTTPClient::errorToString(container->status_code).c_str());
144
145 this->status_momentary_error("failed", 1000);
146 container->end();
147 return nullptr;
148 }
149 if (!is_success(container->status_code)) {
150 ESP_LOGE(TAG, "HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
151 this->status_momentary_error("failed", 1000);
152 // Still return the container, so it can be used to get the status code and error message
153 }
154
155 container->response_headers_.clear();
156 auto header_count = container->client_.headers();
157 for (int i = 0; i < header_count; i++) {
158 const std::string header_name = str_lower_case(container->client_.headerName(i).c_str()); // NOLINT
159 if (should_collect_header(lower_case_collect_headers, header_name)) {
160 std::string header_value = container->client_.header(i).c_str();
161 ESP_LOGD(TAG, "Received response header, name: %s, value: %s", header_name.c_str(), header_value.c_str());
162 container->response_headers_.push_back({header_name, header_value});
163 }
164 }
165
166 // HTTPClient::getSize() returns -1 for chunked transfer encoding (no Content-Length).
167 // When cast to size_t, -1 becomes SIZE_MAX (4294967295 on 32-bit).
168 // The read() method uses a chunked transfer encoding decoder (read_chunked_) to strip
169 // chunk framing and deliver only decoded content. When the final 0-size chunk is received,
170 // is_chunked_ is cleared and content_length is set to the actual decoded size, so
171 // is_read_complete() returns true and callers exit their read loops correctly.
172 int content_length = container->client_.getSize();
173 ESP_LOGD(TAG, "Content-Length: %d", content_length);
174 container->content_length = (size_t) content_length;
175 // -1 (SIZE_MAX when cast to size_t) means chunked transfer encoding
176 container->set_chunked(content_length == -1);
177 container->duration_ms = millis() - start;
178
179 return container;
180}
181
182// Arduino HTTP read implementation
183//
184// WARNING: Return values differ from BSD sockets! See http_request.h for full documentation.
185//
186// Arduino's WiFiClient is inherently non-blocking - available() returns 0 when
187// no data is ready. We use connected() to distinguish "no data yet" from
188// "connection closed".
189//
190// WiFiClient behavior:
191// available() > 0: data ready to read
192// available() == 0 && connected(): no data yet, still connected
193// available() == 0 && !connected(): connection closed
194//
195// We normalize to HttpContainer::read() contract (NOT BSD socket semantics!):
196// > 0: bytes read
197// 0: no data yet, retry <-- NOTE: 0 means retry, NOT EOF!
198// < 0: error/connection closed <-- connection closed returns -1, not 0
199//
200// For chunked transfer encoding, read_chunked_() decodes chunk framing and delivers
201// only the payload data. When the final 0-size chunk is received, it clears is_chunked_
202// and sets content_length = bytes_read_ so is_read_complete() returns true.
203int HttpContainerArduino::read(uint8_t *buf, size_t max_len) {
204 const uint32_t start = millis();
205 watchdog::WatchdogManager wdm(this->parent_->get_watchdog_timeout());
206
207 WiFiClient *stream_ptr = this->client_.getStreamPtr();
208 if (stream_ptr == nullptr) {
209 ESP_LOGE(TAG, "Stream pointer vanished!");
210 return HTTP_ERROR_CONNECTION_CLOSED;
211 }
212
213 if (this->is_chunked_) {
214 int result = this->read_chunked_(buf, max_len, stream_ptr);
215 this->duration_ms += (millis() - start);
216 if (result > 0) {
217 return result;
218 }
219 // result <= 0: check for completion or errors
220 if (this->is_read_complete()) {
221 return 0; // Chunked transfer complete (final 0-size chunk received)
222 }
223 if (result < 0) {
224 return result; // Stream error during chunk decoding
225 }
226 // read_chunked_ returned 0: no data was available (available() was 0).
227 // This happens when the TCP buffer is empty - either more data is in flight,
228 // or the connection dropped. Arduino's connected() returns false only when
229 // both the remote has closed AND the receive buffer is empty, so any buffered
230 // data is fully drained before we report the drop.
231 if (!stream_ptr->connected()) {
232 return HTTP_ERROR_CONNECTION_CLOSED;
233 }
234 return 0; // No data yet, caller should retry
235 }
236
237 // Non-chunked path
238 int available_data = stream_ptr->available();
239 size_t remaining = (this->content_length > 0) ? (this->content_length - this->bytes_read_) : max_len;
240 int bufsize = std::min({max_len, remaining, (size_t) available_data});
241
242 if (bufsize == 0) {
243 this->duration_ms += (millis() - start);
244 if (this->is_read_complete()) {
245 return 0; // All content read successfully
246 }
247 if (!stream_ptr->connected()) {
248 return HTTP_ERROR_CONNECTION_CLOSED;
249 }
250 return 0; // No data yet, caller should retry
251 }
252
253 App.feed_wdt();
254 int read_len = stream_ptr->readBytes(buf, bufsize);
255 this->bytes_read_ += read_len;
256
257 this->duration_ms += (millis() - start);
258
259 return read_len;
260}
261
263 if (this->chunk_remaining_ == 0) {
265 this->chunk_remaining_ = 1; // repurpose as at-start-of-line flag
266 } else {
268 }
269}
270
271// Chunked transfer encoding decoder
272//
273// On Arduino, getStreamPtr() returns raw TCP data. For chunked responses, this includes
274// chunk framing (size headers, CRLF delimiters) mixed with payload data. This decoder
275// strips the framing and delivers only decoded content to the caller.
276//
277// Chunk format (RFC 9112 Section 7.1):
278// <hex-size>[;extension]\r\n
279// <data bytes>\r\n
280// ...
281// 0\r\n
282// [trailer-field\r\n]*
283// \r\n
284//
285// Non-blocking: only processes bytes already in the TCP receive buffer.
286// State (chunk_state_, chunk_remaining_) is preserved between calls, so partial
287// chunk headers or split \r\n sequences resume correctly on the next call.
288// Framing bytes (hex sizes, \r\n) may be consumed without producing output;
289// the caller sees 0 and retries via the normal read timeout logic.
290//
291// WiFiClient::read() returns -1 on error despite available() > 0 (connection reset
292// between check and read). On any stream error (c < 0 or readBytes <= 0), we return
293// already-decoded data if any; otherwise HTTP_ERROR_CONNECTION_CLOSED. The error
294// will surface again on the next call since the stream stays broken.
295//
296// Returns: > 0 decoded bytes, 0 no data available, < 0 error
297int HttpContainerArduino::read_chunked_(uint8_t *buf, size_t max_len, WiFiClient *stream) {
298 int total_decoded = 0;
299
300 while (total_decoded < (int) max_len && this->chunk_state_ != ChunkedState::COMPLETE) {
301 // Non-blocking: only process what's already buffered
302 if (stream->available() == 0)
303 break;
304
305 // CHUNK_DATA reads multiple bytes; handle before the single-byte switch
307 // Only read what's available, what fits in buf, and what remains in this chunk
308 size_t to_read =
309 std::min({max_len - (size_t) total_decoded, this->chunk_remaining_, (size_t) stream->available()});
310 if (to_read == 0)
311 break;
312 App.feed_wdt();
313 int read_len = stream->readBytes(buf + total_decoded, to_read);
314 if (read_len <= 0)
315 return total_decoded > 0 ? total_decoded : HTTP_ERROR_CONNECTION_CLOSED;
316 total_decoded += read_len;
317 this->chunk_remaining_ -= read_len;
318 this->bytes_read_ += read_len;
319 if (this->chunk_remaining_ == 0)
321 continue;
322 }
323
324 // All other states consume a single byte
325 int c = stream->read();
326 if (c < 0)
327 return total_decoded > 0 ? total_decoded : HTTP_ERROR_CONNECTION_CLOSED;
328
329 switch (this->chunk_state_) {
330 // Parse hex chunk size, one byte at a time: "<hex>[;ext]\r\n"
331 // Note: if no hex digits are parsed (e.g., bare \r\n), chunk_remaining_ stays 0
332 // and is treated as the final chunk. This is intentionally lenient — on embedded
333 // devices, rejecting malformed framing is less useful than terminating cleanly.
334 // Overflow of chunk_remaining_ from extremely long hex strings (>8 digits on
335 // 32-bit) is not checked; >4GB chunks are unrealistic on embedded targets and
336 // would simply cause fewer bytes to be read from that chunk.
338 if (c == '\n') {
339 // \n terminates the size line; chunk_remaining_ == 0 means last chunk
341 } else {
342 uint8_t hex = parse_hex_char(c);
343 if (hex != INVALID_HEX_CHAR) {
344 this->chunk_remaining_ = (this->chunk_remaining_ << 4) | hex;
345 } else if (c != '\r') {
346 this->chunk_state_ = ChunkedState::CHUNK_HEADER_EXT; // ';' starts extension, skip to \n
347 }
348 }
349 break;
350
351 // Skip chunk extension bytes until \n (e.g., ";name=value\r\n")
353 if (c == '\n') {
355 }
356 break;
357
358 // Consume \r\n trailing each chunk's data
360 if (c == '\n') {
362 this->chunk_remaining_ = 0; // reset for next chunk's hex accumulation
363 }
364 // else: \r is consumed silently, next iteration gets \n
365 break;
366
367 // Consume optional trailer headers and terminating empty line after final chunk.
368 // Per RFC 9112 Section 7.1: "0\r\n" is followed by optional "field\r\n" lines
369 // and a final "\r\n". chunk_remaining_ is repurposed as a flag: 1 = at start
370 // of line (may be the empty terminator), 0 = mid-line (reading a trailer field).
372 if (c == '\n') {
373 if (this->chunk_remaining_ != 0) {
374 this->chunk_state_ = ChunkedState::COMPLETE; // Empty line terminates trailers
375 } else {
376 this->chunk_remaining_ = 1; // End of trailer field, at start of next line
377 }
378 } else if (c != '\r') {
379 this->chunk_remaining_ = 0; // Non-CRLF char: reading a trailer field
380 }
381 // \r doesn't change the flag — it's part of \r\n line endings
382 break;
383
384 default:
385 break;
386 }
387
389 // Clear chunked flag and set content_length to actual decoded size so
390 // is_read_complete() returns true and callers exit their read loops
391 this->is_chunked_ = false;
392 this->content_length = this->bytes_read_;
393 }
394 }
395
396 return total_decoded;
397}
398
400 watchdog::WatchdogManager wdm(this->parent_->get_watchdog_timeout());
401 this->client_.end();
402}
403
404} // namespace esphome::http_request
405
406#endif // USE_ARDUINO && !USE_ESP32
uint8_t status
Definition bl0942.h:8
void feed_wdt()
Feed the task watchdog.
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.
constexpr uint8_t parse_hex_char(char c)
Definition helpers.h:1238
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t