From 8df459548fd7e46d9cef3e02cd15675072743d9f Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sun, 28 Apr 2013 09:55:43 -0400 Subject: [PATCH] [WebSocket][Http] Bug fix WS require nullifying, more strict on Request parameter --- src/Ratchet/Http/HttpServerInterface.php | 1 + src/Ratchet/Http/Router.php | 14 +++++++++---- src/Ratchet/WebSocket/WsServer.php | 25 ++++++++++++------------ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/Ratchet/Http/HttpServerInterface.php b/src/Ratchet/Http/HttpServerInterface.php index 0730680..79b7d55 100644 --- a/src/Ratchet/Http/HttpServerInterface.php +++ b/src/Ratchet/Http/HttpServerInterface.php @@ -8,6 +8,7 @@ interface HttpServerInterface extends MessageComponentInterface { /** * @param \Ratchet\ConnectionInterface $conn * @param \Guzzle\Http\Message\RequestInterface $request null is default because PHP won't let me overload; don't pass null!!! + * @throws \UnexpectedValueException if a RequestInterface is not passed */ public function onOpen(ConnectionInterface $conn, RequestInterface $request = null); } diff --git a/src/Ratchet/Http/Router.php b/src/Ratchet/Http/Router.php index 9946d7e..e1c1f6d 100644 --- a/src/Ratchet/Http/Router.php +++ b/src/Ratchet/Http/Router.php @@ -16,7 +16,14 @@ class Router implements HttpServerInterface { $this->_matcher = $matcher; } + /** + * {@inheritdoc} + */ public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { + if (null === $request) { + throw new \UnexpectedValueException('$request can not be null'); + } + try { $route = $this->_matcher->match($request->getPath()); } catch (MethodNotAllowedException $nae) { @@ -34,26 +41,25 @@ class Router implements HttpServerInterface { } $conn->controller = $route['_controller']; - $conn->controller->onOpen($conn, $request); } /** - * @{inheritdoc} + * {@inheritdoc} */ function onMessage(ConnectionInterface $from, $msg) { $from->controller->onMessage($from, $msg); } /** - * @{inheritdoc} + * {@inheritdoc} */ function onClose(ConnectionInterface $conn) { $conn->controller->onClose($conn); } /** - * @{inheritdoc} + * {@inheritdoc} */ function onError(ConnectionInterface $conn, \Exception $e) { $conn->controller->onError($conn, $e); diff --git a/src/Ratchet/WebSocket/WsServer.php b/src/Ratchet/WebSocket/WsServer.php index 5245019..24ea580 100644 --- a/src/Ratchet/WebSocket/WsServer.php +++ b/src/Ratchet/WebSocket/WsServer.php @@ -26,7 +26,7 @@ class WsServer implements HttpServerInterface { * Decorated component * @var \Ratchet\MessageComponentInterface */ - protected $_decorating; + public $component; /** * @var \SplObjectStorage @@ -34,9 +34,7 @@ class WsServer implements HttpServerInterface { protected $connections; /** - * For now, array_push accepted subprotocols to this array - * @deprecated - * @temporary + * Holder of accepted protocols, implement through WampServerInterface */ protected $acceptedSubProtocols = array(); @@ -66,7 +64,7 @@ class WsServer implements HttpServerInterface { ->enableVersion(new Version\Hixie76) ; - $this->_decorating = $component; + $this->component = $component; $this->connections = new \SplObjectStorage; } @@ -74,6 +72,10 @@ class WsServer implements HttpServerInterface { * {@inheritdoc} */ public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { + if (null === $request) { + throw new \UnexpectedValueException('$request can not be null'); + } + $conn->WebSocket = new \StdClass; $conn->WebSocket->request = $request; $conn->WebSocket->established = false; @@ -100,7 +102,6 @@ class WsServer implements HttpServerInterface { return $this->close($conn); } - $conn->WebSocket->request = $conn->WebSocket->request; $conn->WebSocket->version = $this->versioner->getVersion($conn->WebSocket->request); } @@ -121,13 +122,13 @@ class WsServer implements HttpServerInterface { return $conn->close(); } - $upgraded = $conn->WebSocket->version->upgradeConnection($conn, $this->_decorating); + $upgraded = $conn->WebSocket->version->upgradeConnection($conn, $this->component); $this->connections->attach($conn, $upgraded); $upgraded->WebSocket->established = true; - return $this->_decorating->onOpen($upgraded); + return $this->component->onOpen($upgraded); } /** @@ -138,7 +139,7 @@ class WsServer implements HttpServerInterface { $decor = $this->connections[$conn]; $this->connections->detach($conn); - $this->_decorating->onClose($decor); + $this->component->onClose($decor); } } @@ -147,7 +148,7 @@ class WsServer implements HttpServerInterface { */ public function onError(ConnectionInterface $conn, \Exception $e) { if ($conn->WebSocket->established) { - $this->_decorating->onError($this->connections[$conn], $e); + $this->component->onError($this->connections[$conn], $e); } else { $conn->close(); } @@ -181,8 +182,8 @@ class WsServer implements HttpServerInterface { */ public function isSubProtocolSupported($name) { if (!$this->isSpGenerated) { - if ($this->_decorating instanceof WsServerInterface) { - $this->acceptedSubProtocols = array_flip($this->_decorating->getSubProtocols()); + if ($this->component instanceof WsServerInterface) { + $this->acceptedSubProtocols = array_flip($this->component->getSubProtocols()); } $this->isSpGenerated = true;