From d597041ca47b994542438448a3b9afa75966e4e0 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sat, 29 Dec 2018 14:27:50 +0100 Subject: [PATCH] Don't leave content-length on empty response --- src/App.h | 11 +++++++---- src/AsyncSocket.h | 5 ----- src/HttpContext.h | 1 + src/HttpContextData.h | 2 +- src/HttpResponse.h | 13 +++++++++---- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/App.h b/src/App.h index 7afbaa4..fa54e7c 100644 --- a/src/App.h +++ b/src/App.h @@ -50,6 +50,10 @@ private: using StaticDispatch::static_dispatch; public: + void registerTag() { + + } + ~TemplatedApp() { } @@ -135,8 +139,6 @@ public: ExtensionsNegotiator extensionsNegotiator(wantedOptions); extensionsNegotiator.readOffer(extensions); - std::cout << extensions << " => " << extensionsNegotiator.generateOffer() << std::endl; - /* Todo: remove these mid string copies */ res->writeHeader("Sec-WebSocket-Extensions", extensionsNegotiator.generateOffer()); @@ -155,6 +157,8 @@ public: /* Add mark, we don't want to end anything */ res->writeHeader("WebSocket-Server", "uWebSockets")->end(); + /* todo: What about HttpResponseData here? */ + /* Adopting a socket invalidates it, do not rely on it directly to carry any data */ WebSocket *webSocket = (WebSocket *) StaticDispatch::static_dispatch(us_ssl_socket_context_adopt_socket, us_socket_context_adopt_socket)( (typename StaticDispatch::SOCKET_CONTEXT_TYPE *) webSocketContext, (typename StaticDispatch::SOCKET_TYPE *) res, sizeof(WebSocketData) + sizeof(UserData)); @@ -171,8 +175,7 @@ public: behavior.open(webSocket, req); } - // todo: perform all the checks such as shutdown, closed, etc! - // bug? or does this happen automatically? no! + /* We do not need to check for any close or shutdown here as we immediately return from get handler */ } else { /* For now we do not support having HTTP and websocket routes on the same URL */ diff --git a/src/AsyncSocket.h b/src/AsyncSocket.h index a289c69..84692be 100644 --- a/src/AsyncSocket.h +++ b/src/AsyncSocket.h @@ -41,13 +41,8 @@ protected: ); } - // we need a type safe realType = getData - /* Get socket extension */ void *getExt() { - - // we might have multiple inheritance so need to know the middle type - return static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this); } diff --git a/src/HttpContext.h b/src/HttpContext.h index 7ca8ffc..7dff24c 100644 --- a/src/HttpContext.h +++ b/src/HttpContext.h @@ -312,6 +312,7 @@ public: }); } + // this can be removed? or at least set by default to something like Apache server does void onUnhandled(std::function *, uWS::HttpRequest *)> handler) { HttpContextData *httpContextData = getSocketContextData(); diff --git a/src/HttpContextData.h b/src/HttpContextData.h index 9011512..2ee56d9 100644 --- a/src/HttpContextData.h +++ b/src/HttpContextData.h @@ -36,7 +36,7 @@ private: }; HttpRouter router; - void *upgradedWebSocket; + void *upgradedWebSocket = nullptr; }; } diff --git a/src/HttpResponse.h b/src/HttpResponse.h index 3c2fca2..b6b18e4 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -95,10 +95,15 @@ private: } else { /* Write content-length on first call */ if (!(httpResponseData->state & HttpResponseData::HTTP_END_CALLED)) { - /* We have a known send size */ - Super::write("Content-Length: ", 16); - writeUnsigned(totalSize); - Super::write("\r\n\r\n", 4); + /* Ending with no response should not leave any content-length */ + if (totalSize) { + /* We have a known send size */ + Super::write("Content-Length: ", 16); + writeUnsigned(totalSize); + Super::write("\r\n\r\n", 4); + } else { + Super::write("\r\n", 2); + } /* Mark end called */ httpResponseData->state |= HttpResponseData::HTTP_END_CALLED;