From 6f821f66346ead5ef7faa0255d5d33142500e555 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Thu, 23 Nov 2023 14:40:36 +0000 Subject: [PATCH] wip --- src/Http/Router.php | 26 ++++++++++++++++---------- src/Http/Server.php | 10 ---------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/Http/Router.php b/src/Http/Router.php index aaafec3f..a33f36c8 100644 --- a/src/Http/Router.php +++ b/src/Http/Router.php @@ -17,8 +17,11 @@ class Router { use ClosesConnections; + protected ServerNegotiator $negotiator; + public function __construct(protected UrlMatcherInterface $matcher) { + $this->negotiator = new ServerNegotiator(new RequestVerifier); } /** @@ -31,10 +34,6 @@ public function dispatch(RequestInterface $request, Connection $connection): mix $context->setMethod($request->getMethod()); $context->setHost($uri->getHost()); - if ($this->isWebSocketRequest($request)) { - $connection = $this->attemptUpgrade($request, $connection); - } - try { $route = $this->matcher->match($uri->getPath()); } catch (MethodNotAllowedException $e) { @@ -43,13 +42,21 @@ public function dispatch(RequestInterface $request, Connection $connection): mix return $this->close($connection, 404, 'Not found.'); } - $response = $route['_controller']($request, $connection, ...Arr::except($route, ['_controller', '_route'])); + $controller = $this->controller($route); - if (! $this->isWebSocketRequest($request)) { - return $connection->send($response)->close(); + if ($this->isWebSocketRequest($request)) { + $wsConnection = $this->attemptUpgrade($request, $connection); + return $controller($request, $wsConnection, ...Arr::except($route, ['_controller', '_route'])); } - return null; + $response = $controller($request, $connection, ...Arr::except($route, ['_controller', '_route'])); + + return $connection->send($response)->close(); + } + + protected function controller($route): callable + { + return $route['_controller']; } /** @@ -65,8 +72,7 @@ protected function isWebSocketRequest(RequestInterface $request): bool */ protected function attemptUpgrade(RequestInterface $request, Connection $connection): WsConnection { - $negotiator = new ServerNegotiator(new RequestVerifier); - $response = $negotiator->handshake($request); + $response = $this->negotiator->handshake($request); $connection->write(Message::toString($response)); diff --git a/src/Http/Server.php b/src/Http/Server.php index 7b5d47f9..c0c10651 100644 --- a/src/Http/Server.php +++ b/src/Http/Server.php @@ -17,10 +17,6 @@ class Server public function __construct(protected ServerInterface $socket, protected Router $router, protected ?LoopInterface $loop = null) { - gc_enable(); - set_time_limit(0); - ob_implicit_flush(); - $this->loop = $loop ?: Loop::get(); $socket->on('connection', $this); @@ -33,12 +29,6 @@ public function __invoke(ConnectionInterface $connection) $connection->on('data', function ($data) use ($connection) { $this->handleRequest($data, $connection); }); - // $connection->on('close', function () use ($connection) { - // $this->handleEnd($conn); - // }); - // $conn->on('error', function (\Exception $e) use ($conn) { - // $this->handleError($e, $conn); - // }); } /**