ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
sse_chunk.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
8
9namespace esphome::web_server {
10
11// Wire framing of one Server-Sent-Events event inside the chunked /events response:
12// "%08x\r\n" + fields + "data: " line ["\r\ndata: " line]... "\r\n" + "\r\n" + "\r\n"
13// The chunk header and the final CRLF (the chunk terminator) do not count toward the
14// chunk length; everything between them does.
15
16// The chunk header comes from web_server_base; the terminator is also the only piece of a null message (no data
17// line, no blank line)
20// A PROGMEM copy that shadows web_server_base::CHUNK_END, whose constexpr array sits in RAM on ESP8266
21extern const char CHUNK_END[];
23// Between two data lines: the end of one and the prefix of the next
24extern const char SSE_SEP[];
25constexpr size_t SSE_SEP_LEN = 8;
26// End of the last data line, the blank line ending the event, and the chunk terminator
27extern const char SSE_SUFFIX[];
28constexpr size_t SSE_SUFFIX_LEN = 6;
29
30// Status, Content-Type and headers of the /events response head
31// In flash on ESP8266; the stream helpers read them with the _P functions
32extern const char SSE_STATUS[];
33extern const char SSE_CONTENT_TYPE[];
34constexpr size_t SSE_HEADERS_COUNT = 3;
36// Hand-framed first chunk holding an empty event (one blank line), as httpd_resp_send_chunk() sent it before
37extern const char SSE_FIRST_CHUNK[];
38
39// Receives one piece of the chunk; ctx is whatever the caller passed to for_each_chunk_piece()
40using ChunkPieceSink = void (*)(void *ctx, const char *piece, size_t len);
41
42// Calls sink for each piece after the prefix: data lines split on \n, \r or \r\n with SSE_SEP
43// between them and SSE_SUFFIX after the last (as ESPAsyncWebServer: a trailing break adds no
44// empty line, an inner one is kept). A null message yields only the chunk terminator.
45void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx);
46
47// Writes the chunk header placeholder, the retry/id/event lines and, with_data, the first
48// "data: " into buf. Returns the prefix length; size - 1 or more means the event name did not fit.
49size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id, uint32_t reconnect, bool with_data);
50
51// Copies the prefix and the message's pieces to dst; returns the bytes written
52size_t copy_chunk(uint8_t *dst, const char *prefix, size_t prefix_len, const char *message, size_t message_len);
53
54// Tail capacity for need bytes: cap when they fit, else max / 2, then max; 0 when need > max
55size_t tail_queue_cap(size_t need, size_t cap, size_t max);
56
57// Moves the unsent bytes [sent, len) of src to the front of dst (dst may be src) and copies the chunk after them.
58// Returns the new length.
59size_t queue_chunk(uint8_t *dst, const uint8_t *src, size_t len, size_t sent, const char *prefix, size_t prefix_len,
60 const char *message, size_t message_len);
61
62} // namespace esphome::web_server
const LogString * message
Definition component.cpp:35
constexpr size_t CHUNK_HDR_LEN
Definition chunk.h:13
void write_chunk_header(char *buf, size_t chunk_len)
Definition chunk.h:19
constexpr size_t CHUNK_END_LEN
Definition chunk.h:16
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
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
const void size_t len
Definition hal.h:68
uint16_t size
Definition helpers.cpp:44
const void * src
Definition hal.h:68
static void uint32_t