Write Date header in all responses

This commit is contained in:
Alex Hultman
2022-10-16 12:37:21 +02:00
parent 4b80e77c83
commit bcf5034e3c
3 changed files with 38 additions and 2 deletions
+3
View File
@@ -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) {
+17 -1
View File
@@ -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);
+18 -1
View File
@@ -23,11 +23,13 @@
#include <vector>
#include <mutex>
#include <map>
#include <time.h>
#include "PerMessageDeflate.h"
#include "MoveOnlyFunction.h"
struct us_timer_t;
namespace uWS {
struct Loop;
@@ -43,6 +45,10 @@ private:
std::map<void *, MoveOnlyFunction<void(Loop *)>> 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;
};
}