Compare commits

..
Author SHA1 Message Date
J. Nick Koston d721dd9a62 Merge branch 'web-server-sse-async-transport' into libretiny-json-arena 2026-11-17 09:19:03 -06:00
J. Nick Koston 7d7f732592 Merge branch 'web-server-event-source-shared' into web-server-sse-async-transport 2026-11-17 09:19:03 -06:00
J. Nick Koston 5412b46618 Merge remote-tracking branch 'origin/web-server-event-source-shared' into web-server-sse-async-transport
# Conflicts:
#	esphome/components/web_server/web_server.cpp
#	esphome/components/web_server/web_server.h
2026-11-15 19:10:10 -06:00
J. Nick Koston f31d7660c9 [web_server] Fix clang-tidy findings in the SSE chunk test 2026-11-14 10:21:16 -06:00
J. Nick Koston a9e3b2a801 Merge remote-tracking branch 'origin/web-server-sse-async-transport' into HEAD 2026-11-14 08:43:06 -06:00
J. Nick Koston f5c0923544 Merge remote-tracking branch 'origin/web-server-event-source-shared' into HEAD
# Conflicts:
#	platformio.ini
2026-11-14 08:42:19 -06:00
J. Nick Koston d08139a3c4 Merge remote-tracking branch 'origin/web-server-sse-async-transport' into libretiny-json-arena 2026-11-14 08:09:31 -06:00
J. Nick Koston 5626360c37 [web_server] Unit test the /events tail queue
Move the copy, compaction and capacity steps of append_chunk_() into sse_chunk, the part of web_server
the host builds, and test the byte order behind a partly sent chunk, growth into a new buffer and the
queue cap.
2026-11-14 08:09:22 -06:00
J. Nick Koston fe6449edca Merge remote-tracking branch 'origin/web-server-sse-async-transport' into libretiny-json-arena 2026-11-14 05:03:43 -06:00
J. Nick Koston 1de99dd7b1 [web_server] Queue /events chunks behind an unsent one instead of dropping them
On ESP8266 a stream writes only once its last send is acked, so the greeting's sorting groups and log
bursts were refused while the ping waited and were dropped. A refused event now waits in the tail
behind the unsent bytes, up to about one TCP segment, and goes out with them after the ACK.
2026-11-14 05:03:32 -06:00
J. Nick Koston 011c9aa307 Merge remote-tracking branch 'origin/web-server-sse-async-transport' into libretiny-json-arena 2026-11-14 03:58:19 -06:00
J. Nick Koston 9cc04c8c90 [web_server_base] On ESP8266, write a stream only once its last send is acked
lwIP on ESP8266 allocates a full MSS pbuf for every segment (LWIP_NETIF_TX_SINGLE_PBUF), so each small
event sent while earlier ones were still unacked held about 1.5 KB of heap, up to 8 per stream. Gate
writes on canSend(), the same limit ESPAsyncWebServer's AsyncEventSource used.
2026-11-14 03:52:11 -06:00
J. Nick Koston 05d3ade5b9 [web_server] Keep the /events tail at its high-water mark on ESP8266 again
Freeing it on drain added alloc/free churn without lowering the peak.
2026-11-14 03:46:24 -06:00
J. Nick Koston 291035c80d Merge remote-tracking branch 'origin/web-server-sse-async-transport' into libretiny-json-arena 2026-11-14 02:49:28 -06:00
J. Nick Koston 87dca59bee [web_server] Free a drained /events tail on ESP8266; no sorting-group warning on a closing stream
Each session kept its tail at its high-water mark; with several clients
on ESP8266 that starved the heap during churn.
2026-11-14 02:48:56 -06:00
J. Nick Koston 07928a96be Merge remote-tracking branch 'origin/web-server-sse-async-transport' into libretiny-json-arena 2026-11-14 01:29:50 -06:00
J. Nick Koston a960d9f161 [web_server_base] Fold the abort into close_client_; say why abort_now never marks LibreTiny streams 2026-11-14 01:29:46 -06:00
J. Nick Koston 1b2629fb21 [web_server_base] Abort a stuck async stream instead of closing it gracefully
A graceful close queues its FIN behind data a stalled reader never takes, so
the pcb stayed until the peer left. Stuck closes now send a RST and free the
pcb; ordinary closes stay graceful.
2026-11-14 01:25:21 -06:00
J. Nick Koston d0af43c479 Merge remote-tracking branch 'origin/web-server-sse-async-transport' into libretiny-json-arena 2026-11-14 01:07:45 -06:00
J. Nick Koston 5733449170 [web_server_base] Time the no-ACK close separately so normal bursts are not throttled 2026-11-14 01:07:41 -06:00
J. Nick Koston a5d9d66f5d [web_server_base] Close an async stream whose writes see no ACK for 20 s
lwIP keeps accepting writes into a large send buffer while the reader's window
is shut, and every send restarts the library's ACK timer, so a stalled reader
was never closed on ESP8266, RP2040 and LibreTiny. Compare the free send buffer
before each write with the one after the previous write instead.
2026-11-14 01:02:43 -06:00
J. Nick Koston f2b3f876bb Merge remote-tracking branch 'origin/web-server-sse-async-transport' into libretiny-json-arena 2026-11-14 00:57:38 -06:00
J. Nick Koston 1f9532a176 [web_server] Forward log lines to /events only from the main loop; include log.h for LogString 2026-11-14 00:57:28 -06:00
J. Nick Koston 5be927619c Merge remote-tracking branch 'origin/web-server-sse-async-transport' into libretiny-json-arena 2026-11-14 00:52:32 -06:00
J. Nick Koston c1ee74d20a [web_server] Brace the single log call 2026-11-14 00:52:29 -06:00
J. Nick Koston 1fc034970d Merge remote-tracking branch 'origin/web-server-sse-async-transport' into libretiny-json-arena 2026-11-14 00:49:48 -06:00
J. Nick Koston 4ac5b55611 [web_server] Build /events state documents in the stack arena on LibreTiny too
LibreTiny's main task has an 8 KB stack on every family; the deepest
/events path with the 2176 B arena measures about 4.8 KB on BK72xx.
2026-11-14 00:46:53 -06:00
J. Nick Koston a1ac45ae28 [web_server] Don't warn about the greeting on a stream that is already closing 2026-11-14 00:45:03 -06:00
J. Nick Koston 1c6593bd05 [web_server] Close a partly written head from the main loop; test start_or_expired and iovecs_total 2026-11-14 00:36:18 -06:00
J. Nick Koston 25182e09fd Merge remote-tracking branch 'origin/web-server-event-source-shared' into web-server-sse-async-transport
# Conflicts:
#	esphome/components/web_server/__init__.py
#	esphome/components/web_server/web_server.h
2026-11-14 00:19:58 -06:00
J. Nick Koston de94563809 [web_server] Let the private network preflight for /events reach its handler; include hal.h for progmem_memcpy 2026-11-14 00:01:50 -06:00
J. Nick Koston b1c0639d58 [web_server] Drop the underscore from the static session hand-off functions; define the default header count for tidy 2026-11-13 23:51:50 -06:00
J. Nick Koston 03ca176563 [web_server] Create the /events session in the hand-off once the connection is taken
No session exists before the head is queued and the client released, so a
request that dies first leaves nothing behind and the attach timeout goes away;
the client is stored before a short head closes it.
2026-11-13 23:34:06 -06:00
J. Nick Koston b34ad3d556 Merge remote-tracking branch 'origin/web-server-event-source-shared' into web-server-sse-async-transport 2026-11-13 23:15:59 -06:00
J. Nick Koston 1019e37daa [web_server] Pace stalled retries, share the stall and gather helpers, drop unattached streams
A stalled stream retries at most every 100 ms, so RP2 does not release the
lwIP lock every loop pass; LibreTiny closes with close(); a head not queued
whole closes before the client is published; a stream still waiting for its
connection after 10 s is dropped. StallClock::start_or_expired and
iovecs_total replace the copies in both transports, StreamIntakeGuard drops
its #ifdef ladder and the EventSession alias is gone.
2026-11-13 23:12:23 -06:00
J. Nick Koston c157ca8d0d [web_server] Keep the /events state small: plain event source, intrusive sessions, SSE strings in flash
AsyncEventSource is no longer an AsyncWebHandler (WebServer routes /events),
sessions are intrusive lists, the SSE constants and head literals live in
PROGMEM on ESP8266, and attach_ no longer clears callbacks clientRelease()
already cleared.
2026-11-13 23:05:02 -06:00
J. Nick Koston a987ec87bd [web_server] Serve /events with ESPHome's own sessions on ESP8266, RP2040 and LibreTiny
Replaces ESPAsyncWebServer's AsyncEventSource and DeferredUpdateEventSource
with the event layer ESP32 uses, and drops the 3.12.1 head-ACK workaround.
2026-11-13 22:53:19 -06:00
J. Nick Koston cbb214b7ea [web_server_base] Add an AsyncClient AsyncStreamResponse for ESPAsyncWebServer platforms
The connection is taken from the request once the head is queued; the
network callbacks only set flags and the main loop owns and frees the
client. Both transports share one stall-and-close helper.
2026-11-13 22:53:18 -06:00
J. Nick Koston 51be31c0f8 [web_server_base] Add add_iovecs() to queue a gather list on an AsyncClient through one scratch buffer 2026-11-13 22:51:01 -06:00
J. Nick Koston 9958c3ad5f Merge remote-tracking branch 'origin/espasyncwebserver-3.12.1' into web-server-sse-async-transport 2026-11-13 22:35:42 -06:00
J. Nick Koston 25ba0e10a7 [web_server] Create the /events client once the head is ACKed instead of queueing the head
No heap allocation beyond the library's own path and no RAM per connection;
a connection lost before the ACK allocates nothing. Remove once a release with
upstream 880c97068abb0f0e63f803cd3c0cac84fa5cd7bb is out.
2026-11-13 20:54:14 -06:00
J. Nick Koston 2aad97b6bf [web_server] Queue the /events head through the client on ESPAsyncWebServer 3.12.1
3.12.1 writes the head outside the client's queue and credits its ACK to
the first queued message, dropping the config ping unsent. Remove once a
release with upstream 880c97068abb0f0e63f803cd3c0cac84fa5cd7bb is out.
2026-11-13 20:42:00 -06:00
J. Nick Koston 8031fd0b98 [async_tcp] Plainer xPortGetCoreID comment; test only async_tcp 2026-11-13 18:50:38 -06:00
J. Nick Koston 5c410105e6 [async_tcp] Always define the xPortGetCoreID stand-in on LibreTiny
CONFIG_ASYNC_TCP_DEBUG compiles AsyncTCP's debug log in at any LibreTiny
log level, so gating on LibreTiny's level missed it.
2026-11-13 18:49:02 -06:00
J. Nick Koston 516feefad1 [libretiny] Name the loglevel helpers like the module's other getters 2026-11-13 18:40:31 -06:00
J. Nick Koston 5380e0ae9f [libretiny] Answer whether LibreTiny logs at debug level; async_tcp asks it 2026-11-13 18:38:54 -06:00
J. Nick Koston 1f28686746 [web_server_base] Simplify the LibreTiny loglevel check and its tests 2026-11-13 18:31:54 -06:00
J. Nick Koston daebdabf09 [web_server_base] Honour LT_LOGLEVEL from LibreTiny framework options for the xPortGetCoreID define 2026-11-13 18:26:53 -06:00
J. Nick Koston 08921be52d [web_server_base] Only define xPortGetCoreID for LibreTiny loglevels that compile log_d in 2026-11-13 18:20:32 -06:00
J. Nick Koston 5297065113 [web_server_base] Build LibreTiny's AsyncTCP 3.5.0 at framework loglevel debug 2026-11-13 17:41:05 -06:00
J. Nick Koston b78f8e55aa [web_server_base] Bump ESPAsyncWebServer to 3.12.1 and LibreTiny AsyncTCP to 3.5.0 2026-11-13 15:15:46 -06:00
21 changed files with 928 additions and 488 deletions

