ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
stream_head.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <cstdint>
6#include <cstring>
7#include <span>
8
9#include <strings.h>
10
12#ifdef USE_ESP8266
13#include <pgmspace.h>
14#endif
15
16// Pure helpers of AsyncStreamResponse, kept free of httpd and lwIP so they build on the host.
17// The iovec type is a template parameter: lwIP defines its own on the device, POSIX on the host.
18
20
21// Gather pieces may be framing literals in flash on ESP8266 (mapped from 0x40200000), where only word reads work.
22// RAM payloads, the hot case, take memcpy.
23#ifdef USE_ESP8266
24inline void stream_memcpy(void *dst, const void *src, size_t len) {
25 if (reinterpret_cast<uintptr_t>(src) < 0x40200000) {
26 std::memcpy(dst, src, len);
27 } else {
28 memcpy_P(dst, src, len);
29 }
30}
31#else
32inline void stream_memcpy(void *dst, const void *src, size_t len) { std::memcpy(dst, src, len); }
33#endif
34
35struct HttpHeader {
36 const char *name;
37 const char *value;
38};
39
41template<typename Iovec> size_t iovecs_total(const Iovec *iov, int count) {
42 size_t total = 0;
43 for (int i = 0; i < count; i++) {
44 total += iov[i].iov_len;
45 }
46 return total;
47}
48
51template<typename Iovec> void advance_iovecs(Iovec *&iov, int &count, size_t sent) {
52 while (count > 0 && sent >= iov->iov_len) {
53 sent -= iov->iov_len;
54 ++iov;
55 --count;
56 }
57 if (count > 0) {
58 iov->iov_base = static_cast<char *>(iov->iov_base) + sent;
59 iov->iov_len -= sent;
60 }
61}
62
64constexpr size_t stream_head_iov_count(size_t header_count) { return 4 + 4 * header_count + 2; }
65
67static constexpr int STREAM_HEAD_MAX_IOV = 24;
68
70inline bool has_header(std::span<const HttpHeader> headers, const char *name) {
71 for (const auto &header : headers) {
72 if (strcasecmp(name, header.name) == 0) {
73 return true;
74 }
75 }
76 return false;
77}
78
83template<typename Iovec, typename Send>
84bool write_stream_head(Send &&send, const char *status, const char *content_type, std::span<const HttpHeader> headers,
85 std::span<const HttpHeader> default_headers, const char *body) {
86 struct Batch {
87 Send &send;
88 int count{0};
89 bool ok{true};
90 Iovec iov[STREAM_HEAD_MAX_IOV]; // left uninitialized on purpose
91
92 void flush() {
93 if (this->ok && this->count > 0) {
94 this->ok = this->send(this->iov, this->count);
95 }
96 this->count = 0;
97 }
98 void add(const char *buf, size_t len) {
99 if (this->count == STREAM_HEAD_MAX_IOV) {
100 this->flush();
101 }
102 this->iov[this->count++] = {const_cast<char *>(buf), len};
103 }
104 void add(const char *str) { this->add(str, strlen(str)); }
105 void add_header(const HttpHeader &header) {
106 this->add("\r\n", 2);
107 this->add(header.name);
108 this->add(": ", 2);
109 this->add(header.value);
110 }
111 } batch{send};
112
113 batch.add("HTTP/1.1 ", 9);
114 batch.add(status);
115 batch.add("\r\nContent-Type: ", 16);
116 batch.add(content_type);
117 for (const auto &header : headers) {
118 batch.add_header(header);
119 }
120 for (const auto &header : default_headers) {
121 if (!has_header(headers, header.name)) {
122 batch.add_header(header);
123 }
124 }
125 batch.add("\r\n\r\n", 4);
126 if (body != nullptr) {
127 batch.add(body);
128 }
129 batch.flush();
130 return batch.ok;
131}
132
134static constexpr uint8_t STREAM_ADD_FLAG_COPY = 0x01;
135static constexpr uint8_t STREAM_ADD_FLAG_MORE = 0x02;
136
140template<typename Client, typename Iovec>
141size_t add_iovecs(Client &client, const Iovec *iov, int count, char *scratch, size_t scratch_size) {
142 size_t taken = 0;
143 size_t fill = 0;
144 int i = 0;
145 size_t offset = 0; // bytes of iov[i] already copied
146 while (i < count) {
147 const size_t piece = std::min(iov[i].iov_len - offset, scratch_size - fill);
148 stream_memcpy(scratch + fill, static_cast<const char *>(iov[i].iov_base) + offset, piece);
149 fill += piece;
150 offset += piece;
151 if (offset == iov[i].iov_len) {
152 ++i;
153 offset = 0;
154 }
155 if (fill < scratch_size && i < count) {
156 continue;
157 }
158 const uint8_t flags = STREAM_ADD_FLAG_COPY | (i < count ? STREAM_ADD_FLAG_MORE : 0);
159 const size_t added = client.add(scratch, fill, flags);
160 taken += added;
161 if (added < fill) {
162 break;
163 }
164 fill = 0;
165 }
166 if (taken != 0) {
167 client.send();
168 }
169 return taken;
170}
171
175 public:
177 void start(uint32_t now) {
178 if (this->started_ms_ == 0) {
179 this->started_ms_ = now != 0 ? now : 1;
180 }
181 }
182 void clear() { this->started_ms_ = 0; }
183 bool is_running() const { return this->started_ms_ != 0; }
185 bool is_expired(uint32_t now, uint32_t timeout_ms) const {
186 return this->started_ms_ != 0 && static_cast<int32_t>(now - (this->started_ms_ + timeout_ms)) >= 0;
187 }
188 uint32_t elapsed(uint32_t now) const { return now - this->started_ms_; }
190 bool start_or_expired(uint32_t now, uint32_t timeout_ms) {
191 this->start(now);
192 return this->is_expired(now, timeout_ms);
193 }
194
195 protected:
197};
198
199} // namespace esphome::web_server_base
uint8_t status
Definition bl0942.h:8
How long sends have made no progress.
void start(uint32_t now)
Starts the clock; a running clock keeps its start time.
bool start_or_expired(uint32_t now, uint32_t timeout_ms)
Starts the clock if needed; true once it has run for timeout_ms.
uint32_t elapsed(uint32_t now) const
bool is_expired(uint32_t now, uint32_t timeout_ms) const
True once the clock has run for timeout_ms.
uint16_t flags
unsigned count
Definition gif.cpp:51
size_t iovecs_total(const Iovec *iov, int count)
Bytes in a gather list.
Definition stream_head.h:41
char name[sizeof(ORIGIN)]
constexpr size_t stream_head_iov_count(size_t header_count)
Pieces in a head: 4 for the status line and Content-Type, 4 per header, the blank line and the body.
Definition stream_head.h:64
void stream_memcpy(void *dst, const void *src, size_t len)
Definition stream_head.h:24
bool has_header(std::span< const HttpHeader > headers, const char *name)
True if headers holds a header named name, compared case insensitively.
Definition stream_head.h:70
void advance_iovecs(Iovec *&iov, int &count, size_t sent)
Drops the first sent bytes from a gather list after a write; a short write resumes inside the element...
Definition stream_head.h:51
bool write_stream_head(Send &&send, const char *status, const char *content_type, std::span< const HttpHeader > headers, std::span< const HttpHeader > default_headers, const char *body)
Gathers the head of a stream response: "HTTP/1.1 <status>", Content-Type, headers,...
Definition stream_head.h:84
size_t add_iovecs(Client &client, const Iovec *iov, int count, char *scratch, size_t scratch_size)
Queues a gather list on an AsyncClient-like client through scratch, so each add() carries up to scrat...
const void size_t len
Definition hal.h:68
const void * src
Definition hal.h:68
static void uint32_t
watchdog_hw scratch[0]
uint32_t len