[WebSocket][Http] Bug fix WS require nullifying, more strict on Request parameter

This commit is contained in:
Chris Boden 2013-04-28 09:55:43 -04:00
parent 1ea0bd3255
commit 8df459548f
3 changed files with 24 additions and 16 deletions

View File

@ -8,6 +8,7 @@ interface HttpServerInterface extends MessageComponentInterface {
/** /**
* @param \Ratchet\ConnectionInterface $conn * @param \Ratchet\ConnectionInterface $conn
* @param \Guzzle\Http\Message\RequestInterface $request null is default because PHP won't let me overload; don't pass null!!! * @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); public function onOpen(ConnectionInterface $conn, RequestInterface $request = null);
} }

View File

@ -16,7 +16,14 @@ class Router implements HttpServerInterface {
$this->_matcher = $matcher; $this->_matcher = $matcher;
} }
/**
* {@inheritdoc}
*/
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) {
if (null === $request) {
throw new \UnexpectedValueException('$request can not be null');
}
try { try {
$route = $this->_matcher->match($request->getPath()); $route = $this->_matcher->match($request->getPath());
} catch (MethodNotAllowedException $nae) { } catch (MethodNotAllowedException $nae) {
@ -34,26 +41,25 @@ class Router implements HttpServerInterface {
} }
$conn->controller = $route['_controller']; $conn->controller = $route['_controller'];
$conn->controller->onOpen($conn, $request); $conn->controller->onOpen($conn, $request);
} }
/** /**
* @{inheritdoc} * {@inheritdoc}
*/ */
function onMessage(ConnectionInterface $from, $msg) { function onMessage(ConnectionInterface $from, $msg) {
$from->controller->onMessage($from, $msg); $from->controller->onMessage($from, $msg);
} }
/** /**
* @{inheritdoc} * {@inheritdoc}
*/ */
function onClose(ConnectionInterface $conn) { function onClose(ConnectionInterface $conn) {
$conn->controller->onClose($conn); $conn->controller->onClose($conn);
} }
/** /**
* @{inheritdoc} * {@inheritdoc}
*/ */
function onError(ConnectionInterface $conn, \Exception $e) { function onError(ConnectionInterface $conn, \Exception $e) {
$conn->controller->onError($conn, $e); $conn->controller->onError($conn, $e);

View File

@ -26,7 +26,7 @@ class WsServer implements HttpServerInterface {
* Decorated component * Decorated component
* @var \Ratchet\MessageComponentInterface * @var \Ratchet\MessageComponentInterface
*/ */
protected $_decorating; public $component;
/** /**
* @var \SplObjectStorage * @var \SplObjectStorage
@ -34,9 +34,7 @@ class WsServer implements HttpServerInterface {
protected $connections; protected $connections;
/** /**
* For now, array_push accepted subprotocols to this array * Holder of accepted protocols, implement through WampServerInterface
* @deprecated
* @temporary
*/ */
protected $acceptedSubProtocols = array(); protected $acceptedSubProtocols = array();
@ -66,7 +64,7 @@ class WsServer implements HttpServerInterface {
->enableVersion(new Version\Hixie76) ->enableVersion(new Version\Hixie76)
; ;
$this->_decorating = $component; $this->component = $component;
$this->connections = new \SplObjectStorage; $this->connections = new \SplObjectStorage;
} }
@ -74,6 +72,10 @@ class WsServer implements HttpServerInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { 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 = new \StdClass;
$conn->WebSocket->request = $request; $conn->WebSocket->request = $request;
$conn->WebSocket->established = false; $conn->WebSocket->established = false;
@ -100,7 +102,6 @@ class WsServer implements HttpServerInterface {
return $this->close($conn); return $this->close($conn);
} }
$conn->WebSocket->request = $conn->WebSocket->request;
$conn->WebSocket->version = $this->versioner->getVersion($conn->WebSocket->request); $conn->WebSocket->version = $this->versioner->getVersion($conn->WebSocket->request);
} }
@ -121,13 +122,13 @@ class WsServer implements HttpServerInterface {
return $conn->close(); return $conn->close();
} }
$upgraded = $conn->WebSocket->version->upgradeConnection($conn, $this->_decorating); $upgraded = $conn->WebSocket->version->upgradeConnection($conn, $this->component);
$this->connections->attach($conn, $upgraded); $this->connections->attach($conn, $upgraded);
$upgraded->WebSocket->established = true; $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]; $decor = $this->connections[$conn];
$this->connections->detach($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) { public function onError(ConnectionInterface $conn, \Exception $e) {
if ($conn->WebSocket->established) { if ($conn->WebSocket->established) {
$this->_decorating->onError($this->connections[$conn], $e); $this->component->onError($this->connections[$conn], $e);
} else { } else {
$conn->close(); $conn->close();
} }
@ -181,8 +182,8 @@ class WsServer implements HttpServerInterface {
*/ */
public function isSubProtocolSupported($name) { public function isSubProtocolSupported($name) {
if (!$this->isSpGenerated) { if (!$this->isSpGenerated) {
if ($this->_decorating instanceof WsServerInterface) { if ($this->component instanceof WsServerInterface) {
$this->acceptedSubProtocols = array_flip($this->_decorating->getSubProtocols()); $this->acceptedSubProtocols = array_flip($this->component->getSubProtocols());
} }
$this->isSpGenerated = true; $this->isSpGenerated = true;