From bcf5034e3cbe5b77ab0075163ebcea1ddfc4557f Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sun, 16 Oct 2022 12:37:21 +0200 Subject: [PATCH] Write Date header in all responses --- src/HttpResponse.h | 3 +++ src/Loop.h | 18 +++++++++++++++++- src/LoopData.h | 19 ++++++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/HttpResponse.h b/src/HttpResponse.h index 83f15c9..f7337db 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -73,6 +73,9 @@ private: /* Called only once per request */ void writeMark() { + /* Date is always written */ + writeHeader("Date", std::string_view(((LoopData *) us_loop_ext(us_socket_context_loop(SSL, (us_socket_context(SSL, (us_socket_t *) this)))))->date, 29)); + /* You can disable this altogether */ #ifndef UWS_HTTPRESPONSE_NO_WRITEMARK if (!Super::getLoopData()->noMark) { diff --git a/src/Loop.h b/src/Loop.h index da3023b..ce8d378 100644 --- a/src/Loop.h +++ b/src/Loop.h @@ -74,7 +74,19 @@ private: } static Loop *create(void *hint) { - return ((Loop *) us_create_loop(hint, wakeupCb, preCb, postCb, sizeof(LoopData)))->init(); + Loop *loop = ((Loop *) us_create_loop(hint, wakeupCb, preCb, postCb, sizeof(LoopData)))->init(); + + /* We also need some timers (should live off the one 4 second timer rather) */ + LoopData *loopData = (LoopData *) us_loop_ext((struct us_loop_t *) loop); + loopData->dateTimer = us_create_timer((struct us_loop_t *) loop, 1, sizeof(LoopData *)); + memcpy(us_timer_ext(loopData->dateTimer), &loopData, sizeof(LoopData *)); + us_timer_set(loopData->dateTimer, [](struct us_timer_t *t) { + LoopData *loopData; + memcpy(&loopData, us_timer_ext(t), sizeof(LoopData *)); + loopData->updateDate(); + }, 1000, 1000); + + return loop; } /* What to do with loops created with existingNativeLoop? */ @@ -115,6 +127,10 @@ public: /* Freeing the default loop should be done once */ void free() { LoopData *loopData = (LoopData *) us_loop_ext((us_loop_t *) this); + + /* Stop and free dateTimer first */ + us_timer_close(loopData->dateTimer); + loopData->~LoopData(); /* uSockets will track whether this loop is owned by us or a borrowed alien loop */ us_loop_free((us_loop_t *) this); diff --git a/src/LoopData.h b/src/LoopData.h index b4cac29..ba09c09 100644 --- a/src/LoopData.h +++ b/src/LoopData.h @@ -23,11 +23,13 @@ #include #include #include +#include #include "PerMessageDeflate.h" - #include "MoveOnlyFunction.h" +struct us_timer_t; + namespace uWS { struct Loop; @@ -43,6 +45,10 @@ private: std::map> postHandlers, preHandlers; public: + LoopData() { + updateDate(); + } + ~LoopData() { /* If we have had App.ws called with compression we need to clear this */ if (zlibContext) { @@ -53,6 +59,15 @@ public: delete [] corkBuffer; } + void updateDate() { + time_t now = time(0); + struct tm tstruct; + tstruct = *gmtime(&now); + strftime(date, 32, "%a, %d %b %Y %X GMT", &tstruct); + } + + char date[32]; + /* Be silent */ bool noMark = false; @@ -68,6 +83,8 @@ public: ZlibContext *zlibContext = nullptr; InflationStream *inflationStream = nullptr; DeflationStream *deflationStream = nullptr; + + us_timer_t *dateTimer; }; }