/**
* Do the handshake.
*
* @param Swoole\Request $request
* @param Swoole\Response $response
* @throws \Exception
* @return bool
*/
public function doHandshake(Swoole\Request $request, Swoole\Response $response)
{
if (!isset($request->header['Sec-WebSocket-Key'])) {
$this->log('Bad protocol implementation: it is not RFC6455.');
return false;
}
$key = $request->header['Sec-WebSocket-Key'];
if (0 === preg_match('#^[+/0-9A-Za-z]{21}[AQgw]==$#', $key) || 16 !== strlen(base64_decode($key))) {
$this->log('Header Sec-WebSocket-Key: $key is illegal.');
return false;
}
/**
* @TODO
* ? Origin;
* ? Sec-WebSocket-Protocol;
* ? Sec-WebSocket-Extensions.
*/
$response->setHttpStatus(101);
$response->addHeaders(array('Upgrade' => 'websocket', 'Connection' => 'Upgrade', 'Sec-WebSocket-Accept' => base64_encode(sha1($key . static::GUID, true)), 'Sec-WebSocket-Version' => self::WEBSOCKET_VERSION));
return true;
}