[Http][Router] Fixed bugs found from unit tests

This commit is contained in:
Chris Boden 2013-04-30 21:24:39 -04:00
parent ec9bef4ee1
commit 1622caadad

View File

@ -2,6 +2,7 @@
namespace Ratchet\Http; namespace Ratchet\Http;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use Guzzle\Http\Message\RequestInterface; use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Message\Response;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Exception\ResourceNotFoundException;
@ -18,6 +19,7 @@ class Router implements HttpServerInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
* @throws \UnexpectedValueException If a controller is not \Ratchet\Http\HttpServerInterface
*/ */
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) {
if (null === $request) { if (null === $request) {
@ -27,9 +29,9 @@ class Router implements HttpServerInterface {
try { try {
$route = $this->_matcher->match($request->getPath()); $route = $this->_matcher->match($request->getPath());
} catch (MethodNotAllowedException $nae) { } catch (MethodNotAllowedException $nae) {
return $this->close($from, 403); return $this->close($conn, 403);
} catch (ResourceNotFoundException $nfe) { } catch (ResourceNotFoundException $nfe) {
return $this->close($from, 404); return $this->close($conn, 404);
} }
if (is_string($route['_controller']) && class_exists($route['_controller'])) { if (is_string($route['_controller']) && class_exists($route['_controller'])) {
@ -37,7 +39,7 @@ class Router implements HttpServerInterface {
} }
if (!($route['_controller'] instanceof 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']; $conn->controller = $route['_controller'];
@ -64,4 +66,19 @@ class Router implements HttpServerInterface {
function onError(ConnectionInterface $conn, \Exception $e) { function onError(ConnectionInterface $conn, \Exception $e) {
$conn->controller->onError($conn, $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();
}
} }