diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index 2aeca35..7382653 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -16,9 +16,6 @@ use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\Matcher\UrlMatcher; -/** - * @todo Security - same origin by default - */ class App { /** * @var \Symfony\Component\Routing\RouteCollection @@ -35,6 +32,12 @@ class App { */ protected $_server; + /** + * The Host passed in construct used for same origin policy + * @var string + */ + protected $httpHost; + /** * @param string $httpHost * @param int $port @@ -50,6 +53,8 @@ class App { $loop = LoopFactory::create(); } + $this->httpHost = $httpHost; + $socket = new Reactor($loop); $socket->listen($port, $address); @@ -72,7 +77,7 @@ class App { /** * @param string $path * @param ComponentInterface $controller - * @return ComponentInterface + * @return Symfony\Component\Routing\Route */ public function route($path, ComponentInterface $controller) { if ($controller instanceof HttpServerInterface || $controller instanceof WsServer) { @@ -85,7 +90,7 @@ class App { $decorated = $controller; } - $this->routes->add(uniqid(), new Route($path, array('_controller' => $decorated))); + $this->routes->add(uniqid(), new Route($path, array('_controller' => $decorated), array(), array(), $this->httpHost)); return $decorated; } diff --git a/src/Ratchet/Http/Router.php b/src/Ratchet/Http/Router.php index 08e0c1a..8e6611e 100644 --- a/src/Ratchet/Http/Router.php +++ b/src/Ratchet/Http/Router.php @@ -26,6 +26,10 @@ class Router implements HttpServerInterface { throw new \UnexpectedValueException('$request can not be null'); } + $context = $this->_matcher->getContext(); + $context->setMethod($request->getMethod()); + $context->setHost($request->getHost()); + try { $route = $this->_matcher->match($request->getPath()); } catch (MethodNotAllowedException $nae) { @@ -57,14 +61,18 @@ class Router implements HttpServerInterface { * {@inheritdoc} */ function onClose(ConnectionInterface $conn) { - $conn->controller->onClose($conn); + if (isset($conn->controller)) { + $conn->controller->onClose($conn); + } } /** * {@inheritdoc} */ function onError(ConnectionInterface $conn, \Exception $e) { - $conn->controller->onError($conn, $e); + if (isset($conn->controller)) { + $conn->controller->onError($conn, $e); + } } /** diff --git a/tests/AutobahnTestSuite/bin/fuzzingserver-stream.php b/tests/AutobahnTestSuite/bin/fuzzingserver-stream.php index b10dc10..ae3db8d 100644 --- a/tests/AutobahnTestSuite/bin/fuzzingserver-stream.php +++ b/tests/AutobahnTestSuite/bin/fuzzingserver-stream.php @@ -4,7 +4,7 @@ $loop = new React\EventLoop\StreamSelectLoop; $sock = new React\Socket\Server($loop); - $app = new Ratchet\Http\HttpServer(new Ratchet\WebSocket\WsServer(new Ratchet\Server\EchoyServer)); + $app = new Ratchet\Http\HttpServer(new Ratchet\WebSocket\WsServer(new Ratchet\Server\EchoServer)); $port = $argc > 1 ? $argv[1] : 8000; $sock->listen($port, '0.0.0.0'); diff --git a/tests/Ratchet/Tests/Http/RouterTest.php b/tests/Ratchet/Tests/Http/RouterTest.php index cc3309b..30abfed 100644 --- a/tests/Ratchet/Tests/Http/RouterTest.php +++ b/tests/Ratchet/Tests/Http/RouterTest.php @@ -16,6 +16,10 @@ class RouterTest extends \PHPUnit_Framework_TestCase { $this->_conn = $this->getMock('\Ratchet\ConnectionInterface'); $this->_req = $this->getMock('\Guzzle\Http\Message\RequestInterface'); $this->_matcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface'); + $this->_matcher + ->expects($this->any()) + ->method('getContext') + ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext'))); $this->_router = new Router($this->_matcher); $this->_req->expects($this->any())->method('getPath')->will($this->returnValue('/whatever'));