ESPHome 2026.11.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
7
8// Wire framing of one Server-Sent-Events event inside the chunked /events response:
9// "%08x\r\n" + fields + "data: " line ["\r\ndata: " line]... "\r\n" + "\r\n" + "\r\n"
10// The chunk header and the final CRLF (the chunk terminator) do not count toward the
11// chunk length; everything between them does.
12
13// HTTP chunk header "%08x\r\n"
14constexpr size_t CHUNK_HDR_LEN = 10;
15// Chunk terminator, also the only piece of a null message (no data line, no blank line)
16constexpr char CHUNK_END[] = "\r\n";
17constexpr size_t CHUNK_END_LEN = sizeof(CHUNK_END) - 1;
18// Between two data lines: the end of one and the prefix of the next
19constexpr char SSE_SEP[] = "\r\ndata: ";
20constexpr size_t SSE_SEP_LEN = sizeof(SSE_SEP) - 1;
21// End of the last data line, the blank line ending the event, and the chunk terminator
22constexpr char SSE_SUFFIX[] = "\r\n\r\n\r\n";
23constexpr size_t SSE_SUFFIX_LEN = sizeof(SSE_SUFFIX) - 1;
24
25// Receives one piece of the chunk; ctx is whatever the caller passed to for_each_chunk_piece()
26using ChunkPieceSink = void (*)(void *ctx, const char *piece, size_t len);
27
28// Calls sink for each piece after the prefix: data lines split on \n, \r or \r\n with SSE_SEP
29// between them and SSE_SUFFIX after the last (as ESPAsyncWebServer: a trailing break adds no
30// empty line, an inner one is kept). A null message yields only the chunk terminator.
31void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx);
32
33// Writes the chunk header placeholder, the retry/id/event lines and, with_data, the first
34// "data: " into buf. Returns the prefix length; size - 1 or more means the event name did not fit.
35size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id, uint32_t reconnect, bool with_data);
36
37// Fills the header placeholder at the start of buf once the chunk length is known
38void write_chunk_header(char *buf, size_t chunk_len);
39
40} // namespace esphome::web_server_idf
const LogString * message
Definition component.cpp:35
constexpr size_t CHUNK_END_LEN
Definition sse_chunk.h:17
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:38
constexpr size_t SSE_SUFFIX_LEN
Definition sse_chunk.h:23
constexpr char CHUNK_END[]
Definition sse_chunk.h:16
constexpr char SSE_SEP[]
Definition sse_chunk.h:19
constexpr size_t SSE_SEP_LEN
Definition sse_chunk.h:20
void(*)(void *ctx, const char *piece, size_t len) ChunkPieceSink
Definition sse_chunk.h:26
void write_chunk_header(char *buf, size_t chunk_len)
Definition sse_chunk.cpp:57
constexpr size_t CHUNK_HDR_LEN
Definition sse_chunk.h:14
constexpr char SSE_SUFFIX[]
Definition sse_chunk.h:22
void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx)
Definition sse_chunk.cpp:10
const void size_t len
Definition hal.h:68
uint16_t size
Definition helpers.cpp:34
static void uint32_t