/** * Tests a good handshake. */ public function testValidateGoodRequest() { // from the RFC $requestString = <<<REQ GET / HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Origin: http://example.com Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13 REQ; $req = HttpRequest::FromString($requestString); $webSocketRequest = new WebSocketRequest($req); self::assertTrue($webSocketRequest->validate()); }
/** * Performs the WebSocket HTTP hand-shake. * * @param HttpRequest $httpRequest * @return HttpResponse The HTTP response to send back to the browser. */ private function performHandshake(HttpRequest $httpRequest) { $req = new WebSocketRequest($httpRequest); if ($req->validate()) { $e = $this->createEvent(WebSocketServer::EVENT_HANDSHAKE); $e->request = $req; $this->notifyEventListeners($e); if (!$req->isRejected()) { $req->handshake(); } } $success = !$req->isRejected() && substr($req->getHttpResponse()->getStatus(), 0, 3) === "101"; fwrite($httpRequest->getStream(), $req->getHttpResponse()); return $success; }