From 19aa6ec9af810f141c42f6404b59a5d80cca3e31 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Fri, 18 Jan 2019 17:20:21 +0100 Subject: [PATCH] Implement hasResponded --- src/HttpContext.h | 3 ++- src/HttpResponse.h | 23 +++++++++++++++-------- src/HttpResponseData.h | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/HttpContext.h b/src/HttpContext.h index bc044df..af093d5 100644 --- a/src/HttpContext.h +++ b/src/HttpContext.h @@ -135,8 +135,9 @@ private: HttpResponseData *httpResponseData = (HttpResponseData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) s); httpResponseData->offset = 0; httpResponseData->state = 0; + httpResponseData->state |= HttpResponseData::HTTP_RESPONSE_PENDING; - /* Route the method and URL */ + /* Route the method and URL (unhandled should close or end it by default) */ httpContextData->router.route(httpRequest->getMethod(), httpRequest->getUrl(), { (HttpResponse *) s, httpRequest }); diff --git a/src/HttpResponse.h b/src/HttpResponse.h index e8bb4a2..8596729 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -63,6 +63,16 @@ private: Super::write(buf, length); } + /* When we are done with a response we mark it like so */ + void markDone(HttpResponseData *httpResponseData) { + httpResponseData->onAborted = nullptr; + /* Also remove onWritable so that we do not emit when draining behind the scenes. */ + httpResponseData->onWritable = nullptr; + + /* We are done with this request */ + httpResponseData->state &= ~HttpResponseData::HTTP_RESPONSE_PENDING; + } + /* 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) { @@ -92,7 +102,7 @@ private: /* Terminating 0 chunk */ Super::write("\r\n0\r\n\r\n", 7); - // todo: here we reach the end, so remove onAborted, onWritable, and set HTTP_RESPONDED_TO + markDone(httpResponseData); /* tryEnd can never fail when in chunked mode, since we do not have tryWrite (yet), only write */ Super::timeout(HTTP_TIMEOUT_S); @@ -131,11 +141,7 @@ private: /* Remove onAborted function if we reach the end */ if (httpResponseData->offset == totalSize) { - httpResponseData->onAborted = nullptr; - /* Also remove onWritable so that we do not emit when draining behind the scenes. */ - httpResponseData->onWritable = nullptr; - - // todo: set HTTP_RESPONDED_TO here and use in the emittance of new requests + markDone(httpResponseData); } return success; @@ -236,8 +242,9 @@ public: /* Checking if we have fully responded and are ready for another request */ bool hasResponded() { - // todo: implement - return true; + HttpResponseData *httpResponseData = getHttpResponseData(); + + return !(httpResponseData->state & HttpResponseData::HTTP_RESPONSE_PENDING); } /* Attach handler for writable HTTP response */ diff --git a/src/HttpResponseData.h b/src/HttpResponseData.h index 86b3481..8d0576a 100644 --- a/src/HttpResponseData.h +++ b/src/HttpResponseData.h @@ -38,7 +38,7 @@ private: HTTP_STATUS_CALLED = 1, // used HTTP_WRITE_CALLED = 2, // used HTTP_END_CALLED = 4, // used - HTTP_RESPONDED_TO = 8, // used + HTTP_RESPONSE_PENDING = 8, // used HTTP_ENDED_STREAM_OUT = 16 // not used };