Hostname check verification refs #102

host parameter of App now verified on incoming connections
This commit is contained in:
Chris Boden 2013-06-09 14:09:57 -04:00
parent 40f267834a
commit 0edd37af3f
4 changed files with 25 additions and 8 deletions

View File

@ -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;
}

View File

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

View File

@ -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');

View File

@ -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'));