20static_assert(STREAM_ADD_FLAG_COPY == ASYNC_WRITE_FLAG_COPY && STREAM_ADD_FLAG_MORE == ASYNC_WRITE_FLAG_MORE,
21 "add_iovecs() flags must match the TCP library");
24static constexpr size_t ADD_SCRATCH_SIZE = 256;
28static void close_now(AsyncClient *client) {
38struct AsyncClientPcb : AsyncClient {
39 static tcp_pcb *of(AsyncClient *client) {
return client->*(&AsyncClientPcb::_pcb); }
46static bool abort_now(AsyncClient *client) {
53 tcp_pcb *pcb = AsyncClientPcb::of(client);
55 tcp_err(pcb,
nullptr);
63static String head_string(
const char *str) {
return String(
reinterpret_cast<const __FlashStringHelper *
>(str)); }
65static const char *head_string(
const char *str) {
return str; }
69static int status_code(
const char *status) {
71 for (
int i = 0; i < 3; i++) {
72 code = code * 10 + (
progmem_read_byte(
reinterpret_cast<const uint8_t *
>(status + i)) -
'0');
80class StreamHandoffResponse final :
public AsyncWebServerResponse {
82 StreamHandoffResponse(AsyncStreamResponse *stream,
const char *status,
const char *content_type,
83 std::span<const HttpHeader> headers,
const char *body)
84 : stream_(stream), headers_(headers), body_(body) {
85 this->_code = status_code(status);
86 this->_contentType = head_string(content_type);
87 this->_sendContentLength =
false;
89 ~StreamHandoffResponse()
override {
90 if (this->stream_ !=
nullptr) {
95 void _respond(AsyncWebServerRequest *request)
override {
96 AsyncClient *client = request->client();
98 for (
const auto &header : this->headers_) {
99 this->addHeader(head_string(header.name), head_string(header.value));
102 this->_assembleHead(head, request->version());
103 if (this->body_ !=
nullptr) {
104 head.concat(head_string(this->body_));
106 const size_t added = client->add(head.c_str(), head.length());
112 const size_t rest_len = head.length() - added;
115 if (head_rest !=
nullptr) {
116 std::memcpy(head_rest.get(), head.c_str() + added, rest_len + 1);
119 const bool head_kept = rest_len == 0 || head_rest !=
nullptr;
121 AsyncStreamResponse *stream = this->stream_;
122 this->stream_ =
nullptr;
123 stream->
attach_(request->clientRelease(), std::move(head_rest), head_kept);
125 bool _sourceValid()
const override {
return true; }
129 AsyncStreamResponse *stream_;
130 std::span<const HttpHeader> headers_;
135 std::span<const HttpHeader> headers,
const char *body) {
141 client->setRxTimeout(0);
144 client->setNoDelay(
true);
148 client->onDisconnect(
149 [](
void *arg, AsyncClient *) {
151 std::memory_order_release);
155 [](
void *arg, AsyncClient *,
uint32_t) {
173 AsyncClient *client = this->
client_;
174 if (client ==
nullptr) {
179 client->onDisconnect(
nullptr,
nullptr);
180 client->onTimeout(
nullptr,
nullptr);
216 if (!this->
client_->canSend()) {
218 this->close_stuck_(now - this->
last_ack_ms_, LOG_STR(
"without an ACK"));
226 const size_t space_before = this->
client_->space();
229 char scratch[ADD_SCRATCH_SIZE];
232 const size_t space_after = this->
client_->space();
236 if (space_after + sent > space_before) {
250 this->close_stuck_(now - this->
last_ack_ms_, LOG_STR(
"without an ACK"));
253 return static_cast<ssize_t>(sent);
256 return this->
stall_or_close_(LOG_STR(
"without send progress")) ? -1 : 0;
261 return this->
write(&iov, 1);
273void AsyncStreamResponse::close_stuck_(
uint32_t elapsed_ms,
const LogString *reason) {
274 ESP_LOGW(TAG,
"Closing stuck stream after %" PRIu32
" ms %s", elapsed_ms, LOG_STR_ARG(
reason));
300 }
else if (!abort_now(this->
client_)) {
317 const size_t len = strlen(rest.get());
319 if (sent >= 0 &&
static_cast<size_t>(sent) <
len) {
320 memmove(rest.get(), rest.get() + sent,
len - sent + 1);
void begin(AsyncWebServerRequest *request, const char *status, const char *content_type, std::span< const HttpHeader > headers, const char *body=nullptr)
Network context, from a request handler, once.
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...