ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
sse_chunk.cpp
Go to the documentation of this file.
1#include "sse_chunk.h"
2
3#include <cinttypes>
4#include <cstring>
5
6#include "esphome/core/hal.h"
8
9namespace esphome::web_server {
10
11const char CHUNK_END[] PROGMEM = "\r\n";
12const char SSE_SEP[] PROGMEM = "\r\ndata: ";
13const char SSE_SUFFIX[] PROGMEM = "\r\n\r\n\r\n";
14const char SSE_STATUS[] PROGMEM = "200 OK";
15const char SSE_CONTENT_TYPE[] PROGMEM = "text/event-stream";
16const char SSE_FIRST_CHUNK[] PROGMEM = "2\r\n\r\n\r\n";
17static const char SSE_TRANSFER_ENCODING[] PROGMEM = "Transfer-Encoding";
18static const char SSE_CHUNKED[] PROGMEM = "chunked";
19static const char SSE_CACHE_CONTROL[] PROGMEM = "Cache-Control";
20static const char SSE_NO_CACHE[] PROGMEM = "no-cache";
21static const char SSE_CONNECTION[] PROGMEM = "Connection";
22static const char SSE_KEEP_ALIVE[] PROGMEM = "keep-alive";
23// Word aligned pointers, which ESP8266 reads straight from flash
25 {SSE_TRANSFER_ENCODING, SSE_CHUNKED},
26 {SSE_CACHE_CONTROL, SSE_NO_CACHE},
27 {SSE_CONNECTION, SSE_KEEP_ALIVE},
28};
29
30void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx) {
31 if (message == nullptr) {
32 sink(ctx, CHUNK_END, CHUNK_END_LEN);
33 return;
34 }
35 const char *pos = message;
36 const char *end = message + message_len;
37 for (;;) {
38 const size_t remaining = end - pos;
39 const auto *n = static_cast<const char *>(memchr(pos, '\n', remaining));
40 // Only a \r before the next \n can end this line, so the search stops there instead of
41 // rescanning the rest of the message for every line
42 const auto *r = static_cast<const char *>(memchr(pos, '\r', n != nullptr ? n - pos : remaining));
43 if (n == nullptr && r == nullptr) {
44 sink(ctx, pos, remaining);
45 break;
46 }
47 const char *brk = (r != nullptr && (n == nullptr || r < n)) ? r : n;
48 sink(ctx, pos, brk - pos);
49 pos = brk + ((brk == r && brk + 1 == n) ? 2 : 1);
50 if (pos >= end) {
51 break;
52 }
53 sink(ctx, SSE_SEP, SSE_SEP_LEN);
54 }
55 sink(ctx, SSE_SUFFIX, SSE_SUFFIX_LEN);
56}
57
58size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id, uint32_t reconnect, bool with_data) {
59 size_t len = CHUNK_HDR_LEN;
60 if (reconnect) {
61 len = buf_append_printf(buf, size, len, "retry: %" PRIu32 "\r\n", reconnect);
62 }
63 if (id) {
64 len = buf_append_printf(buf, size, len, "id: %" PRIu32 "\r\n", id);
65 }
66 if (event && *event) {
67 len = buf_append_str(buf, size, len, "event: ");
68 len = buf_append_str_p(buf, size, len, event);
69 len = buf_append_str(buf, size, len, "\r\n");
70 }
71 if (with_data) {
72 len = buf_append_str(buf, size, len, "data: ");
73 }
74 return len;
75}
76
77size_t copy_chunk(uint8_t *dst, const char *prefix, size_t prefix_len, const char *message, size_t message_len) {
78 uint8_t *const start = dst;
79 std::memcpy(dst, prefix, prefix_len);
80 dst += prefix_len;
82 message, message_len,
83 [](void *ctx, const char *piece, size_t len) {
84 auto &out = *static_cast<uint8_t **>(ctx);
86 out += len;
87 },
88 &dst);
89 return dst - start;
90}
91
92size_t tail_queue_cap(size_t need, size_t cap, size_t max) {
93 if (need > max) {
94 return 0;
95 }
96 if (need <= cap) {
97 return cap;
98 }
99 return need <= max / 2 ? max / 2 : max;
100}
101
102size_t queue_chunk(uint8_t *dst, const uint8_t *src, size_t len, size_t sent, const char *prefix, size_t prefix_len,
103 const char *message, size_t message_len) {
104 const size_t pending = len - sent;
105 std::memmove(dst, src + sent, pending);
106 return pending + copy_chunk(dst + pending, prefix, prefix_len, message, message_len);
107}
108
109} // namespace esphome::web_server
const LogString * message
Definition component.cpp:35
std::vector< uint8_t > & out
Definition gif.cpp:49
void stream_memcpy(void *dst, const void *src, size_t len)
Definition stream_head.h:24
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 char CHUNK_END[]
Definition sse_chunk.cpp:11
const char SSE_SEP[]
Definition sse_chunk.cpp:12
const web_server_base::HttpHeader SSE_HEADERS[SSE_HEADERS_COUNT]
Definition sse_chunk.cpp:24
const char SSE_STATUS[]
Definition sse_chunk.cpp:14
void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx)
Definition sse_chunk.cpp:30
constexpr uint8_t INDEX_GZ[] PROGMEM
size_t copy_chunk(uint8_t *dst, const char *prefix, size_t prefix_len, const char *message, size_t message_len)
Definition sse_chunk.cpp:77
constexpr size_t SSE_SEP_LEN
Definition sse_chunk.h:25
size_t tail_queue_cap(size_t need, size_t cap, size_t max)
Definition sse_chunk.cpp:92
const char SSE_CONTENT_TYPE[]
Definition sse_chunk.cpp:15
const char SSE_SUFFIX[]
Definition sse_chunk.cpp:13
constexpr size_t SSE_HEADERS_COUNT
Definition sse_chunk.h:34
void(*)(void *ctx, const char *piece, size_t len) ChunkPieceSink
Definition sse_chunk.h:40
const char SSE_FIRST_CHUNK[]
Definition sse_chunk.cpp:16
size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id, uint32_t reconnect, bool with_data)
Definition sse_chunk.cpp:58
constexpr size_t SSE_SUFFIX_LEN
Definition sse_chunk.h:28
size_t buf_append_str_p(char *buf, size_t size, size_t pos, PGM_P str)
Safely append a PROGMEM string to buffer, returning new position (capped at size).
Definition helpers.cpp:32
size_t buf_append_str(char *buf, size_t size, size_t pos, const char *str)
Safely append a string to buffer, returning new position (capped at size).
Definition helpers.h:1605
const void size_t len
Definition hal.h:68
uint16_t size
Definition helpers.cpp:44
size_t size_t pos
Definition helpers.h:1530
const void * src
Definition hal.h:68
static void uint32_t
uint8_t end[39]
Definition sun_gtil2.cpp:17