From 7f9def422f1d4e7b8eb5a85a1b45c044024975f8 Mon Sep 17 00:00:00 2001 From: Ondrej Pokorny Date: Tue, 25 Feb 2025 20:25:27 +0100 Subject: [PATCH] fix KeepConnectionTimeout loop --- packages/fcl-web/src/base/fphttpserver.pp | 29 +++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/fcl-web/src/base/fphttpserver.pp b/packages/fcl-web/src/base/fphttpserver.pp index a3185c8859..a1e04d1d05 100644 --- a/packages/fcl-web/src/base/fphttpserver.pp +++ b/packages/fcl-web/src/base/fphttpserver.pp @@ -1332,26 +1332,29 @@ end; procedure TFPHTTPConnectionThread.Execute; var - AttemptsLeft: Integer; + WaitUntil: QWord; + + procedure SetWaitUntil; + begin + if Connection.KeepConnectionTimeout>0 then + WaitUntil := GetTickCount64+Connection.KeepConnectionTimeout + else + WaitUntil := 0; + end; begin try // Always handle first request Connection.HandleRequest; - if (Connection.KeepConnectionIdleTimeout>0) and (Connection.KeepConnectionTimeout>0) then - AttemptsLeft := Connection.KeepConnectionTimeout div Connection.KeepConnectionIdleTimeout - else - AttemptsLeft := -1; // infinitely - While not Terminated and Connection.AllowNewRequest and (AttemptsLeft<>0) do + SetWaitUntil; + While not Terminated and Connection.AllowNewRequest and (WaitUntil>0) and (GetTickCount64 idle begin - if AttemptsLeft>0 then - Dec(AttemptsLeft); - if AttemptsLeft<>0 then - Connection.DoKeepConnectionIdle; - end; + Connection.HandleRequest; + SetWaitUntil; + end + else // KeepConnectionIdleTimeout was reached without a new request -> idle + Connection.DoKeepConnectionIdle; end; except on E : Exception do