[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 c416cb70a8
commit d956432e3c

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;