diff --git a/src/HttpResponse.h b/src/HttpResponse.h index fb2e3b1a9..9d7d0513e 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -422,6 +422,25 @@ struct HttpResponse : public AsyncSocket { return this; } + /* Begin writing the response body. Useful for chunked encodings whose first chunk is not yet known */ + void beginWrite() { + /* Write status if not already done */ + writeStatus(HTTP_200_OK); + + HttpResponseData *httpResponseData = getHttpResponseData(); + + if (!(httpResponseData->state & HttpResponseData::HTTP_WRITE_CALLED)) { + /* Write mark on first call to write */ + writeMark(); + + writeHeader("Transfer-Encoding", "chunked"); + httpResponseData->state |= HttpResponseData::HTTP_WRITE_CALLED; + + /* Start of the body */ + Super::write("\r\n", 2); + } + } + /* End without a body (no content-length) or end with a spoofed content-length. */ void endWithoutBody(std::optional reportedContentLength = std::nullopt, bool closeConnection = false) { if (reportedContentLength.has_value()) { diff --git a/tests/Makefile b/tests/Makefile deleted file mode 100644 index 86dc82b9b..000000000 --- a/tests/Makefile +++ /dev/null @@ -1,32 +0,0 @@ -default: - $(CXX) -std=c++17 -fsanitize=address Query.cpp -o Query - ./Query - $(CXX) -std=c++17 -fsanitize=address ChunkedEncoding.cpp -o ChunkedEncoding - ./ChunkedEncoding - $(CXX) -std=c++17 -fsanitize=address TopicTree.cpp -o TopicTree - ./TopicTree - $(CXX) -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG -std=c++17 -g -fsanitize=address HttpRouter.cpp -o HttpRouter - ./HttpRouter - $(CXX) -std=c++17 -fsanitize=address BloomFilter.cpp -o BloomFilter - ./BloomFilter - $(CXX) -std=c++17 -fsanitize=address ExtensionsNegotiator.cpp -o ExtensionsNegotiator - ./ExtensionsNegotiator - $(CXX) -std=c++17 -fsanitize=address HttpParser.cpp -o HttpParser - ./HttpParser - -performance: - $(CXX) -std=c++17 HttpRouter.cpp -O3 -o HttpRouter - ./HttpRouter - -smoke: - ../Crc32 & - sleep 1 - ~/.deno/bin/deno run --allow-net smoke.mjs - node smoke.mjs - pkill Crc32 - -compliance: - ../EchoBody & - sleep 1 - ~/.deno/bin/deno run --allow-net ../h1spec/http_test.ts localhost 3000 - pkill EchoBody