Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 23, 2023
1 parent 25a1770 commit 6f821f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
26 changes: 16 additions & 10 deletions src/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ class Router
{
use ClosesConnections;

protected ServerNegotiator $negotiator;

public function __construct(protected UrlMatcherInterface $matcher)
{
$this->negotiator = new ServerNegotiator(new RequestVerifier);
}

/**
Expand All @@ -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) {
Expand All @@ -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'];
}

/**
Expand All @@ -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));

Expand Down
10 changes: 0 additions & 10 deletions src/Http/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
// });
}

/**
Expand Down

0 comments on commit 6f821f6

Please sign in to comment.