Skip to content

Commit a16c068

Browse files
author
shartung
committed
Tighten tryWrite example and chunk header handling
1 parent a5c168c commit a16c068

2 files changed

Lines changed: 2 additions & 4 deletions

File tree

examples/ChunkedResponse.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace {
1111
const std::string payload(16 * 1024 * 1024, 'x');
1212

1313
struct ResponseState {
14-
uintmax_t baseOffset = 0;
1514
bool aborted = false;
1615
};
1716

@@ -21,7 +20,7 @@ bool tryWriteLoop(uWS::HttpResponse<SSL> *res, ResponseState *state) {
2120
return true;
2221
}
2322

24-
uintmax_t sent = res->getWriteOffset() - state->baseOffset;
23+
uintmax_t sent = res->getWriteOffset();
2524
std::string_view remaining = payload;
2625
remaining.remove_prefix((size_t) sent);
2726
if (res->tryWrite(remaining)) {
@@ -42,7 +41,6 @@ int main() {
4241
.passphrase = "1234"
4342
}).get("/*", [](auto *res, auto */*req*/) {
4443
auto state = std::make_shared<ResponseState>();
45-
state->baseOffset = res->getWriteOffset();
4644

4745
res->writeHeader("Content-Type", "application/octet-stream");
4846
res->onAborted([state]() {

src/HttpResponse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ struct HttpResponse : public AsyncSocket<SSL> {
113113
bool failed = false;
114114
if (data.length()) {
115115
if (!insideChunk) {
116-
char chunkHeader[34];
116+
char chunkHeader[12];
117117
unsigned int chunkHeaderLength = formatChunkHeader((unsigned int) data.length(), chunkHeader);
118118
/* A chunk header must never be optional, or getWriteOffset/onWritable semantics would break. */
119119
failed = Super::write(chunkHeader, (int) chunkHeaderLength, false).second;

0 commit comments

Comments
 (0)