Exemplo n.º 1
0
 /**
  * Testing Helper random string generator with spaces
  *
  * @return void
  */
 public function testRandomGenerator()
 {
     $string = Helper::generateRandomString(10, TRUE, FALSE);
     $this->assertTrue(strlen($string) == 10, "string should contain 10 characters");
     $this->assertTrue(strpos($string, ' ') !== FALSE, "spaces should have been found");
 }
Exemplo n.º 2
0
 /**
  * Make Original handshake with the server
  *
  * @param bool $origin whether or not to provide the origin header. currently unsupported
  *
  * @return bool TRUE on succes FALSE on failure
  */
 protected function makeHandshake($origin = FALSE)
 {
     try {
         $protocol = 'http';
         if ($this->ssl) {
             $protocol = 'ssl';
         }
         $key = base64_encode(Helper::generateRandomString(16, FALSE, TRUE));
         $header = "GET /gremlin HTTP/1.1\r\n";
         $header .= "Upgrade: websocket\r\n";
         $header .= "Connection: Upgrade\r\n";
         $header .= "Sec-WebSocket-Key: " . $key . "\r\n";
         $header .= "Host: " . $this->host . "\r\n";
         if ($origin !== TRUE) {
             $header .= "Sec-WebSocket-Origin: " . $protocol . "://" . $this->host . ":" . $this->port . "\r\n";
         }
         $header .= "Sec-WebSocket-Version: 13\r\n\r\n";
         @fwrite($this->_socket, $header);
         $response = @fread($this->_socket, 1500);
         preg_match('#Sec-WebSocket-Accept:\\s(.*)$#mU', $response, $matches);
         $keyAccept = trim($matches[1]);
         $expectedResponse = base64_encode(pack('H*', sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
         return $keyAccept === $expectedResponse ? TRUE : FALSE;
     } catch (\Exception $e) {
         $this->error("Could not finalise handshake, Maybe the server was unreachable", 500, TRUE);
     }
 }