From b647f20a6ddb0ac191519481a71de71fce4c6c9a Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Tue, 27 Dec 2022 15:17:07 +0100 Subject: [PATCH] maxLifetime impl. WIP, waiting for uSockets bump --- src/App.h | 11 +++++++++-- src/HttpResponse.h | 3 +++ src/WebSocketContext.h | 6 ++++++ src/WebSocketContextData.h | 1 + 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/App.h b/src/App.h index b448a72..2712378 100644 --- a/src/App.h +++ b/src/App.h @@ -261,8 +261,14 @@ public: std::terminate(); } - if (behavior.idleTimeout % 4) { - std::cerr << "Warning: idleTimeout should be a multiple of 4!" << std::endl; + /* Maximum idleTimeout is ≈17 minutes */ + if (behavior.idleTimeout % 4 || behavior.idleTimeout >= 254 * 4) { + std::cerr << "Warning: idleTimeout should be a multiple of 4 and must be less than or equal to 1016 seconds!" << std::endl; + } + + /* Maximum maxLifetime is ≈4 hours */ + if (behavior.maxLifetime % 60 || behavior.maxLifetime > 254 * 60) { + std::cerr << "Warning: maxLifetime should be a multiple of 60 and must be less than or equal to 15240 seconds!" << std::endl; } /* If we don't have a TopicTree yet, create one now */ @@ -366,6 +372,7 @@ public: webSocketContext->getExt()->closeOnBackpressureLimit = behavior.closeOnBackpressureLimit; webSocketContext->getExt()->resetIdleTimeoutOnSend = behavior.resetIdleTimeoutOnSend; webSocketContext->getExt()->sendPingsAutomatically = behavior.sendPingsAutomatically; + webSocketContext->getExt()->maxLifetime = behavior.maxLifetime; webSocketContext->getExt()->compression = behavior.compression; /* Calculate idleTimeoutCompnents */ diff --git a/src/HttpResponse.h b/src/HttpResponse.h index 2b2d4e9..180725b 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -332,6 +332,9 @@ public: httpContextData->upgradedWebSocket = webSocket; } + /* Arm maxLifetime timeout */ + us_socket_long_timeout(SSL, (us_socket_t *) webSocket, webSocketContextData->maxLifetime); + /* Arm idleTimeout */ us_socket_timeout(SSL, (us_socket_t *) webSocket, webSocketContextData->idleTimeoutComponents.first); diff --git a/src/WebSocketContext.h b/src/WebSocketContext.h index 7200693..4dd4aa0 100644 --- a/src/WebSocketContext.h +++ b/src/WebSocketContext.h @@ -371,6 +371,12 @@ private: return s; }); + us_socket_context_on_long_timeout(SSL, getSocketContext(), [](auto *s) { + ((WebSocket *) s)->end(); + + return s; + }); + /* Handle socket timeouts, simply close them so to not confuse client with FIN */ us_socket_context_on_timeout(SSL, getSocketContext(), [](auto *s) { diff --git a/src/WebSocketContextData.h b/src/WebSocketContextData.h index 8c41894..e5ae49a 100644 --- a/src/WebSocketContextData.h +++ b/src/WebSocketContextData.h @@ -76,6 +76,7 @@ public: bool closeOnBackpressureLimit; bool resetIdleTimeoutOnSend; bool sendPingsAutomatically; + unsigned short maxLifetime; /* These are calculated on creation */ std::pair idleTimeoutComponents;