No files matched your search

+4 -7
View File
@@ -526,8 +526,9 @@ async def to_code(config: ConfigType) -> None:
cg.add(paren.set_port(port))
cg.add_define("USE_WEBSERVER")
cg.add_define("USE_WEBSERVER_PORT", port)
if CORE.is_esp32:
# The ESP-IDF event source builds state documents in a stack arena
if CORE.is_esp32 or CORE.is_libretiny:
# The event source builds state documents in a 2 KB stack arena; LibreTiny's main task has 8 KB,
# ESP8266 and RP2040 only 4 KB
enable_arena()
cg.add_define("USE_WEBSERVER_VERSION", version)
if version >= 2:
@@ -614,7 +615,7 @@ async def to_code(config: ConfigType) -> None:
def FILTER_SOURCE_FILES() -> list[str]:
"""Filter out web_server_v1.cpp when version is not 1, the ESP32 event stream elsewhere, and the server on host."""
"""Filter out web_server_v1.cpp when version is not 1, and the server on host."""
files_to_filter: list[str] = []
# The host has no web server; it builds only sse_chunk.cpp, for its unit tests
@@ -629,10 +630,6 @@ def FILTER_SOURCE_FILES() -> list[str]:
)
return files_to_filter
# The /events implementation is ESP32 only
if not CORE.is_esp32:
files_to_filter.extend(("event_source.cpp", "sse_chunk.cpp"))
# web_server_v1.cpp is only needed when version is 1
config = CORE.config.get(DOMAIN, {})
if config.get(CONF_VERSION, 2) != 1:
+122 -85
View File
@@ -1,18 +1,19 @@
#include "event_source.h"
#if defined(USE_ESP32) && defined(USE_WEBSERVER)
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
#include <algorithm>
#include <cstring>
#include <iterator>
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "web_server.h"
namespace esphome::web_server {
ESPHOME_LOG_TAG(TAG, "web_server_idf");
ESPHOME_LOG_TAG(TAG, "web_server");
// Only prepare_json_() may hold the JSON arena: every other frame in this file is capped below one
// arena. Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
@@ -21,79 +22,102 @@ ESPHOME_LOG_TAG(TAG, "web_server_idf");
#endif
AsyncEventSource::~AsyncEventSource() {
LockGuard guard{this->pending_mutex_};
for (auto *vec : {&this->sessions_, &this->pending_sessions_}) {
for (auto *ses : *vec) {
web_server_base::StreamIntakeGuard guard{this->pending_mutex_};
for (auto *ses : {this->sessions_, this->pending_sessions_.load(std::memory_order_relaxed)}) {
while (ses != nullptr) {
auto *next = ses->next_;
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
ses = next;
}
}
}
void AsyncEventSource::handleRequest(AsyncWebServerRequest *request) {
// Httpd task: set up the live httpd_req_t and park the session; main loop does the rest.
void AsyncEventSource::handle_request(AsyncWebServerRequest *request, WebServer *ws) {
// Network context: start the stream and park the session; the main loop does the rest.
#ifdef USE_ESP32
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory,clang-analyzer-cplusplus.NewDeleteLeaks)
auto *rsp = new AsyncEventSourceResponse(request, this, this->web_server_);
{
LockGuard guard{this->pending_mutex_};
this->pending_sessions_.push_back(rsp);
this->has_pending_sessions_.store(true, std::memory_order_release);
}
this->web_server_->enable_loop_soon_any_context();
auto *rsp = new AsyncEventSourceResponse(ws);
rsp->begin(request, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, SSE_FIRST_CHUNK);
this->push_pending_(rsp);
#else
// The session is created only once the head is queued and the connection is ours
web_server_base::AsyncStreamResponse::accept(
request, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, SSE_FIRST_CHUNK,
{&AsyncEventSource::create_session, &AsyncEventSource::publish_session, this, ws});
#endif
}
void AsyncEventSource::push_pending_(AsyncEventSourceResponse *rsp) {
{
web_server_base::StreamIntakeGuard guard{this->pending_mutex_};
rsp->next_ = this->pending_sessions_.load(std::memory_order_relaxed);
this->pending_sessions_.store(rsp, std::memory_order_release);
}
rsp->web_server_->enable_loop_soon_any_context();
}
#ifndef USE_ESP32
web_server_base::AsyncStreamResponse *AsyncEventSource::create_session(void *ws) {
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory,clang-analyzer-cplusplus.NewDeleteLeaks)
return new AsyncEventSourceResponse(static_cast<WebServer *>(ws));
}
void AsyncEventSource::publish_session(void *source, web_server_base::AsyncStreamResponse *stream) {
static_cast<AsyncEventSource *>(source)->push_pending_(static_cast<AsyncEventSourceResponse *>(stream));
}
#endif
// clang-analyzer traces a false-positive leak path from loop() through
// adopt_pending_sessions_main_loop_() into start_session_main_loop_() and
// finally ArduinoJson. Suppress along the entire in-our-code call chain.
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
bool AsyncEventSource::loop() {
// Fast path: one atomic load per tick. Slow path is out-of-line on connect.
if (this->has_pending_sessions_.load(std::memory_order_acquire)) {
if (this->pending_sessions_.load(std::memory_order_acquire) != nullptr) {
this->adopt_pending_sessions_main_loop_();
}
// Clean up dead sessions safely
// This follows the ESP-IDF pattern where free_ctx marks resources as dead
// and the main loop handles the actual cleanup to avoid race conditions
for (size_t i = 0; i < this->sessions_.size();) {
auto *ses = this->sessions_[i];
// If the session has a dead socket (marked by destroy callback)
// The network side only marks a session dead; the main loop frees it, so nothing races the free
for (AsyncEventSourceResponse **link = &this->sessions_; *link != nullptr;) {
auto *ses = *link;
if (ses->safe_to_delete()) {
// destroy() already logged the close with the fd; don't double-log here.
*link = ses->next_;
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
// Remove by swapping with last element (O(1) removal, order doesn't matter for sessions)
this->sessions_[i] = this->sessions_.back();
this->sessions_.pop_back();
} else {
ses->loop();
++i;
link = &ses->next_;
}
}
// A session still waiting for httpd to commit its context keeps the loop alive too
return !this->sessions_.empty() || this->has_pending_sessions_.load(std::memory_order_acquire);
// A session still waiting for its connection keeps the loop alive too
return this->sessions_ != nullptr || this->pending_sessions_.load(std::memory_order_acquire) != nullptr;
}
void AsyncEventSource::adopt_pending_sessions_main_loop_() {
std::vector<AsyncEventSourceResponse *> incoming;
AsyncEventSourceResponse *incoming;
{
LockGuard guard{this->pending_mutex_};
incoming.swap(this->pending_sessions_);
this->has_pending_sessions_.store(false, std::memory_order_relaxed);
web_server_base::StreamIntakeGuard guard{this->pending_mutex_};
// Plain load and store under the guard: RP2040 has no atomic read-modify-write
incoming = this->pending_sessions_.load(std::memory_order_acquire);
this->pending_sessions_.store(nullptr, std::memory_order_relaxed);
}
for (auto *rsp : incoming) {
while (incoming != nullptr) {
auto *rsp = incoming;
incoming = rsp->next_;
// Already disconnected? Drop it; skip session start on a dead session.
if (rsp->safe_to_delete()) {
delete rsp; // NOLINT(cppcoreguidelines-owning-memory)
continue;
}
// httpd commits the session context only after the creating handler returns, so stay
// pending until then; httpd_req_cleanup() always commits it or calls destroy()
// httpd commits a stream after the creating handler returns (or destroys it), so it stays pending until then;
// on ESPAsyncWebServer a session is published already attached
if (!rsp->is_attached()) {
LockGuard guard{this->pending_mutex_};
this->pending_sessions_.push_back(rsp);
this->has_pending_sessions_.store(true, std::memory_order_release);
web_server_base::StreamIntakeGuard guard{this->pending_mutex_};
rsp->next_ = this->pending_sessions_.load(std::memory_order_relaxed);
this->pending_sessions_.store(rsp, std::memory_order_release);
continue;
}
this->sessions_.push_back(rsp);
rsp->next_ = this->sessions_;
this->sessions_ = rsp;
rsp->start_session_main_loop_();
}
}
@@ -101,31 +125,24 @@ void AsyncEventSource::adopt_pending_sessions_main_loop_() {
void AsyncEventSource::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
for (auto *ses : this->sessions_) {
for (auto *ses = this->sessions_; ses != nullptr; ses = ses->next_) {
if (ses->is_connected()) {
ses->try_send_nodefer(message, message_len, event, id, reconnect);
}
}
}
void AsyncEventSource::deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator) {
// Skip if no connected clients to avoid unnecessary processing
if (this->empty())
return;
for (auto *ses : this->sessions_) {
void AsyncEventSource::deferrable_send_state(void *source, message_generator_t *message_generator) {
for (auto *ses = this->sessions_; ses != nullptr; ses = ses->next_) {
if (ses->is_connected()) {
ses->deferrable_send_state(source, detail_all, message_generator);
ses->deferrable_send_state(source, false, message_generator);
}
}
}
AsyncEventSourceResponse::AsyncEventSourceResponse(AsyncWebServerRequest *request, AsyncEventSource *server,
WebServer *ws)
: server_(server), web_server_(ws), entities_iterator_(ws, this) {
// Httpd task only. start_session_main_loop_() sends the greeting and starts the iterator.
this->begin(request, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, SSE_FIRST_CHUNK);
}
static_assert(web_server_base::stream_head_iov_count(std::size(SSE_HEADERS) + WEB_SERVER_DEFAULT_HEADERS_COUNT) <=
// Network context only. start_session_main_loop_() sends the greeting and starts the iterator.
AsyncEventSourceResponse::AsyncEventSourceResponse(WebServer *ws) : web_server_(ws), entities_iterator_(ws, this) {}
static_assert(web_server_base::stream_head_iov_count(SSE_HEADERS_COUNT + WEB_SERVER_DEFAULT_HEADERS_COUNT) <=
web_server_base::STREAM_HEAD_MAX_IOV,
"the event stream head must go out in one write");
@@ -137,7 +154,10 @@ void AsyncEventSourceResponse::start_session_main_loop_() {
// allocation; nothing retries the greeting, so close and let the client reconnect.
auto message = ws->get_config_json();
if (!this->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000)) {
ESP_LOGW(TAG, "Config not sent to fd %d", this->log_id());
// A stream already closing (its head did not go out whole) is not a failed greeting
if (!this->is_closing()) {
ESP_LOGW(TAG, "Config not sent to stream %d", this->log_id());
}
this->close();
return;
}
@@ -150,12 +170,14 @@ void AsyncEventSourceResponse::start_session_main_loop_() {
root["sorting_weight"] = group.second.weight;
message = builder.serialize();
// a (very) large number of these should be able to be queued initially without defer
// since the only thing in the send buffer at this point is the initial ping/config.
// A refusal means the socket is full or closing; the remaining groups are not sent. The
// session stays up on purpose: partial grouping beats a reconnect loop on a slow link.
// Groups the socket does not take yet queue in the tail behind the ping. A refusal means the
// tail is full or the stream is closing; the remaining groups are not sent. The session stays
// up on purpose: partial grouping beats a reconnect loop on a slow link.
if (!this->try_send_nodefer(message.c_str(), message.size(), "sorting_group")) {
ESP_LOGW(TAG, "Sorting groups not sent to fd %d", this->log_id());
// A stream closing mid-greeting is not a failed send
if (!this->is_closing()) {
ESP_LOGW(TAG, "Sorting groups not sent to stream %d", this->log_id());
}
break;
}
}
@@ -256,22 +278,37 @@ bool AsyncEventSourceResponse::stash_chunk_(const char *prefix, size_t prefix_le
}
return false;
}
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
dst += prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &out = *static_cast<uint8_t **>(ctx);
std::memcpy(out, piece, len);
out += len;
},
&dst);
copy_chunk(this->tail_.get(), prefix, prefix_len, message, message_len);
this->tail_len_ = total;
this->tail_sent_ = sent;
return true;
}
bool AsyncEventSourceResponse::append_chunk_(const char *prefix, size_t prefix_len, const char *message,
size_t message_len, size_t total) {
const size_t cap = tail_queue_cap(this->tail_len_ - this->tail_sent_ + total, this->tail_cap_, TAIL_QUEUE_SIZE);
if (cap == 0) {
return false;
}
uint8_t *src = this->tail_.get();
RAMUniquePtr<uint8_t[]> grown;
if (cap != this->tail_cap_) {
// Kept once grown, so a burst does not allocate per event
grown = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(cap);
if (!grown) {
return false;
}
}
uint8_t *dst = grown ? grown.get() : src;
this->tail_len_ = queue_chunk(dst, src, this->tail_len_, this->tail_sent_, prefix, prefix_len, message, message_len);
this->tail_sent_ = 0;
if (grown) {
this->tail_ = std::move(grown);
this->tail_cap_ = cap;
}
return true;
}
void AsyncEventSourceResponse::loop() {
AsyncStreamResponse::loop();
if (this->is_closing()) {
@@ -298,8 +335,12 @@ AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_js
// Log lines emitted while the generator runs are dropped for this session: on the large path they would fill
// tail_ before it is overwritten, and on the small path they would go out ahead of the event being built.
SendGuard guard{*this};
#ifdef USE_JSON_ARENA
json::JsonArena<json::JSON_ARENA_SIZE> arena;
json::JsonBuilder builder(&arena);
#else
json::JsonBuilder builder;
#endif
generator(this->web_server_, source, builder);
*size_out = builder.serialize_to(buffer, JSON_BUF_SIZE);
if (*size_out < JSON_BUF_SIZE) {
@@ -333,7 +374,7 @@ AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_js
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
std::memcpy(dst + prefix_len + json_len, SSE_SUFFIX, SSE_SUFFIX_LEN);
progmem_memcpy(dst + prefix_len + json_len, SSE_SUFFIX, SSE_SUFFIX_LEN);
this->tail_len_ = total;
this->tail_sent_ = 0;
return JsonPrepareResult::JSON_PREPARE_RESULT_TAIL;
@@ -343,7 +384,7 @@ AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_js
#endif
bool AsyncEventSourceResponse::send_json_(void *source, message_generator_t *generator) {
if (!this->ready_to_send_()) {
if (!this->drain_if_idle_() || this->tail_len_ != 0) {
return false;
}
char buffer[JSON_BUF_SIZE];
@@ -380,28 +421,20 @@ void AsyncEventSourceResponse::tail_alloc_failed_(size_t cap) {
this->no_memory_logged_ = true;
ESP_LOGW(TAG, "No memory for a %zu byte chunk", cap);
}
const uint32_t now = App.get_loop_component_start_time();
if (!this->stall_.is_running()) {
this->stall_.start(now);
return;
}
if (this->stall_.is_expired(now, SEND_STALL_TIMEOUT_MS)) {
ESP_LOGW(TAG, "Closing EventSource after %" PRIu32 " ms without memory", this->stall_.elapsed(now));
this->close();
}
this->stall_or_close_(LOG_STR("without memory"));
}
bool AsyncEventSourceResponse::ready_to_send_() {
bool AsyncEventSourceResponse::drain_if_idle_() {
if (this->sending_ || !this->is_connected() || this->is_closing()) {
return false;
}
drain_tail_();
return !this->is_closing() && this->tail_len_ == 0;
return !this->is_closing();
}
bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
if (!this->ready_to_send_()) {
if (!this->drain_if_idle_()) {
return false;
}
SendGuard guard{*this};
@@ -445,6 +478,10 @@ bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t mess
write_chunk_header(prefix, g.total - CHUNK_HDR_LEN - CHUNK_END_LEN);
g.iov[0] = {prefix, prefix_len};
if (this->tail_len_ != 0) {
// Behind an unsent chunk (on ESP8266, one waiting for its ACK): queue it rather than drop it
return this->append_chunk_(prefix, prefix_len, message, message_len, g.total);
}
// A message with more lines than the list holds skips straight to the tail
const ssize_t sent = g.fits ? this->write(g.iov, g.iovcnt) : 0;
if (sent < 0) {
@@ -488,4 +525,4 @@ void AsyncEventSourceResponse::deferrable_send_state(void *source, bool detail_a
} // namespace esphome::web_server
#endif // USE_ESP32 && USE_WEBSERVER
#endif // USE_WEBSERVER && (USE_ESP32 || USE_ARDUINO)
+42 -40
View File
@@ -1,7 +1,7 @@
#pragma once
#include "esphome/core/defines.h"
#if defined(USE_ESP32) && defined(USE_WEBSERVER)
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
#include "esphome/components/json/json_util.h"
#include "esphome/components/web_server_base/stream_response.h"
@@ -60,15 +60,15 @@ class AsyncEventSourceResponse : public web_server_base::AsyncStreamResponse {
void loop();
protected:
AsyncEventSourceResponse(AsyncWebServerRequest *request, AsyncEventSource *server, WebServer *ws);
explicit AsyncEventSourceResponse(WebServer *ws);
// Main-loop only: sends initial ping/config/sorting_groups, starts entity iterator.
void start_session_main_loop_();
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
void process_deferred_queue_();
// A new chunk may go out: not re-entered from a log line, session alive, tail empty
bool ready_to_send_();
// Not re-entered from a log line and the session is alive after pushing the tail
bool drain_if_idle_();
// Push what is left of the chunk in tail_ to the socket
void drain_tail_();
// Grow tail_ to hold len bytes, kept at its high-water mark. False on OOM.
@@ -76,6 +76,8 @@ class AsyncEventSourceResponse : public web_server_base::AsyncStreamResponse {
// Keep the whole chunk in tail_ and continue from sent; false when the tail cannot be allocated.
bool stash_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total,
size_t sent);
// Queue a whole chunk behind the unsent part of the tail; false when it does not fit TAIL_QUEUE_SIZE or OOM
bool append_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total);
// Send a state event; JSON too large for the stack buffer is serialized into tail_ instead
bool send_json_(void *source, message_generator_t *generator);
enum class JsonPrepareResult : uint8_t {
@@ -101,12 +103,13 @@ class AsyncEventSourceResponse : public web_server_base::AsyncStreamResponse {
// Frees the tail and the queue's storage once the session is closing
void release_buffers_();
AsyncEventSource *server_;
std::vector<DeferredEvent> deferred_queue_;
WebServer *web_server_;
// Next session in AsyncEventSource's sessions or pending list
AsyncEventSourceResponse *next_{nullptr};
ListEntitiesIterator entities_iterator_;
// One chunk the socket did not take whole, allocated on the first stall; the only heap use
// on the send path
// Chunks the socket did not take yet, allocated on the first stall; the only heap use on the
// send path
RAMUniquePtr<uint8_t[]> tail_;
uint16_t tail_cap_{0};
uint16_t tail_len_{0}; // Zero means nothing pending
@@ -114,63 +117,62 @@ class AsyncEventSourceResponse : public web_server_base::AsyncStreamResponse {
bool sending_{false};
// The no-memory warning went out during the current stall; cleared on send progress
bool no_memory_logged_{false};
#ifdef USE_ESP8266
// The 4 KB loop stack: smaller send frames, anything larger goes through the tail
static constexpr size_t MAX_SEND_LINES = 8;
static constexpr size_t JSON_BUF_SIZE = 512;
#else
// The longest multi line log message in the tree (a climate dump_config) has 22 lines; a
// longer one goes through the tail
static constexpr size_t MAX_SEND_LINES = 22;
// Stack buffer for a state event's JSON; a larger document is serialized into the tail
static constexpr size_t JSON_BUF_SIZE = 1024;
#endif
static constexpr size_t MAX_SEND_IOV = 1 + 2 * MAX_SEND_LINES;
// Chunk header, retry/id/event lines and the first "data: "
static constexpr size_t PREFIX_BUF_SIZE = 128;
// Stack buffer for a state event's JSON; a larger document is serialized into the tail
static constexpr size_t JSON_BUF_SIZE = 1024;
// Same ceiling JsonBuilder::serialize() applies (max_heap_size in json_util.cpp); a larger
// document is dropped before anything is on the wire
static constexpr size_t JSON_MAX_SIZE = 5120;
// Most an unsent chunk and the events queued behind it may hold: about one TCP segment
static constexpr size_t TAIL_QUEUE_SIZE = 1460;
// Most RAM a stalled session keeps: the largest state document plus any accepted framing
static constexpr size_t TAIL_MAX_SIZE = JSON_MAX_SIZE + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN;
};
class AsyncEventSource : public AsyncWebHandler {
friend class AsyncEventSourceResponse;
// Not an AsyncWebHandler: WebServer routes /events here, which keeps the handler base and its guard out of RAM
class AsyncEventSource {
public:
AsyncEventSource(StringRef url, WebServer *ws) : url_(url), web_server_(ws) {}
~AsyncEventSource() override;
AsyncEventSource() = default;
~AsyncEventSource();
// NOLINTNEXTLINE(readability-identifier-naming)
bool canHandle(AsyncWebServerRequest *request) const override {
if (request->method() != HTTP_GET)
return false;
char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
return request->url_to(url_buf) == this->url_;
}
// NOLINTNEXTLINE(readability-identifier-naming)
void handleRequest(AsyncWebServerRequest *request) override;
void handle_request(AsyncWebServerRequest *request, WebServer *ws);
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
// State events to every session; each session's own listing sends the detail_all events
void deferrable_send_state(void *source, message_generator_t *message_generator);
/// Returns true if there are sessions remaining (including pending cleanup).
bool loop();
bool empty() { return this->count() == 0; }
size_t count() const { return this->sessions_.size(); }
bool empty() const { return this->sessions_ == nullptr; }
protected:
// Cold path: move sessions from pending_sessions_ into sessions_ and greet each one.
// Cold path: move sessions from the pending list into sessions_ and greet each one.
void __attribute__((noinline, cold)) adopt_pending_sessions_main_loop_();
// Network context: queue a new session for the main loop
void push_pending_(AsyncEventSourceResponse *rsp);
#ifndef USE_ESP32
// web_server_base::StreamAcceptor callbacks, network context
static web_server_base::AsyncStreamResponse *create_session(void *ws);
static void publish_session(void *source, web_server_base::AsyncStreamResponse *stream);
#endif
StringRef url_; // Must outlive this object (string literal)
// Main-loop only. Vector: SSE sessions are 1-5 connections, linear search beats set.
std::vector<AsyncEventSourceResponse *> sessions_;
// Httpd-task intake; guarded by pending_mutex_, gated by has_pending_sessions_.
std::vector<AsyncEventSourceResponse *> pending_sessions_;
Mutex pending_mutex_;
WebServer *web_server_;
std::atomic<bool> has_pending_sessions_{false};
// Main-loop only; intrusive list through AsyncEventSourceResponse::next_
AsyncEventSourceResponse *sessions_{nullptr};
// Network-context intake, guarded by pending_mutex_; non-null means work to adopt
std::atomic<AsyncEventSourceResponse *> pending_sessions_{nullptr};
[[no_unique_address]] Mutex pending_mutex_;
};
} // namespace esphome::web_server
#endif // USE_ESP32 && USE_WEBSERVER
#endif // USE_WEBSERVER && (USE_ESP32 || USE_ARDUINO)
@@ -11,18 +11,12 @@ class JsonBuilder;
namespace esphome::web_server {
// The /events session the iterator lists entities to
#ifdef USE_ESP32
class AsyncEventSourceResponse;
using EventSession = AsyncEventSourceResponse;
#elif defined(USE_ARDUINO)
class DeferredUpdateEventSource;
using EventSession = DeferredUpdateEventSource;
#endif
class WebServer;
class ListEntitiesIterator final : public ComponentIterator {
public:
ListEntitiesIterator(const WebServer *ws, EventSession *session) : web_server_(ws), session_(session) {}
ListEntitiesIterator(const WebServer *ws, AsyncEventSourceResponse *session) : web_server_(ws), session_(session) {}
// Entity overrides (generated from entity_types.h).
// Implementations live in list_entities.cpp.
@@ -40,7 +34,7 @@ class ListEntitiesIterator final : public ComponentIterator {
void send_all_(void *source, void (*generator)(WebServer *, void *, json::JsonBuilder &));
const WebServer *web_server_;
EventSession *session_;
AsyncEventSourceResponse *session_;
};
} // namespace esphome::web_server
+56 -3
View File
@@ -3,10 +3,30 @@
#include <cinttypes>
#include <cstring>
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
namespace esphome::web_server {
const char CHUNK_END[] PROGMEM = "\r\n";
const char SSE_SEP[] PROGMEM = "\r\ndata: ";
const char SSE_SUFFIX[] PROGMEM = "\r\n\r\n\r\n";
const char SSE_STATUS[] PROGMEM = "200 OK";
const char SSE_CONTENT_TYPE[] PROGMEM = "text/event-stream";
const char SSE_FIRST_CHUNK[] PROGMEM = "2\r\n\r\n\r\n";
static const char SSE_TRANSFER_ENCODING[] PROGMEM = "Transfer-Encoding";
static const char SSE_CHUNKED[] PROGMEM = "chunked";
static const char SSE_CACHE_CONTROL[] PROGMEM = "Cache-Control";
static const char SSE_NO_CACHE[] PROGMEM = "no-cache";
static const char SSE_CONNECTION[] PROGMEM = "Connection";
static const char SSE_KEEP_ALIVE[] PROGMEM = "keep-alive";
// Word aligned pointers, which ESP8266 reads straight from flash
const web_server_base::HttpHeader SSE_HEADERS[SSE_HEADERS_COUNT] PROGMEM = {
{SSE_TRANSFER_ENCODING, SSE_CHUNKED},
{SSE_CACHE_CONTROL, SSE_NO_CACHE},
{SSE_CONNECTION, SSE_KEEP_ALIVE},
};
void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx) {
if (message == nullptr) {
sink(ctx, CHUNK_END, CHUNK_END_LEN);
@@ -44,9 +64,8 @@ size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id
len = buf_append_printf(buf, size, len, "id: %" PRIu32 "\r\n", id);
}
if (event && *event) {
len = buf_append_str(buf, size, len, "event: ");
len = buf_append_str(buf, size, len, event);
len = buf_append_str(buf, size, len, "\r\n");
// event is not a literal, so it cannot go through buf_append_str (a PSTR() wrapper on ESP8266)
len = buf_append_printf(buf, size, len, "event: %s\r\n", event);
}
if (with_data) {
len = buf_append_str(buf, size, len, "data: ");
@@ -63,4 +82,38 @@ void write_chunk_header(char *buf, size_t chunk_len) {
buf[9] = '\n';
}
size_t copy_chunk(uint8_t *dst, const char *prefix, size_t prefix_len, const char *message, size_t message_len) {
uint8_t *const start = dst;
std::memcpy(dst, prefix, prefix_len);
dst += prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &out = *static_cast<uint8_t **>(ctx);
progmem_memcpy(out, piece, len);
out += len;
},
&dst);
return dst - start;
}
size_t tail_queue_cap(size_t need, size_t cap, size_t max) {
if (need > max) {
return 0;
}
if (need <= cap) {
return cap;
}
return need <= max / 2 ? max / 2 : max;
}
size_t queue_chunk(uint8_t *dst, const uint8_t *src, size_t len, size_t sent, const char *prefix, size_t prefix_len,
const char *message, size_t message_len) {
const size_t pending = len - sent;
if (dst != src || sent != 0) {
std::memmove(dst, src + sent, pending);
}
return pending + copy_chunk(dst + pending, prefix, prefix_len, message, message_len);
}
} // namespace esphome::web_server
+23 -14
View File
@@ -15,25 +15,23 @@ namespace esphome::web_server {
// HTTP chunk header "%08x\r\n"
constexpr size_t CHUNK_HDR_LEN = 10;
// Chunk terminator, also the only piece of a null message (no data line, no blank line)
constexpr char CHUNK_END[] = "\r\n";
constexpr size_t CHUNK_END_LEN = sizeof(CHUNK_END) - 1;
extern const char CHUNK_END[];
constexpr size_t CHUNK_END_LEN = 2;
// Between two data lines: the end of one and the prefix of the next
constexpr char SSE_SEP[] = "\r\ndata: ";
constexpr size_t SSE_SEP_LEN = sizeof(SSE_SEP) - 1;
extern const char SSE_SEP[];
constexpr size_t SSE_SEP_LEN = 8;
// End of the last data line, the blank line ending the event, and the chunk terminator
constexpr char SSE_SUFFIX[] = "\r\n\r\n\r\n";
constexpr size_t SSE_SUFFIX_LEN = sizeof(SSE_SUFFIX) - 1;
extern const char SSE_SUFFIX[];
constexpr size_t SSE_SUFFIX_LEN = 6;
// Status, Content-Type and headers of the /events response head
constexpr char SSE_STATUS[] = "200 OK";
constexpr char SSE_CONTENT_TYPE[] = "text/event-stream";
constexpr web_server_base::HttpHeader SSE_HEADERS[] = {
{"Transfer-Encoding", "chunked"},
{"Cache-Control", "no-cache"},
{"Connection", "keep-alive"},
};
// In flash on ESP8266; the stream helpers read them with the _P functions
extern const char SSE_STATUS[];
extern const char SSE_CONTENT_TYPE[];
constexpr size_t SSE_HEADERS_COUNT = 3;
extern const web_server_base::HttpHeader SSE_HEADERS[SSE_HEADERS_COUNT];
// Hand-framed first chunk holding an empty event (one blank line), as httpd_resp_send_chunk() sent it before
constexpr char SSE_FIRST_CHUNK[] = "2\r\n\r\n\r\n";
extern const char SSE_FIRST_CHUNK[];
// Receives one piece of the chunk; ctx is whatever the caller passed to for_each_chunk_piece()
using ChunkPieceSink = void (*)(void *ctx, const char *piece, size_t len);
@@ -50,4 +48,15 @@ size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id
// Fills the header placeholder at the start of buf once the chunk length is known
void write_chunk_header(char *buf, size_t chunk_len);
// Copies the prefix and the message's pieces to dst; returns the bytes written
size_t copy_chunk(uint8_t *dst, const char *prefix, size_t prefix_len, const char *message, size_t message_len);
// Tail capacity for need bytes: cap when they fit, else max / 2, then max; 0 when need > max
size_t tail_queue_cap(size_t need, size_t cap, size_t max);
// Moves the unsent bytes [sent, len) of src to the front of dst (dst may be src) and copies the chunk after them.
// Returns the new length.
size_t queue_chunk(uint8_t *dst, const uint8_t *src, size_t len, size_t sent, const char *prefix, size_t prefix_len,
const char *message, size_t message_len);
} // namespace esphome::web_server
+7 -207
View File
@@ -8,14 +8,11 @@
#include "esphome/core/application.h"
#include "esphome/core/defines.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/core/util.h"
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
#include "StreamString.h"
#endif
#include <cstdlib>
#ifdef USE_LIGHT
@@ -219,197 +216,6 @@ EntityMatchResult UrlMatch::match_entity(EntityBase *entity) const {
return result;
}
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
// BEGIN ESPAsyncWebServer 3.12.1 workaround: remove once a release containing upstream commit
// 880c97068abb0f0e63f803cd3c0cac84fa5cd7bb is out and we bump to it.
// 3.12.1 creates the SSE client before the head is ACKed and credits the head's ACK to its first queued messages,
// dropping or truncating them. Create the client once the head is ACKed instead, as 3.9.6 did.
class HeadAckedEventSourceResponse final : public AsyncEventSourceResponse {
public:
HeadAckedEventSourceResponse(DeferredUpdateEventSourceList *list, WebServer *ws)
: AsyncEventSourceResponse(nullptr), list_(list), ws_(ws) {}
void _respond(AsyncWebServerRequest *request) override {
String head;
this->_assembleHead(head, request->version());
request->client()->write(head.c_str(), this->_headLength);
}
// Every ACK until clientRelease() is the head's, and clientRelease() unhooks this. _state must stay
// RESPONSE_SETUP: past RESPONSE_WAIT_ACK the request would close its released (null) client after this returns.
size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time) override {
this->_ackedLength += len;
if (this->_ackedLength >= this->_headLength)
this->list_->add_client_(this->ws_, request->clientRelease());
return 0;
}
private:
DeferredUpdateEventSourceList *list_;
WebServer *ws_;
};
// END ESPAsyncWebServer 3.12.1 workaround
// helper for allowing only unique entries in the queue
void __attribute__((flatten)) DeferredUpdateEventSource::deq_push_back_with_dedup_(
void *source, message_generator_t *message_generator) {
DeferredEvent item(source, message_generator);
// Use range-based for loop instead of std::find_if to reduce template instantiation overhead and binary size
for (auto &event : this->deferred_queue_) {
if (event == item) {
return; // Already in queue, no need to update since items are equal
}
}
this->deferred_queue_.push_back(item);
}
void DeferredUpdateEventSource::process_deferred_queue_() {
while (!deferred_queue_.empty()) {
DeferredEvent &de = deferred_queue_.front();
json::JsonBuilder builder;
de.message_generator_(web_server_, de.source_, builder);
auto message = builder.serialize();
if (this->send(message.c_str(), "state") != DISCARDED) {
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
deferred_queue_.erase(deferred_queue_.begin());
this->consecutive_send_failures_ = 0; // Reset failure count on successful send
} else {
// NOTE: Similar logic exists in web_server_idf/web_server_idf.cpp in AsyncStreamResponse::write().
// The close mechanisms are platform-specific (this path calls close() directly; the IDF path is time-based and
// closes through HTTPD to preserve session ownership), but both drop a client after roughly 20 seconds without
// send progress. Keep that stall policy in sync when changing either side.
this->consecutive_send_failures_++;
if (this->consecutive_send_failures_ >= MAX_CONSECUTIVE_SEND_FAILURES) {
// Too many failures, connection is likely dead
ESP_LOGW(TAG, "Closing stuck EventSource connection after %" PRIu16 " failed sends",
this->consecutive_send_failures_);
this->close();
this->deferred_queue_.clear();
}
break;
}
}
}
void DeferredUpdateEventSource::loop() {
process_deferred_queue_();
// One step per loop; refusals retry next pass
this->entities_iterator_.try_advance(1);
}
void DeferredUpdateEventSource::deferrable_send_state(void *source, bool detail_all,
message_generator_t *message_generator) {
// Skip if no connected clients to avoid unnecessary deferred queue processing
if (this->count() == 0)
return;
// Let the detail_all listing finish before bare state events, so the web UI never shows unnamed entries and
// initial connect sends less
if (!detail_all && !this->entities_iterator_.completed())
return;
if (!deferred_queue_.empty())
process_deferred_queue_();
if (!deferred_queue_.empty()) {
// deferred queue still not empty which means downstream event queue full, no point trying to send first
deq_push_back_with_dedup_(source, message_generator);
} else {
json::JsonBuilder builder;
message_generator(web_server_, source, builder);
auto message = builder.serialize();
if (this->send(message.c_str(), "state") == DISCARDED) {
deq_push_back_with_dedup_(source, message_generator);
} else {
this->consecutive_send_failures_ = 0; // Reset failure count on successful send
}
}
}
// used for logs plus the initial ping/config
void DeferredUpdateEventSource::try_send_nodefer(const char *message, size_t message_len, const char *event,
uint32_t id, uint32_t reconnect) {
// ESPAsyncWebServer's send() only accepts null-terminated strings
(void) message_len;
this->send(message, event, id, reconnect);
}
bool DeferredUpdateEventSourceList::loop() {
for (DeferredUpdateEventSource *dues : *this) {
dues->loop();
}
return !this->empty();
}
void DeferredUpdateEventSourceList::deferrable_send_state(void *source, bool detail_all,
message_generator_t *message_generator) {
// Skip if no event sources (no connected clients) to avoid unnecessary iteration
if (this->empty())
return;
for (DeferredUpdateEventSource *dues : *this) {
dues->deferrable_send_state(source, detail_all, message_generator);
}
}
void DeferredUpdateEventSourceList::try_send_nodefer(const char *message, size_t message_len, const char *event,
uint32_t id, uint32_t reconnect) {
for (DeferredUpdateEventSource *dues : *this) {
dues->try_send_nodefer(message, message_len, event, id, reconnect);
}
}
// BEGIN ESPAsyncWebServer 3.12.1 workaround (see HeadAckedEventSourceResponse): once removed, add_new_client creates
// the source as add_client_ does and ends with es->handleRequest(request); add_client_ goes away.
void DeferredUpdateEventSourceList::add_new_client(WebServer *ws, AsyncWebServerRequest *request) {
request->send(new HeadAckedEventSourceResponse(this, ws)); // NOLINT
}
void DeferredUpdateEventSourceList::add_client_(WebServer *ws, AsyncClient *client) {
DeferredUpdateEventSource *es = new DeferredUpdateEventSource(ws, ESPHOME_F("/events"));
this->push_back(es);
es->onConnect([this, es](AsyncEventSourceClient *client) { this->on_client_connect_(es); });
es->onDisconnect([this, es](AsyncEventSourceClient *client) { this->on_client_disconnect_(es); });
new AsyncEventSourceClient(client, es); // NOLINT: owned by es
ws->enable_loop_soon_any_context();
}
// END ESPAsyncWebServer 3.12.1 workaround
void DeferredUpdateEventSourceList::on_client_connect_(DeferredUpdateEventSource *source) {
WebServer *ws = source->web_server_;
ws->defer([ws, source]() {
// Configure reconnect timeout and send config
// this should always go through since the AsyncEventSourceClient event queue is empty on connect
auto message = ws->get_config_json();
source->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000);
#ifdef USE_WEBSERVER_SORTING
for (auto &group : ws->sorting_groups_) {
json::JsonBuilder builder;
JsonObject root = builder.root();
root[ESPHOME_F("name")] = linked(group.second.name);
root[ESPHOME_F("sorting_weight")] = group.second.weight;
auto group_msg = builder.serialize();
// up to 31 groups should be able to be queued initially without defer
source->try_send_nodefer(group_msg.c_str(), group_msg.size(), "sorting_group");
}
#endif
source->entities_iterator_.begin(ws->include_internal_);
});
}
void DeferredUpdateEventSourceList::on_client_disconnect_(DeferredUpdateEventSource *source) {
source->web_server_->defer([this, source]() {
// This method was called via WebServer->defer() and is no longer executing in the
// context of the network callback. The object is now dead and can be safely deleted.
this->remove(source);
delete source; // NOLINT
});
}
#endif
#ifdef USE_WEBSERVER_CAPTIVE
WebServer *global_web_server = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
#endif
@@ -455,9 +261,6 @@ void WebServer::setup() {
}
#endif
#ifdef USE_ESP32
this->base_->add_handler(&this->events_);
#endif
this->base_->add_handler(this);
#ifdef USE_WEBSERVER_CAPTIVE
// Not-found fallback (outside the auth middleware): the OS captive portal probes hit
@@ -532,6 +335,9 @@ void WebServer::handle_not_found_(AsyncWebServerRequest *request) {
void WebServer::on_log(uint8_t level, const char *tag, const char *message, size_t message_len) {
(void) level;
(void) tag;
// Sessions are main-loop state; RP2 logs from its network IRQ arrive here directly, so drop those
if (!is_main_loop_thread())
return;
this->events_.try_send_nodefer(message, std::min(message_len, LOG_EVENT_MAX_LEN), "log", millis());
}
#endif
@@ -693,7 +499,7 @@ static void set_json_icon_state_value(JsonObject root, EntityBase *obj, ProgmemS
void WebServer::send_state_event_(void *source, const EntityBase *entity, message_generator_t *generator) {
if (!entity->is_internal() || this->include_internal_)
this->events_.deferrable_send_state(source, false, generator);
this->events_.deferrable_send_state(source, generator);
}
#ifdef USE_SENSOR
@@ -2278,10 +2084,8 @@ bool WebServer::canHandle(AsyncWebServerRequest *request) const {
// Static URL checks - use ESPHOME_F to keep strings in flash on ESP8266
if (url == ESPHOME_F("/"))
return true;
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
if (url == ESPHOME_F("/events"))
if (method == HTTP_GET && url == ESPHOME_F("/events"))
return true;
#endif
#ifdef USE_WEBSERVER_CSS_INCLUDE
if (url == ESPHOME_F("/0.css"))
return true;
@@ -2424,20 +2228,16 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) {
this->handle_index_request(request);
return;
}
#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
if (request->method() == HTTP_OPTIONS && request->hasHeader(ESPHOME_F("Access-Control-Request-Private-Network"))) {
this->handle_pna_cors_request(request);
return;
}
#endif
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
if (url == ESPHOME_F("/events")) {
this->events_.add_new_client(this, request);
this->events_.handle_request(request, this);
return;
}
#endif
#ifdef USE_WEBSERVER_CSS_INCLUDE
if (url == ESPHOME_F("/0.css")) {
+1 -106
View File
@@ -7,9 +7,7 @@
#include "esphome/components/web_server_base/captive_dns.h"
// The host only builds sse_chunk.cpp for unit tests, but other tests can define USE_WEBSERVER there
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
#ifdef USE_ESP32
#include "event_source.h"
#endif
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/entity_includes.h"
@@ -19,7 +17,6 @@
#endif
#include <functional>
#include <list>
#include <map>
#include <string>
#include <utility>
@@ -102,100 +99,6 @@ struct SortingGroup {
enum JsonDetail { DETAIL_ALL, DETAIL_STATE };
/*
In order to defer updates in arduino mode, we need to create one AsyncEventSource per incoming request to /events.
This is because only minimal changes were made to the ESPAsyncWebServer lib_dep, it was undesirable to put deferred
update logic into that library. We need one deferred queue per connection so instead of one AsyncEventSource with
multiple clients, we have multiple event sources with one client each. This is slightly awkward which is why it's
implemented in a more straightforward way for ESP-IDF. Arduino platform will eventually go away and this workaround
can be forgotten.
*/
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
using message_generator_t = void(WebServer *, void *, json::JsonBuilder &);
class DeferredUpdateEventSourceList;
class DeferredUpdateEventSource final : public AsyncEventSource {
friend class DeferredUpdateEventSourceList;
/*
This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
the same component are backed up, and take up only two pointers of memory. The entry in the deferred queue (a
std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only two
pointers per entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors
publishing because of dedup) would take up only 0.8 kB.
*/
struct DeferredEvent {
friend class DeferredUpdateEventSource;
protected:
void *source_;
message_generator_t *message_generator_;
public:
DeferredEvent(void *source, message_generator_t *message_generator)
: source_(source), message_generator_(message_generator) {}
bool operator==(const DeferredEvent &test) const {
return (source_ == test.source_ && message_generator_ == test.message_generator_);
}
};
static_assert(sizeof(DeferredEvent) == sizeof(void *) + sizeof(message_generator_t *),
"DeferredEvent should have no padding");
protected:
// surface a couple methods from the base class
using AsyncEventSource::handleRequest;
using AsyncEventSource::send;
ListEntitiesIterator entities_iterator_;
// vector is used very specifically for its zero memory overhead even though items are popped from the front (memory
// footprint is more important than speed here)
std::vector<DeferredEvent> deferred_queue_;
WebServer *web_server_;
uint16_t consecutive_send_failures_{0};
static constexpr uint16_t MAX_CONSECUTIVE_SEND_FAILURES = 2500; // ~20 seconds at 125Hz loop rate
// helper for allowing only unique entries in the queue
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
void process_deferred_queue_();
public:
DeferredUpdateEventSource(WebServer *ws, const String &url)
: AsyncEventSource(url), entities_iterator_(ListEntitiesIterator(ws, this)), web_server_(ws) {}
void loop();
// detail_all events go out while entities are still being listed; state events wait until listing is done
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
};
class DeferredUpdateEventSourceList final : public std::list<DeferredUpdateEventSource *> {
friend class HeadAckedEventSourceResponse; // ESPAsyncWebServer 3.12.1 workaround, remove with it
protected:
void on_client_connect_(DeferredUpdateEventSource *source);
void on_client_disconnect_(DeferredUpdateEventSource *source);
void add_client_(WebServer *ws, AsyncClient *client); // ESPAsyncWebServer 3.12.1 workaround, remove with it
public:
/// Returns true if there are event sources remaining (including pending cleanup).
bool loop();
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
void add_new_client(WebServer *ws, AsyncWebServerRequest *request);
};
#endif
/** This class allows users to create a web server with their ESP nodes.
*
* Behind the scenes it's using AsyncWebServer to set up the server. It exposes 3 things:
@@ -206,10 +109,6 @@ class DeferredUpdateEventSourceList final : public std::list<DeferredUpdateEvent
* can be found under https://esphome.io/web-api/.
*/
class WebServer final : public Component, public AsyncWebHandler {
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
friend class DeferredUpdateEventSourceList;
#endif
public:
WebServer(web_server_base::WebServerBase *base);
@@ -614,11 +513,7 @@ class WebServer final : public Component, public AsyncWebHandler {
}
web_server_base::WebServerBase *base_;
#ifdef USE_ESP32
AsyncEventSource events_{StringRef::from_lit("/events"), this};
#elif USE_ARDUINO
DeferredUpdateEventSourceList events_;
#endif
AsyncEventSource events_;
#ifdef USE_WEBSERVER_CAPTIVE
void handle_not_found_(AsyncWebServerRequest *request);
web_server_base::CaptiveDNS dns_;
@@ -0,0 +1,246 @@
#include "async_stream_response.h"
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
#include <cinttypes>
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#ifndef USE_LIBRETINY
#include <lwip/tcp.h>
#endif
namespace esphome::web_server_base {
ESPHOME_LOG_TAG(TAG, "web_server_base");
static_assert(STREAM_ADD_FLAG_COPY == ASYNC_WRITE_FLAG_COPY && STREAM_ADD_FLAG_MORE == ASYNC_WRITE_FLAG_MORE,
"add_iovecs() flags must match the TCP library");
// Bytes coalesced into one AsyncClient::add(); on the loop stack, so kept small
static constexpr size_t ADD_SCRATCH_SIZE = 256;
// Closes now and runs the disconnect callback. AsyncTCP (LibreTiny) deprecates close(bool); ESPAsyncTCP and
// RPAsyncTCP need it, as their close() waits for the next poll.
static void close_now(AsyncClient *client) {
#ifdef USE_LIBRETINY
client->close();
#else
client->close(true);
#endif
}
#ifndef USE_LIBRETINY
// Reaches the library's protected pcb
struct AsyncClientPcb : AsyncClient {
static tcp_pcb *of(AsyncClient *client) { return client->*(&AsyncClientPcb::_pcb); }
};
#endif
// Sends a RST and frees the pcb now. A graceful close queues its FIN behind data a stalled reader never takes, so
// it would hold the pcb until the peer leaves. False on ESP8266/RP2, where the caller marks the stream disconnected;
// LibreTiny's callback runs here or later from a queued error event that may still name the client, so never mark it.
static bool abort_now(AsyncClient *client) {
#ifdef USE_LIBRETINY
// AsyncTCP resets the pcb callbacks in the tcpip thread, aborts, then runs the disconnect callback here
client->abort();
return true;
#else
// ESPAsyncTCP and RPAsyncTCP leave the pcb's error callback set, and RPAsyncTCP's would use the pcb lwIP freed
tcp_pcb *pcb = AsyncClientPcb::of(client);
if (pcb != nullptr)
tcp_err(pcb, nullptr);
client->abort();
return false;
#endif
}
// Writes the head, then creates the stream with the connection. The library runs _respond() in the network context
// right after the handler returns; clientRelease() then unhooks the request, which frees itself and this response.
// A request that ends before _respond() never creates a stream, so nothing is left to free.
class StreamHandoffResponse final : public AsyncWebServerResponse {
public:
StreamHandoffResponse(const char *status, const char *content_type, std::span<const HttpHeader> headers,
const char *body, const StreamAcceptor &acceptor)
: status_(status), content_type_(content_type), headers_(headers), body_(body), acceptor_(acceptor) {}
void _respond(AsyncWebServerRequest *request) override {
AsyncClient *client = request->client();
// The library's default headers only reach its own responses, so this raw head writes them itself
HttpHeader defaults[WEB_SERVER_DEFAULT_HEADERS_COUNT];
size_t default_count = 0;
for (const auto &header : DefaultHeaders::Instance()) {
if (default_count == WEB_SERVER_DEFAULT_HEADERS_COUNT) {
break;
}
defaults[default_count++] = {header.name().c_str(), header.value().c_str()};
}
char scratch[ADD_SCRATCH_SIZE];
const bool head_sent = write_stream_head<StreamIovec>(
[client, &scratch](StreamIovec *iov, int count) {
return add_iovecs(*client, iov, count, scratch, sizeof(scratch)) == iovecs_total(iov, count);
},
this->status_, this->content_type_, this->headers_, std::span<const HttpHeader>(defaults, default_count),
this->body_);
AsyncStreamResponse *stream = this->acceptor_.create(this->acceptor_.ctx);
stream->attach_(request->clientRelease(), head_sent);
this->acceptor_.publish(this->acceptor_.owner, stream);
}
bool _sourceValid() const override { return true; }
protected:
const char *status_;
const char *content_type_;
std::span<const HttpHeader> headers_;
const char *body_;
StreamAcceptor acceptor_;
};
void AsyncStreamResponse::accept(AsyncWebServerRequest *request, const char *status, const char *content_type,
std::span<const HttpHeader> headers, const char *body,
const StreamAcceptor &acceptor) {
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory) the request owns and frees its response
request->send(new StreamHandoffResponse(status, content_type, headers, body, acceptor));
}
void AsyncStreamResponse::attach_(AsyncClient *client, bool head_sent) {
client->setRxTimeout(0);
// Backstop for data left unacked once writes stop; each send restarts it
client->setAckTimeout(SEND_STALL_TIMEOUT_MS);
client->setNoDelay(true);
// clientRelease() already cleared the request's data, ack, poll and error handlers
// Network context: store a flag and nothing else; the main loop frees the client. On LibreTiny async_tcp runs above
// the loop task and touches nothing after this callback, so the loop cannot free the client under it.
client->onDisconnect(
[](void *arg, AsyncClient *) {
static_cast<AsyncStreamResponse *>(arg)->disconnected_.store(true, std::memory_order_release);
},
this);
client->onTimeout(
[](void *arg, AsyncClient *, uint32_t) {
static_cast<AsyncStreamResponse *>(arg)->ack_timed_out_.store(true, std::memory_order_release);
},
this);
this->client_ = client;
// A partly queued head must not be continued by later writes. Closing here, inside the client's receive callback,
// could free the pcb the library still uses after it, so the main loop closes it once the callback has returned.
this->close_requested_ = !head_sent;
}
AsyncStreamResponse::~AsyncStreamResponse() {
AsyncClient *client = this->client_;
if (client == nullptr) {
return;
}
// Held so the RP2 network IRQ cannot run while the client goes away
LwIPLock lock;
client->onDisconnect(nullptr, nullptr);
client->onTimeout(nullptr, nullptr);
delete client; // NOLINT(cppcoreguidelines-owning-memory)
}
ssize_t AsyncStreamResponse::write(StreamIovec *iov, int iovcnt) {
if (this->close_requested_) {
return -1;
}
// Nothing to send is not a stall
if (iovecs_total(iov, iovcnt) == 0) {
return 0;
}
if (!this->is_connected()) {
this->close();
return -1;
}
size_t sent = 0;
const uint32_t now = App.get_loop_component_start_time();
if (!this->stall_.is_running() || now - this->last_try_ms_ >= STALL_RETRY_MS) {
this->last_try_ms_ = now;
// RP2: the network IRQ may free the pcb between the check and the use, so hold it out across both
LwIPLock lock;
#ifdef USE_ESP8266
// Every segment holds a full MSS pbuf on the heap until acked (LWIP_NETIF_TX_SINGLE_PBUF), so write only once
// the last send is acked, like ESPAsyncWebServer. Not a stall: the library's ACK timeout covers a silent peer.
if (this->is_connected() && !this->client_->canSend())
return 0;
#endif
if (this->is_connected()) {
// Only an ACK grows the send buffer's free space (lwIP snd_buf). ESPAsyncTCP and RPAsyncTCP call onAck
// only once everything is acked, so it cannot measure this.
if (this->client_->space() > this->space_after_write_)
this->last_ack_ms_ = now;
char scratch[ADD_SCRATCH_SIZE];
sent = add_iovecs(*this->client_, iov, iovcnt, scratch, sizeof(scratch));
if (sent > 0)
this->space_after_write_ = this->client_->space();
}
}
if (sent > 0) {
this->stall_.clear();
// lwIP keeps queueing into a large buffer while the window is shut, so the buffer may never fill
if (now - this->last_ack_ms_ >= SEND_STALL_TIMEOUT_MS) {
ESP_LOGW(TAG, "Closing stuck stream after %" PRIu32 " ms without an ACK", now - this->last_ack_ms_);
this->abort_();
return -1;
}
return static_cast<ssize_t>(sent);
}
// Send buffer full, try again later; a buffer that stays full past the stall timeout is closed
return this->stall_or_close_(LOG_STR("without send progress")) ? -1 : 0;
}
ssize_t AsyncStreamResponse::write(const void *data, size_t len) {
StreamIovec iov = {const_cast<void *>(data), len};
return this->write(&iov, 1);
}
bool AsyncStreamResponse::stall_or_close_(const LogString *reason) {
const uint32_t now = App.get_loop_component_start_time();
if (!this->stall_.start_or_expired(now, SEND_STALL_TIMEOUT_MS)) {
return false;
}
ESP_LOGW(TAG, "Closing stuck stream after %" PRIu32 " ms %s", this->stall_.elapsed(now), LOG_STR_ARG(reason));
this->abort_();
return true;
}
void AsyncStreamResponse::abort_() {
this->close_requested_ = true;
this->close_client_(true);
}
void AsyncStreamResponse::close() {
this->close_requested_ = true;
this->close_client_();
}
void AsyncStreamResponse::close_client_(bool abort) {
if (this->close_sent_ || !this->is_connected()) {
return;
}
this->close_sent_ = true;
LwIPLock lock;
if (!this->is_connected()) {
return;
}
if (!abort) {
close_now(this->client_);
} else if (!abort_now(this->client_)) {
this->disconnected_.store(true, std::memory_order_release);
}
}
void AsyncStreamResponse::loop() {
if (this->close_requested_) {
this->close_client_();
return;
}
if (this->ack_timed_out_.load(std::memory_order_acquire)) {
ESP_LOGW(TAG, "Closing stream after %" PRIu32 " ms without an ACK", SEND_STALL_TIMEOUT_MS);
this->abort_();
}
}
} // namespace esphome::web_server_base
#endif // !USE_ESP32 && USE_ARDUINO
@@ -0,0 +1,108 @@
#pragma once
#include "esphome/core/defines.h"
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
#include <ESPAsyncWebServer.h>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <span>
#include <sys/types.h>
#include "esphome/core/log.h"
#include "stream_head.h"
namespace esphome::web_server_base {
/// One entry of the gather list AsyncStreamResponse::write() takes; the TCP libraries take plain buffers
struct StreamIovec {
void *iov_base;
size_t iov_len;
};
class AsyncStreamResponse;
/// Who makes and takes the stream accept() creates: create(ctx) allocates it, publish(owner, stream) hands the
/// attached stream to the main loop. Both run in the network context.
struct StreamAcceptor {
AsyncStreamResponse *(*create)(void *ctx);
void (*publish)(void *owner, AsyncStreamResponse *stream);
void *owner;
void *ctx;
};
/// A long lived response the main loop writes to without blocking, on ESPAsyncWebServer. It exists only once the
/// head is queued and the connection taken from the request; the network callbacks only set flags, and the main
/// loop owns and frees the AsyncClient. Writes like web_server_idf's AsyncStreamResponse.
class AsyncStreamResponse {
public:
AsyncStreamResponse() = default;
AsyncStreamResponse(const AsyncStreamResponse &) = delete;
AsyncStreamResponse &operator=(const AsyncStreamResponse &) = delete;
/// Main loop, once safe_to_delete(): frees the connection
~AsyncStreamResponse();
/// Network context, from a request handler. Queues the status line, Content-Type, headers, the default headers and
/// body (a C string or null), then has the acceptor create the stream, attach the connection and publish it. A
/// chunked response passes {"Transfer-Encoding", "chunked"} as its first header and frames each chunk itself.
static void accept(AsyncWebServerRequest *request, const char *status, const char *content_type,
std::span<const HttpHeader> headers, const char *body, const StreamAcceptor &acceptor);
/// Main loop. Non-blocking gather write. Returns the bytes accepted, which may be fewer than offered: the caller
/// keeps the rest and continues from there. 0 means the send buffer is full, the last send is not acked yet (ESP8266
/// only) or the connection is not ours yet; -1 means the stream is closing. SEND_STALL_TIMEOUT_MS of refused writes,
/// or of writes with no ACK in between, also aborts it (RST).
ssize_t write(StreamIovec *iov, int iovcnt);
ssize_t write(const void *data, size_t len);
/// Main loop. Closes the connection; safe to call more than once.
void close();
/// Main loop, every pass. Aborts (RST) a stream whose last send went unacknowledged for SEND_STALL_TIMEOUT_MS.
void loop();
/// Always true: a stream is published only once it has its connection.
bool is_attached() const { return true; }
/// The client is still connected.
bool is_connected() const { return !this->disconnected_.load(std::memory_order_acquire); }
bool is_closing() const { return this->close_requested_; }
/// An id for log lines only
int log_id() const { return static_cast<int>(reinterpret_cast<uintptr_t>(this) & 0xFFFF); }
/// The connection is gone; no network callback still references this object.
bool safe_to_delete() const { return this->disconnected_.load(std::memory_order_acquire); }
static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000;
// While stalled, retry at most this often: on RP2 each try releases the lwIP lock, which postpones a polled
// Ethernet chip's 20 ms receive poll
static constexpr uint32_t STALL_RETRY_MS = 100;
protected:
friend class StreamHandoffResponse;
// Network context, before publishing: take the connection; a head not queued whole closes it
void attach_(AsyncClient *client, bool head_sent);
// Starts the stall clock; once it has run SEND_STALL_TIMEOUT_MS, logs reason and aborts. True when it did.
bool stall_or_close_(const LogString *reason);
// Graceful FIN, or with abort a RST that frees the pcb now (a stuck stream's FIN would queue behind its data)
void close_client_(bool abort = false);
void abort_();
// Set before publishing; the main loop owns it and frees it once disconnected_
AsyncClient *client_{nullptr};
// Runs while sends make no progress
StallClock stall_;
uint32_t last_try_ms_{0};
// Send buffer free after the last write that queued bytes, and when an ACK was last seen; main loop only
size_t space_after_write_{0};
uint32_t last_ack_ms_{0};
// Set from the network callbacks with plain stores (no read-modify-write: RP2040 has none)
std::atomic<bool> disconnected_{false};
std::atomic<bool> ack_timed_out_{false};
// Main loop only
bool close_requested_{false};
bool close_sent_{false};
};
} // namespace esphome::web_server_base
#endif // !USE_ESP32 && USE_ARDUINO
@@ -1,5 +1,6 @@
#pragma once
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
@@ -7,16 +8,44 @@
#include <strings.h>
#include "esphome/core/defines.h"
#ifdef USE_ESP8266
#include <pgmspace.h>
#endif
// Pure helpers of AsyncStreamResponse, kept free of httpd and lwIP so they build on the host.
// The iovec type is a template parameter: lwIP defines its own on the device, POSIX on the host.
namespace esphome::web_server_base {
// The stream helpers take strings and pieces that may sit in flash on ESP8266: only word reads
// work there, so copies and string scans go through the _P functions (which also read RAM)
#ifdef USE_ESP8266
#define STREAM_PSTR(s) PSTR(s)
inline size_t stream_strlen(const char *s) { return strlen_P(s); }
inline int stream_strcasecmp(const char *ram, const char *any) { return strcasecmp_P(ram, any); }
inline void stream_memcpy(void *dst, const void *src, size_t len) { memcpy_P(dst, src, len); }
#else
#define STREAM_PSTR(s) (s)
inline size_t stream_strlen(const char *s) { return strlen(s); }
inline int stream_strcasecmp(const char *ram, const char *any) { return strcasecmp(ram, any); }
inline void stream_memcpy(void *dst, const void *src, size_t len) { std::memcpy(dst, src, len); }
#endif
struct HttpHeader {
const char *name;
const char *value;
};
/// Bytes in a gather list
template<typename Iovec> size_t iovecs_total(const Iovec *iov, int count) {
size_t total = 0;
for (int i = 0; i < count; i++) {
total += iov[i].iov_len;
}
return total;
}
/// Drops the first sent bytes from a gather list after a write; a short write resumes inside the
/// element it stopped in. count reaches 0 once everything went out.
template<typename Iovec> void advance_iovecs(Iovec *&iov, int &count, size_t sent) {
@@ -40,7 +69,7 @@ static constexpr int STREAM_HEAD_MAX_IOV = 24;
/// True if headers holds a header named name, compared case insensitively
inline bool has_header(std::span<const HttpHeader> headers, const char *name) {
for (const auto &header : headers) {
if (strcasecmp(header.name, name) == 0) {
if (stream_strcasecmp(name, header.name) == 0) {
return true;
}
}
@@ -72,18 +101,18 @@ bool write_stream_head(Send &&send, const char *status, const char *content_type
}
this->iov[this->count++] = {const_cast<char *>(buf), len};
}
void add(const char *str) { this->add(str, strlen(str)); }
void add(const char *str) { this->add(str, stream_strlen(str)); }
void add_header(const HttpHeader &header) {
this->add("\r\n", 2);
this->add(STREAM_PSTR("\r\n"), 2);
this->add(header.name);
this->add(": ", 2);
this->add(STREAM_PSTR(": "), 2);
this->add(header.value);
}
} batch{send};
batch.add("HTTP/1.1 ", 9);
batch.add(STREAM_PSTR("HTTP/1.1 "), 9);
batch.add(status);
batch.add("\r\nContent-Type: ", 16);
batch.add(STREAM_PSTR("\r\nContent-Type: "), 16);
batch.add(content_type);
for (const auto &header : headers) {
batch.add_header(header);
@@ -93,7 +122,7 @@ bool write_stream_head(Send &&send, const char *status, const char *content_type
batch.add_header(header);
}
}
batch.add("\r\n\r\n", 4);
batch.add(STREAM_PSTR("\r\n\r\n"), 4);
if (body != nullptr) {
batch.add(body);
}
@@ -101,6 +130,45 @@ bool write_stream_head(Send &&send, const char *status, const char *content_type
return batch.ok;
}
/// AsyncClient::add() flags; the three TCP libraries use the same values (checked where they are included)
static constexpr uint8_t STREAM_ADD_FLAG_COPY = 0x01;
static constexpr uint8_t STREAM_ADD_FLAG_MORE = 0x02;
/// Queues a gather list on an AsyncClient-like client through scratch, so each add() carries up to scratch_size bytes
/// (on LibreTiny every add() is a round trip into the tcpip thread). Stops at the first add() that takes less than
/// offered, calls send() once if anything was taken, and returns the bytes taken.
template<typename Client, typename Iovec>
size_t add_iovecs(Client &client, const Iovec *iov, int count, char *scratch, size_t scratch_size) {
size_t taken = 0;
size_t fill = 0;
int i = 0;
size_t offset = 0; // bytes of iov[i] already copied
while (i < count) {
const size_t piece = std::min(iov[i].iov_len - offset, scratch_size - fill);
stream_memcpy(scratch + fill, static_cast<const char *>(iov[i].iov_base) + offset, piece);
fill += piece;
offset += piece;
if (offset == iov[i].iov_len) {
++i;
offset = 0;
}
if (fill < scratch_size && i < count) {
continue;
}
const uint8_t flags = STREAM_ADD_FLAG_COPY | (i < count ? STREAM_ADD_FLAG_MORE : 0);
const size_t added = client.add(scratch, fill, flags);
taken += added;
if (added < fill) {
break;
}
fill = 0;
}
if (taken != 0) {
client.send();
}
return taken;
}
/// How long sends have made no progress. Zero means not running, so a start at millis() == 0 records 1.
/// The comparison is wraparound safe across a millis() rollover.
class StallClock {
@@ -118,6 +186,11 @@ class StallClock {
return this->started_ms_ != 0 && static_cast<int32_t>(now - (this->started_ms_ + timeout_ms)) >= 0;
}
uint32_t elapsed(uint32_t now) const { return now - this->started_ms_; }
/// Starts the clock if needed; true once it has run for timeout_ms
bool start_or_expired(uint32_t now, uint32_t timeout_ms) {
this->start(now);
return this->is_expired(now, timeout_ms);
}
protected:
uint32_t started_ms_{0};
@@ -5,6 +5,7 @@
// The long lived stream response the /events layer writes to, for the platform's web server
#ifdef USE_ESP32
#include "esphome/components/web_server_idf/web_server_idf.h"
#include "esphome/core/helpers.h"
#include <sys/socket.h>
@@ -12,5 +13,24 @@ namespace esphome::web_server_base {
using web_server_idf::AsyncStreamResponse;
// One entry of the gather list AsyncStreamResponse::write() takes
using StreamIovec = ::iovec;
// Guards the hand-off of new streams from the server task to the main loop
using StreamIntakeGuard = LockGuard;
} // namespace esphome::web_server_base
#elif defined(USE_ARDUINO)
// ESP8266, RP2040 and LibreTiny: ESPAsyncWebServer, with the connection taken from the request
#include "async_stream_response.h"
#include "esphome/core/helpers.h"
namespace esphome::web_server_base {
// Guards the hand-off of new streams from the network context to the main loop: the lwIP lock keeps RP2's network
// IRQ out, the mutex LibreTiny's async_tcp task; each is a no-op where it is not needed (both on ESP8266)
class StreamIntakeGuard {
public:
explicit StreamIntakeGuard(Mutex &mutex) : guard_(mutex) {}
protected:
LwIPLock lock_;
LockGuard guard_;
};
} // namespace esphome::web_server_base
#endif
@@ -887,11 +887,7 @@ ssize_t AsyncStreamResponse::write(struct iovec *iov, int iovcnt) {
return -1;
}
// Nothing to send is not a stall
size_t total = 0;
for (int i = 0; i < iovcnt; i++) {
total += iov[i].iov_len;
}
if (total == 0) {
if (web_server_base::iovecs_total(iov, iovcnt) == 0) {
return 0;
}
// httpd closes the socket before it clears the session, so the fd number may already be a new client's.
@@ -922,14 +918,17 @@ ssize_t AsyncStreamResponse::write(struct iovec *iov, int iovcnt) {
return -1;
}
// Socket buffer full, try again later; a socket that stays full past the stall timeout is closed
return this->stall_or_close_(LOG_STR("without send progress")) ? -1 : 0;
}
bool AsyncStreamResponse::stall_or_close_(const LogString *reason) {
const uint32_t now = App.get_loop_component_start_time();
this->stall_.start(now);
if (this->stall_.is_expired(now, SEND_STALL_TIMEOUT_MS)) {
ESP_LOGW(TAG, "Closing stuck stream after %" PRIu32 " ms without send progress", this->stall_.elapsed(now));
this->close();
return -1;
if (!this->stall_.start_or_expired(now, SEND_STALL_TIMEOUT_MS)) {
return false;
}
return 0;
ESP_LOGW(TAG, "Closing stuck stream after %" PRIu32 " ms %s", this->stall_.elapsed(now), LOG_STR_ARG(reason));
this->close();
return true;
}
ssize_t AsyncStreamResponse::write(const void *data, size_t len) {
@@ -3,6 +3,7 @@
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/core/string_ref.h"
#include <esp_http_server.h>
@@ -317,6 +318,8 @@ class AsyncStreamResponse {
// socket_closed_
bool owns_session_(int fd) const { return fd != 0 && httpd_sess_get_ctx(this->hd_, fd) == this; }
static void destroy(void *p);
// Starts the stall clock; once it has run SEND_STALL_TIMEOUT_MS, logs reason and closes. True when it closed.
bool stall_or_close_(const LogString *reason);
httpd_handle_t hd_{};
std::atomic<int> fd_{};
+3
View File
@@ -823,6 +823,7 @@
#define USE_WEBSERVER_LOCAL
#define USE_WEBSERVER_OTA
#define USE_WEBSERVER_OTA_DISABLED
#define WEB_SERVER_DEFAULT_HEADERS_COUNT 1
#endif
// USE_RP2 is the canonical platform define for the RP2 chip family. The
@@ -859,6 +860,7 @@
#define ESPHOME_WIFI_IP_STATE_LISTENERS 2
#define USE_ETHERNET_IP_STATE_LISTENERS
#define ESPHOME_ETHERNET_IP_STATE_LISTENERS 2
#define WEB_SERVER_DEFAULT_HEADERS_COUNT 1
#endif
#ifdef USE_LIBRETINY
@@ -895,6 +897,7 @@
#define USE_WEBSERVER_AUTH_DIGEST
#define USE_WEBSERVER_CAPTIVE
#define USE_WEBSERVER_PORT 80 // NOLINT
#define WEB_SERVER_DEFAULT_HEADERS_COUNT 1
#define USE_ESPHOME_TASK_LOG_BUFFER
#define ESPHOME_TASK_LOG_BUFFER_SIZE 768
#endif
@@ -0,0 +1,2 @@
packages:
web_server: !include common_v3.yaml
@@ -0,0 +1,2 @@
packages:
web_server: !include common_v3.yaml
@@ -0,0 +1,2 @@
packages:
web_server: !include common_v3.yaml
@@ -0,0 +1,105 @@
#ifdef USE_HOST
#include <gtest/gtest.h>
#include <sys/uio.h>
#include <cstdint>
#include <string>
#include <vector>
#include "esphome/components/web_server_base/stream_head.h"
namespace esphome::web_server::testing {
using namespace web_server_base; // NOLINT(google-build-using-namespace)
// Records each add() and send() like an AsyncClient whose send buffer holds space bytes
struct FakeClient {
size_t space{SIZE_MAX};
std::string bytes;
std::vector<uint8_t> flags;
int sends{0};
size_t add(const char *data, size_t len, uint8_t apiflags) {
this->flags.push_back(apiflags);
const size_t taken = std::min(len, this->space);
this->bytes.append(data, taken);
this->space -= taken;
return taken;
}
bool send() {
this->sends++;
return true;
}
};
static std::vector<struct iovec> pieces(std::vector<std::string> &parts) {
std::vector<struct iovec> iov;
for (auto &part : parts) {
iov.push_back({part.data(), part.size()});
}
return iov;
}
TEST(AddIovecs, CoalescesPiecesIntoOneAdd) {
std::vector<std::string> parts{"abc", "de", "fghij"};
auto iov = pieces(parts);
FakeClient client;
char scratch[64];
EXPECT_EQ(add_iovecs(client, iov.data(), static_cast<int>(iov.size()), scratch, sizeof(scratch)), 10u);
EXPECT_EQ(client.bytes, "abcdefghij");
ASSERT_EQ(client.flags.size(), 1u);
EXPECT_EQ(client.flags[0], STREAM_ADD_FLAG_COPY);
EXPECT_EQ(client.sends, 1);
}
TEST(AddIovecs, SplitsAtScratchSizeWithMoreOnAllButLast) {
std::vector<std::string> parts{"0123456789", "abcdefghij", "XYZ"};
auto iov = pieces(parts);
FakeClient client;
char scratch[8];
EXPECT_EQ(add_iovecs(client, iov.data(), static_cast<int>(iov.size()), scratch, sizeof(scratch)), 23u);
EXPECT_EQ(client.bytes, "0123456789abcdefghijXYZ");
ASSERT_EQ(client.flags.size(), 3u);
EXPECT_EQ(client.flags[0], STREAM_ADD_FLAG_COPY | STREAM_ADD_FLAG_MORE);
EXPECT_EQ(client.flags[1], STREAM_ADD_FLAG_COPY | STREAM_ADD_FLAG_MORE);
EXPECT_EQ(client.flags[2], STREAM_ADD_FLAG_COPY);
EXPECT_EQ(client.sends, 1);
}
TEST(AddIovecs, StopsAtPartialAccept) {
std::vector<std::string> parts{"0123456789", "abcdefghij"};
auto iov = pieces(parts);
FakeClient client;
client.space = 12;
char scratch[8];
EXPECT_EQ(add_iovecs(client, iov.data(), static_cast<int>(iov.size()), scratch, sizeof(scratch)), 12u);
EXPECT_EQ(client.bytes, "0123456789ab");
EXPECT_EQ(client.flags.size(), 2u); // nothing offered after the short add()
EXPECT_EQ(client.sends, 1);
}
TEST(AddIovecs, ZeroSpaceSendsNothing) {
std::vector<std::string> parts{"abc"};
auto iov = pieces(parts);
FakeClient client;
client.space = 0;
char scratch[8];
EXPECT_EQ(add_iovecs(client, iov.data(), static_cast<int>(iov.size()), scratch, sizeof(scratch)), 0u);
EXPECT_EQ(client.flags.size(), 1u);
EXPECT_EQ(client.sends, 0);
}
TEST(AddIovecs, EmptyPiecesAreSkipped) {
std::vector<std::string> parts{"", "ab", "", "c"};
auto iov = pieces(parts);
FakeClient client;
char scratch[8];
EXPECT_EQ(add_iovecs(client, iov.data(), static_cast<int>(iov.size()), scratch, sizeof(scratch)), 3u);
EXPECT_EQ(client.bytes, "abc");
ASSERT_EQ(client.flags.size(), 1u);
EXPECT_EQ(client.flags[0], STREAM_ADD_FLAG_COPY);
}
} // namespace esphome::web_server::testing
#endif // USE_HOST
@@ -1,5 +1,6 @@
#include <gtest/gtest.h>
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstring>
@@ -128,4 +129,77 @@ TEST(SseChunk, MatchesReferenceOnRandomMessages) {
}
}
// Queues one event behind the unsent bytes of src as append_chunk_() does; returns the new length
static size_t queue_event(uint8_t *dst, const uint8_t *src, size_t len, size_t sent, const std::string &message,
const char *event) {
char prefix[128];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), event, 0, 0, true);
size_t total = prefix_len;
for_each_chunk_piece(
message.data(), message.size(), [](void *ctx, const char *, size_t n) { *static_cast<size_t *>(ctx) += n; },
&total);
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
return queue_chunk(dst, src, len, sent, prefix, prefix_len, message.data(), message.size());
}
static std::string as_string(const uint8_t *buf, size_t len) { return {reinterpret_cast<const char *>(buf), len}; }
TEST(SseChunk, CopyChunkMatchesBuiltChunk) {
const std::string want = build_chunk("a\nb", 3, "log", 0, 0);
char prefix[128];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), "log", 0, 0, true);
std::memcpy(prefix, want.data(), CHUNK_HDR_LEN); // the header build_chunk() filled in
uint8_t buf[128];
EXPECT_EQ(copy_chunk(buf, prefix, prefix_len, "a\nb", 3), want.size());
EXPECT_EQ(as_string(buf, want.size()), want);
}
TEST(SseChunk, QueueIntoEmptyBuffer) {
const std::string want = build_chunk("a\nb", 3, "log", 0, 0);
uint8_t buf[128];
EXPECT_EQ(queue_event(buf, buf, 0, 0, "a\nb", "log"), want.size());
EXPECT_EQ(as_string(buf, want.size()), want);
}
TEST(SseChunk, QueueBehindPartlySentChunkCompactsInPlace) {
const std::string first = build_chunk(R"({"name":"ping"})", 15, "ping", 0, 0);
const std::string second = build_chunk(R"({"name":"group"})", 16, "sorting_group", 0, 0);
uint8_t buf[256];
std::copy(first.begin(), first.end(), buf);
const size_t sent = 7; // part of the first chunk is already on the wire
const size_t len = queue_event(buf, buf, first.size(), sent, R"({"name":"group"})", "sorting_group");
EXPECT_EQ(as_string(buf, len), first.substr(sent) + second);
}
TEST(SseChunk, QueueIntoGrownBufferKeepsOrder) {
const std::string first = build_chunk("first", 5, "log", 0, 0);
uint8_t old_buf[64];
std::copy(first.begin(), first.end(), old_buf);
uint8_t grown[256];
size_t len = queue_event(grown, old_buf, first.size(), 3, "second", "log");
len = queue_event(grown, grown, len, 0, "third\nline", "log");
EXPECT_EQ(as_string(grown, len),
first.substr(3) + build_chunk("second", 6, "log", 0, 0) + build_chunk("third\nline", 10, "log", 0, 0));
}
TEST(SseChunk, QueueAfterFirstChunkFullySent) {
const std::string first = build_chunk("x", 1, "log", 0, 0);
uint8_t buf[128];
std::copy(first.begin(), first.end(), buf);
const size_t len = queue_event(buf, buf, first.size(), first.size(), "y", "log");
EXPECT_EQ(as_string(buf, len), build_chunk("y", 1, "log", 0, 0));
}
TEST(SseChunk, TailQueueCap) {
constexpr size_t max = 1460;
EXPECT_EQ(tail_queue_cap(max + 1, 5000, max), 0u); // refused even when a large state grew the tail
EXPECT_EQ(tail_queue_cap(100, 300, max), 300u); // fits in place
EXPECT_EQ(tail_queue_cap(300, 300, max), 300u);
EXPECT_EQ(tail_queue_cap(301, 300, max), max / 2); // first growth: half
EXPECT_EQ(tail_queue_cap(max / 2, 0, max), max / 2);
EXPECT_EQ(tail_queue_cap(max / 2 + 1, max / 2, max), max); // then all of it
EXPECT_EQ(tail_queue_cap(max, max / 2, max), max);
EXPECT_EQ(tail_queue_cap(max, 2048, max), 2048u); // a larger tail is used as is
}
} // namespace esphome::web_server::testing
@@ -295,6 +295,22 @@ TEST(StallClock, AcrossMillisWraparound) {
EXPECT_EQ(clock.elapsed(start + TIMEOUT), TIMEOUT);
}
TEST(StallClock, StartOrExpiredStartsOnceAndExpiresAtTheTimeout) {
StallClock clock;
EXPECT_FALSE(clock.start_or_expired(1000, TIMEOUT)); // first call starts the clock
EXPECT_TRUE(clock.is_running());
EXPECT_FALSE(clock.start_or_expired(1000 + TIMEOUT - 1, TIMEOUT)); // a later call keeps the start
EXPECT_TRUE(clock.start_or_expired(1000 + TIMEOUT, TIMEOUT));
EXPECT_EQ(clock.elapsed(1000 + TIMEOUT), TIMEOUT);
}
TEST(IovecsTotal, SumsEveryElementIncludingEmptyOnes) {
char a[3], b[5];
struct iovec iov[] = {{a, sizeof(a)}, {nullptr, 0}, {b, sizeof(b)}};
EXPECT_EQ(iovecs_total(iov, 3), 8U);
EXPECT_EQ(iovecs_total(iov, 0), 0U);
}
} // namespace esphome::web_server::testing
#endif // USE_HOST