diff --git a/lib/Ratchet/Application/WebSocket/App.php b/lib/Ratchet/Application/WebSocket/App.php index 75e7207..9feb7f8 100644 --- a/lib/Ratchet/Application/WebSocket/App.php +++ b/lib/Ratchet/Application/WebSocket/App.php @@ -216,16 +216,14 @@ class App implements ApplicationInterface, ConfiguratorInterface { * @todo Verify the first line of the HTTP header as per page 16 of RFC 6455 */ protected function getVersion(RequestInterface $request) { - $headers = $request->getHeaders(); - foreach ($this->_versions as $name => $instance) { if (null !== $instance) { - if ($instance::isProtocol($headers)) { + if ($instance::isProtocol($request)) { return $instance; } } else { $ns = __NAMESPACE__ . "\\Version\\{$name}"; - if ($ns::isProtocol($headers)) { + if ($ns::isProtocol($request)) { $this->_versions[$name] = new $ns; return $this->_versions[$name]; } diff --git a/lib/Ratchet/Application/WebSocket/Version/Hixie76.php b/lib/Ratchet/Application/WebSocket/Version/Hixie76.php index 4540714..5f98e3c 100644 --- a/lib/Ratchet/Application/WebSocket/Version/Hixie76.php +++ b/lib/Ratchet/Application/WebSocket/Version/Hixie76.php @@ -1,5 +1,6 @@ getHeader('Sec-WebSocket-Key2')); } /** * @param string * @return string */ - public function handshake($message) { + public function handshake(RequestInterface $request) { + $message = $request->getRawHeaders() . $request->getResponse()->getBody(true); + $buffer = $message; $resource = $host = $origin = $key1 = $key2 = $protocol = $code = $handshake = null; diff --git a/lib/Ratchet/Application/WebSocket/Version/HyBi10.php b/lib/Ratchet/Application/WebSocket/Version/HyBi10.php index ee24fe2..920b966 100644 --- a/lib/Ratchet/Application/WebSocket/Version/HyBi10.php +++ b/lib/Ratchet/Application/WebSocket/Version/HyBi10.php @@ -1,18 +1,14 @@ = 6 && (int)$headers['Sec-WebSocket-Version'] < 13) { - return true; - } - } - - return false; + public static function isProtocol(RequestInterface $request) { + $version = (int)$request->getHeader('Sec-WebSocket-Version', -1); + return ($version >= 6 && $version < 13); } /** diff --git a/lib/Ratchet/Application/WebSocket/Version/RFC6455.php b/lib/Ratchet/Application/WebSocket/Version/RFC6455.php index fbb003f..ad41e66 100644 --- a/lib/Ratchet/Application/WebSocket/Version/RFC6455.php +++ b/lib/Ratchet/Application/WebSocket/Version/RFC6455.php @@ -21,14 +21,9 @@ class RFC6455 implements VersionInterface { /** * @todo Change the request to be a Guzzle RequestInterface */ - public static function isProtocol($headers) { - if (isset($headers['Sec-WebSocket-Version'])) { - if ((int)$headers['Sec-WebSocket-Version'] == 13) { - return true; - } - } - - return false; + public static function isProtocol(RequestInterface $request) { + $version = (int)$request->getHeader('Sec-WebSocket-Version', -1); + return (13 === $version); } /** @@ -36,11 +31,8 @@ class RFC6455 implements VersionInterface { * I kept this as an array and combined in App for future considerations...easier to add a subprotol as a key value than edit a string * @todo Decide what to do on failure...currently throwing an exception and I think socket connection is closed. Should be sending 40x error - but from where? */ - public function handshake($message) { - $headers = $message->getHeaders(); - $key = $this->sign($headers['Sec-WebSocket-Key']); - - if (true !== $this->_verifier->verifyAll($message)) { + public function handshake(RequestInterface $request) { + if (true !== $this->_verifier->verifyAll($request)) { throw new \InvalidArgumentException('Invalid HTTP header'); } @@ -48,7 +40,7 @@ class RFC6455 implements VersionInterface { '' => 'HTTP/1.1 101 Switching Protocols' , 'Upgrade' => 'websocket' , 'Connection' => 'Upgrade' - , 'Sec-WebSocket-Accept' => $this->sign($headers['Sec-WebSocket-Key']) + , 'Sec-WebSocket-Accept' => $this->sign($request->getHeader('Sec-WebSocket-Key')) // , 'Sec-WebSocket-Protocol' => '' ); } diff --git a/lib/Ratchet/Application/WebSocket/Version/VersionInterface.php b/lib/Ratchet/Application/WebSocket/Version/VersionInterface.php index ebc9d8f..8a13b70 100644 --- a/lib/Ratchet/Application/WebSocket/Version/VersionInterface.php +++ b/lib/Ratchet/Application/WebSocket/Version/VersionInterface.php @@ -1,5 +1,6 @@