diff --git a/src/Ratchet/Http/Router.php b/src/Ratchet/Http/Router.php index e1c1f6d..08e0c1a 100644 --- a/src/Ratchet/Http/Router.php +++ b/src/Ratchet/Http/Router.php @@ -2,6 +2,7 @@ namespace Ratchet\Http; use Ratchet\ConnectionInterface; use Guzzle\Http\Message\RequestInterface; +use Guzzle\Http\Message\Response; use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\ResourceNotFoundException; @@ -18,6 +19,7 @@ class Router implements HttpServerInterface { /** * {@inheritdoc} + * @throws \UnexpectedValueException If a controller is not \Ratchet\Http\HttpServerInterface */ public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { if (null === $request) { @@ -27,9 +29,9 @@ class Router implements HttpServerInterface { try { $route = $this->_matcher->match($request->getPath()); } catch (MethodNotAllowedException $nae) { - return $this->close($from, 403); + return $this->close($conn, 403); } catch (ResourceNotFoundException $nfe) { - return $this->close($from, 404); + return $this->close($conn, 404); } if (is_string($route['_controller']) && class_exists($route['_controller'])) { @@ -37,7 +39,7 @@ class Router implements HttpServerInterface { } if (!($route['_controller'] instanceof HttpServerInterface)) { - throw new \UnexpectedValueException('All routes must implement Ratchet\HttpServerInterface'); + throw new \UnexpectedValueException('All routes must implement Ratchet\Http\HttpServerInterface'); } $conn->controller = $route['_controller']; @@ -64,4 +66,19 @@ class Router implements HttpServerInterface { function onError(ConnectionInterface $conn, \Exception $e) { $conn->controller->onError($conn, $e); } + + /** + * Close a connection with an HTTP response + * @param \Ratchet\ConnectionInterface $conn + * @param int $code HTTP status code + * @return null + */ + protected function close(ConnectionInterface $conn, $code = 400) { + $response = new Response($code, array( + 'X-Powered-By' => \Ratchet\VERSION + )); + + $conn->send((string)$response); + $conn->close(); + } } \ No newline at end of file