Beispiel #1
0
 /**
  * Perform the handshake
  * $user - The user/client that requests the websocket connection
  * $headers - an array containing the HTTP headers sent
  */
 function doHandshake(MWebsocketUser $user, $headers)
 {
     $origin = $headers['Origin'];
     $host = $headers['Host'];
     $status = $headers['status'];
     $statusFields = explode(' ', $status);
     $resource = $statusFields[1];
     $upgrade = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" . "Upgrade: WebSocket\r\n" . "Connection: Upgrade\r\n" . "Sec-WebSocket-Protocol: " . $app . "\r\n" . "Sec-WebSocket-Origin: " . $origin . "\r\n" . "Sec-WebSocket-Location: ws://" . $host . $statusFields[1] . "\r\n" . "\r\n" . "\r\n";
     socket_write($user->socket(), $upgrade, strlen($upgrade));
     $user->setHandshakeDone();
     $user->setProtocol(new MWebsocketProtocol76());
     return true;
 }
Beispiel #2
0
 /**
  * Perform the handshake
  *
  * $user - the user/client that initiated connection
  * $headers - an array of HTTP headers sent by the user
  */
 function doHandshake(MWebsocketUser $user, $headers)
 {
     // Grab the security keys
     $strkey1 = $headers['Sec-WebSocket-Key1'];
     $strkey2 = $headers['Sec-WebSocket-Key2'];
     // Grab the other items needed to reply
     $data = $headers['body'];
     $origin = $headers['Origin'];
     $host = $headers['Host'];
     $status = $headers['status'];
     $statusFields = explode(' ', $status);
     $resource = $statusFields[1];
     // Compute the hash from the keys provided
     $pattern = '/[^\\d]*/';
     $replacement = '';
     $numkey1 = preg_replace($pattern, $replacement, $strkey1);
     $numkey2 = preg_replace($pattern, $replacement, $strkey2);
     $pattern = '/[^ ]*/';
     $replacement = '';
     $spaces1 = strlen(preg_replace($pattern, $replacement, $strkey1));
     $spaces2 = strlen(preg_replace($pattern, $replacement, $strkey2));
     if ($spaces1 == 0 || $spaces2 == 0 || fmod($numkey1, $spaces1) != 0 || fmod($numkey2, $spaces2) != 0) {
         echo "failed handshake\n";
         return false;
     }
     $ctx = hash_init('md5');
     // Pack the has for tranmission
     hash_update($ctx, pack("N", $numkey1 / $spaces1));
     hash_update($ctx, pack("N", $numkey2 / $spaces2));
     hash_update($ctx, $data);
     $hash_data = hash_final($ctx, true);
     // Send the upgrade response
     if (isset($headers['Sec-WebSocket-Protocol'])) {
         $upgrade = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" . "Upgrade: WebSocket\r\n" . "Connection: Upgrade\r\n" . "Sec-WebSocket-Protocol: " . $app . "\r\n" . "Sec-WebSocket-Origin: " . $origin . "\r\n" . "Sec-WebSocket-Location: ws://" . $host . $statusFields[1] . "\r\n" . "\r\n" . $hash_data;
     } else {
         $upgrade = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" . "Upgrade: WebSocket\r\n" . "Connection: Upgrade\r\n" . "Sec-WebSocket-Origin: " . $origin . "\r\n" . "Sec-WebSocket-Location: ws://" . $host . $statusFields[1] . "\r\n" . "\r\n" . $hash_data;
     }
     socket_write($user->socket(), $upgrade, strlen($upgrade));
     $user->setHandshakeDone();
     // $user->setTranscoder(new BasicTranscoder());
     $user->setProtocol(new MWebsocketProtocol76());
     return true;
 }
Beispiel #3
0
 /**
  * Perform the HyBi Handshake operation
  *
  * $user - The user/client that is trying to establish connection
  * $headers - An array of headers sent in the Websocket connect request
  */
 function doHandshake(MWebsocketUser $user, $headers)
 {
     // Get the key sent from the client
     $strkey1 = $headers['Sec-WebSocket-Key'];
     // Append the Magic ID
     $keyPlusMagic = $strkey1 . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
     // Get the raw sha1 then encode it
     $shaAcceptKey = sha1($keyPlusMagic, true);
     $socketAccept = base64_encode($shaAcceptKey);
     // Grab the rest of the headers needed
     if (isset($headers['Origin'])) {
         $origin = $headers['Origin'];
     }
     if (isset($headers["Sec-WebSocket-Origin"])) {
         $origin = $headers["Sec-WebSocket-Origin"];
     }
     $host = $headers['Host'];
     $status = $headers['status'];
     $statusFields = explode(' ', $status);
     $resource = $statusFields[1];
     if (isset($headers['Sec-WebSocket-Extensions'])) {
         $exts = explode(',', $headers['Sec-WebSocket-Extensions']);
     }
     // Now create the upgrade response
     $upgrade = "HTTP/1.1 101 Switching Protocols\r\n" . "Upgrade: WebSocket\r\n" . "Connection: Upgrade\r\n" . "Sec-WebSocket-Version: 8\r\n";
     if (isset($origin)) {
         $upgrade .= "Sec-WebSocket-Origin: " . $origin . "\r\n";
     }
     if (isset($headers['Sec-WebSocket-Protocol'])) {
         $upgrade = $upgrade . "Sec-WebSocket-Protocol: " . $app . "\r\n";
     }
     if (isset($headers['Sec-WebSocket-Extensions'])) {
         //@TODO - need to process the Extensions and figure out what is supported
         //$upgrade = $upgrade."Sec-WebSocket-Extensions: ". $exts[0] . "\r\n";
     }
     $upgrade = $upgrade . "Sec-WebSocket-Accept: " . $socketAccept . "\r\n" . "\r\n";
     //socket_write($user->socket(),$upgrade.chr(0),strlen($upgrade.chr(0)));
     socket_write($user->socket(), $upgrade, strlen($upgrade));
     $user->setHandshakeDone();
     $user->setProtocol(new MWebsocketProtocolHyBi());
     return true;
 }
Beispiel #4
0
 /**
  * Takes the appropriate action to close the connection down
  */
 private function sendFatalErrorResponse(MWebsocketUser $user)
 {
     // Just close the socket if in handhake mode
     if (!$user->handshakeDone()) {
         MWebsocket::getInstance($this->_controller)->disconnect($user->socket());
         return;
     } else {
         //send a status code and then close
     }
 }