Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
WebServer: Implement "Connection: keep-alive"
Responses can be sent in chunked form or not on an individual basis by setting ResponseIface_WebServer::isChunked. Currently, we set this to true for all HTTP/1.1 requests. (In the future, it should be decided by examining the request headers.) When true, we return the following header fields: Transfer-Encoding: chunked\r\n Connection: keep-alive\r\n The message body is then sent in chunked form using OutPipe_HTTPChunked, which implements the protocol described by https://en.wikipedia.org/wiki/Chunked_transfer_encoding. The connection is kept alive for additional requests.
- Loading branch information
Showing
11 changed files
with
207 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /*------------------------------------ | ||
| ///\ Plywood C++ Framework | ||
| \\\/ https://plywood.arc80.com/ | ||
| ------------------------------------*/ | ||
| #include <web-common/Core.h> | ||
| #include <web-common/OutPipe_HTTPChunked.h> | ||
|
|
||
| namespace ply { | ||
| namespace web { | ||
|
|
||
| PLY_NO_INLINE void OutPipe_HTTPChunked_destroy(OutPipe* outPipe_) { | ||
| OutPipe_HTTPChunked* outPipe = static_cast<OutPipe_HTTPChunked*>(outPipe_); | ||
| // End of chunk stream | ||
| *outPipe->outs->strWriter() << "0\r\n\r\n"; | ||
| outPipe->outs->flushMem(); | ||
| } | ||
|
|
||
| PLY_NO_INLINE bool OutPipe_HTTPChunked_write(OutPipe* outPipe_, ConstBufferView srcBuf) { | ||
| OutPipe_HTTPChunked* outPipe = static_cast<OutPipe_HTTPChunked*>(outPipe_); | ||
| if (outPipe->chunkMode) { | ||
| PLY_ASSERT(srcBuf.numBytes > 0); | ||
| outPipe->outs->strWriter()->format("{}\r\n", fmt::Hex{srcBuf.numBytes, true}); | ||
| } | ||
| outPipe->outs->write(srcBuf); | ||
| if (outPipe->chunkMode) { | ||
| *outPipe->outs->strWriter() << "\r\n"; | ||
| } | ||
| return !outPipe->outs->atEOF(); | ||
| } | ||
|
|
||
| PLY_NO_INLINE bool OutPipe_HTTPChunked_flush(OutPipe* outPipe_, bool toDevice) { | ||
| OutPipe_HTTPChunked* outPipe = static_cast<OutPipe_HTTPChunked*>(outPipe_); | ||
| return outPipe->outs->flush(toDevice); | ||
| } | ||
|
|
||
| OutPipe::Funcs OutPipe_HTTPChunked::Funcs_ = { | ||
| OutPipe_HTTPChunked_destroy, | ||
| OutPipe_HTTPChunked_write, | ||
| OutPipe_HTTPChunked_flush, | ||
| OutPipe::seek_Empty, | ||
| }; | ||
|
|
||
| } // namespace web | ||
| } // namespace ply |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /*------------------------------------ | ||
| ///\ Plywood C++ Framework | ||
| \\\/ https://plywood.arc80.com/ | ||
| ------------------------------------*/ | ||
| #pragma once | ||
| #include <web-common/Core.h> | ||
|
|
||
| namespace ply { | ||
| namespace web { | ||
|
|
||
| //----------------------------------------------------------------------- | ||
| // OutPipe_HTTPChunked | ||
| //----------------------------------------------------------------------- | ||
| struct OutPipe_HTTPChunked : OutPipe { | ||
| static Funcs Funcs_; | ||
| OptionallyOwned<OutStream> outs; | ||
| bool chunkMode = false; | ||
|
|
||
| PLY_INLINE OutPipe_HTTPChunked(OptionallyOwned<OutStream>&& outs) | ||
| : OutPipe{&Funcs_}, outs{std::move(outs)} { | ||
| } | ||
| PLY_INLINE void setChunkMode(bool chunkMode) { | ||
| this->chunkMode = chunkMode; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace web | ||
| } // namespace ply |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.