Client refactor
Class is now re-entrant instead of keeping state Remove non-specified default RFC headers Accept strict URI type to cut down on error handling
This commit is contained in:
parent
d4ea99ffc0
commit
affba40d16
@ -1,83 +1,50 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Ratchet\RFC6455\Handshake;
|
||||
|
||||
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use GuzzleHttp\Psr7\Uri;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
|
||||
class ClientNegotiator implements ClientNegotiatorInterface {
|
||||
public $defaultHeaders = [
|
||||
/**
|
||||
* @var ResponseVerifier
|
||||
*/
|
||||
private $verifier;
|
||||
|
||||
/**
|
||||
* @var \Psr\Http\Message\RequestInterface
|
||||
*/
|
||||
public $defaultHeader;
|
||||
|
||||
function __construct() {
|
||||
$this->verifier = new ResponseVerifier;
|
||||
|
||||
$this->defaultHeader = new Request('GET', '', [
|
||||
'Connection' => 'Upgrade'
|
||||
, 'Cache-Control' => 'no-cache'
|
||||
, 'Pragma' => 'no-cache'
|
||||
, 'Upgrade' => 'websocket'
|
||||
, 'Sec-WebSocket-Version' => 13
|
||||
, 'User-Agent' => "RatchetRFC/0.0.0"
|
||||
];
|
||||
|
||||
/** @var Request */
|
||||
public $request;
|
||||
|
||||
/** @var Response */
|
||||
public $response;
|
||||
|
||||
/** @var ResponseVerifier */
|
||||
public $verifier;
|
||||
|
||||
private $websocketKey = '';
|
||||
|
||||
function __construct($path = null)
|
||||
{
|
||||
if (!is_string($path)) $path = "/";
|
||||
$request = new Request("GET", $path);
|
||||
|
||||
$request = $request->withUri(new Uri("ws://127.0.0.1:9001" . $path));
|
||||
|
||||
$this->request = $request;
|
||||
|
||||
$this->verifier = new ResponseVerifier();
|
||||
|
||||
$this->websocketKey = $this->generateKey();
|
||||
]);
|
||||
}
|
||||
|
||||
public function addRequiredHeaders() {
|
||||
foreach ($this->defaultHeaders as $k => $v) {
|
||||
// remove any header that is there now
|
||||
$this->request = $this->request->withoutHeader($k);
|
||||
$this->request = $this->request->withHeader($k, $v);
|
||||
}
|
||||
$this->request = $this->request->withoutHeader("Sec-WebSocket-Key");
|
||||
$this->request = $this->request->withHeader("Sec-WebSocket-Key", $this->websocketKey);
|
||||
$this->request = $this->request->withoutHeader("Host")
|
||||
->withHeader("Host", $this->request->getUri()->getHost() . ":" . $this->request->getUri()->getPort());
|
||||
public function generateRequest(UriInterface $uri) {
|
||||
return $this->defaultHeader->withUri($uri)
|
||||
->withoutHeader("Sec-WebSocket-Key")
|
||||
->withHeader("Sec-WebSocket-Key", $this->generateKey());
|
||||
}
|
||||
|
||||
public function getRequest() {
|
||||
$this->addRequiredHeaders();
|
||||
return $this->request;
|
||||
public function validateResponse(RequestInterface $request, ResponseInterface $response) {
|
||||
return $this->verifier->verifyAll($request, $response);
|
||||
}
|
||||
|
||||
public function getResponse() {
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
public function validateResponse(Response $response) {
|
||||
$this->response = $response;
|
||||
|
||||
return $this->verifier->verifyAll($this->getRequest(), $response);
|
||||
}
|
||||
|
||||
protected function generateKey() {
|
||||
public function generateKey() {
|
||||
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwzyz1234567890+/=';
|
||||
$charRange = strlen($chars) - 1;
|
||||
$key = '';
|
||||
for ($i = 0; $i < 16; $i++) {
|
||||
$key .= $chars[mt_rand(0, $charRange)];
|
||||
}
|
||||
|
||||
return base64_encode($key);
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Ratchet\RFC6455\Handshake;
|
||||
|
||||
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class ResponseVerifier {
|
||||
public function verifyAll(Request $request, Response $response) {
|
||||
public function verifyAll(RequestInterface $request, ResponseInterface $response) {
|
||||
$passes = 0;
|
||||
|
||||
$passes += (int)$this->verifyStatus($response->getStatusCode());
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
use GuzzleHttp\Psr7\Uri;
|
||||
use React\Promise\Deferred;
|
||||
use Ratchet\RFC6455\Messaging\Protocol\Frame;
|
||||
use Ratchet\RFC6455\Messaging\Protocol\Message;
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
|
||||
@ -48,8 +48,8 @@ function getTestCases() {
|
||||
$deferred = new Deferred();
|
||||
|
||||
$factory->create($testServer, 9001)->then(function (\React\Stream\Stream $stream) use ($deferred) {
|
||||
$cn = new \Ratchet\RFC6455\Handshake\ClientNegotiator("/getCaseCount");
|
||||
$cnRequest = $cn->getRequest();
|
||||
$cn = new \Ratchet\RFC6455\Handshake\ClientNegotiator();
|
||||
$cnRequest = $cn->generateRequest(new Uri('ws://127.0.0.1:9001/getCaseCount'));
|
||||
|
||||
$rawResponse = "";
|
||||
$response = null;
|
||||
@ -57,7 +57,7 @@ function getTestCases() {
|
||||
/** @var \Ratchet\RFC6455\Messaging\Streaming\MessageStreamer $ms */
|
||||
$ms = null;
|
||||
|
||||
$stream->on('data', function ($data) use ($stream, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context) {
|
||||
$stream->on('data', function ($data) use ($stream, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest) {
|
||||
if ($response === null) {
|
||||
$rawResponse .= $data;
|
||||
$pos = strpos($rawResponse, "\r\n\r\n");
|
||||
@ -66,7 +66,7 @@ function getTestCases() {
|
||||
$rawResponse = substr($rawResponse, 0, $pos + 4);
|
||||
$response = \GuzzleHttp\Psr7\parse_response($rawResponse);
|
||||
|
||||
if (!$cn->validateResponse($response)) {
|
||||
if (!$cn->validateResponse($cnRequest, $response)) {
|
||||
$stream->end();
|
||||
$deferred->reject();
|
||||
} else {
|
||||
@ -105,15 +105,15 @@ function runTest($case)
|
||||
$deferred = new Deferred();
|
||||
|
||||
$factory->create($testServer, 9001)->then(function (\React\Stream\Stream $stream) use ($deferred, $casePath, $case) {
|
||||
$cn = new \Ratchet\RFC6455\Handshake\ClientNegotiator($casePath);
|
||||
$cnRequest = $cn->getRequest();
|
||||
$cn = new \Ratchet\RFC6455\Handshake\ClientNegotiator();
|
||||
$cnRequest = $cn->generateRequest(new Uri('ws://127.0.0.1:9001' . $casePath));
|
||||
|
||||
$rawResponse = "";
|
||||
$response = null;
|
||||
|
||||
$ms = null;
|
||||
|
||||
$stream->on('data', function ($data) use ($stream, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context) {
|
||||
$stream->on('data', function ($data) use ($stream, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest) {
|
||||
if ($response === null) {
|
||||
$rawResponse .= $data;
|
||||
$pos = strpos($rawResponse, "\r\n\r\n");
|
||||
@ -122,7 +122,7 @@ function runTest($case)
|
||||
$rawResponse = substr($rawResponse, 0, $pos + 4);
|
||||
$response = \GuzzleHttp\Psr7\parse_response($rawResponse);
|
||||
|
||||
if (!$cn->validateResponse($response)) {
|
||||
if (!$cn->validateResponse($cnRequest, $response)) {
|
||||
$stream->end();
|
||||
$deferred->reject();
|
||||
} else {
|
||||
@ -155,8 +155,8 @@ function createReport() {
|
||||
|
||||
$factory->create($testServer, 9001)->then(function (\React\Stream\Stream $stream) use ($deferred) {
|
||||
$reportPath = "/updateReports?agent=" . AGENT . "&shutdownOnComplete=true";
|
||||
$cn = new \Ratchet\RFC6455\Handshake\ClientNegotiator($reportPath);
|
||||
$cnRequest = $cn->getRequest();
|
||||
$cn = new \Ratchet\RFC6455\Handshake\ClientNegotiator();
|
||||
$cnRequest = $cn->generateRequest(new Uri('ws://127.0.0.1:9001' . $reportPath));
|
||||
|
||||
$rawResponse = "";
|
||||
$response = null;
|
||||
@ -164,7 +164,7 @@ function createReport() {
|
||||
/** @var \Ratchet\RFC6455\Messaging\Streaming\MessageStreamer $ms */
|
||||
$ms = null;
|
||||
|
||||
$stream->on('data', function ($data) use ($stream, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context) {
|
||||
$stream->on('data', function ($data) use ($stream, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest) {
|
||||
if ($response === null) {
|
||||
$rawResponse .= $data;
|
||||
$pos = strpos($rawResponse, "\r\n\r\n");
|
||||
@ -173,7 +173,7 @@ function createReport() {
|
||||
$rawResponse = substr($rawResponse, 0, $pos + 4);
|
||||
$response = \GuzzleHttp\Psr7\parse_response($rawResponse);
|
||||
|
||||
if (!$cn->validateResponse($response)) {
|
||||
if (!$cn->validateResponse($cnRequest, $response)) {
|
||||
$stream->end();
|
||||
$deferred->reject();
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user