Skip to content

Commit 8a1f360

Browse files
Add beginWrite for "flushing" headers
Co-authored-by: notaphplover <notaphplover@users.noreply.github.com>
1 parent 7fd2be6 commit 8a1f360

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/HttpResponse.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Authored by Alex Hultman, 2018-2025.
2+
* Authored by Alex Hultman, 2018-2026.
33
* Intellectual property of third-party.
44
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -427,6 +427,25 @@ struct HttpResponse : public AsyncSocket<SSL> {
427427
return this;
428428
}
429429

430+
/* Begin writing the response body. Useful for chunked encodings whose first chunk is not yet known */
431+
void beginWrite() {
432+
/* Write status if not already done */
433+
writeStatus(HTTP_200_OK);
434+
435+
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
436+
437+
if (!(httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED)) {
438+
/* Write mark on first call to write */
439+
writeMark();
440+
441+
writeHeader("Transfer-Encoding", "chunked");
442+
httpResponseData->state |= HttpResponseData<SSL>::HTTP_WRITE_CALLED;
443+
444+
/* Start of the body */
445+
Super::write("\r\n", 2);
446+
}
447+
}
448+
430449
/* End without a body (no content-length) or end with a spoofed content-length. */
431450
void endWithoutBody(std::optional<size_t> reportedContentLength = std::nullopt, bool closeConnection = false) {
432451
if (reportedContentLength.has_value()) {

0 commit comments

Comments
 (0)