ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
web_server_idf.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
2
3#include <cstdarg>
4#include <memory>
5#include <cstring>
6#include <cctype>
7#include <cinttypes>
8
10#include "esphome/core/log.h"
11
12#include "esp_tls_crypto.h"
13#include <freertos/FreeRTOS.h>
14#include <freertos/task.h>
15
16#include "utils.h"
17#include "web_server_idf.h"
18
19#ifdef USE_WEBSERVER_OTA
20#include <multipart_parser.h>
21#include "multipart.h" // For parse_multipart_boundary and other utils
22#endif
23
24#ifdef USE_WEBSERVER
27#endif // USE_WEBSERVER
28
29// Include socket headers after Arduino headers to avoid IPADDR_NONE/INADDR_NONE macro conflicts
30#include <cerrno>
31#include <sys/socket.h>
32
33namespace esphome {
34namespace web_server_idf {
35
36#ifndef HTTPD_409
37#define HTTPD_409 "409 Conflict"
38#endif
39
40#define CRLF_STR "\r\n"
41#define CRLF_LEN (sizeof(CRLF_STR) - 1)
42
43static const char *const TAG = "web_server_idf";
44
45// Global instance to avoid guard variable (saves 8 bytes)
46// This is initialized at program startup before any threads
47namespace {
48// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
49DefaultHeaders default_headers_instance;
50} // namespace
51
52DefaultHeaders &DefaultHeaders::Instance() { return default_headers_instance; }
53
54namespace {
55// Non-blocking send function to prevent watchdog timeouts when TCP buffers are full
70int nonblocking_send(httpd_handle_t hd, int sockfd, const char *buf, size_t buf_len, int flags) {
71 if (buf == nullptr) {
72 return HTTPD_SOCK_ERR_INVALID;
73 }
74
75 // Use MSG_DONTWAIT to prevent blocking when TCP send buffer is full
76 int ret = send(sockfd, buf, buf_len, flags | MSG_DONTWAIT);
77 if (ret < 0) {
78 if (errno == EAGAIN || errno == EWOULDBLOCK) {
79 // Buffer full - retry later
80 return HTTPD_SOCK_ERR_TIMEOUT;
81 }
82 // Real error
83 ESP_LOGD(TAG, "send error: errno %d", errno);
84 return HTTPD_SOCK_ERR_FAIL;
85 }
86 return ret;
87}
88} // namespace
89
91 if (this->server_) {
92 httpd_stop(this->server_);
93 this->server_ = nullptr;
94 }
95}
96
98 if (this->server_) {
99 this->end();
100 }
101 httpd_config_t config = HTTPD_DEFAULT_CONFIG();
102 config.server_port = this->port_;
103 config.uri_match_fn = [](const char * /*unused*/, const char * /*unused*/, size_t /*unused*/) { return true; };
104 if (httpd_start(&this->server_, &config) == ESP_OK) {
105 const httpd_uri_t handler_get = {
106 .uri = "",
107 .method = HTTP_GET,
109 .user_ctx = this,
110 };
111 httpd_register_uri_handler(this->server_, &handler_get);
112
113 const httpd_uri_t handler_post = {
114 .uri = "",
115 .method = HTTP_POST,
117 .user_ctx = this,
118 };
119 httpd_register_uri_handler(this->server_, &handler_post);
120
121 const httpd_uri_t handler_options = {
122 .uri = "",
123 .method = HTTP_OPTIONS,
125 .user_ctx = this,
126 };
127 httpd_register_uri_handler(this->server_, &handler_options);
128 }
129}
130
131esp_err_t AsyncWebServer::request_post_handler(httpd_req_t *r) {
132 ESP_LOGVV(TAG, "Enter AsyncWebServer::request_post_handler. uri=%s", r->uri);
133 auto content_type = request_get_header(r, "Content-Type");
134
135 if (!request_has_header(r, "Content-Length")) {
136 ESP_LOGW(TAG, "Content length is required for post: %s", r->uri);
137 httpd_resp_send_err(r, HTTPD_411_LENGTH_REQUIRED, nullptr);
138 return ESP_OK;
139 }
140
141 if (content_type.has_value()) {
142 const char *content_type_char = content_type.value().c_str();
143
144 // Check most common case first
145 if (stristr(content_type_char, "application/x-www-form-urlencoded") != nullptr) {
146 // Normal form data - proceed with regular handling
147#ifdef USE_WEBSERVER_OTA
148 } else if (stristr(content_type_char, "multipart/form-data") != nullptr) {
149 auto *server = static_cast<AsyncWebServer *>(r->user_ctx);
150 return server->handle_multipart_upload_(r, content_type_char);
151#endif
152 } else {
153 ESP_LOGW(TAG, "Unsupported content type for POST: %s", content_type_char);
154 // fallback to get handler to support backward compatibility
156 }
157 }
158
159 // Handle regular form data
160 if (r->content_len > CONFIG_HTTPD_MAX_REQ_HDR_LEN) {
161 ESP_LOGW(TAG, "Request size is to big: %zu", r->content_len);
162 httpd_resp_send_err(r, HTTPD_400_BAD_REQUEST, nullptr);
163 return ESP_FAIL;
164 }
165
166 std::string post_query;
167 if (r->content_len > 0) {
168 post_query.resize(r->content_len);
169 const int ret = httpd_req_recv(r, &post_query[0], r->content_len + 1);
170 if (ret <= 0) { // 0 return value indicates connection closed
171 if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
172 httpd_resp_send_err(r, HTTPD_408_REQ_TIMEOUT, nullptr);
173 return ESP_ERR_TIMEOUT;
174 }
175 httpd_resp_send_err(r, HTTPD_400_BAD_REQUEST, nullptr);
176 return ESP_FAIL;
177 }
178 }
179
180 AsyncWebServerRequest req(r, std::move(post_query));
181 return static_cast<AsyncWebServer *>(r->user_ctx)->request_handler_(&req);
182}
183
184esp_err_t AsyncWebServer::request_handler(httpd_req_t *r) {
185 ESP_LOGVV(TAG, "Enter AsyncWebServer::request_handler. method=%u, uri=%s", r->method, r->uri);
187 return static_cast<AsyncWebServer *>(r->user_ctx)->request_handler_(&req);
188}
189
191 for (auto *handler : this->handlers_) {
192 if (handler->canHandle(request)) {
193 // At now process only basic requests.
194 // OTA requires multipart request support and handleUpload for it
195 handler->handleRequest(request);
196 return ESP_OK;
197 }
198 }
199 if (this->on_not_found_) {
200 this->on_not_found_(request);
201 return ESP_OK;
202 }
203 return ESP_ERR_NOT_FOUND;
204}
205
207 delete this->rsp_;
208 for (auto *param : this->params_) {
209 delete param; // NOLINT(cppcoreguidelines-owning-memory)
210 }
211}
212
213bool AsyncWebServerRequest::hasHeader(const char *name) const { return request_has_header(*this, name); }
214
216 return request_get_header(*this, name);
217}
218
219std::string AsyncWebServerRequest::url() const {
220 auto *str = strchr(this->req_->uri, '?');
221 if (str == nullptr) {
222 return this->req_->uri;
223 }
224 return std::string(this->req_->uri, str - this->req_->uri);
225}
226
227std::string AsyncWebServerRequest::host() const { return this->get_header("Host").value(); }
228
230 httpd_resp_send(*this, response->get_content_data(), response->get_content_size());
231}
232
233void AsyncWebServerRequest::send(int code, const char *content_type, const char *content) {
234 this->init_response_(nullptr, code, content_type);
235 if (content) {
236 httpd_resp_send(*this, content, HTTPD_RESP_USE_STRLEN);
237 } else {
238 httpd_resp_send(*this, nullptr, 0);
239 }
240}
241
242void AsyncWebServerRequest::redirect(const std::string &url) {
243 httpd_resp_set_status(*this, "302 Found");
244 httpd_resp_set_hdr(*this, "Location", url.c_str());
245 httpd_resp_send(*this, nullptr, 0);
246}
247
248void AsyncWebServerRequest::init_response_(AsyncWebServerResponse *rsp, int code, const char *content_type) {
249 // Set status code - use constants for common codes, default to 500 for unknown codes
250 const char *status;
251 switch (code) {
252 case 200:
253 status = HTTPD_200;
254 break;
255 case 404:
256 status = HTTPD_404;
257 break;
258 case 409:
259 status = HTTPD_409;
260 break;
261 default:
262 status = HTTPD_500;
263 break;
264 }
265 httpd_resp_set_status(*this, status);
266
267 if (content_type && *content_type) {
268 httpd_resp_set_type(*this, content_type);
269 }
270 httpd_resp_set_hdr(*this, "Accept-Ranges", "none");
271
272 for (const auto &pair : DefaultHeaders::Instance().headers_) {
273 httpd_resp_set_hdr(*this, pair.first.c_str(), pair.second.c_str());
274 }
275
276 delete this->rsp_;
277 this->rsp_ = rsp;
278}
279
280#ifdef USE_WEBSERVER_AUTH
281bool AsyncWebServerRequest::authenticate(const char *username, const char *password) const {
282 if (username == nullptr || password == nullptr || *username == 0) {
283 return true;
284 }
285 auto auth = this->get_header("Authorization");
286 if (!auth.has_value()) {
287 return false;
288 }
289
290 auto *auth_str = auth.value().c_str();
291
292 const auto auth_prefix_len = sizeof("Basic ") - 1;
293 if (strncmp("Basic ", auth_str, auth_prefix_len) != 0) {
294 ESP_LOGW(TAG, "Only Basic authorization supported yet");
295 return false;
296 }
297
298 std::string user_info;
299 user_info += username;
300 user_info += ':';
301 user_info += password;
302
303 size_t n = 0, out;
304 esp_crypto_base64_encode(nullptr, 0, &n, reinterpret_cast<const uint8_t *>(user_info.c_str()), user_info.size());
305
306 auto digest = std::unique_ptr<char[]>(new char[n + 1]);
307 esp_crypto_base64_encode(reinterpret_cast<uint8_t *>(digest.get()), n, &out,
308 reinterpret_cast<const uint8_t *>(user_info.c_str()), user_info.size());
309
310 return strcmp(digest.get(), auth_str + auth_prefix_len) == 0;
311}
312
313void AsyncWebServerRequest::requestAuthentication(const char *realm) const {
314 httpd_resp_set_hdr(*this, "Connection", "keep-alive");
315 auto auth_val = str_sprintf("Basic realm=\"%s\"", realm ? realm : "Login Required");
316 httpd_resp_set_hdr(*this, "WWW-Authenticate", auth_val.c_str());
317 httpd_resp_send_err(*this, HTTPD_401_UNAUTHORIZED, nullptr);
318}
319#endif
320
322 // Check cache first - only successful lookups are cached
323 for (auto *param : this->params_) {
324 if (param->name() == name) {
325 return param;
326 }
327 }
328
329 // Look up value from query strings
331 if (!val.has_value()) {
332 auto url_query = request_get_url_query(*this);
333 if (url_query.has_value()) {
334 val = query_key_value(url_query.value(), name);
335 }
336 }
337
338 // Don't cache misses to avoid wasting memory when handlers check for
339 // optional parameters that don't exist in the request
340 if (!val.has_value()) {
341 return nullptr;
342 }
343
344 auto *param = new AsyncWebParameter(name, val.value()); // NOLINT(cppcoreguidelines-owning-memory)
345 this->params_.push_back(param);
346 return param;
347}
348
349void AsyncWebServerResponse::addHeader(const char *name, const char *value) {
350 httpd_resp_set_hdr(*this->req_, name, value);
351}
352
353void AsyncResponseStream::print(float value) {
354 // Use stack buffer to avoid temporary string allocation
355 // Size: sign (1) + digits (10) + decimal (1) + precision (6) + exponent (5) + null (1) = 24, use 32 for safety
356 char buf[32];
357 int len = snprintf(buf, sizeof(buf), "%f", value);
358 this->content_.append(buf, len);
359}
360
361void AsyncResponseStream::printf(const char *fmt, ...) {
362 va_list args;
363
364 va_start(args, fmt);
365 const int length = vsnprintf(nullptr, 0, fmt, args);
366 va_end(args);
367
368 std::string str;
369 str.resize(length);
370
371 va_start(args, fmt);
372 vsnprintf(&str[0], length + 1, fmt, args);
373 va_end(args);
374
375 this->print(str);
376}
377
378#ifdef USE_WEBSERVER
380 for (auto *ses : this->sessions_) {
381 delete ses; // NOLINT(cppcoreguidelines-owning-memory)
382 }
383}
384
386 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory,clang-analyzer-cplusplus.NewDeleteLeaks)
387 auto *rsp = new AsyncEventSourceResponse(request, this, this->web_server_);
388 if (this->on_connect_) {
389 this->on_connect_(rsp);
390 }
391 this->sessions_.push_back(rsp);
392}
393
395 // Clean up dead sessions safely
396 // This follows the ESP-IDF pattern where free_ctx marks resources as dead
397 // and the main loop handles the actual cleanup to avoid race conditions
398 for (size_t i = 0; i < this->sessions_.size();) {
399 auto *ses = this->sessions_[i];
400 // If the session has a dead socket (marked by destroy callback)
401 if (ses->fd_.load() == 0) {
402 ESP_LOGD(TAG, "Removing dead event source session");
403 delete ses; // NOLINT(cppcoreguidelines-owning-memory)
404 // Remove by swapping with last element (O(1) removal, order doesn't matter for sessions)
405 this->sessions_[i] = this->sessions_.back();
406 this->sessions_.pop_back();
407 } else {
408 ses->loop();
409 ++i;
410 }
411 }
412}
413
414void AsyncEventSource::try_send_nodefer(const char *message, const char *event, uint32_t id, uint32_t reconnect) {
415 for (auto *ses : this->sessions_) {
416 if (ses->fd_.load() != 0) { // Skip dead sessions
417 ses->try_send_nodefer(message, event, id, reconnect);
418 }
419 }
420}
421
422void AsyncEventSource::deferrable_send_state(void *source, const char *event_type,
423 message_generator_t *message_generator) {
424 // Skip if no connected clients to avoid unnecessary processing
425 if (this->empty())
426 return;
427 for (auto *ses : this->sessions_) {
428 if (ses->fd_.load() != 0) { // Skip dead sessions
429 ses->deferrable_send_state(source, event_type, message_generator);
430 }
431 }
432}
433
437 : server_(server), web_server_(ws), entities_iterator_(new esphome::web_server::ListEntitiesIterator(ws, server)) {
438 httpd_req_t *req = *request;
439
440 httpd_resp_set_status(req, HTTPD_200);
441 httpd_resp_set_type(req, "text/event-stream");
442 httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
443 httpd_resp_set_hdr(req, "Connection", "keep-alive");
444
445 for (const auto &pair : DefaultHeaders::Instance().headers_) {
446 httpd_resp_set_hdr(req, pair.first.c_str(), pair.second.c_str());
447 }
448
449 httpd_resp_send_chunk(req, CRLF_STR, CRLF_LEN);
450
451 req->sess_ctx = this;
452 req->free_ctx = AsyncEventSourceResponse::destroy;
453
454 this->hd_ = req->handle;
455 this->fd_.store(httpd_req_to_sockfd(req));
456
457 // Use non-blocking send to prevent watchdog timeouts when TCP buffers are full
458 httpd_sess_set_send_override(this->hd_, this->fd_.load(), nonblocking_send);
459
460 // Configure reconnect timeout and send config
461 // this should always go through since the tcp send buffer is empty on connect
462 std::string message = ws->get_config_json();
463 this->try_send_nodefer(message.c_str(), "ping", millis(), 30000);
464
465#ifdef USE_WEBSERVER_SORTING
466 for (auto &group : ws->sorting_groups_) {
467 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
468 json::JsonBuilder builder;
469 JsonObject root = builder.root();
470 root["name"] = group.second.name;
471 root["sorting_weight"] = group.second.weight;
472 message = builder.serialize();
473 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
474
475 // a (very) large number of these should be able to be queued initially without defer
476 // since the only thing in the send buffer at this point is the initial ping/config
477 this->try_send_nodefer(message.c_str(), "sorting_group");
478 }
479#endif
480
482
483 // just dump them all up-front and take advantage of the deferred queue
484 // on second thought that takes too long, but leaving the commented code here for debug purposes
485 // while(!this->entities_iterator_->completed()) {
486 // this->entities_iterator_->advance();
487 //}
488}
489
491 auto *rsp = static_cast<AsyncEventSourceResponse *>(ptr);
492 ESP_LOGD(TAG, "Event source connection closed (fd: %d)", rsp->fd_.load());
493 // Mark as dead by setting fd to 0 - will be cleaned up in the main loop
494 rsp->fd_.store(0);
495 // Note: We don't delete or remove from set here to avoid race conditions
496}
497
498// helper for allowing only unique entries in the queue
500 DeferredEvent item(source, message_generator);
501
502 // Use range-based for loop instead of std::find_if to reduce template instantiation overhead and binary size
503 for (auto &event : this->deferred_queue_) {
504 if (event == item) {
505 return; // Already in queue, no need to update since items are equal
506 }
507 }
508 this->deferred_queue_.push_back(item);
509}
510
512 while (!deferred_queue_.empty()) {
513 DeferredEvent &de = deferred_queue_.front();
514 std::string message = de.message_generator_(web_server_, de.source_);
515 if (this->try_send_nodefer(message.c_str(), "state")) {
516 // O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
517 deferred_queue_.erase(deferred_queue_.begin());
518 } else {
519 break;
520 }
521 }
522}
523
525 if (event_buffer_.empty()) {
526 return;
527 }
528 if (event_bytes_sent_ == event_buffer_.size()) {
529 event_buffer_.resize(0);
531 return;
532 }
533
534 size_t remaining = event_buffer_.size() - event_bytes_sent_;
535 int bytes_sent =
536 httpd_socket_send(this->hd_, this->fd_.load(), event_buffer_.c_str() + event_bytes_sent_, remaining, 0);
537 if (bytes_sent == HTTPD_SOCK_ERR_TIMEOUT) {
538 // EAGAIN/EWOULDBLOCK - socket buffer full, try again later
539 // NOTE: Similar logic exists in web_server/web_server.cpp in DeferredUpdateEventSource::process_deferred_queue_()
540 // The implementations differ due to platform-specific APIs (HTTPD_SOCK_ERR_TIMEOUT vs DISCARDED, fd_.store(0) vs
541 // close()), but the failure counting and timeout logic should be kept in sync. If you change this logic, also
542 // update the Arduino implementation.
545 // Too many failures, connection is likely dead
546 ESP_LOGW(TAG, "Closing stuck EventSource connection after %" PRIu16 " failed sends",
548 this->fd_.store(0); // Mark for cleanup
549 this->deferred_queue_.clear();
550 }
551 return;
552 }
553 if (bytes_sent == HTTPD_SOCK_ERR_FAIL) {
554 // Real socket error - connection will be closed by httpd and destroy callback will be called
555 return;
556 }
557 if (bytes_sent <= 0) {
558 // Unexpected error or zero bytes sent
559 ESP_LOGW(TAG, "Unexpected send result: %d", bytes_sent);
560 return;
561 }
562
563 // Successful send - reset failure counter
565 event_bytes_sent_ += bytes_sent;
566
567 // Log partial sends for debugging
568 if (event_bytes_sent_ < event_buffer_.size()) {
569 ESP_LOGV(TAG, "Partial send: %d/%zu bytes (total: %zu/%zu)", bytes_sent, remaining, event_bytes_sent_,
570 event_buffer_.size());
571 }
572
573 if (event_bytes_sent_ == event_buffer_.size()) {
574 event_buffer_.resize(0);
576 }
577}
578
585
586bool AsyncEventSourceResponse::try_send_nodefer(const char *message, const char *event, uint32_t id,
587 uint32_t reconnect) {
588 if (this->fd_.load() == 0) {
589 return false;
590 }
591
593 if (!event_buffer_.empty()) {
594 // there is still pending event data to send first
595 return false;
596 }
597
598 // 8 spaces are standing in for the hexidecimal chunk length to print later
599 const char chunk_len_header[] = " " CRLF_STR;
600 const int chunk_len_header_len = sizeof(chunk_len_header) - 1;
601
602 event_buffer_.append(chunk_len_header);
603
604 // Use stack buffer for formatting numeric fields to avoid temporary string allocations
605 // Size: "retry: " (7) + max uint32 (10 digits) + CRLF (2) + null (1) = 20 bytes, use 32 for safety
606 constexpr size_t num_buf_size = 32;
607 char num_buf[num_buf_size];
608
609 if (reconnect) {
610 int len = snprintf(num_buf, num_buf_size, "retry: %" PRIu32 CRLF_STR, reconnect);
611 event_buffer_.append(num_buf, len);
612 }
613
614 if (id) {
615 int len = snprintf(num_buf, num_buf_size, "id: %" PRIu32 CRLF_STR, id);
616 event_buffer_.append(num_buf, len);
617 }
618
619 if (event && *event) {
620 event_buffer_.append("event: ", sizeof("event: ") - 1);
621 event_buffer_.append(event);
622 event_buffer_.append(CRLF_STR, CRLF_LEN);
623 }
624
625 if (message && *message) {
626 event_buffer_.append("data: ", sizeof("data: ") - 1);
627 event_buffer_.append(message);
628 event_buffer_.append(CRLF_STR, CRLF_LEN);
629 }
630
631 if (event_buffer_.empty()) {
632 return true;
633 }
634
635 event_buffer_.append(CRLF_STR, CRLF_LEN);
636 event_buffer_.append(CRLF_STR, CRLF_LEN);
637
638 // chunk length header itself and the final chunk terminating CRLF are not counted as part of the chunk
639 int chunk_len = event_buffer_.size() - CRLF_LEN - chunk_len_header_len;
640 char chunk_len_str[9];
641 snprintf(chunk_len_str, 9, "%08x", chunk_len);
642 std::memcpy(&event_buffer_[0], chunk_len_str, 8);
643
646
647 return true;
648}
649
650void AsyncEventSourceResponse::deferrable_send_state(void *source, const char *event_type,
651 message_generator_t *message_generator) {
652 // allow all json "details_all" to go through before publishing bare state events, this avoids unnamed entries showing
653 // up in the web GUI and reduces event load during initial connect
654 if (!entities_iterator_->completed() && 0 != strcmp(event_type, "state_detail_all"))
655 return;
656
657 if (source == nullptr)
658 return;
659 if (event_type == nullptr)
660 return;
661 if (message_generator == nullptr)
662 return;
663
664 if (0 != strcmp(event_type, "state_detail_all") && 0 != strcmp(event_type, "state")) {
665 ESP_LOGE(TAG, "Can't defer non-state event");
666 }
667
670
671 if (!event_buffer_.empty() || !deferred_queue_.empty()) {
672 // outgoing event buffer or deferred queue still not empty which means downstream tcp send buffer full, no point
673 // trying to send first
674 deq_push_back_with_dedup_(source, message_generator);
675 } else {
676 std::string message = message_generator(web_server_, source);
677 if (!this->try_send_nodefer(message.c_str(), "state")) {
678 deq_push_back_with_dedup_(source, message_generator);
679 }
680 }
681}
682#endif
683
684#ifdef USE_WEBSERVER_OTA
685esp_err_t AsyncWebServer::handle_multipart_upload_(httpd_req_t *r, const char *content_type) {
686 static constexpr size_t MULTIPART_CHUNK_SIZE = 1460; // Match Arduino AsyncWebServer buffer size
687 static constexpr size_t YIELD_INTERVAL_BYTES = 16 * 1024; // Yield every 16KB to prevent watchdog
688
689 // Parse boundary and create reader
690 const char *boundary_start;
691 size_t boundary_len;
692 if (!parse_multipart_boundary(content_type, &boundary_start, &boundary_len)) {
693 ESP_LOGE(TAG, "Failed to parse multipart boundary");
694 httpd_resp_send_err(r, HTTPD_400_BAD_REQUEST, nullptr);
695 return ESP_FAIL;
696 }
697
699 AsyncWebHandler *handler = nullptr;
700 for (auto *h : this->handlers_) {
701 if (h->canHandle(&req)) {
702 handler = h;
703 break;
704 }
705 }
706
707 if (!handler) {
708 ESP_LOGW(TAG, "No handler found for OTA request");
709 httpd_resp_send_err(r, HTTPD_404_NOT_FOUND, nullptr);
710 return ESP_OK;
711 }
712
713 // Upload state
714 std::string filename;
715 size_t index = 0;
716 // Create reader on heap to reduce stack usage
717 auto reader = std::make_unique<MultipartReader>("--" + std::string(boundary_start, boundary_len));
718
719 // Configure callbacks
720 reader->set_data_callback([&](const uint8_t *data, size_t len) {
721 if (!reader->has_file() || !len)
722 return;
723
724 if (filename.empty()) {
725 filename = reader->get_current_part().filename;
726 ESP_LOGV(TAG, "Processing file: '%s'", filename.c_str());
727 handler->handleUpload(&req, filename, 0, nullptr, 0, false); // Start
728 }
729
730 handler->handleUpload(&req, filename, index, const_cast<uint8_t *>(data), len, false);
731 index += len;
732 });
733
734 reader->set_part_complete_callback([&]() {
735 if (index > 0) {
736 handler->handleUpload(&req, filename, index, nullptr, 0, true); // End
737 filename.clear();
738 index = 0;
739 }
740 });
741
742 // Process data
743 std::unique_ptr<char[]> buffer(new char[MULTIPART_CHUNK_SIZE]);
744 size_t bytes_since_yield = 0;
745
746 for (size_t remaining = r->content_len; remaining > 0;) {
747 int recv_len = httpd_req_recv(r, buffer.get(), std::min(remaining, MULTIPART_CHUNK_SIZE));
748
749 if (recv_len <= 0) {
750 httpd_resp_send_err(r, recv_len == HTTPD_SOCK_ERR_TIMEOUT ? HTTPD_408_REQ_TIMEOUT : HTTPD_400_BAD_REQUEST,
751 nullptr);
752 return recv_len == HTTPD_SOCK_ERR_TIMEOUT ? ESP_ERR_TIMEOUT : ESP_FAIL;
753 }
754
755 if (reader->parse(buffer.get(), recv_len) != static_cast<size_t>(recv_len)) {
756 ESP_LOGW(TAG, "Multipart parser error");
757 httpd_resp_send_err(r, HTTPD_400_BAD_REQUEST, nullptr);
758 return ESP_FAIL;
759 }
760
761 remaining -= recv_len;
762 bytes_since_yield += recv_len;
763
764 if (bytes_since_yield > YIELD_INTERVAL_BYTES) {
765 vTaskDelay(1);
766 bytes_since_yield = 0;
767 }
768 }
769
770 handler->handleRequest(&req);
771 return ESP_OK;
772}
773#endif // USE_WEBSERVER_OTA
774
775} // namespace web_server_idf
776} // namespace esphome
777
778#endif // !defined(USE_ESP32)
uint8_t h
Definition bl0906.h:2
uint8_t status
Definition bl0942.h:8
void begin(bool include_internal=false)
Builder class for creating JSON documents without lambdas.
Definition json_util.h:62
value_type const & value() const
Definition optional.h:94
This class allows users to create a web server with their ESP nodes.
Definition web_server.h:173
std::string get_config_json()
Return the webserver configuration as JSON.
std::map< uint64_t, SortingGroup > sorting_groups_
Definition web_server.h:501
std::vector< AsyncEventSourceResponse * > sessions_
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator)
esphome::web_server::WebServer * web_server_
void try_send_nodefer(const char *message, const char *event=nullptr, uint32_t id=0, uint32_t reconnect=0)
void handleRequest(AsyncWebServerRequest *request) override
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator)
esphome::web_server::WebServer * web_server_
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator)
AsyncEventSourceResponse(const AsyncWebServerRequest *request, esphome::web_server_idf::AsyncEventSource *server, esphome::web_server::WebServer *ws)
std::unique_ptr< esphome::web_server::ListEntitiesIterator > entities_iterator_
bool try_send_nodefer(const char *message, const char *event=nullptr, uint32_t id=0, uint32_t reconnect=0)
void printf(const char *fmt,...) __attribute__((format(printf
virtual void handleRequest(AsyncWebServerRequest *request)
virtual void handleUpload(AsyncWebServerRequest *request, const std::string &filename, size_t index, uint8_t *data, size_t len, bool final)
std::function< void(AsyncWebServerRequest *request)> on_not_found_
static esp_err_t request_post_handler(httpd_req_t *r)
std::vector< AsyncWebHandler * > handlers_
esp_err_t request_handler_(AsyncWebServerRequest *request) const
esp_err_t handle_multipart_upload_(httpd_req_t *r, const char *content_type)
static esp_err_t request_handler(httpd_req_t *r)
AsyncWebParameter * getParam(const std::string &name)
optional< std::string > get_header(const char *name) const
void send(AsyncWebServerResponse *response)
void init_response_(AsyncWebServerResponse *rsp, int code, const char *content_type)
void requestAuthentication(const char *realm=nullptr) const
bool authenticate(const char *username, const char *password) const
std::vector< AsyncWebParameter * > params_
virtual const char * get_content_data() const =0
void addHeader(const char *name, const char *value)
const char * message
Definition component.cpp:38
uint16_t flags
mopeka_std_values val[4]
const char *const TAG
Definition spi.cpp:8
optional< std::string > request_get_url_query(httpd_req_t *req)
Definition utils.cpp:56
optional< std::string > request_get_header(httpd_req_t *req, const char *name)
Definition utils.cpp:39
bool parse_multipart_boundary(const char *content_type, const char **boundary_start, size_t *boundary_len)
std::string(esphome::web_server::WebServer *, void *) message_generator_t
optional< std::string > query_key_value(const std::string &query_url, const std::string &key)
Definition utils.cpp:74
const char * stristr(const char *haystack, const char *needle)
Definition utils.cpp:104
bool request_has_header(httpd_req_t *req, const char *name)
Definition utils.cpp:37
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:500
std::string str_sprintf(const char *fmt,...)
Definition helpers.cpp:222
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:30
std::string print()
uint16_t length
Definition tt21100.cpp:0