From 0709ec05a64b067a76138c987bdb64fe6de9fb3b Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Mon, 4 Feb 2019 00:39:45 +0100 Subject: [PATCH] Even zero is a valid content-length --- src/App.h | 2 +- src/HttpResponse.h | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/App.h b/src/App.h index 21800b7..afd82b4 100644 --- a/src/App.h +++ b/src/App.h @@ -176,7 +176,7 @@ public: } /* This will add our mark */ - res->end(); + res->upgrade(); /* Move any backpressure */ std::string backpressure(std::move(((AsyncSocketData *) res->getHttpResponseData())->buffer)); diff --git a/src/HttpResponse.h b/src/HttpResponse.h index 897f49d..f81ce52 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -82,7 +82,7 @@ private: /* Returns true on success, indicating that it might be feasible to write more data. * Will start timeout if stream reaches totalSize or write failure. */ - bool internalEnd(std::string_view data, int totalSize, bool optional) { + bool internalEnd(std::string_view data, int totalSize, bool optional, bool allowContentLength = true) { /* Write status if not already done */ writeStatus(HTTP_200_OK); @@ -120,9 +120,9 @@ private: /* Write mark, this propagates to WebSockets too */ writeMark(); - /* Ending with no response should not leave any content-length */ - if (totalSize) { - /* We have a known send size */ + /* WebSocket upgrades does not allow content-length */ + if (allowContentLength) { + /* Even zero is a valid content-length */ Super::write("Content-Length: ", 16); writeUnsigned(totalSize); Super::write("\r\n\r\n", 4); @@ -158,6 +158,11 @@ private: } } + /* This call is identical to end, but will never write content-length and is thus suitable for upgrades */ + void upgrade() { + internalEnd({nullptr, 0}, 0, false, false); + } + public: /* Immediately terminate this Http response */ using Super::close;