From c6a801f1efd87c04d158f6acdaf2da21e544310a Mon Sep 17 00:00:00 2001 From: Mike Almond Date: Tue, 7 Feb 2012 17:14:52 -0500 Subject: [PATCH] [Refactor] Fixing stuff for Chris Fixing sub protocol agreement between server and client --- .../Component/WebSocket/Version/Hixie76.php | 19 +++++++------------ .../WebSocket/WebSocketComponent.php | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Ratchet/Component/WebSocket/Version/Hixie76.php b/src/Ratchet/Component/WebSocket/Version/Hixie76.php index 6ad1eb5..c4f081e 100644 --- a/src/Ratchet/Component/WebSocket/Version/Hixie76.php +++ b/src/Ratchet/Component/WebSocket/Version/Hixie76.php @@ -23,23 +23,18 @@ class Hixie76 implements VersionInterface { /** * @param string * @return string - * @todo Unhack this mess...or wait for Hixie to die (HURRY UP APPLE) */ public function handshake(RequestInterface $request) { $body = $this->sign($request->getHeader('Sec-WebSocket-Key1'), $request->getHeader('Sec-WebSocket-Key2'), $request->getBody()); - + $headers = array( 'Upgrade' => 'WebSocket' , 'Connection' => 'Upgrade' , 'Sec-WebSocket-Origin' => $request->getHeader('Origin') , 'Sec-WebSocket-Location' => 'ws://' . $request->getHeader('Host') . $request->getPath() ); - if ($request->getHeader('Sec-WebSocket-Protocol')) { - $headers['Sec-WebSocket-Protocol'] = $request->getHeader('Sec-WebSocket-Protocol'); - } - - $response = new \Guzzle\Http\Message\Response('101', $headers, $body); - return $response; + + return new \Guzzle\Http\Message\Response('101', $headers, $body); } public function newMessage() { @@ -55,18 +50,18 @@ class Hixie76 implements VersionInterface { } public function generateKeyNumber($key) { - + if (substr_count($key, ' ') == 0) { return ''; } $int = preg_replace('[\D]', '', $key) / substr_count($key, ' '); return (is_int($int)) ? $int : ''; - - + + } protected function sign($key1, $key2, $code) { - + return md5( pack('N', $this->generateKeyNumber($key1)) . pack('N', $this->generateKeyNumber($key2)) diff --git a/src/Ratchet/Component/WebSocket/WebSocketComponent.php b/src/Ratchet/Component/WebSocket/WebSocketComponent.php index 4286800..192894c 100644 --- a/src/Ratchet/Component/WebSocket/WebSocketComponent.php +++ b/src/Ratchet/Component/WebSocket/WebSocketComponent.php @@ -79,6 +79,25 @@ class WebSocketComponent implements MessageComponentInterface { $response = $from->WebSocket->version->handshake($from->WebSocket->headers); $from->WebSocket->handshake = true; + + + // This block is to be moved/changed later + + $agreed_protocols = array(); + + $requested_protocols = $from->WebSocket->headers->getTokenizedHeader('Sec-WebSocket-Protocol', ','); + + foreach ($this->accepted_subprotocols as $sub_protocol) { + if (false !== $requested_protocols->hasValue($sub_protocol)) { + $agreed_protocols[] = $sub_protocol; + } + } + + if (count($agreed_protocols) > 0) { + + // $response['Sec-WebSocket-Protocol'] = implode(',', $agreed_protocols); + $response->setHeader('Sec-WebSocket-Protocol', implode(',', $agreed_protocols)); + } $header = (string)$response; $comp = $this->_factory->newComposite();