/** * Try the handshake by trying different protocol implementation. * * @return void * @throws \Hoa\Websocket\Exception * @throws \Hoa\Websocket\Exception\BadProtocol */ protected function doHandshake() { static $_tail = ['A', 'Q', 'g', 'w']; $connection = $this->getConnection(); $connection->connect(); $connection->setStreamBlocking(true); $key = substr(base64_encode(Consistency::uuid()), 0, 21) . $_tail[mt_rand(0, 3)] . '=='; $expected = base64_encode(sha1($key . Protocol\Rfc6455::GUID, true)); if (null === ($host = $this->getHost())) { throw new Exception('Host name is null. Please, use the %s::setHost() method.', 0, __CLASS__); } $connection->writeAll($request = 'GET ' . $this->getEndPoint() . ' HTTP/1.1' . CRLF . 'Host: ' . $host . CRLF . 'User-Agent: Hoa' . CRLF . 'Upgrade: WebSocket' . CRLF . 'Connection: Upgrade' . CRLF . 'Pragma: no-cache' . CRLF . 'Cache-Control: no-cache' . CRLF . 'Sec-WebSocket-Key: ' . $key . CRLF . 'Sec-WebSocket-Version: 13' . CRLF . CRLF); $buffer = $connection->read(2048); $response = $this->getResponse(); $response->parse($buffer); if ($response::STATUS_SWITCHING_PROTOCOLS !== $response['status'] || 'websocket' !== strtolower($response['upgrade']) || 'upgrade' !== strtolower($response['connection']) || $expected !== $response['sec-websocket-accept']) { throw new Exception\BadProtocol('Handshake has failed, the server did not return a valid ' . 'response.' . "\n\n" . 'Client:' . "\n" . ' %s' . "\n" . 'Server:' . "\n" . ' %s', 0, [str_replace("\n", "\n" . ' ', $request), str_replace("\n", "\n" . ' ', $buffer)]); } $currentNode = $connection->getCurrentNode(); $currentNode->setHandshake(SUCCEED); $currentNode->setProtocolImplementation(new Protocol\Rfc6455($connection)); $this->getListener()->fire('open', new Event\Bucket()); return; }
/** * The entry method. * * @return int */ public function main() { $dryRun = false; $classes = []; while (false !== ($c = $this->getOption($v))) { switch ($c) { case 'n': foreach ($this->parser->parseSpecialValue($v) as $namespace) { $namespace = trim(str_replace('.', '\\', $namespace), '\\'); if (false === ($pos = strpos($namespace, '\\'))) { throw new Console\Exception('Namespace %s is too short.', 0, $namespace); } $tail = substr($namespace, strpos($namespace, '\\') + 1); $root = resolve('hoa://Library/' . str_replace('\\', '/', $tail)); $classes = array_merge($classes, static::findClasses($root, $namespace)); } break; case 'c': foreach ($this->parser->parseSpecialValue($v) as $class) { $classes[] = $class; } break; case 'd': $dryRun = $v; break; case '__ambiguous': $this->resolveOptionAmbiguity($v); break; case 'h': case '?': default: return $this->usage(); } } if (empty($classes)) { return $this->usage(); } foreach ($classes as $i => $class) { $classes[$i] = str_replace('.', '\\', $class); } $generator = new \Atoum\PraspelExtension\Praspel\Generator(); $generator->setTestNamespacer(function ($namespace) { $parts = explode('\\', $namespace); return implode('\\', array_slice($parts, 0, 2)) . '\\Test\\Praspel\\Unit' . (isset($parts[2]) ? '\\' . implode('\\', array_slice($parts, 2)) : ''); }); $phpBinary = Consistency::getPHPBinary() ?: Console\Processus::localte('php'); $envVariable = '__HOA_ATOUM_PRASPEL_EXTENSION_' . md5(Consistency::uuid()); $reflection = null; $buffer = null; $reflectionner = new Console\Processus($phpBinary); $reflectionner->on('input', function (Event\Bucket $bucket) use($envVariable) { $bucket->getSource()->writeAll('<?php' . "\n" . 'require_once \'' . dirname(__DIR__) . DS . '.bootstrap.atoum.php\';' . "\n" . '$class = getenv(\'' . $envVariable . '\');' . "\n" . 'if (class_exists(\'\\mageekguy\\atoum\\scripts\\runner\', false)) {' . "\n" . ' \\atoum\\scripts\\runner::disableAutorun();' . "\n" . '}' . "\n" . '$reflection = new \\Atoum\\PraspelExtension\\Praspel\\Reflection\\RClass($class);' . "\n" . 'echo serialize($reflection), "\\n";'); return false; }); $reflectionner->on('output', function (Event\Bucket $bucket) use(&$buffer) { $data = $bucket->getData(); $buffer .= $data['line'] . "\n"; return; }); $reflectionner->on('stop', function () use(&$buffer, &$reflection) { $handle = @unserialize($buffer); if (false === $handle) { echo $buffer, "\n"; return; } $reflection = $handle; return; }); foreach ($classes as $class) { $status = $class . ' (in '; echo ' ⌛ ', $status; putenv($envVariable . '=' . $class); $buffer = null; $reflection = null; $reflectionner->run(); $output = $generator->generate($reflection); $parts = explode('\\', $class); $paths = resolve('hoa://Library/' . $parts[1] . '/' . 'Test/Praspel/Unit/' . implode('/', array_slice($parts, 2)) . '.php', false, true); $max = 0; $thePath = 0; foreach ($paths as $path) { $length = Ustring\Search::lcp($reflection->getFilename(), $path); if ($length > $max) { $thePath = $path; } } $statusTail = (40 < strlen($thePath) ? '…' . substr($thePath, -39) : $thePath) . ')'; echo $statusTail; $status .= $statusTail; if (false === $reflection->isInstantiable()) { Console\Cursor::clear('↔'); echo ' ⚡️ ', $status, '; not instantiable.', "\n"; continue; } $dirname = dirname($thePath); if (false === is_dir($dirname)) { if (false === $dryRun) { mkdir($dirname, 0755, true); } else { echo "\n", static::info('Creating directory: ' . $dirname . '.'); } } if (false === $dryRun) { file_put_contents($thePath, $output); } else { echo "\n", static::info('Content of the ' . $thePath . ':'), "\n"; Console\Cursor::colorize('foreground(yellow)'); echo ' ┏', "\n", ' ┃ ', str_replace("\n", "\n" . ' ┃ ', trim($output)), "\n", ' ┗', "\n"; Console\Cursor::colorize('foreground(normal)'); } Console\Cursor::clear('↔'); echo ' ', Console\Chrome\Text::colorize('✔︎', 'foreground(green)'), ' ', $status, "\n"; } return; }