From 438530e9156ace9d787ea6de1e47807c248da530 Mon Sep 17 00:00:00 2001 From: chaz6chez Date: Tue, 2 Apr 2024 17:08:48 +0800 Subject: [PATCH] fixed api service --- src/ApiService.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/ApiService.php b/src/ApiService.php index dd0e7c9..d81a1d5 100644 --- a/src/ApiService.php +++ b/src/ApiService.php @@ -46,14 +46,26 @@ public static function getStorage(): \Redis /** * @param Response $response * @param TcpConnection $connection debug模式下传入null + * @param Request|null $request * @return void */ - public function send(Response $response, TcpConnection $connection): void + public function send(Response $response, TcpConnection $connection, ?Request $request = null): void { $response->withHeader('Content-Type', 'application/json'); $response->withHeader('Server', 'workbunny/webman-push-server'); $response->withHeader('Version', Server::$version); - $connection->send($response); + if ($request) { + $keepAlive = $request->header('connection'); + if ( + ($keepAlive === null and $request->protocolVersion() === '1.1') + or $keepAlive === 'keep-alive' + or $keepAlive === 'Keep-Alive' + ) { + $connection->send($response); + return; + } + } + $connection->close($response); } /** @inheritDoc */ @@ -76,7 +88,7 @@ public function onMessage(TcpConnection $connection, $data): void $handler = $res[1] ?? null; $params = $res[2] ?? []; if(!$handler instanceof Closure) { - $this->send(\Workbunny\WebmanPushServer\response(404, 'Not Found.'), $connection); + $this->send(\Workbunny\WebmanPushServer\response(404, 'Not Found.'), $connection, $data); return; } $response = call_user_func(array_reduce( @@ -91,10 +103,10 @@ function (...$arguments) use ($handler) { } ), Server::getServer(), $data, $params); if(!$response instanceof Response){ - $this->send(\Workbunny\WebmanPushServer\response(500, 'Server Error.'), $connection); + $this->send(\Workbunny\WebmanPushServer\response(500, 'Server Error.'), $connection, $data); return; } - $this->send($response, $connection); + $this->send($response, $connection, $data); } /** @inheritDoc */