Merge pull request #60 from ratchetphp/v0.4
Some checks are pending
CI / PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }} (client, ubuntu-22.04, 7.4) (push) Waiting to run
CI / PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }} (client, ubuntu-22.04, 8) (push) Waiting to run
CI / PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }} (client, ubuntu-22.04, 8.1) (push) Waiting to run
CI / PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }} (client, ubuntu-22.04, 8.2) (push) Waiting to run
CI / PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }} (client, ubuntu-22.04, 8.3) (push) Waiting to run
CI / PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }} (client, ubuntu-22.04, 8.4) (push) Waiting to run
CI / PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }} (server, ubuntu-22.04, 7.4) (push) Waiting to run
CI / PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }} (server, ubuntu-22.04, 8) (push) Waiting to run
CI / PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }} (server, ubuntu-22.04, 8.1) (push) Waiting to run
CI / PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }} (server, ubuntu-22.04, 8.2) (push) Waiting to run
CI / PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }} (server, ubuntu-22.04, 8.3) (push) Waiting to run
CI / PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }} (server, ubuntu-22.04, 8.4) (push) Waiting to run

PHP 7.4 as new minimum requirement and PHP 8 compatibility
This commit is contained in:
Matt Bonneau 2025-02-23 20:18:22 -05:00 committed by GitHub
commit 859d95f85d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
30 changed files with 520 additions and 639 deletions

View File

@ -6,22 +6,24 @@ on:
jobs: jobs:
PHPUnit: PHPUnit:
name: PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) name: PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }}
runs-on: ubuntu-20.04 runs-on: ${{ matrix.os }}
strategy: strategy:
matrix: matrix:
os:
- ubuntu-22.04
env: env:
- client - client
- server - server
php: php:
- 7.4 - 7.4
- 7.3 - 8.0
- 7.2 - 8.1
- 7.1 - 8.2
- 7.0 - 8.3
- 5.6 - 8.4
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v4
- name: Setup PHP - name: Setup PHP
uses: shivammathur/setup-php@v2 uses: shivammathur/setup-php@v2
with: with:
@ -33,11 +35,4 @@ jobs:
- run: sh tests/ab/run_ab_tests.sh - run: sh tests/ab/run_ab_tests.sh
env: env:
ABTEST: ${{ matrix.env }} ABTEST: ${{ matrix.env }}
SKIP_DEFLATE: _skip_deflate
if: ${{ matrix.php <= 5.6 }}
- run: sh tests/ab/run_ab_tests.sh
env:
ABTEST: ${{ matrix.env }}
if: ${{ matrix.php >= 7.0 }}
- run: vendor/bin/phpunit --verbose - run: vendor/bin/phpunit --verbose

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.phpunit.result.cache
composer.lock composer.lock
vendor vendor
tests/ab/reports tests/ab/reports

View File

@ -26,12 +26,14 @@
} }
}, },
"require": { "require": {
"php": ">=5.4.2", "php": ">=7.4",
"guzzlehttp/psr7": "^2 || ^1.7" "psr/http-factory-implementation": "^1.0",
"symfony/polyfill-php80": "^1.15"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^5.7", "phpunit/phpunit": "^9.5",
"react/socket": "^1.3" "react/socket": "^1.3",
"guzzlehttp/psr7": "^2.7"
}, },
"scripts": { "scripts": {
"abtest-client": "ABTEST=client && sh tests/ab/run_ab_tests.sh", "abtest-client": "ABTEST=client && sh tests/ab/run_ab_tests.sh",

View File

@ -1,27 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<phpunit <phpunit
forceCoversAnnotation="true" forceCoversAnnotation="true"
mapTestClassNameToCoveredClassName="true"
bootstrap="tests/bootstrap.php" bootstrap="tests/bootstrap.php"
colors="true" colors="true"
backupGlobals="false" backupGlobals="false"
backupStaticAttributes="false" backupStaticAttributes="false"
syntaxCheck="false"
stopOnError="false" stopOnError="false"
> >
<testsuites> <testsuites>
<testsuite name="tests"> <testsuite name="tests">
<directory>tests</directory> <directory>./tests</directory>
<exclude>
<directory>test/ab</directory>
</exclude>
</testsuite> </testsuite>
</testsuites> </testsuites>
<filter> <coverage processUncoveredFiles="true">
<whitelist> <include>
<directory>./src/</directory> <directory suffix=".php">./src</directory>
</whitelist> </include>
</filter> </coverage>
</phpunit> </phpunit>

View File

@ -3,38 +3,35 @@ namespace Ratchet\RFC6455\Handshake;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use GuzzleHttp\Psr7\Request; use Psr\Http\Message\RequestFactoryInterface;
class ClientNegotiator { class ClientNegotiator {
/** private ResponseVerifier $verifier;
* @var ResponseVerifier
*/
private $verifier;
/** private RequestInterface $defaultHeader;
* @var \Psr\Http\Message\RequestInterface
*/
private $defaultHeader;
function __construct(PermessageDeflateOptions $perMessageDeflateOptions = null) { private RequestFactoryInterface $requestFactory;
public function __construct(
RequestFactoryInterface $requestFactory,
?PermessageDeflateOptions $perMessageDeflateOptions = null
) {
$this->verifier = new ResponseVerifier; $this->verifier = new ResponseVerifier;
$this->requestFactory = $requestFactory;
$this->defaultHeader = new Request('GET', '', [ $this->defaultHeader = $this->requestFactory
'Connection' => 'Upgrade' ->createRequest('GET', '')
, 'Upgrade' => 'websocket' ->withHeader('Connection' , 'Upgrade')
, 'Sec-WebSocket-Version' => $this->getVersion() ->withHeader('Upgrade' , 'websocket')
, 'User-Agent' => "Ratchet" ->withHeader('Sec-WebSocket-Version', $this->getVersion())
]); ->withHeader('User-Agent' , 'Ratchet');
if ($perMessageDeflateOptions === null) { $perMessageDeflateOptions ??= PermessageDeflateOptions::createDisabled();
$perMessageDeflateOptions = PermessageDeflateOptions::createDisabled();
}
// https://bugs.php.net/bug.php?id=73373 // https://bugs.php.net/bug.php?id=73373
// https://bugs.php.net/bug.php?id=74240 - need >=7.1.4 or >=7.0.18 // https://bugs.php.net/bug.php?id=74240 - need >=7.1.4 or >=7.0.18
if ($perMessageDeflateOptions->isEnabled() && if ($perMessageDeflateOptions->isEnabled() && !PermessageDeflateOptions::permessageDeflateSupported()) {
!PermessageDeflateOptions::permessageDeflateSupported()) { trigger_error('permessage-deflate is being disabled because it is not supported by your PHP version.', E_USER_NOTICE);
trigger_error('permessage-deflate is being disabled because it is not support by your PHP version.', E_USER_NOTICE);
$perMessageDeflateOptions = PermessageDeflateOptions::createDisabled(); $perMessageDeflateOptions = PermessageDeflateOptions::createDisabled();
} }
if ($perMessageDeflateOptions->isEnabled() && !function_exists('deflate_add')) { if ($perMessageDeflateOptions->isEnabled() && !function_exists('deflate_add')) {
@ -45,16 +42,16 @@ class ClientNegotiator {
$this->defaultHeader = $perMessageDeflateOptions->addHeaderToRequest($this->defaultHeader); $this->defaultHeader = $perMessageDeflateOptions->addHeaderToRequest($this->defaultHeader);
} }
public function generateRequest(UriInterface $uri) { public function generateRequest(UriInterface $uri): RequestInterface {
return $this->defaultHeader->withUri($uri) return $this->defaultHeader->withUri($uri)
->withHeader("Sec-WebSocket-Key", $this->generateKey()); ->withHeader('Sec-WebSocket-Key', $this->generateKey());
} }
public function validateResponse(RequestInterface $request, ResponseInterface $response) { public function validateResponse(RequestInterface $request, ResponseInterface $response): bool {
return $this->verifier->verifyAll($request, $response); return $this->verifier->verifyAll($request, $response);
} }
public function generateKey() { public function generateKey(): string {
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwzyz1234567890+/='; $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwzyz1234567890+/=';
$charRange = strlen($chars) - 1; $charRange = strlen($chars) - 1;
$key = ''; $key = '';
@ -65,7 +62,7 @@ class ClientNegotiator {
return base64_encode($key); return base64_encode($key);
} }
public function getVersion() { public function getVersion(): int {
return 13; return 13;
} }
} }

View File

@ -1,6 +1,7 @@
<?php <?php
namespace Ratchet\RFC6455\Handshake; namespace Ratchet\RFC6455\Handshake;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/** /**
* A standard interface for interacting with the various version of the WebSocket protocol * A standard interface for interacting with the various version of the WebSocket protocol
@ -14,34 +15,34 @@ interface NegotiatorInterface {
* @param RequestInterface $request * @param RequestInterface $request
* @return bool * @return bool
*/ */
function isProtocol(RequestInterface $request); public function isProtocol(RequestInterface $request): bool;
/** /**
* Although the version has a name associated with it the integer returned is the proper identification * Although the version has a name associated with it the integer returned is the proper identification
* @return int * @return int
*/ */
function getVersionNumber(); public function getVersionNumber(): int;
/** /**
* Perform the handshake and return the response headers * Perform the handshake and return the response headers
* @param RequestInterface $request * @param RequestInterface $request
* @return \Psr\Http\Message\ResponseInterface * @return ResponseInterface
*/ */
function handshake(RequestInterface $request); public function handshake(RequestInterface $request): ResponseInterface;
/** /**
* Add supported protocols. If the request has any matching the response will include one * Add supported protocols. If the request has any matching the response will include one
* @param array $protocols * @param array $protocols
*/ */
function setSupportedSubProtocols(array $protocols); public function setSupportedSubProtocols(array $protocols): void;
/** /**
* If enabled and support for a subprotocol has been added handshake * If enabled and support for a subprotocol has been added handshake
* will not upgrade if a match between request and supported subprotocols * will not upgrade if a match between request and supported subprotocols
* @param boolean $enable * @param boolean $enable
* @todo Consider extending this interface and moving this there. * @todo Consider extending this interface and moving this there.
* The spec does says the server can fail for this reason, but * The spec does say the server can fail for this reason, but
* it is not a requirement. This is an implementation detail. * it is not a requirement. This is an implementation detail.
*/ */
function setStrictSubProtocolCheck($enable); public function setStrictSubProtocolCheck(bool $enable): void;
} }

View File

@ -8,21 +8,21 @@ use Psr\Http\Message\ResponseInterface;
final class PermessageDeflateOptions final class PermessageDeflateOptions
{ {
const MAX_WINDOW_BITS = 15; public const MAX_WINDOW_BITS = 15;
/* this is a private instead of const for 5.4 compatibility */
private static $VALID_BITS = ['8', '9', '10', '11', '12', '13', '14', '15'];
private $deflateEnabled = false; private const VALID_BITS = [8, 9, 10, 11, 12, 13, 14, 15];
private $server_no_context_takeover; private bool $deflateEnabled = false;
private $client_no_context_takeover;
private $server_max_window_bits; private ?bool $server_no_context_takeover = null;
private $client_max_window_bits; private ?bool $client_no_context_takeover = null;
private ?int $server_max_window_bits = null;
private ?int $client_max_window_bits = null;
private function __construct() { } private function __construct() { }
public static function createEnabled() { public static function createEnabled() {
$new = new static(); $new = new self();
$new->deflateEnabled = true; $new->deflateEnabled = true;
$new->client_max_window_bits = self::MAX_WINDOW_BITS; $new->client_max_window_bits = self::MAX_WINDOW_BITS;
$new->client_no_context_takeover = false; $new->client_no_context_takeover = false;
@ -33,35 +33,35 @@ final class PermessageDeflateOptions
} }
public static function createDisabled() { public static function createDisabled() {
return new static(); return new self();
} }
public function withClientNoContextTakeover() { public function withClientNoContextTakeover(): self {
$new = clone $this; $new = clone $this;
$new->client_no_context_takeover = true; $new->client_no_context_takeover = true;
return $new; return $new;
} }
public function withoutClientNoContextTakeover() { public function withoutClientNoContextTakeover(): self {
$new = clone $this; $new = clone $this;
$new->client_no_context_takeover = false; $new->client_no_context_takeover = false;
return $new; return $new;
} }
public function withServerNoContextTakeover() { public function withServerNoContextTakeover(): self {
$new = clone $this; $new = clone $this;
$new->server_no_context_takeover = true; $new->server_no_context_takeover = true;
return $new; return $new;
} }
public function withoutServerNoContextTakeover() { public function withoutServerNoContextTakeover(): self {
$new = clone $this; $new = clone $this;
$new->server_no_context_takeover = false; $new->server_no_context_takeover = false;
return $new; return $new;
} }
public function withServerMaxWindowBits($bits = self::MAX_WINDOW_BITS) { public function withServerMaxWindowBits(int $bits = self::MAX_WINDOW_BITS): self {
if (!in_array($bits, self::$VALID_BITS)) { if (!in_array($bits, self::VALID_BITS)) {
throw new \Exception('server_max_window_bits must have a value between 8 and 15.'); throw new \Exception('server_max_window_bits must have a value between 8 and 15.');
} }
$new = clone $this; $new = clone $this;
@ -69,8 +69,8 @@ final class PermessageDeflateOptions
return $new; return $new;
} }
public function withClientMaxWindowBits($bits = self::MAX_WINDOW_BITS) { public function withClientMaxWindowBits(int $bits = self::MAX_WINDOW_BITS): self {
if (!in_array($bits, self::$VALID_BITS)) { if (!in_array($bits, self::VALID_BITS)) {
throw new \Exception('client_max_window_bits must have a value between 8 and 15.'); throw new \Exception('client_max_window_bits must have a value between 8 and 15.');
} }
$new = clone $this; $new = clone $this;
@ -86,7 +86,7 @@ final class PermessageDeflateOptions
* @return PermessageDeflateOptions[] * @return PermessageDeflateOptions[]
* @throws \Exception * @throws \Exception
*/ */
public static function fromRequestOrResponse(MessageInterface $requestOrResponse) { public static function fromRequestOrResponse(MessageInterface $requestOrResponse): array {
$optionSets = []; $optionSets = [];
$extHeader = preg_replace('/\s+/', '', join(', ', $requestOrResponse->getHeader('Sec-Websocket-Extensions'))); $extHeader = preg_replace('/\s+/', '', join(', ', $requestOrResponse->getHeader('Sec-Websocket-Extensions')));
@ -103,7 +103,7 @@ final class PermessageDeflateOptions
} }
array_shift($parts); array_shift($parts);
$options = new static(); $options = new self();
$options->deflateEnabled = true; $options->deflateEnabled = true;
foreach ($parts as $part) { foreach ($parts as $part) {
$kv = explode('=', $part); $kv = explode('=', $part);
@ -119,15 +119,18 @@ final class PermessageDeflateOptions
$value = true; $value = true;
break; break;
case "server_max_window_bits": case "server_max_window_bits":
if (!in_array($value, self::$VALID_BITS)) { $value = (int) $value;
if (!in_array($value, self::VALID_BITS)) {
throw new InvalidPermessageDeflateOptionsException($key . ' must have a value between 8 and 15.'); throw new InvalidPermessageDeflateOptionsException($key . ' must have a value between 8 and 15.');
} }
break; break;
case "client_max_window_bits": case "client_max_window_bits":
if ($value === null) { if ($value === null) {
$value = '15'; $value = 15;
} else {
$value = (int) $value;
} }
if (!in_array($value, self::$VALID_BITS)) { if (!in_array($value, self::VALID_BITS)) {
throw new InvalidPermessageDeflateOptionsException($key . ' must have no value or a value between 8 and 15.'); throw new InvalidPermessageDeflateOptionsException($key . ' must have no value or a value between 8 and 15.');
} }
break; break;
@ -154,39 +157,39 @@ final class PermessageDeflateOptions
} }
// always put a disabled on the end // always put a disabled on the end
$optionSets[] = new static(); $optionSets[] = new self();
return $optionSets; return $optionSets;
} }
/** /**
* @return mixed * @return bool|null
*/ */
public function getServerNoContextTakeover() public function getServerNoContextTakeover(): ?bool
{ {
return $this->server_no_context_takeover; return $this->server_no_context_takeover;
} }
/** /**
* @return mixed * @return bool|null
*/ */
public function getClientNoContextTakeover() public function getClientNoContextTakeover(): ?bool
{ {
return $this->client_no_context_takeover; return $this->client_no_context_takeover;
} }
/** /**
* @return mixed * @return int|null
*/ */
public function getServerMaxWindowBits() public function getServerMaxWindowBits(): ?int
{ {
return $this->server_max_window_bits; return $this->server_max_window_bits;
} }
/** /**
* @return mixed * @return int|null
*/ */
public function getClientMaxWindowBits() public function getClientMaxWindowBits(): ?int
{ {
return $this->client_max_window_bits; return $this->client_max_window_bits;
} }
@ -194,7 +197,7 @@ final class PermessageDeflateOptions
/** /**
* @return bool * @return bool
*/ */
public function isEnabled() public function isEnabled(): bool
{ {
return $this->deflateEnabled; return $this->deflateEnabled;
} }
@ -203,7 +206,7 @@ final class PermessageDeflateOptions
* @param ResponseInterface $response * @param ResponseInterface $response
* @return ResponseInterface * @return ResponseInterface
*/ */
public function addHeaderToResponse(ResponseInterface $response) public function addHeaderToResponse(ResponseInterface $response): ResponseInterface
{ {
if (!$this->deflateEnabled) { if (!$this->deflateEnabled) {
return $response; return $response;
@ -226,7 +229,7 @@ final class PermessageDeflateOptions
return $response->withAddedHeader('Sec-Websocket-Extensions', $header); return $response->withAddedHeader('Sec-Websocket-Extensions', $header);
} }
public function addHeaderToRequest(RequestInterface $request) { public function addHeaderToRequest(RequestInterface $request): RequestInterface {
if (!$this->deflateEnabled) { if (!$this->deflateEnabled) {
return $request; return $request;
} }
@ -249,7 +252,7 @@ final class PermessageDeflateOptions
return $request->withAddedHeader('Sec-Websocket-Extensions', $header); return $request->withAddedHeader('Sec-Websocket-Extensions', $header);
} }
public static function permessageDeflateSupported($version = PHP_VERSION) { public static function permessageDeflateSupported(string $version = PHP_VERSION): bool {
if (!function_exists('deflate_init')) { if (!function_exists('deflate_init')) {
return false; return false;
} }

View File

@ -8,14 +8,14 @@ use Psr\Http\Message\RequestInterface;
* @todo Currently just returning invalid - should consider returning appropriate HTTP status code error #s * @todo Currently just returning invalid - should consider returning appropriate HTTP status code error #s
*/ */
class RequestVerifier { class RequestVerifier {
const VERSION = 13; public const VERSION = 13;
/** /**
* Given an array of the headers this method will run through all verification methods * Given an array of the headers this method will run through all verification methods
* @param RequestInterface $request * @param RequestInterface $request
* @return bool TRUE if all headers are valid, FALSE if 1 or more were invalid * @return bool TRUE if all headers are valid, FALSE if 1 or more were invalid
*/ */
public function verifyAll(RequestInterface $request) { public function verifyAll(RequestInterface $request): bool {
$passes = 0; $passes = 0;
$passes += (int)$this->verifyMethod($request->getMethod()); $passes += (int)$this->verifyMethod($request->getMethod());
@ -27,7 +27,7 @@ class RequestVerifier {
$passes += (int)$this->verifyKey($request->getHeader('Sec-WebSocket-Key')); $passes += (int)$this->verifyKey($request->getHeader('Sec-WebSocket-Key'));
$passes += (int)$this->verifyVersion($request->getHeader('Sec-WebSocket-Version')); $passes += (int)$this->verifyVersion($request->getHeader('Sec-WebSocket-Version'));
return (8 === $passes); return 8 === $passes;
} }
/** /**
@ -35,8 +35,8 @@ class RequestVerifier {
* @param string * @param string
* @return bool * @return bool
*/ */
public function verifyMethod($val) { public function verifyMethod(string $val): bool {
return ('get' === strtolower($val)); return 'get' === strtolower($val);
} }
/** /**
@ -44,15 +44,15 @@ class RequestVerifier {
* @param string|int * @param string|int
* @return bool * @return bool
*/ */
public function verifyHTTPVersion($val) { public function verifyHTTPVersion($val): bool {
return (1.1 <= (double)$val); return 1.1 <= (double)$val;
} }
/** /**
* @param string * @param string
* @return bool * @return bool
*/ */
public function verifyRequestURI($val) { public function verifyRequestURI(string $val): bool {
if ($val[0] !== '/') { if ($val[0] !== '/') {
return false; return false;
} }
@ -73,8 +73,8 @@ class RequestVerifier {
* @return bool * @return bool
* @todo Once I fix HTTP::getHeaders just verify this isn't NULL or empty...or maybe need to verify it's a valid domain??? Or should it equal $_SERVER['HOST'] ? * @todo Once I fix HTTP::getHeaders just verify this isn't NULL or empty...or maybe need to verify it's a valid domain??? Or should it equal $_SERVER['HOST'] ?
*/ */
public function verifyHost(array $hostHeader) { public function verifyHost(array $hostHeader): bool {
return (1 === count($hostHeader)); return 1 === count($hostHeader);
} }
/** /**
@ -82,8 +82,8 @@ class RequestVerifier {
* @param array $upgradeHeader MUST equal "websocket" * @param array $upgradeHeader MUST equal "websocket"
* @return bool * @return bool
*/ */
public function verifyUpgradeRequest(array $upgradeHeader) { public function verifyUpgradeRequest(array $upgradeHeader): bool {
return (1 === count($upgradeHeader) && 'websocket' === strtolower($upgradeHeader[0])); return 1 === count($upgradeHeader) && 'websocket' === strtolower($upgradeHeader[0]);
} }
/** /**
@ -91,13 +91,11 @@ class RequestVerifier {
* @param array $connectionHeader MUST include "Upgrade" * @param array $connectionHeader MUST include "Upgrade"
* @return bool * @return bool
*/ */
public function verifyConnection(array $connectionHeader) { public function verifyConnection(array $connectionHeader): bool {
foreach ($connectionHeader as $l) { foreach ($connectionHeader as $l) {
$upgrades = array_filter( $upgrades = array_filter(
array_map('trim', array_map('strtolower', explode(',', $l))), array_map('trim', array_map('strtolower', explode(',', $l))),
function ($x) { static fn (string $x) => 'upgrade' === $x
return 'upgrade' === $x;
}
); );
if (count($upgrades) > 0) { if (count($upgrades) > 0) {
return true; return true;
@ -113,8 +111,8 @@ class RequestVerifier {
* @todo The spec says we don't need to base64_decode - can I just check if the length is 24 and not decode? * @todo The spec says we don't need to base64_decode - can I just check if the length is 24 and not decode?
* @todo Check the spec to see what the encoding of the key could be * @todo Check the spec to see what the encoding of the key could be
*/ */
public function verifyKey(array $keyHeader) { public function verifyKey(array $keyHeader): bool {
return (1 === count($keyHeader) && 16 === strlen(base64_decode($keyHeader[0]))); return 1 === count($keyHeader) && 16 === strlen(base64_decode($keyHeader[0]));
} }
/** /**
@ -122,33 +120,33 @@ class RequestVerifier {
* @param string[] $versionHeader MUST equal ["13"] * @param string[] $versionHeader MUST equal ["13"]
* @return bool * @return bool
*/ */
public function verifyVersion(array $versionHeader) { public function verifyVersion(array $versionHeader): bool {
return (1 === count($versionHeader) && static::VERSION === (int)$versionHeader[0]); return 1 === count($versionHeader) && static::VERSION === (int)$versionHeader[0];
} }
/** /**
* @todo Write logic for this method. See section 4.2.1.8 * @todo Write logic for this method. See section 4.2.1.8
*/ */
public function verifyProtocol($val) { public function verifyProtocol($val): bool {
return true;
} }
/** /**
* @todo Write logic for this method. See section 4.2.1.9 * @todo Write logic for this method. See section 4.2.1.9
*/ */
public function verifyExtensions($val) { public function verifyExtensions($val): bool {
return true;
} }
public function getPermessageDeflateOptions(array $requestHeader, array $responseHeader) { public function getPermessageDeflateOptions(array $requestHeader, array $responseHeader): array {
$headerChecker = static fn (string $val) => 'permessage-deflate' === substr($val, 0, strlen('permessage-deflate'));
$deflate = true; $deflate = true;
if (!isset($requestHeader['Sec-WebSocket-Extensions']) || count(array_filter($requestHeader['Sec-WebSocket-Extensions'], function ($val) { if (!isset($requestHeader['Sec-WebSocket-Extensions']) || count(array_filter($requestHeader['Sec-WebSocket-Extensions'], $headerChecker)) === 0) {
return 'permessage-deflate' === substr($val, 0, strlen('permessage-deflate'));
})) === 0) {
$deflate = false; $deflate = false;
} }
if (!isset($responseHeader['Sec-WebSocket-Extensions']) || count(array_filter($responseHeader['Sec-WebSocket-Extensions'], function ($val) { if (!isset($responseHeader['Sec-WebSocket-Extensions']) || count(array_filter($responseHeader['Sec-WebSocket-Extensions'], $headerChecker)) === 0) {
return 'permessage-deflate' === substr($val, 0, strlen('permessage-deflate'));
})) === 0) {
$deflate = false; $deflate = false;
} }

View File

@ -4,7 +4,7 @@ use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
class ResponseVerifier { class ResponseVerifier {
public function verifyAll(RequestInterface $request, ResponseInterface $response) { public function verifyAll(RequestInterface $request, ResponseInterface $response): bool {
$passes = 0; $passes = 0;
$passes += (int)$this->verifyStatus($response->getStatusCode()); $passes += (int)$this->verifyStatus($response->getStatusCode());
@ -26,31 +26,31 @@ class ResponseVerifier {
return (6 === $passes); return (6 === $passes);
} }
public function verifyStatus($status) { public function verifyStatus(int $status): bool {
return ((int)$status === 101); return $status === 101;
} }
public function verifyUpgrade(array $upgrade) { public function verifyUpgrade(array $upgrade): bool {
return (in_array('websocket', array_map('strtolower', $upgrade))); return in_array('websocket', array_map('strtolower', $upgrade));
} }
public function verifyConnection(array $connection) { public function verifyConnection(array $connection): bool {
return (in_array('upgrade', array_map('strtolower', $connection))); return in_array('upgrade', array_map('strtolower', $connection));
} }
public function verifySecWebSocketAccept($swa, $key) { public function verifySecWebSocketAccept(array $swa, array $key): bool {
return ( return
1 === count($swa) && 1 === count($swa) &&
1 === count($key) && 1 === count($key) &&
$swa[0] === $this->sign($key[0]) $swa[0] === $this->sign($key[0])
); ;
} }
public function sign($key) { public function sign(string $key): string {
return base64_encode(sha1($key . NegotiatorInterface::GUID, true)); return base64_encode(sha1($key . NegotiatorInterface::GUID, true));
} }
public function verifySubProtocol(array $requestHeader, array $responseHeader) { public function verifySubProtocol(array $requestHeader, array $responseHeader): bool {
if (0 === count($responseHeader)) { if (0 === count($responseHeader)) {
return true; return true;
} }
@ -60,7 +60,7 @@ class ResponseVerifier {
return count($responseHeader) === 1 && count(array_intersect($responseHeader, $requestedProtocols)) === 1; return count($responseHeader) === 1 && count(array_intersect($responseHeader, $requestedProtocols)) === 1;
} }
public function verifyExtensions(array $requestHeader, array $responseHeader) { public function verifyExtensions(array $requestHeader, array $responseHeader): int {
if (in_array('permessage-deflate', $responseHeader)) { if (in_array('permessage-deflate', $responseHeader)) {
return strpos(implode(',', $requestHeader), 'permessage-deflate') !== false ? 1 : 0; return strpos(implode(',', $requestHeader), 'permessage-deflate') !== false ? 1 : 0;
} }

View File

@ -1,26 +1,32 @@
<?php <?php
namespace Ratchet\RFC6455\Handshake; namespace Ratchet\RFC6455\Handshake;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
/** /**
* The latest version of the WebSocket protocol * The latest version of the WebSocket protocol
* @todo Unicode: return mb_convert_encoding(pack("N",$u), mb_internal_encoding(), 'UCS-4BE'); * @todo Unicode: return mb_convert_encoding(pack("N",$u), mb_internal_encoding(), 'UCS-4BE');
*/ */
class ServerNegotiator implements NegotiatorInterface { class ServerNegotiator implements NegotiatorInterface {
/** private RequestVerifier $verifier;
* @var \Ratchet\RFC6455\Handshake\RequestVerifier
*/
private $verifier;
private $_supportedSubProtocols = []; private ResponseFactoryInterface $responseFactory;
private $_strictSubProtocols = false; private array $_supportedSubProtocols = [];
private $enablePerMessageDeflate = false; private bool $_strictSubProtocols = false;
public function __construct(RequestVerifier $requestVerifier, $enablePerMessageDeflate = false) { private bool $enablePerMessageDeflate = false;
public function __construct(
RequestVerifier $requestVerifier,
ResponseFactoryInterface $responseFactory,
$enablePerMessageDeflate = false
) {
$this->verifier = $requestVerifier; $this->verifier = $requestVerifier;
$this->responseFactory = $responseFactory;
// https://bugs.php.net/bug.php?id=73373 // https://bugs.php.net/bug.php?id=73373
// https://bugs.php.net/bug.php?id=74240 - need >=7.1.4 or >=7.0.18 // https://bugs.php.net/bug.php?id=74240 - need >=7.1.4 or >=7.0.18
@ -38,85 +44,85 @@ class ServerNegotiator implements NegotiatorInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isProtocol(RequestInterface $request) { public function isProtocol(RequestInterface $request): bool {
return $this->verifier->verifyVersion($request->getHeader('Sec-WebSocket-Version')); return $this->verifier->verifyVersion($request->getHeader('Sec-WebSocket-Version'));
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getVersionNumber() { public function getVersionNumber(): int {
return RequestVerifier::VERSION; return RequestVerifier::VERSION;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handshake(RequestInterface $request) { public function handshake(RequestInterface $request): ResponseInterface {
$response = $this->responseFactory->createResponse();
if (true !== $this->verifier->verifyMethod($request->getMethod())) { if (true !== $this->verifier->verifyMethod($request->getMethod())) {
return new Response(405, ['Allow' => 'GET']); return $response->withHeader('Allow', 'GET')->withStatus(405);
} }
if (true !== $this->verifier->verifyHTTPVersion($request->getProtocolVersion())) { if (true !== $this->verifier->verifyHTTPVersion($request->getProtocolVersion())) {
return new Response(505); return $response->withStatus(505);
} }
if (true !== $this->verifier->verifyRequestURI($request->getUri()->getPath())) { if (true !== $this->verifier->verifyRequestURI($request->getUri()->getPath())) {
return new Response(400); return $response->withStatus(400);
} }
if (true !== $this->verifier->verifyHost($request->getHeader('Host'))) { if (true !== $this->verifier->verifyHost($request->getHeader('Host'))) {
return new Response(400); return $response->withStatus(400);
} }
$upgradeSuggestion = [ $upgradeResponse = $response
'Connection' => 'Upgrade', ->withHeader('Connection' , 'Upgrade')
'Upgrade' => 'websocket', ->withHeader('Upgrade' , 'websocket')
'Sec-WebSocket-Version' => $this->getVersionNumber() ->withHeader('Sec-WebSocket-Version', $this->getVersionNumber());
];
if (count($this->_supportedSubProtocols) > 0) { if (count($this->_supportedSubProtocols) > 0) {
$upgradeSuggestion['Sec-WebSocket-Protocol'] = implode(', ', array_keys($this->_supportedSubProtocols)); $upgradeResponse = $upgradeResponse->withHeader(
'Sec-WebSocket-Protocol', implode(', ', array_keys($this->_supportedSubProtocols))
);
} }
if (true !== $this->verifier->verifyUpgradeRequest($request->getHeader('Upgrade'))) { if (true !== $this->verifier->verifyUpgradeRequest($request->getHeader('Upgrade'))) {
return new Response(426, $upgradeSuggestion, null, '1.1', 'Upgrade header MUST be provided'); return $upgradeResponse->withStatus(426, 'Upgrade header MUST be provided');
} }
if (true !== $this->verifier->verifyConnection($request->getHeader('Connection'))) { if (true !== $this->verifier->verifyConnection($request->getHeader('Connection'))) {
return new Response(400, [], null, '1.1', 'Connection Upgrade MUST be requested'); return $response->withStatus(400, 'Connection Upgrade MUST be requested');
} }
if (true !== $this->verifier->verifyKey($request->getHeader('Sec-WebSocket-Key'))) { if (true !== $this->verifier->verifyKey($request->getHeader('Sec-WebSocket-Key'))) {
return new Response(400, [], null, '1.1', 'Invalid Sec-WebSocket-Key'); return $response->withStatus(400, 'Invalid Sec-WebSocket-Key');
} }
if (true !== $this->verifier->verifyVersion($request->getHeader('Sec-WebSocket-Version'))) { if (true !== $this->verifier->verifyVersion($request->getHeader('Sec-WebSocket-Version'))) {
return new Response(426, $upgradeSuggestion); return $upgradeResponse->withStatus(426);
} }
$headers = [];
$subProtocols = $request->getHeader('Sec-WebSocket-Protocol'); $subProtocols = $request->getHeader('Sec-WebSocket-Protocol');
if (count($subProtocols) > 0 || (count($this->_supportedSubProtocols) > 0 && $this->_strictSubProtocols)) { if (count($subProtocols) > 0 || (count($this->_supportedSubProtocols) > 0 && $this->_strictSubProtocols)) {
$subProtocols = array_map('trim', explode(',', implode(',', $subProtocols))); $subProtocols = array_map('trim', explode(',', implode(',', $subProtocols)));
$match = array_reduce($subProtocols, function($accumulator, $protocol) { $match = array_reduce($subProtocols, fn ($accumulator, $protocol) => $accumulator ?: (isset($this->_supportedSubProtocols[$protocol]) ? $protocol : null), null);
return $accumulator ?: (isset($this->_supportedSubProtocols[$protocol]) ? $protocol : null);
}, null);
if ($this->_strictSubProtocols && null === $match) { if ($this->_strictSubProtocols && null === $match) {
return new Response(426, $upgradeSuggestion, null, '1.1', 'No Sec-WebSocket-Protocols requested supported'); return $upgradeResponse->withStatus(426, 'No Sec-WebSocket-Protocols requested supported');
} }
if (null !== $match) { if (null !== $match) {
$headers['Sec-WebSocket-Protocol'] = $match; $response = $response->withHeader('Sec-WebSocket-Protocol', $match);
} }
} }
$response = new Response(101, array_merge($headers, [ $response = $response
'Upgrade' => 'websocket' ->withStatus(101)
, 'Connection' => 'Upgrade' ->withHeader('Upgrade' , 'websocket')
, 'Sec-WebSocket-Accept' => $this->sign((string)$request->getHeader('Sec-WebSocket-Key')[0]) ->withHeader('Connection' , 'Upgrade')
, 'X-Powered-By' => 'Ratchet' ->withHeader('Sec-WebSocket-Accept', $this->sign((string)$request->getHeader('Sec-WebSocket-Key')[0]))
])); ->withHeader('X-Powered-By' , 'Ratchet');
try { try {
$perMessageDeflateRequest = PermessageDeflateOptions::fromRequestOrResponse($request)[0]; $perMessageDeflateRequest = PermessageDeflateOptions::fromRequestOrResponse($request)[0];
@ -137,14 +143,14 @@ class ServerNegotiator implements NegotiatorInterface {
* @return string * @return string
* @internal * @internal
*/ */
public function sign($key) { public function sign(string $key): string {
return base64_encode(sha1($key . static::GUID, true)); return base64_encode(sha1($key . static::GUID, true));
} }
/** /**
* @param array $protocols * @param array $protocols
*/ */
function setSupportedSubProtocols(array $protocols) { public function setSupportedSubProtocols(array $protocols): void {
$this->_supportedSubProtocols = array_flip($protocols); $this->_supportedSubProtocols = array_flip($protocols);
} }
@ -153,10 +159,10 @@ class ServerNegotiator implements NegotiatorInterface {
* will not upgrade if a match between request and supported subprotocols * will not upgrade if a match between request and supported subprotocols
* @param boolean $enable * @param boolean $enable
* @todo Consider extending this interface and moving this there. * @todo Consider extending this interface and moving this there.
* The spec does says the server can fail for this reason, but * The spec does say the server can fail for this reason, but
* it is not a requirement. This is an implementation detail. * it is not a requirement. This is an implementation detail.
*/ */
function setStrictSubProtocolCheck($enable) { public function setStrictSubProtocolCheck(bool $enable): void {
$this->_strictSubProtocols = (boolean)$enable; $this->_strictSubProtocols = $enable;
} }
} }

View File

@ -2,10 +2,7 @@
namespace Ratchet\RFC6455\Messaging; namespace Ratchet\RFC6455\Messaging;
class CloseFrameChecker { class CloseFrameChecker {
private $validCloseCodes = []; private array $validCloseCodes = [
public function __construct() {
$this->validCloseCodes = [
Frame::CLOSE_NORMAL, Frame::CLOSE_NORMAL,
Frame::CLOSE_GOING_AWAY, Frame::CLOSE_GOING_AWAY,
Frame::CLOSE_PROTOCOL, Frame::CLOSE_PROTOCOL,
@ -16,9 +13,8 @@ class CloseFrameChecker {
Frame::CLOSE_MAND_EXT, Frame::CLOSE_MAND_EXT,
Frame::CLOSE_SRV_ERR, Frame::CLOSE_SRV_ERR,
]; ];
}
public function __invoke($val) { public function __invoke(int $val): bool {
return ($val >= 3000 && $val <= 4999) || in_array($val, $this->validCloseCodes); return ($val >= 3000 && $val <= 4999) || in_array($val, $this->validCloseCodes);
} }
} }

View File

@ -1,34 +1,28 @@
<?php <?php
namespace Ratchet\RFC6455\Messaging; namespace Ratchet\RFC6455\Messaging;
interface DataInterface { interface DataInterface extends \Stringable {
/** /**
* Determine if the message is complete or still fragmented * Determine if the message is complete or still fragmented
* @return bool * @return bool
*/ */
function isCoalesced(); public function isCoalesced(): bool;
/** /**
* Get the number of bytes the payload is set to be * Get the number of bytes the payload is set to be
* @return int * @return int
*/ */
function getPayloadLength(); public function getPayloadLength(): int;
/** /**
* Get the payload (message) sent from peer * Get the payload (message) sent from peer
* @return string * @return string
*/ */
function getPayload(); public function getPayload(): string;
/** /**
* Get raw contents of the message * Get raw contents of the message
* @return string * @return string
*/ */
function getContents(); public function getContents(): string;
/**
* Should return the unmasked payload received from peer
* @return string
*/
function __toString();
} }

View File

@ -26,40 +26,34 @@ class Frame implements FrameInterface {
/** /**
* The contents of the frame * The contents of the frame
* @var string
*/ */
protected $data = ''; protected string $data = '';
/** /**
* Number of bytes received from the frame * Number of bytes received from the frame
* @var int
*/ */
public $bytesRecvd = 0; public int $bytesRecvd = 0;
/** /**
* Number of bytes in the payload (as per framing protocol) * Number of bytes in the payload (as per framing protocol)
* @var int
*/ */
protected $defPayLen = -1; protected int $defPayLen = -1;
/** /**
* If the frame is coalesced this is true * If the frame is coalesced this is true
* This is to prevent doing math every time ::isCoalesced is called * This is to prevent doing math every time ::isCoalesced is called
* @var boolean
*/ */
private $isCoalesced = false; private bool $isCoalesced = false;
/** /**
* The unpacked first byte of the frame * The unpacked first byte of the frame
* @var int
*/ */
protected $firstByte = -1; protected int $firstByte = -1;
/** /**
* The unpacked second byte of the frame * The unpacked second byte of the frame
* @var int
*/ */
protected $secondByte = -1; protected int $secondByte = -1;
/** /**
* @var callable * @var callable
@ -73,10 +67,8 @@ class Frame implements FrameInterface {
* @param int $opcode * @param int $opcode
* @param callable<\UnderflowException> $ufExceptionFactory * @param callable<\UnderflowException> $ufExceptionFactory
*/ */
public function __construct($payload = null, $final = true, $opcode = 1, callable $ufExceptionFactory = null) { public function __construct(?string $payload = null, bool $final = true, int $opcode = 1, ?callable $ufExceptionFactory = null) {
$this->ufeg = $ufExceptionFactory ?: static function($msg = '') { $this->ufeg = $ufExceptionFactory ?: static fn (string $msg = '') => new \UnderflowException($msg);
return new \UnderflowException($msg);
};
if (null === $payload) { if (null === $payload) {
return; return;
@ -103,7 +95,7 @@ class Frame implements FrameInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isCoalesced() { public function isCoalesced(): bool {
if (true === $this->isCoalesced) { if (true === $this->isCoalesced) {
return true; return true;
} }
@ -123,7 +115,7 @@ class Frame implements FrameInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function addBuffer($buf) { public function addBuffer(string $buf): void {
$len = strlen($buf); $len = strlen($buf);
$this->data .= $buf; $this->data .= $buf;
@ -141,7 +133,7 @@ class Frame implements FrameInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isFinal() { public function isFinal(): bool {
if (-1 === $this->firstByte) { if (-1 === $this->firstByte) {
throw call_user_func($this->ufeg, 'Not enough bytes received to determine if this is the final frame in message'); throw call_user_func($this->ufeg, 'Not enough bytes received to determine if this is the final frame in message');
} }
@ -149,7 +141,7 @@ class Frame implements FrameInterface {
return 128 === ($this->firstByte & 128); return 128 === ($this->firstByte & 128);
} }
public function setRsv1($value = true) { public function setRsv1(bool $value = true): self {
if (strlen($this->data) == 0) { if (strlen($this->data) == 0) {
throw new \UnderflowException("Cannot set Rsv1 because there is no data."); throw new \UnderflowException("Cannot set Rsv1 because there is no data.");
} }
@ -170,7 +162,7 @@ class Frame implements FrameInterface {
* @return boolean * @return boolean
* @throws \UnderflowException * @throws \UnderflowException
*/ */
public function getRsv1() { public function getRsv1(): bool {
if (-1 === $this->firstByte) { if (-1 === $this->firstByte) {
throw call_user_func($this->ufeg, 'Not enough bytes received to determine reserved bit'); throw call_user_func($this->ufeg, 'Not enough bytes received to determine reserved bit');
} }
@ -182,7 +174,7 @@ class Frame implements FrameInterface {
* @return boolean * @return boolean
* @throws \UnderflowException * @throws \UnderflowException
*/ */
public function getRsv2() { public function getRsv2(): bool {
if (-1 === $this->firstByte) { if (-1 === $this->firstByte) {
throw call_user_func($this->ufeg, 'Not enough bytes received to determine reserved bit'); throw call_user_func($this->ufeg, 'Not enough bytes received to determine reserved bit');
} }
@ -194,7 +186,7 @@ class Frame implements FrameInterface {
* @return boolean * @return boolean
* @throws \UnderflowException * @throws \UnderflowException
*/ */
public function getRsv3() { public function getRsv3(): bool {
if (-1 === $this->firstByte) { if (-1 === $this->firstByte) {
throw call_user_func($this->ufeg, 'Not enough bytes received to determine reserved bit'); throw call_user_func($this->ufeg, 'Not enough bytes received to determine reserved bit');
} }
@ -205,7 +197,7 @@ class Frame implements FrameInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isMasked() { public function isMasked(): bool {
if (-1 === $this->secondByte) { if (-1 === $this->secondByte) {
throw call_user_func($this->ufeg, "Not enough bytes received ({$this->bytesRecvd}) to determine if mask is set"); throw call_user_func($this->ufeg, "Not enough bytes received ({$this->bytesRecvd}) to determine if mask is set");
} }
@ -216,7 +208,7 @@ class Frame implements FrameInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getMaskingKey() { public function getMaskingKey(): string {
if (!$this->isMasked()) { if (!$this->isMasked()) {
return ''; return '';
} }
@ -234,7 +226,7 @@ class Frame implements FrameInterface {
* Create a 4 byte masking key * Create a 4 byte masking key
* @return string * @return string
*/ */
public function generateMaskingKey() { public function generateMaskingKey(): string {
$mask = ''; $mask = '';
for ($i = 1; $i <= static::MASK_LENGTH; $i++) { for ($i = 1; $i <= static::MASK_LENGTH; $i++) {
@ -251,7 +243,7 @@ class Frame implements FrameInterface {
* @throws \InvalidArgumentException If there is an issue with the given masking key * @throws \InvalidArgumentException If there is an issue with the given masking key
* @return Frame * @return Frame
*/ */
public function maskPayload($maskingKey = null) { public function maskPayload(?string $maskingKey = null): self {
if (null === $maskingKey) { if (null === $maskingKey) {
$maskingKey = $this->generateMaskingKey(); $maskingKey = $this->generateMaskingKey();
} }
@ -282,7 +274,7 @@ class Frame implements FrameInterface {
* @throws \UnderFlowException If the frame is not coalesced * @throws \UnderFlowException If the frame is not coalesced
* @return Frame * @return Frame
*/ */
public function unMaskPayload() { public function unMaskPayload(): self {
if (!$this->isCoalesced()) { if (!$this->isCoalesced()) {
throw call_user_func($this->ufeg, 'Frame must be coalesced before applying mask'); throw call_user_func($this->ufeg, 'Frame must be coalesced before applying mask');
} }
@ -311,7 +303,7 @@ class Frame implements FrameInterface {
* @throws \UnderflowException If using the payload but enough hasn't been buffered * @throws \UnderflowException If using the payload but enough hasn't been buffered
* @return string The masked string * @return string The masked string
*/ */
public function applyMask($maskingKey, $payload = null) { public function applyMask(string $maskingKey, ?string $payload = null): string {
if (null === $payload) { if (null === $payload) {
if (!$this->isCoalesced()) { if (!$this->isCoalesced()) {
throw call_user_func($this->ufeg, 'Frame must be coalesced to apply a mask'); throw call_user_func($this->ufeg, 'Frame must be coalesced to apply a mask');
@ -332,7 +324,7 @@ class Frame implements FrameInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getOpcode() { public function getOpcode(): int {
if (-1 === $this->firstByte) { if (-1 === $this->firstByte) {
throw call_user_func($this->ufeg, 'Not enough bytes received to determine opcode'); throw call_user_func($this->ufeg, 'Not enough bytes received to determine opcode');
} }
@ -345,7 +337,7 @@ class Frame implements FrameInterface {
* @return int * @return int
* @throws \UnderflowException If the buffer doesn't have enough data to determine this * @throws \UnderflowException If the buffer doesn't have enough data to determine this
*/ */
protected function getFirstPayloadVal() { protected function getFirstPayloadVal(): int {
if (-1 === $this->secondByte) { if (-1 === $this->secondByte) {
throw call_user_func($this->ufeg, 'Not enough bytes received'); throw call_user_func($this->ufeg, 'Not enough bytes received');
} }
@ -357,7 +349,7 @@ class Frame implements FrameInterface {
* @return int (7|23|71) Number of bits defined for the payload length in the fame * @return int (7|23|71) Number of bits defined for the payload length in the fame
* @throws \UnderflowException * @throws \UnderflowException
*/ */
protected function getNumPayloadBits() { protected function getNumPayloadBits(): int {
if (-1 === $this->secondByte) { if (-1 === $this->secondByte) {
throw call_user_func($this->ufeg, 'Not enough bytes received'); throw call_user_func($this->ufeg, 'Not enough bytes received');
} }
@ -387,14 +379,14 @@ class Frame implements FrameInterface {
* This just returns the number of bytes used in the frame to describe the payload length (as opposed to # of bits) * This just returns the number of bytes used in the frame to describe the payload length (as opposed to # of bits)
* @see getNumPayloadBits * @see getNumPayloadBits
*/ */
protected function getNumPayloadBytes() { protected function getNumPayloadBytes(): int {
return (1 + $this->getNumPayloadBits()) / 8; return (1 + $this->getNumPayloadBits()) / 8;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPayloadLength() { public function getPayloadLength(): int {
if ($this->defPayLen !== -1) { if ($this->defPayLen !== -1) {
return $this->defPayLen; return $this->defPayLen;
} }
@ -424,7 +416,7 @@ class Frame implements FrameInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPayloadStartingByte() { public function getPayloadStartingByte(): int {
return 1 + $this->getNumPayloadBytes() + ($this->isMasked() ? static::MASK_LENGTH : 0); return 1 + $this->getNumPayloadBytes() + ($this->isMasked() ? static::MASK_LENGTH : 0);
} }
@ -432,7 +424,7 @@ class Frame implements FrameInterface {
* {@inheritdoc} * {@inheritdoc}
* @todo Consider not checking mask, always returning the payload, masked or not * @todo Consider not checking mask, always returning the payload, masked or not
*/ */
public function getPayload() { public function getPayload(): string {
if (!$this->isCoalesced()) { if (!$this->isCoalesced()) {
throw call_user_func($this->ufeg, 'Can not return partial message'); throw call_user_func($this->ufeg, 'Can not return partial message');
} }
@ -444,11 +436,11 @@ class Frame implements FrameInterface {
* Get the raw contents of the frame * Get the raw contents of the frame
* @todo This is untested, make sure the substr is right - trying to return the frame w/o the overflow * @todo This is untested, make sure the substr is right - trying to return the frame w/o the overflow
*/ */
public function getContents() { public function getContents(): string {
return substr($this->data, 0, $this->getPayloadStartingByte() + $this->getPayloadLength()); return substr($this->data, 0, $this->getPayloadStartingByte() + $this->getPayloadLength());
} }
public function __toString() { public function __toString(): string {
$payload = (string)substr($this->data, $this->getPayloadStartingByte(), $this->getPayloadLength()); $payload = (string)substr($this->data, $this->getPayloadStartingByte(), $this->getPayloadLength());
if ($this->isMasked()) { if ($this->isMasked()) {
@ -463,7 +455,7 @@ class Frame implements FrameInterface {
* This method will take the extra bytes off the end and return them * This method will take the extra bytes off the end and return them
* @return string * @return string
*/ */
public function extractOverflow() { public function extractOverflow(): string {
if ($this->isCoalesced()) { if ($this->isCoalesced()) {
$endPoint = $this->getPayloadLength(); $endPoint = $this->getPayloadLength();
$endPoint += $this->getPayloadStartingByte(); $endPoint += $this->getPayloadStartingByte();

View File

@ -6,33 +6,33 @@ interface FrameInterface extends DataInterface {
* Add incoming data to the frame from peer * Add incoming data to the frame from peer
* @param string * @param string
*/ */
function addBuffer($buf); public function addBuffer(string $buf): void;
/** /**
* Is this the final frame in a fragmented message? * Is this the final frame in a fragmented message?
* @return bool * @return bool
*/ */
function isFinal(); public function isFinal(): bool;
/** /**
* Is the payload masked? * Is the payload masked?
* @return bool * @return bool
*/ */
function isMasked(); public function isMasked(): bool;
/** /**
* @return int * @return int
*/ */
function getOpcode(); public function getOpcode(): int;
/** /**
* @return int * @return int
*/ */
//function getReceivedPayloadLength(); //public function getReceivedPayloadLength(): int;
/** /**
* 32-big string * 32-big string
* @return string * @return string
*/ */
function getMaskingKey(); public function getMaskingKey(): string;
} }

View File

@ -2,53 +2,43 @@
namespace Ratchet\RFC6455\Messaging; namespace Ratchet\RFC6455\Messaging;
class Message implements \IteratorAggregate, MessageInterface { class Message implements \IteratorAggregate, MessageInterface {
/** private \SplDoublyLinkedList $_frames;
* @var \SplDoublyLinkedList
*/
private $_frames;
/** private int $len;
* @var int
*/
private $len;
#[\ReturnTypeWillChange]
public function __construct() { public function __construct() {
$this->_frames = new \SplDoublyLinkedList; $this->_frames = new \SplDoublyLinkedList;
$this->len = 0; $this->len = 0;
} }
#[\ReturnTypeWillChange] public function getIterator(): \Traversable {
public function getIterator() {
return $this->_frames; return $this->_frames;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
#[\ReturnTypeWillChange] public function count(): int {
public function count() {
return count($this->_frames); return count($this->_frames);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
#[\ReturnTypeWillChange] public function isCoalesced(): bool {
public function isCoalesced() {
if (count($this->_frames) == 0) { if (count($this->_frames) == 0) {
return false; return false;
} }
$last = $this->_frames->top(); $last = $this->_frames->top();
return ($last->isCoalesced() && $last->isFinal()); return $last->isCoalesced() && $last->isFinal();
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function addFrame(FrameInterface $fragment) { public function addFrame(FrameInterface $fragment): MessageInterface {
$this->len += $fragment->getPayloadLength(); $this->len += $fragment->getPayloadLength();
$this->_frames->push($fragment); $this->_frames->push($fragment);
@ -58,7 +48,7 @@ class Message implements \IteratorAggregate, MessageInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getOpcode() { public function getOpcode(): int {
if (count($this->_frames) == 0) { if (count($this->_frames) == 0) {
throw new \UnderflowException('No frames have been added to this message'); throw new \UnderflowException('No frames have been added to this message');
} }
@ -69,14 +59,14 @@ class Message implements \IteratorAggregate, MessageInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPayloadLength() { public function getPayloadLength(): int {
return $this->len; return $this->len;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPayload() { public function getPayload(): string {
if (!$this->isCoalesced()) { if (!$this->isCoalesced()) {
throw new \UnderflowException('Message has not been put back together yet'); throw new \UnderflowException('Message has not been put back together yet');
} }
@ -87,7 +77,7 @@ class Message implements \IteratorAggregate, MessageInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getContents() { public function getContents(): string {
if (!$this->isCoalesced()) { if (!$this->isCoalesced()) {
throw new \UnderflowException("Message has not been put back together yet"); throw new \UnderflowException("Message has not been put back together yet");
} }
@ -101,7 +91,7 @@ class Message implements \IteratorAggregate, MessageInterface {
return $buffer; return $buffer;
} }
public function __toString() { public function __toString(): string {
$buffer = ''; $buffer = '';
foreach ($this->_frames as $frame) { foreach ($this->_frames as $frame) {
@ -114,7 +104,7 @@ class Message implements \IteratorAggregate, MessageInterface {
/** /**
* @return boolean * @return boolean
*/ */
public function isBinary() { public function isBinary(): bool {
if ($this->_frames->isEmpty()) { if ($this->_frames->isEmpty()) {
throw new \UnderflowException('Not enough data has been received to determine if message is binary'); throw new \UnderflowException('Not enough data has been received to determine if message is binary');
} }
@ -125,7 +115,7 @@ class Message implements \IteratorAggregate, MessageInterface {
/** /**
* @return boolean * @return boolean
*/ */
public function getRsv1() { public function getRsv1(): bool {
if ($this->_frames->isEmpty()) { if ($this->_frames->isEmpty()) {
return false; return false;
//throw new \UnderflowException('Not enough data has been received to determine if message is binary'); //throw new \UnderflowException('Not enough data has been received to determine if message is binary');

View File

@ -4,25 +4,16 @@ namespace Ratchet\RFC6455\Messaging;
use Ratchet\RFC6455\Handshake\PermessageDeflateOptions; use Ratchet\RFC6455\Handshake\PermessageDeflateOptions;
class MessageBuffer { class MessageBuffer {
/** private CloseFrameChecker $closeFrameChecker;
* @var \Ratchet\RFC6455\Messaging\CloseFrameChecker
*/
private $closeFrameChecker;
/** /**
* @var callable * @var callable
*/ */
private $exceptionFactory; private $exceptionFactory;
/** private ?MessageInterface $messageBuffer = null;
* @var \Ratchet\RFC6455\Messaging\Message
*/
private $messageBuffer;
/** private ?FrameInterface $frameBuffer = null;
* @var \Ratchet\RFC6455\Messaging\Frame
*/
private $frameBuffer;
/** /**
* @var callable * @var callable
@ -34,71 +25,55 @@ class MessageBuffer {
*/ */
private $onControl; private $onControl;
/** private bool $checkForMask;
* @var bool
*/
private $checkForMask;
/** /**
* @var callable * @var callable
*/ */
private $sender; private $sender;
/** private string $leftovers = '';
* @var string
*/ private int $streamingMessageOpCode = -1;
private $leftovers;
private PermessageDeflateOptions $permessageDeflateOptions;
private bool $deflateEnabled;
private int $maxMessagePayloadSize;
private int $maxFramePayloadSize;
private bool $compressedMessage = false;
/** /**
* @var int * @var resource|bool|null
*/ */
private $streamingMessageOpCode = -1; private $inflator = null;
/** /**
* @var PermessageDeflateOptions * @var resource|bool|null
*/ */
private $permessageDeflateOptions; private $deflator = null;
/** public function __construct(
* @var bool
*/
private $deflateEnabled = false;
/**
* @var int
*/
private $maxMessagePayloadSize;
/**
* @var int
*/
private $maxFramePayloadSize;
/**
* @var bool
*/
private $compressedMessage;
function __construct(
CloseFrameChecker $frameChecker, CloseFrameChecker $frameChecker,
callable $onMessage, callable $onMessage,
callable $onControl = null, ?callable $onControl = null,
$expectMask = true, bool $expectMask = true,
$exceptionFactory = null, ?callable $exceptionFactory = null,
$maxMessagePayloadSize = null, // null for default - zero for no limit ?int $maxMessagePayloadSize = null, // null for default - zero for no limit
$maxFramePayloadSize = null, // null for default - zero for no limit ?int $maxFramePayloadSize = null, // null for default - zero for no limit
callable $sender = null, ?callable $sender = null,
PermessageDeflateOptions $permessageDeflateOptions = null ?PermessageDeflateOptions $permessageDeflateOptions = null
) { ) {
$this->closeFrameChecker = $frameChecker; $this->closeFrameChecker = $frameChecker;
$this->checkForMask = (bool)$expectMask; $this->checkForMask = $expectMask;
$this->exceptionFactory ?: $exceptionFactory = function($msg) { $this->exceptionFactory = $exceptionFactory ?: static fn (string $msg) => new \UnderflowException($msg);
return new \UnderflowException($msg);
};
$this->onMessage = $onMessage; $this->onMessage = $onMessage;
$this->onControl = $onControl ?: function() {}; $this->onControl = $onControl ?: static function (): void {};
$this->sender = $sender; $this->sender = $sender;
@ -110,10 +85,6 @@ class MessageBuffer {
throw new \InvalidArgumentException('sender must be set when deflate is enabled'); throw new \InvalidArgumentException('sender must be set when deflate is enabled');
} }
$this->compressedMessage = false;
$this->leftovers = '';
$memory_limit_bytes = static::getMemoryLimit(); $memory_limit_bytes = static::getMemoryLimit();
if ($maxMessagePayloadSize === null) { if ($maxMessagePayloadSize === null) {
@ -123,18 +94,18 @@ class MessageBuffer {
$maxFramePayloadSize = (int)($memory_limit_bytes / 4); $maxFramePayloadSize = (int)($memory_limit_bytes / 4);
} }
if (!is_int($maxFramePayloadSize) || $maxFramePayloadSize > 0x7FFFFFFFFFFFFFFF || $maxFramePayloadSize < 0) { // this should be interesting on non-64 bit systems if ($maxFramePayloadSize > 0x7FFFFFFFFFFFFFFF || $maxFramePayloadSize < 0) { // this should be interesting on non-64 bit systems
throw new \InvalidArgumentException($maxFramePayloadSize . ' is not a valid maxFramePayloadSize'); throw new \InvalidArgumentException($maxFramePayloadSize . ' is not a valid maxFramePayloadSize');
} }
$this->maxFramePayloadSize = $maxFramePayloadSize; $this->maxFramePayloadSize = $maxFramePayloadSize;
if (!is_int($maxMessagePayloadSize) || $maxMessagePayloadSize > 0x7FFFFFFFFFFFFFFF || $maxMessagePayloadSize < 0) { if ($maxMessagePayloadSize > 0x7FFFFFFFFFFFFFFF || $maxMessagePayloadSize < 0) {
throw new \InvalidArgumentException($maxMessagePayloadSize . 'is not a valid maxMessagePayloadSize'); throw new \InvalidArgumentException($maxMessagePayloadSize . 'is not a valid maxMessagePayloadSize');
} }
$this->maxMessagePayloadSize = $maxMessagePayloadSize; $this->maxMessagePayloadSize = $maxMessagePayloadSize;
} }
public function onData($data) { public function onData(string $data): void {
$data = $this->leftovers . $data; $data = $this->leftovers . $data;
$dataLen = strlen($data); $dataLen = strlen($data);
@ -214,9 +185,9 @@ class MessageBuffer {
/** /**
* @param string $data * @param string $data
* @return null * @return void
*/ */
private function processData($data) { private function processData(string $data): void {
$this->messageBuffer ?: $this->messageBuffer = $this->newMessage(); $this->messageBuffer ?: $this->messageBuffer = $this->newMessage();
$this->frameBuffer ?: $this->frameBuffer = $this->newFrame(); $this->frameBuffer ?: $this->frameBuffer = $this->newFrame();
@ -235,7 +206,7 @@ class MessageBuffer {
$onControl($this->frameBuffer, $this); $onControl($this->frameBuffer, $this);
if (Frame::OP_CLOSE === $opcode) { if (Frame::OP_CLOSE === $opcode) {
return ''; return;
} }
} else { } else {
if ($this->messageBuffer->count() === 0 && $this->frameBuffer->getRsv1()) { if ($this->messageBuffer->count() === 0 && $this->frameBuffer->getRsv1()) {
@ -273,10 +244,10 @@ class MessageBuffer {
/** /**
* Check a frame to be added to the current message buffer * Check a frame to be added to the current message buffer
* @param \Ratchet\RFC6455\Messaging\FrameInterface|FrameInterface $frame * @param FrameInterface $frame
* @return \Ratchet\RFC6455\Messaging\FrameInterface|FrameInterface * @return FrameInterface
*/ */
public function frameCheck(FrameInterface $frame) { public function frameCheck(FrameInterface $frame): FrameInterface {
if ((false !== $frame->getRsv1() && !$this->deflateEnabled) || if ((false !== $frame->getRsv1() && !$this->deflateEnabled) ||
false !== $frame->getRsv2() || false !== $frame->getRsv2() ||
false !== $frame->getRsv3() false !== $frame->getRsv3()
@ -323,13 +294,11 @@ class MessageBuffer {
} }
return $frame; return $frame;
break;
case Frame::OP_PING: case Frame::OP_PING:
case Frame::OP_PONG: case Frame::OP_PONG:
break; break;
default: default:
return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected an invalid OP code'); return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected an invalid OP code');
break;
} }
return $frame; return $frame;
@ -348,7 +317,7 @@ class MessageBuffer {
/** /**
* Determine if a message is valid * Determine if a message is valid
* @param \Ratchet\RFC6455\Messaging\MessageInterface * @param MessageInterface
* @return bool|int true if valid - false if incomplete - int of recommended close code * @return bool|int true if valid - false if incomplete - int of recommended close code
*/ */
public function checkMessage(MessageInterface $message) { public function checkMessage(MessageInterface $message) {
@ -361,7 +330,7 @@ class MessageBuffer {
return true; return true;
} }
private function checkUtf8($string) { private function checkUtf8(string $string): bool {
if (extension_loaded('mbstring')) { if (extension_loaded('mbstring')) {
return mb_check_encoding($string, 'UTF-8'); return mb_check_encoding($string, 'UTF-8');
} }
@ -370,27 +339,27 @@ class MessageBuffer {
} }
/** /**
* @return \Ratchet\RFC6455\Messaging\MessageInterface * @return MessageInterface
*/ */
public function newMessage() { public function newMessage(): MessageInterface {
return new Message; return new Message;
} }
/** /**
* @param string|null $payload * @param string|null $payload
* @param bool|null $final * @param bool $final
* @param int|null $opcode * @param int $opcode
* @return \Ratchet\RFC6455\Messaging\FrameInterface * @return FrameInterface
*/ */
public function newFrame($payload = null, $final = null, $opcode = null) { public function newFrame(?string $payload = null, bool $final = true, int $opcode = Frame::OP_TEXT): FrameInterface {
return new Frame($payload, $final, $opcode, $this->exceptionFactory); return new Frame($payload, $final, $opcode, $this->exceptionFactory);
} }
public function newCloseFrame($code, $reason = '') { public function newCloseFrame(int $code, string $reason = ''): FrameInterface {
return $this->newFrame(pack('n', $code) . $reason, true, Frame::OP_CLOSE); return $this->newFrame(pack('n', $code) . $reason, true, Frame::OP_CLOSE);
} }
public function sendFrame(Frame $frame) { public function sendFrame(FrameInterface $frame): void {
if ($this->sender === null) { if ($this->sender === null) {
throw new \Exception('To send frames using the MessageBuffer, sender must be set.'); throw new \Exception('To send frames using the MessageBuffer, sender must be set.');
} }
@ -408,7 +377,7 @@ class MessageBuffer {
$sender($frame->getContents()); $sender($frame->getContents());
} }
public function sendMessage($messagePayload, $final = true, $isBinary = false) { public function sendMessage(string $messagePayload, bool $final = true, bool $isBinary = false): void {
$opCode = $isBinary ? Frame::OP_BINARY : Frame::OP_TEXT; $opCode = $isBinary ? Frame::OP_BINARY : Frame::OP_TEXT;
if ($this->streamingMessageOpCode === -1) { if ($this->streamingMessageOpCode === -1) {
$this->streamingMessageOpCode = $opCode; $this->streamingMessageOpCode = $opCode;
@ -431,29 +400,27 @@ class MessageBuffer {
} }
} }
private $inflator; private function getDeflateNoContextTakeover(): ?bool {
private function getDeflateNoContextTakeover() {
return $this->checkForMask ? return $this->checkForMask ?
$this->permessageDeflateOptions->getServerNoContextTakeover() : $this->permessageDeflateOptions->getServerNoContextTakeover() :
$this->permessageDeflateOptions->getClientNoContextTakeover(); $this->permessageDeflateOptions->getClientNoContextTakeover();
} }
private function getDeflateWindowBits() { private function getDeflateWindowBits(): int {
return $this->checkForMask ? $this->permessageDeflateOptions->getServerMaxWindowBits() : $this->permessageDeflateOptions->getClientMaxWindowBits(); return $this->checkForMask ? $this->permessageDeflateOptions->getServerMaxWindowBits() : $this->permessageDeflateOptions->getClientMaxWindowBits();
} }
private function getInflateNoContextTakeover() { private function getInflateNoContextTakeover(): ?bool {
return $this->checkForMask ? return $this->checkForMask ?
$this->permessageDeflateOptions->getClientNoContextTakeover() : $this->permessageDeflateOptions->getClientNoContextTakeover() :
$this->permessageDeflateOptions->getServerNoContextTakeover(); $this->permessageDeflateOptions->getServerNoContextTakeover();
} }
private function getInflateWindowBits() { private function getInflateWindowBits(): int {
return $this->checkForMask ? $this->permessageDeflateOptions->getClientMaxWindowBits() : $this->permessageDeflateOptions->getServerMaxWindowBits(); return $this->checkForMask ? $this->permessageDeflateOptions->getClientMaxWindowBits() : $this->permessageDeflateOptions->getServerMaxWindowBits();
} }
private function inflateFrame(Frame $frame) { private function inflateFrame(FrameInterface $frame): Frame {
if ($this->inflator === null) { if ($this->inflator === null) {
$this->inflator = inflate_init( $this->inflator = inflate_init(
ZLIB_ENCODING_RAW, ZLIB_ENCODING_RAW,
@ -480,16 +447,14 @@ class MessageBuffer {
); );
} }
private $deflator; private function deflateFrame(FrameInterface $frame): FrameInterface
private function deflateFrame(Frame $frame)
{ {
if ($frame->getRsv1()) { if ($frame->getRsv1()) {
return $frame; // frame is already deflated return $frame; // frame is already deflated
} }
if ($this->deflator === null) { if ($this->deflator === null) {
$bits = (int)$this->getDeflateWindowBits(); $bits = $this->getDeflateWindowBits();
if ($bits === 8) { if ($bits === 8) {
$bits = 9; $bits = 9;
} }
@ -549,7 +514,7 @@ class MessageBuffer {
* @param null|string $memory_limit * @param null|string $memory_limit
* @return int * @return int
*/ */
private static function getMemoryLimit($memory_limit = null) { private static function getMemoryLimit(?string $memory_limit = null): int {
$memory_limit = $memory_limit === null ? \trim(\ini_get('memory_limit')) : $memory_limit; $memory_limit = $memory_limit === null ? \trim(\ini_get('memory_limit')) : $memory_limit;
$memory_limit_bytes = 0; $memory_limit_bytes = 0;
if ($memory_limit !== '') { if ($memory_limit !== '') {

View File

@ -6,15 +6,15 @@ interface MessageInterface extends DataInterface, \Traversable, \Countable {
* @param FrameInterface $fragment * @param FrameInterface $fragment
* @return MessageInterface * @return MessageInterface
*/ */
function addFrame(FrameInterface $fragment); public function addFrame(FrameInterface $fragment): self;
/** /**
* @return int * @return int
*/ */
function getOpcode(); public function getOpcode(): int;
/** /**
* @return bool * @return bool
*/ */
function isBinary(); public function isBinary(): bool;
} }

View File

@ -3,10 +3,13 @@
namespace Ratchet\RFC6455\Test; namespace Ratchet\RFC6455\Test;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/**
* @coversNothing
*/
class AbResultsTest extends TestCase { class AbResultsTest extends TestCase {
private function verifyAutobahnResults($fileName) { private function verifyAutobahnResults(string $fileName): void {
if (!file_exists($fileName)) { if (!file_exists($fileName)) {
return $this->markTestSkipped('Autobahn TestSuite results not found'); $this->markTestSkipped('Autobahn TestSuite results not found');
} }
$resultsJson = file_get_contents($fileName); $resultsJson = file_get_contents($fileName);
@ -22,11 +25,11 @@ class AbResultsTest extends TestCase {
} }
} }
public function testAutobahnClientResults() { public function testAutobahnClientResults(): void {
$this->verifyAutobahnResults(__DIR__ . '/ab/reports/clients/index.json'); $this->verifyAutobahnResults(__DIR__ . '/ab/reports/clients/index.json');
} }
public function testAutobahnServerResults() { public function testAutobahnServerResults(): void {
$this->verifyAutobahnResults(__DIR__ . '/ab/reports/servers/index.json'); $this->verifyAutobahnResults(__DIR__ . '/ab/reports/servers/index.json');
} }
} }

View File

@ -4,18 +4,21 @@ use GuzzleHttp\Psr7\Message;
use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Uri;
use Ratchet\RFC6455\Handshake\InvalidPermessageDeflateOptionsException; use Ratchet\RFC6455\Handshake\InvalidPermessageDeflateOptionsException;
use Ratchet\RFC6455\Handshake\PermessageDeflateOptions; use Ratchet\RFC6455\Handshake\PermessageDeflateOptions;
use Ratchet\RFC6455\Messaging\FrameInterface;
use Ratchet\RFC6455\Messaging\MessageBuffer; use Ratchet\RFC6455\Messaging\MessageBuffer;
use Ratchet\RFC6455\Handshake\ClientNegotiator; use Ratchet\RFC6455\Handshake\ClientNegotiator;
use Ratchet\RFC6455\Messaging\CloseFrameChecker; use Ratchet\RFC6455\Messaging\CloseFrameChecker;
use Ratchet\RFC6455\Messaging\MessageInterface; use Ratchet\RFC6455\Messaging\MessageInterface;
use React\Promise\Deferred; use React\Promise\Deferred;
use Ratchet\RFC6455\Messaging\Frame; use Ratchet\RFC6455\Messaging\Frame;
use React\Promise\PromiseInterface;
use GuzzleHttp\Psr7\HttpFactory;
use React\Socket\ConnectionInterface; use React\Socket\ConnectionInterface;
use React\Socket\Connector; use React\Socket\Connector;
require __DIR__ . '/../bootstrap.php'; require __DIR__ . '/../bootstrap.php';
define('AGENT', 'RatchetRFC/0.3'); define('AGENT', 'RatchetRFC/0.4');
$testServer = $argc > 1 ? $argv[1] : "127.0.0.1"; $testServer = $argc > 1 ? $argv[1] : "127.0.0.1";
@ -23,23 +26,21 @@ $loop = React\EventLoop\Factory::create();
$connector = new Connector($loop); $connector = new Connector($loop);
function echoStreamerFactory($conn, $permessageDeflateOptions = null) function echoStreamerFactory(ConnectionInterface $conn, ?PermessageDeflateOptions $permessageDeflateOptions = null): MessageBuffer
{ {
$permessageDeflateOptions = $permessageDeflateOptions ?: PermessageDeflateOptions::createDisabled(); $permessageDeflateOptions = $permessageDeflateOptions ?: PermessageDeflateOptions::createDisabled();
return new \Ratchet\RFC6455\Messaging\MessageBuffer( return new MessageBuffer(
new \Ratchet\RFC6455\Messaging\CloseFrameChecker, new CloseFrameChecker,
function (\Ratchet\RFC6455\Messaging\MessageInterface $msg, MessageBuffer $messageBuffer) use ($conn) { static function (MessageInterface $msg, MessageBuffer $messageBuffer) use ($conn): void {
$messageBuffer->sendMessage($msg->getPayload(), true, $msg->isBinary()); $messageBuffer->sendMessage($msg->getPayload(), true, $msg->isBinary());
}, },
function (\Ratchet\RFC6455\Messaging\FrameInterface $frame, MessageBuffer $messageBuffer) use ($conn) { static function (FrameInterface $frame, MessageBuffer $messageBuffer) use ($conn) {
switch ($frame->getOpcode()) { switch ($frame->getOpcode()) {
case Frame::OP_PING: case Frame::OP_PING:
return $conn->write((new Frame($frame->getPayload(), true, Frame::OP_PONG))->maskPayload()->getContents()); return $conn->write((new Frame($frame->getPayload(), true, Frame::OP_PONG))->maskPayload()->getContents());
break;
case Frame::OP_CLOSE: case Frame::OP_CLOSE:
return $conn->end((new Frame($frame->getPayload(), true, Frame::OP_CLOSE))->maskPayload()->getContents()); return $conn->end((new Frame($frame->getPayload(), true, Frame::OP_CLOSE))->maskPayload()->getContents());
break;
} }
}, },
false, false,
@ -51,14 +52,14 @@ function echoStreamerFactory($conn, $permessageDeflateOptions = null)
); );
} }
function getTestCases() { function getTestCases(): PromiseInterface {
global $testServer; global $testServer;
global $connector; global $connector;
$deferred = new Deferred(); $deferred = new Deferred();
$connector->connect($testServer . ':9002')->then(function (ConnectionInterface $connection) use ($deferred, $testServer) { $connector->connect($testServer . ':9002')->then(static function (ConnectionInterface $connection) use ($deferred, $testServer): void {
$cn = new ClientNegotiator(); $cn = new ClientNegotiator(new HttpFactory());
$cnRequest = $cn->generateRequest(new Uri('ws://' . $testServer . ':9002/getCaseCount')); $cnRequest = $cn->generateRequest(new Uri('ws://' . $testServer . ':9002/getCaseCount'));
$rawResponse = ""; $rawResponse = "";
@ -67,7 +68,7 @@ function getTestCases() {
/** @var MessageBuffer $ms */ /** @var MessageBuffer $ms */
$ms = null; $ms = null;
$connection->on('data', function ($data) use ($connection, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest) { $connection->on('data', static function ($data) use ($connection, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest): void {
if ($response === null) { if ($response === null) {
$rawResponse .= $data; $rawResponse .= $data;
$pos = strpos($rawResponse, "\r\n\r\n"); $pos = strpos($rawResponse, "\r\n\r\n");
@ -82,7 +83,7 @@ function getTestCases() {
} else { } else {
$ms = new MessageBuffer( $ms = new MessageBuffer(
new CloseFrameChecker, new CloseFrameChecker,
function (MessageInterface $msg) use ($deferred, $connection) { static function (MessageInterface $msg) use ($deferred, $connection): void {
$deferred->resolve($msg->getPayload()); $deferred->resolve($msg->getPayload());
$connection->close(); $connection->close();
}, },
@ -91,7 +92,7 @@ function getTestCases() {
null, null,
null, null,
null, null,
function () {} static function (): void {}
); );
} }
} }
@ -109,10 +110,11 @@ function getTestCases() {
return $deferred->promise(); return $deferred->promise();
} }
$cn = new \Ratchet\RFC6455\Handshake\ClientNegotiator( $cn = new ClientNegotiator(
new HttpFactory(),
PermessageDeflateOptions::permessageDeflateSupported() ? PermessageDeflateOptions::createEnabled() : null); PermessageDeflateOptions::permessageDeflateSupported() ? PermessageDeflateOptions::createEnabled() : null);
function runTest($case) function runTest(int $case)
{ {
global $connector; global $connector;
global $testServer; global $testServer;
@ -122,8 +124,9 @@ function runTest($case)
$deferred = new Deferred(); $deferred = new Deferred();
$connector->connect($testServer . ':9002')->then(function (ConnectionInterface $connection) use ($deferred, $casePath, $case, $testServer) { $connector->connect($testServer . ':9002')->then(static function (ConnectionInterface $connection) use ($deferred, $casePath, $case, $testServer): void {
$cn = new ClientNegotiator( $cn = new ClientNegotiator(
new HttpFactory(),
PermessageDeflateOptions::permessageDeflateSupported() ? PermessageDeflateOptions::createEnabled() : null); PermessageDeflateOptions::permessageDeflateSupported() ? PermessageDeflateOptions::createEnabled() : null);
$cnRequest = $cn->generateRequest(new Uri('ws://' . $testServer . ':9002' . $casePath)); $cnRequest = $cn->generateRequest(new Uri('ws://' . $testServer . ':9002' . $casePath));
@ -132,7 +135,7 @@ function runTest($case)
$ms = null; $ms = null;
$connection->on('data', function ($data) use ($connection, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest) { $connection->on('data', static function ($data) use ($connection, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest): void {
if ($response === null) { if ($response === null) {
$rawResponse .= $data; $rawResponse .= $data;
$pos = strpos($rawResponse, "\r\n\r\n"); $pos = strpos($rawResponse, "\r\n\r\n");
@ -165,8 +168,8 @@ function runTest($case)
} }
}); });
$connection->on('close', function () use ($deferred) { $connection->on('close', static function () use ($deferred): void {
$deferred->resolve(); $deferred->resolve(null);
}); });
$connection->write(Message::toString($cnRequest)); $connection->write(Message::toString($cnRequest));
@ -175,17 +178,17 @@ function runTest($case)
return $deferred->promise(); return $deferred->promise();
} }
function createReport() { function createReport(): PromiseInterface {
global $connector; global $connector;
global $testServer; global $testServer;
$deferred = new Deferred(); $deferred = new Deferred();
$connector->connect($testServer . ':9002')->then(function (ConnectionInterface $connection) use ($deferred, $testServer) { $connector->connect($testServer . ':9002')->then(static function (ConnectionInterface $connection) use ($deferred, $testServer): void {
// $reportPath = "/updateReports?agent=" . AGENT . "&shutdownOnComplete=true"; // $reportPath = "/updateReports?agent=" . AGENT . "&shutdownOnComplete=true";
// we will stop it using docker now instead of just shutting down // we will stop it using docker now instead of just shutting down
$reportPath = "/updateReports?agent=" . AGENT; $reportPath = "/updateReports?agent=" . AGENT;
$cn = new ClientNegotiator(); $cn = new ClientNegotiator(new HttpFactory());
$cnRequest = $cn->generateRequest(new Uri('ws://' . $testServer . ':9002' . $reportPath)); $cnRequest = $cn->generateRequest(new Uri('ws://' . $testServer . ':9002' . $reportPath));
$rawResponse = ""; $rawResponse = "";
@ -194,7 +197,7 @@ function createReport() {
/** @var MessageBuffer $ms */ /** @var MessageBuffer $ms */
$ms = null; $ms = null;
$connection->on('data', function ($data) use ($connection, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest) { $connection->on('data', static function ($data) use ($connection, &$rawResponse, &$response, &$ms, $cn, $deferred, &$context, $cnRequest): void {
if ($response === null) { if ($response === null) {
$rawResponse .= $data; $rawResponse .= $data;
$pos = strpos($rawResponse, "\r\n\r\n"); $pos = strpos($rawResponse, "\r\n\r\n");
@ -209,7 +212,7 @@ function createReport() {
} else { } else {
$ms = new MessageBuffer( $ms = new MessageBuffer(
new CloseFrameChecker, new CloseFrameChecker,
function (MessageInterface $msg) use ($deferred, $connection) { static function (MessageInterface $msg) use ($deferred, $connection): void {
$deferred->resolve($msg->getPayload()); $deferred->resolve($msg->getPayload());
$connection->close(); $connection->close();
}, },
@ -218,7 +221,7 @@ function createReport() {
null, null,
null, null,
null, null,
function () {} static function (): void {}
); );
} }
} }
@ -242,16 +245,16 @@ $testPromises = [];
getTestCases()->then(function ($count) use ($loop) { getTestCases()->then(function ($count) use ($loop) {
$allDeferred = new Deferred(); $allDeferred = new Deferred();
$runNextCase = function () use (&$i, &$runNextCase, $count, $allDeferred) { $runNextCase = static function () use (&$i, &$runNextCase, $count, $allDeferred): void {
$i++; $i++;
if ($i > $count) { if ($i > $count) {
$allDeferred->resolve(); $allDeferred->resolve(null);
return; return;
} }
echo "Running test $i/$count..."; echo "Running test $i/$count...";
$startTime = microtime(true); $startTime = microtime(true);
runTest($i) runTest($i)
->then(function () use ($startTime) { ->then(static function () use ($startTime): void {
echo " completed " . round((microtime(true) - $startTime) * 1000) . " ms\n"; echo " completed " . round((microtime(true) - $startTime) * 1000) . " ms\n";
}) })
->then($runNextCase); ->then($runNextCase);
@ -260,7 +263,7 @@ getTestCases()->then(function ($count) use ($loop) {
$i = 0; $i = 0;
$runNextCase(); $runNextCase();
$allDeferred->promise()->then(function () { $allDeferred->promise()->then(static function (): void {
createReport(); createReport();
}); });
}); });

View File

@ -4,7 +4,7 @@
} }
, "outdir": "/reports/servers" , "outdir": "/reports/servers"
, "servers": [{ , "servers": [{
"agent": "RatchetRFC/0.3" "agent": "RatchetRFC/0.4"
, "url": "ws://host.ratchet.internal:9001" , "url": "ws://host.ratchet.internal:9001"
, "options": {"version": 18} , "options": {"version": 18}
}] }]

View File

@ -4,7 +4,7 @@
} }
, "outdir": "/reports/servers" , "outdir": "/reports/servers"
, "servers": [{ , "servers": [{
"agent": "RatchetRFC/0.3" "agent": "RatchetRFC/0.4"
, "url": "ws://host.ratchet.internal:9001" , "url": "ws://host.ratchet.internal:9001"
, "options": {"version": 18} , "options": {"version": 18}
}] }]

0
tests/ab/run_ab_tests.sh Normal file → Executable file
View File

View File

@ -3,10 +3,14 @@
use GuzzleHttp\Psr7\Message; use GuzzleHttp\Psr7\Message;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use Ratchet\RFC6455\Handshake\PermessageDeflateOptions; use Ratchet\RFC6455\Handshake\PermessageDeflateOptions;
use Ratchet\RFC6455\Handshake\RequestVerifier;
use Ratchet\RFC6455\Handshake\ServerNegotiator;
use Ratchet\RFC6455\Messaging\CloseFrameChecker;
use Ratchet\RFC6455\Messaging\MessageBuffer; use Ratchet\RFC6455\Messaging\MessageBuffer;
use Ratchet\RFC6455\Messaging\MessageInterface; use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\RFC6455\Messaging\FrameInterface; use Ratchet\RFC6455\Messaging\FrameInterface;
use Ratchet\RFC6455\Messaging\Frame; use Ratchet\RFC6455\Messaging\Frame;
use GuzzleHttp\Psr7\HttpFactory;
require_once __DIR__ . "/../bootstrap.php"; require_once __DIR__ . "/../bootstrap.php";
@ -14,17 +18,22 @@ $loop = \React\EventLoop\Factory::create();
$socket = new \React\Socket\Server('0.0.0.0:9001', $loop); $socket = new \React\Socket\Server('0.0.0.0:9001', $loop);
$closeFrameChecker = new \Ratchet\RFC6455\Messaging\CloseFrameChecker; $closeFrameChecker = new CloseFrameChecker;
$negotiator = new \Ratchet\RFC6455\Handshake\ServerNegotiator(new \Ratchet\RFC6455\Handshake\RequestVerifier, PermessageDeflateOptions::permessageDeflateSupported());
$negotiator = new ServerNegotiator(
new RequestVerifier,
new HttpFactory(),
PermessageDeflateOptions::permessageDeflateSupported()
);
$uException = new \UnderflowException; $uException = new \UnderflowException;
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) use ($negotiator, $closeFrameChecker, $uException, $socket) { $socket->on('connection', static function (React\Socket\ConnectionInterface $connection) use ($negotiator, $closeFrameChecker, $uException, $socket): void {
$headerComplete = false; $headerComplete = false;
$buffer = ''; $buffer = '';
$parser = null; $parser = null;
$connection->on('data', function ($data) use ($connection, &$parser, &$headerComplete, &$buffer, $negotiator, $closeFrameChecker, $uException, $socket) { $connection->on('data', static function ($data) use ($connection, &$parser, &$headerComplete, &$buffer, $negotiator, $closeFrameChecker, $uException, $socket): void {
if ($headerComplete) { if ($headerComplete) {
$parser->onData($data); $parser->onData($data);
return; return;
@ -58,10 +67,10 @@ $socket->on('connection', function (React\Socket\ConnectionInterface $connection
// we support any valid permessage deflate // we support any valid permessage deflate
$deflateOptions = PermessageDeflateOptions::fromRequestOrResponse($psrRequest)[0]; $deflateOptions = PermessageDeflateOptions::fromRequestOrResponse($psrRequest)[0];
$parser = new \Ratchet\RFC6455\Messaging\MessageBuffer($closeFrameChecker, $parser = new MessageBuffer($closeFrameChecker,
function (MessageInterface $message, MessageBuffer $messageBuffer) { static function (MessageInterface $message, MessageBuffer $messageBuffer): void {
$messageBuffer->sendMessage($message->getPayload(), true, $message->isBinary()); $messageBuffer->sendMessage($message->getPayload(), true, $message->isBinary());
}, function (FrameInterface $frame) use ($connection, &$parser) { }, static function (FrameInterface $frame) use ($connection, &$parser): void {
switch ($frame->getOpCode()) { switch ($frame->getOpCode()) {
case Frame::OP_CLOSE: case Frame::OP_CLOSE:
$connection->end($frame->getContents()); $connection->end($frame->getContents());
@ -70,9 +79,7 @@ $socket->on('connection', function (React\Socket\ConnectionInterface $connection
$connection->write($parser->newFrame($frame->getPayload(), true, Frame::OP_PONG)->getContents()); $connection->write($parser->newFrame($frame->getPayload(), true, Frame::OP_PONG)->getContents());
break; break;
} }
}, true, function () use ($uException) { }, true, static fn (): \Exception => $uException,
return $uException;
},
null, null,
null, null,
[$connection, 'write'], [$connection, 'write'],

View File

@ -5,9 +5,12 @@ namespace Ratchet\RFC6455\Test\Unit\Handshake;
use Ratchet\RFC6455\Handshake\PermessageDeflateOptions; use Ratchet\RFC6455\Handshake\PermessageDeflateOptions;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/**
* @covers Ratchet\RFC6455\Handshake\PermessageDeflateOptions
*/
class PermessageDeflateOptionsTest extends TestCase class PermessageDeflateOptionsTest extends TestCase
{ {
public static function versionSupportProvider() { public static function versionSupportProvider(): array {
return [ return [
['7.0.17', false], ['7.0.17', false],
['7.0.18', true], ['7.0.18', true],
@ -24,7 +27,7 @@ class PermessageDeflateOptionsTest extends TestCase
* @requires function deflate_init * @requires function deflate_init
* @dataProvider versionSupportProvider * @dataProvider versionSupportProvider
*/ */
public function testVersionSupport($version, $supported) { public function testVersionSupport(string $version, bool $supported): void {
$this->assertEquals($supported, PermessageDeflateOptions::permessageDeflateSupported($version)); $this->assertEquals($supported, PermessageDeflateOptions::permessageDeflateSupported($version));
} }
} }

View File

@ -14,11 +14,11 @@ class RequestVerifierTest extends TestCase {
*/ */
protected $_v; protected $_v;
public function setUp() { protected function setUp(): void {
$this->_v = new RequestVerifier(); $this->_v = new RequestVerifier();
} }
public static function methodProvider() { public static function methodProvider(): array {
return array( return array(
array(true, 'GET'), array(true, 'GET'),
array(true, 'get'), array(true, 'get'),
@ -32,11 +32,11 @@ class RequestVerifierTest extends TestCase {
/** /**
* @dataProvider methodProvider * @dataProvider methodProvider
*/ */
public function testMethodMustBeGet($result, $in) { public function testMethodMustBeGet(bool $result, string $in): void {
$this->assertEquals($result, $this->_v->verifyMethod($in)); $this->assertEquals($result, $this->_v->verifyMethod($in));
} }
public static function httpVersionProvider() { public static function httpVersionProvider(): array {
return array( return array(
array(true, 1.1), array(true, 1.1),
array(true, '1.1'), array(true, '1.1'),
@ -56,11 +56,11 @@ class RequestVerifierTest extends TestCase {
/** /**
* @dataProvider httpVersionProvider * @dataProvider httpVersionProvider
*/ */
public function testHttpVersionIsAtLeast1Point1($expected, $in) { public function testHttpVersionIsAtLeast1Point1(bool $expected, $in): void {
$this->assertEquals($expected, $this->_v->verifyHTTPVersion($in)); $this->assertEquals($expected, $this->_v->verifyHTTPVersion($in));
} }
public static function uRIProvider() { public static function uRIProvider(): array {
return array( return array(
array(true, '/chat'), array(true, '/chat'),
array(true, '/hello/world?key=val'), array(true, '/hello/world?key=val'),
@ -74,11 +74,11 @@ class RequestVerifierTest extends TestCase {
/** /**
* @dataProvider URIProvider * @dataProvider URIProvider
*/ */
public function testRequestUri($expected, $in) { public function testRequestUri(bool $expected, string $in): void {
$this->assertEquals($expected, $this->_v->verifyRequestURI($in)); $this->assertEquals($expected, $this->_v->verifyRequestURI($in));
} }
public static function hostProvider() { public static function hostProvider(): array {
return array( return array(
array(true, ['server.example.com']), array(true, ['server.example.com']),
array(false, []) array(false, [])
@ -88,11 +88,11 @@ class RequestVerifierTest extends TestCase {
/** /**
* @dataProvider HostProvider * @dataProvider HostProvider
*/ */
public function testVerifyHostIsSet($expected, $in) { public function testVerifyHostIsSet(bool $expected, array $in): void {
$this->assertEquals($expected, $this->_v->verifyHost($in)); $this->assertEquals($expected, $this->_v->verifyHost($in));
} }
public static function upgradeProvider() { public static function upgradeProvider(): array {
return array( return array(
array(true, ['websocket']), array(true, ['websocket']),
array(true, ['Websocket']), array(true, ['Websocket']),
@ -105,11 +105,11 @@ class RequestVerifierTest extends TestCase {
/** /**
* @dataProvider upgradeProvider * @dataProvider upgradeProvider
*/ */
public function testVerifyUpgradeIsWebSocket($expected, $val) { public function testVerifyUpgradeIsWebSocket(bool $expected, array $val): void {
$this->assertEquals($expected, $this->_v->verifyUpgradeRequest($val)); $this->assertEquals($expected, $this->_v->verifyUpgradeRequest($val));
} }
public static function connectionProvider() { public static function connectionProvider(): array {
return array( return array(
array(true, ['Upgrade']), array(true, ['Upgrade']),
array(true, ['upgrade']), array(true, ['upgrade']),
@ -129,11 +129,11 @@ class RequestVerifierTest extends TestCase {
/** /**
* @dataProvider connectionProvider * @dataProvider connectionProvider
*/ */
public function testConnectionHeaderVerification($expected, $val) { public function testConnectionHeaderVerification(bool $expected, array $val): void {
$this->assertEquals($expected, $this->_v->verifyConnection($val)); $this->assertEquals($expected, $this->_v->verifyConnection($val));
} }
public static function keyProvider() { public static function keyProvider(): array {
return array( return array(
array(true, ['hkfa1L7uwN6DCo4IS3iWAw==']), array(true, ['hkfa1L7uwN6DCo4IS3iWAw==']),
array(true, ['765vVoQpKSGJwPzJIMM2GA==']), array(true, ['765vVoQpKSGJwPzJIMM2GA==']),
@ -154,11 +154,11 @@ class RequestVerifierTest extends TestCase {
/** /**
* @dataProvider keyProvider * @dataProvider keyProvider
*/ */
public function testKeyIsBase64Encoded16BitNonce($expected, $val) { public function testKeyIsBase64Encoded16BitNonce(bool $expected, array $val): void {
$this->assertEquals($expected, $this->_v->verifyKey($val)); $this->assertEquals($expected, $this->_v->verifyKey($val));
} }
public static function versionProvider() { public static function versionProvider(): array {
return array( return array(
array(true, [13]), array(true, [13]),
array(true, ['13']), array(true, ['13']),
@ -174,7 +174,7 @@ class RequestVerifierTest extends TestCase {
/** /**
* @dataProvider versionProvider * @dataProvider versionProvider
*/ */
public function testVersionEquals13($expected, $in) { public function testVersionEquals13(bool $expected, array $in): void {
$this->assertEquals($expected, $this->_v->verifyVersion($in)); $this->assertEquals($expected, $this->_v->verifyVersion($in));
} }
} }

View File

@ -14,11 +14,11 @@ class ResponseVerifierTest extends TestCase {
*/ */
protected $_v; protected $_v;
public function setUp() { protected function setUp(): void {
$this->_v = new ResponseVerifier; $this->_v = new ResponseVerifier;
} }
public static function subProtocolsProvider() { public static function subProtocolsProvider(): array {
return [ return [
[true, ['a'], ['a']] [true, ['a'], ['a']]
, [true, ['c', 'd', 'a'], ['a']] , [true, ['c', 'd', 'a'], ['a']]
@ -33,7 +33,7 @@ class ResponseVerifierTest extends TestCase {
/** /**
* @dataProvider subProtocolsProvider * @dataProvider subProtocolsProvider
*/ */
public function testVerifySubProtocol($expected, $request, $response) { public function testVerifySubProtocol(bool $expected, array $request, array $response): void {
$this->assertEquals($expected, $this->_v->verifySubProtocol($request, $response)); $this->assertEquals($expected, $this->_v->verifySubProtocol($request, $response));
} }
} }

View File

@ -3,14 +3,18 @@
namespace Ratchet\RFC6455\Test\Unit\Handshake; namespace Ratchet\RFC6455\Test\Unit\Handshake;
use GuzzleHttp\Psr7\Message; use GuzzleHttp\Psr7\Message;
use GuzzleHttp\Psr7\HttpFactory;
use Ratchet\RFC6455\Handshake\RequestVerifier; use Ratchet\RFC6455\Handshake\RequestVerifier;
use Ratchet\RFC6455\Handshake\ServerNegotiator; use Ratchet\RFC6455\Handshake\ServerNegotiator;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/**
* @covers Ratchet\RFC6455\Handshake\ServerNegotiator
*/
class ServerNegotiatorTest extends TestCase class ServerNegotiatorTest extends TestCase
{ {
public function testNoUpgradeRequested() { public function testNoUpgradeRequested(): void {
$negotiator = new ServerNegotiator(new RequestVerifier()); $negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$requestText = 'GET / HTTP/1.1 $requestText = 'GET / HTTP/1.1
Host: 127.0.0.1:6789 Host: 127.0.0.1:6789
@ -37,8 +41,8 @@ Accept-Language: en-US,en;q=0.8
$this->assertEquals('13', $response->getHeaderLine('Sec-WebSocket-Version')); $this->assertEquals('13', $response->getHeaderLine('Sec-WebSocket-Version'));
} }
public function testNoConnectionUpgradeRequested() { public function testNoConnectionUpgradeRequested(): void {
$negotiator = new ServerNegotiator(new RequestVerifier()); $negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$requestText = 'GET / HTTP/1.1 $requestText = 'GET / HTTP/1.1
Host: 127.0.0.1:6789 Host: 127.0.0.1:6789
@ -63,8 +67,8 @@ Accept-Language: en-US,en;q=0.8
$this->assertEquals('Connection Upgrade MUST be requested', $response->getReasonPhrase()); $this->assertEquals('Connection Upgrade MUST be requested', $response->getReasonPhrase());
} }
public function testInvalidSecWebsocketKey() { public function testInvalidSecWebsocketKey(): void {
$negotiator = new ServerNegotiator(new RequestVerifier()); $negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$requestText = 'GET / HTTP/1.1 $requestText = 'GET / HTTP/1.1
Host: 127.0.0.1:6789 Host: 127.0.0.1:6789
@ -90,8 +94,8 @@ Accept-Language: en-US,en;q=0.8
$this->assertEquals('Invalid Sec-WebSocket-Key', $response->getReasonPhrase()); $this->assertEquals('Invalid Sec-WebSocket-Key', $response->getReasonPhrase());
} }
public function testInvalidSecWebsocketVersion() { public function testInvalidSecWebsocketVersion(): void {
$negotiator = new ServerNegotiator(new RequestVerifier()); $negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$requestText = 'GET / HTTP/1.1 $requestText = 'GET / HTTP/1.1
Host: 127.0.0.1:6789 Host: 127.0.0.1:6789
@ -120,8 +124,8 @@ Accept-Language: en-US,en;q=0.8
$this->assertEquals('13', $response->getHeaderLine('Sec-WebSocket-Version')); $this->assertEquals('13', $response->getHeaderLine('Sec-WebSocket-Version'));
} }
public function testBadSubprotocolResponse() { public function testBadSubprotocolResponse(): void {
$negotiator = new ServerNegotiator(new RequestVerifier()); $negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$negotiator->setStrictSubProtocolCheck(true); $negotiator->setStrictSubProtocolCheck(true);
$negotiator->setSupportedSubProtocols([]); $negotiator->setSupportedSubProtocols([]);
@ -154,8 +158,8 @@ Accept-Language: en-US,en;q=0.8
$this->assertEquals('13', $response->getHeaderLine('Sec-WebSocket-Version')); $this->assertEquals('13', $response->getHeaderLine('Sec-WebSocket-Version'));
} }
public function testNonStrictSubprotocolDoesNotIncludeHeaderWhenNoneAgreedOn() { public function testNonStrictSubprotocolDoesNotIncludeHeaderWhenNoneAgreedOn(): void {
$negotiator = new ServerNegotiator(new RequestVerifier()); $negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$negotiator->setStrictSubProtocolCheck(false); $negotiator->setStrictSubProtocolCheck(false);
$negotiator->setSupportedSubProtocols(['someproto']); $negotiator->setSupportedSubProtocols(['someproto']);
@ -187,9 +191,9 @@ Accept-Language: en-US,en;q=0.8
$this->assertFalse($response->hasHeader('Sec-WebSocket-Protocol')); $this->assertFalse($response->hasHeader('Sec-WebSocket-Protocol'));
} }
public function testSuggestsAppropriateSubprotocol() public function testSuggestsAppropriateSubprotocol(): void
{ {
$negotiator = new ServerNegotiator(new RequestVerifier()); $negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$negotiator->setStrictSubProtocolCheck(true); $negotiator->setStrictSubProtocolCheck(true);
$negotiator->setSupportedSubProtocols(['someproto']); $negotiator->setSupportedSubProtocols(['someproto']);

View File

@ -20,7 +20,7 @@ class FrameTest extends TestCase {
protected $_packer; protected $_packer;
public function setUp() { protected function setUp(): void {
$this->_frame = new Frame; $this->_frame = new Frame;
} }
@ -29,7 +29,7 @@ class FrameTest extends TestCase {
* @param string of 1's and 0's * @param string of 1's and 0's
* @return string * @return string
*/ */
public static function encode($in) { public static function encode(string $in): string {
if (strlen($in) > 8) { if (strlen($in) > 8) {
$out = ''; $out = '';
while (strlen($in) >= 8) { while (strlen($in) >= 8) {
@ -46,7 +46,7 @@ class FrameTest extends TestCase {
* param string The UTF8 message * param string The UTF8 message
* param string The WebSocket framed message, then base64_encoded * param string The WebSocket framed message, then base64_encoded
*/ */
public static function UnframeMessageProvider() { public static function UnframeMessageProvider(): array {
return array( return array(
array('Hello World!', 'gYydAIfa1WXrtvIg0LXvbOP7'), array('Hello World!', 'gYydAIfa1WXrtvIg0LXvbOP7'),
array('!@#$%^&*()-=_+[]{}\|/.,<>`~', 'gZv+h96r38f9j9vZ+IHWrvOWoayF9oX6gtfRqfKXwOeg'), array('!@#$%^&*()-=_+[]{}\|/.,<>`~', 'gZv+h96r38f9j9vZ+IHWrvOWoayF9oX6gtfRqfKXwOeg'),
@ -58,7 +58,7 @@ class FrameTest extends TestCase {
); );
} }
public static function underflowProvider() { public static function underflowProvider(): array {
return array( return array(
array('isFinal', ''), array('isFinal', ''),
array('getRsv1', ''), array('getRsv1', ''),
@ -75,19 +75,9 @@ class FrameTest extends TestCase {
/** /**
* @dataProvider underflowProvider * @dataProvider underflowProvider
*
* @covers Ratchet\RFC6455\Messaging\Frame::isFinal
* @covers Ratchet\RFC6455\Messaging\Frame::getRsv1
* @covers Ratchet\RFC6455\Messaging\Frame::getRsv2
* @covers Ratchet\RFC6455\Messaging\Frame::getRsv3
* @covers Ratchet\RFC6455\Messaging\Frame::getOpcode
* @covers Ratchet\RFC6455\Messaging\Frame::isMasked
* @covers Ratchet\RFC6455\Messaging\Frame::getPayloadLength
* @covers Ratchet\RFC6455\Messaging\Frame::getMaskingKey
* @covers Ratchet\RFC6455\Messaging\Frame::getPayload
*/ */
public function testUnderflowExceptionFromAllTheMethodsMimickingBuffering($method, $bin) { public function testUnderflowExceptionFromAllTheMethodsMimickingBuffering(string $method, string $bin): void {
$this->setExpectedException('\UnderflowException'); $this->expectException(\UnderflowException::class);
if (!empty($bin)) { if (!empty($bin)) {
$this->_frame->addBuffer(static::encode($bin)); $this->_frame->addBuffer(static::encode($bin));
} }
@ -100,7 +90,7 @@ class FrameTest extends TestCase {
* param int Given, what is the expected opcode * param int Given, what is the expected opcode
* param string of 0|1 Each character represents a bit in the byte * param string of 0|1 Each character represents a bit in the byte
*/ */
public static function firstByteProvider() { public static function firstByteProvider(): array {
return array( return array(
array(false, false, false, true, 8, '00011000'), array(false, false, false, true, 8, '00011000'),
array(true, false, true, false, 10, '10101010'), array(true, false, true, false, 10, '10101010'),
@ -113,20 +103,16 @@ class FrameTest extends TestCase {
/** /**
* @dataProvider firstByteProvider * @dataProvider firstByteProvider
* covers Ratchet\RFC6455\Messaging\Frame::isFinal
*/ */
public function testFinCodeFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) { public function testFinCodeFromBits(bool $fin, bool $rsv1, bool $rsv2, bool $rsv3, int $opcode, string $bin): void {
$this->_frame->addBuffer(static::encode($bin)); $this->_frame->addBuffer(static::encode($bin));
$this->assertEquals($fin, $this->_frame->isFinal()); $this->assertEquals($fin, $this->_frame->isFinal());
} }
/** /**
* @dataProvider firstByteProvider * @dataProvider firstByteProvider
* covers Ratchet\RFC6455\Messaging\Frame::getRsv1
* covers Ratchet\RFC6455\Messaging\Frame::getRsv2
* covers Ratchet\RFC6455\Messaging\Frame::getRsv3
*/ */
public function testGetRsvFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) { public function testGetRsvFromBits(bool $fin, bool $rsv1, bool $rsv2, bool $rsv3, int $opcode, string $bin): void {
$this->_frame->addBuffer(static::encode($bin)); $this->_frame->addBuffer(static::encode($bin));
$this->assertEquals($rsv1, $this->_frame->getRsv1()); $this->assertEquals($rsv1, $this->_frame->getRsv1());
$this->assertEquals($rsv2, $this->_frame->getRsv2()); $this->assertEquals($rsv2, $this->_frame->getRsv2());
@ -135,32 +121,29 @@ class FrameTest extends TestCase {
/** /**
* @dataProvider firstByteProvider * @dataProvider firstByteProvider
* covers Ratchet\RFC6455\Messaging\Frame::getOpcode
*/ */
public function testOpcodeFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) { public function testOpcodeFromBits(bool $fin, bool $rsv1, bool $rsv2, bool $rsv3, int $opcode, string $bin): void {
$this->_frame->addBuffer(static::encode($bin)); $this->_frame->addBuffer(static::encode($bin));
$this->assertEquals($opcode, $this->_frame->getOpcode()); $this->assertEquals($opcode, $this->_frame->getOpcode());
} }
/** /**
* @dataProvider UnframeMessageProvider * @dataProvider UnframeMessageProvider
* covers Ratchet\RFC6455\Messaging\Frame::isFinal
*/ */
public function testFinCodeFromFullMessage($msg, $encoded) { public function testFinCodeFromFullMessage(string $msg, string $encoded): void {
$this->_frame->addBuffer(base64_decode($encoded)); $this->_frame->addBuffer(base64_decode($encoded));
$this->assertTrue($this->_frame->isFinal()); $this->assertTrue($this->_frame->isFinal());
} }
/** /**
* @dataProvider UnframeMessageProvider * @dataProvider UnframeMessageProvider
* covers Ratchet\RFC6455\Messaging\Frame::getOpcode
*/ */
public function testOpcodeFromFullMessage($msg, $encoded) { public function testOpcodeFromFullMessage(string $msg, string $encoded): void {
$this->_frame->addBuffer(base64_decode($encoded)); $this->_frame->addBuffer(base64_decode($encoded));
$this->assertEquals(1, $this->_frame->getOpcode()); $this->assertEquals(1, $this->_frame->getOpcode());
} }
public static function payloadLengthDescriptionProvider() { public static function payloadLengthDescriptionProvider(): array {
return array( return array(
array(7, '01110101'), array(7, '01110101'),
array(7, '01111101'), array(7, '01111101'),
@ -173,10 +156,8 @@ class FrameTest extends TestCase {
/** /**
* @dataProvider payloadLengthDescriptionProvider * @dataProvider payloadLengthDescriptionProvider
* covers Ratchet\RFC6455\Messaging\Frame::addBuffer
* covers Ratchet\RFC6455\Messaging\Frame::getFirstPayloadVal
*/ */
public function testFirstPayloadDesignationValue($bits, $bin) { public function testFirstPayloadDesignationValue(int $bits, string $bin): void {
$this->_frame->addBuffer(static::encode($this->_firstByteFinText)); $this->_frame->addBuffer(static::encode($this->_firstByteFinText));
$this->_frame->addBuffer(static::encode($bin)); $this->_frame->addBuffer(static::encode($bin));
$ref = new \ReflectionClass($this->_frame); $ref = new \ReflectionClass($this->_frame);
@ -185,22 +166,18 @@ class FrameTest extends TestCase {
$this->assertEquals(bindec($bin), $cb->invoke($this->_frame)); $this->assertEquals(bindec($bin), $cb->invoke($this->_frame));
} }
/** public function testFirstPayloadValUnderflow(): void {
* covers Ratchet\RFC6455\Messaging\Frame::getFirstPayloadVal
*/
public function testFirstPayloadValUnderflow() {
$ref = new \ReflectionClass($this->_frame); $ref = new \ReflectionClass($this->_frame);
$cb = $ref->getMethod('getFirstPayloadVal'); $cb = $ref->getMethod('getFirstPayloadVal');
$cb->setAccessible(true); $cb->setAccessible(true);
$this->setExpectedException('UnderflowException'); $this->expectException(\UnderflowException::class);
$cb->invoke($this->_frame); $cb->invoke($this->_frame);
} }
/** /**
* @dataProvider payloadLengthDescriptionProvider * @dataProvider payloadLengthDescriptionProvider
* covers Ratchet\RFC6455\Messaging\Frame::getNumPayloadBits
*/ */
public function testDetermineHowManyBitsAreUsedToDescribePayload($expected_bits, $bin) { public function testDetermineHowManyBitsAreUsedToDescribePayload(int $expected_bits, string $bin): void {
$this->_frame->addBuffer(static::encode($this->_firstByteFinText)); $this->_frame->addBuffer(static::encode($this->_firstByteFinText));
$this->_frame->addBuffer(static::encode($bin)); $this->_frame->addBuffer(static::encode($bin));
$ref = new \ReflectionClass($this->_frame); $ref = new \ReflectionClass($this->_frame);
@ -209,18 +186,15 @@ class FrameTest extends TestCase {
$this->assertEquals($expected_bits, $cb->invoke($this->_frame)); $this->assertEquals($expected_bits, $cb->invoke($this->_frame));
} }
/** public function testgetNumPayloadBitsUnderflow(): void {
* covers Ratchet\RFC6455\Messaging\Frame::getNumPayloadBits
*/
public function testgetNumPayloadBitsUnderflow() {
$ref = new \ReflectionClass($this->_frame); $ref = new \ReflectionClass($this->_frame);
$cb = $ref->getMethod('getNumPayloadBits'); $cb = $ref->getMethod('getNumPayloadBits');
$cb->setAccessible(true); $cb->setAccessible(true);
$this->setExpectedException('UnderflowException'); $this->expectException(\UnderflowException::class);
$cb->invoke($this->_frame); $cb->invoke($this->_frame);
} }
public function secondByteProvider() { public function secondByteProvider(): array {
return array( return array(
array(true, 1, '10000001'), array(true, 1, '10000001'),
array(false, 1, '00000001'), array(false, 1, '00000001'),
@ -229,9 +203,8 @@ class FrameTest extends TestCase {
} }
/** /**
* @dataProvider secondByteProvider * @dataProvider secondByteProvider
* covers Ratchet\RFC6455\Messaging\Frame::isMasked
*/ */
public function testIsMaskedReturnsExpectedValue($masked, $payload_length, $bin) { public function testIsMaskedReturnsExpectedValue(bool $masked, int $payload_length, string $bin): void {
$this->_frame->addBuffer(static::encode($this->_firstByteFinText)); $this->_frame->addBuffer(static::encode($this->_firstByteFinText));
$this->_frame->addBuffer(static::encode($bin)); $this->_frame->addBuffer(static::encode($bin));
$this->assertEquals($masked, $this->_frame->isMasked()); $this->assertEquals($masked, $this->_frame->isMasked());
@ -239,18 +212,16 @@ class FrameTest extends TestCase {
/** /**
* @dataProvider UnframeMessageProvider * @dataProvider UnframeMessageProvider
* covers Ratchet\RFC6455\Messaging\Frame::isMasked
*/ */
public function testIsMaskedFromFullMessage($msg, $encoded) { public function testIsMaskedFromFullMessage(string $msg, string $encoded): void {
$this->_frame->addBuffer(base64_decode($encoded)); $this->_frame->addBuffer(base64_decode($encoded));
$this->assertTrue($this->_frame->isMasked()); $this->assertTrue($this->_frame->isMasked());
} }
/** /**
* @dataProvider secondByteProvider * @dataProvider secondByteProvider
* covers Ratchet\RFC6455\Messaging\Frame::getPayloadLength
*/ */
public function testGetPayloadLengthWhenOnlyFirstFrameIsUsed($masked, $payload_length, $bin) { public function testGetPayloadLengthWhenOnlyFirstFrameIsUsed(bool $masked, int $payload_length, string $bin): void {
$this->_frame->addBuffer(static::encode($this->_firstByteFinText)); $this->_frame->addBuffer(static::encode($this->_firstByteFinText));
$this->_frame->addBuffer(static::encode($bin)); $this->_frame->addBuffer(static::encode($bin));
$this->assertEquals($payload_length, $this->_frame->getPayloadLength()); $this->assertEquals($payload_length, $this->_frame->getPayloadLength());
@ -258,15 +229,14 @@ class FrameTest extends TestCase {
/** /**
* @dataProvider UnframeMessageProvider * @dataProvider UnframeMessageProvider
* covers Ratchet\RFC6455\Messaging\Frame::getPayloadLength
* @todo Not yet testing when second additional payload length descriptor * @todo Not yet testing when second additional payload length descriptor
*/ */
public function testGetPayloadLengthFromFullMessage($msg, $encoded) { public function testGetPayloadLengthFromFullMessage(string $msg, string $encoded): void {
$this->_frame->addBuffer(base64_decode($encoded)); $this->_frame->addBuffer(base64_decode($encoded));
$this->assertEquals(strlen($msg), $this->_frame->getPayloadLength()); $this->assertEquals(strlen($msg), $this->_frame->getPayloadLength());
} }
public function maskingKeyProvider() { public function maskingKeyProvider(): array {
$frame = new Frame; $frame = new Frame;
return array( return array(
array($frame->generateMaskingKey()), array($frame->generateMaskingKey()),
@ -277,35 +247,30 @@ class FrameTest extends TestCase {
/** /**
* @dataProvider maskingKeyProvider * @dataProvider maskingKeyProvider
* covers Ratchet\RFC6455\Messaging\Frame::getMaskingKey
* @todo I I wrote the dataProvider incorrectly, skipping for now * @todo I I wrote the dataProvider incorrectly, skipping for now
*/ */
public function testGetMaskingKey($mask) { public function testGetMaskingKey(string $mask): void {
$this->_frame->addBuffer(static::encode($this->_firstByteFinText)); $this->_frame->addBuffer(static::encode($this->_firstByteFinText));
$this->_frame->addBuffer(static::encode($this->_secondByteMaskedSPL)); $this->_frame->addBuffer(static::encode($this->_secondByteMaskedSPL));
$this->_frame->addBuffer($mask); $this->_frame->addBuffer($mask);
$this->assertEquals($mask, $this->_frame->getMaskingKey()); $this->assertEquals($mask, $this->_frame->getMaskingKey());
} }
/** public function testGetMaskingKeyOnUnmaskedPayload(): void {
* covers Ratchet\RFC6455\Messaging\Frame::getMaskingKey
*/
public function testGetMaskingKeyOnUnmaskedPayload() {
$frame = new Frame('Hello World!'); $frame = new Frame('Hello World!');
$this->assertEquals('', $frame->getMaskingKey()); $this->assertEquals('', $frame->getMaskingKey());
} }
/** /**
* @dataProvider UnframeMessageProvider * @dataProvider UnframeMessageProvider
* covers Ratchet\RFC6455\Messaging\Frame::getPayload
* @todo Move this test to bottom as it requires all methods of the class * @todo Move this test to bottom as it requires all methods of the class
*/ */
public function testUnframeFullMessage($unframed, $base_framed) { public function testUnframeFullMessage(string $unframed, string $base_framed): void {
$this->_frame->addBuffer(base64_decode($base_framed)); $this->_frame->addBuffer(base64_decode($base_framed));
$this->assertEquals($unframed, $this->_frame->getPayload()); $this->assertEquals($unframed, $this->_frame->getPayload());
} }
public static function messageFragmentProvider() { public static function messageFragmentProvider(): array {
return array( return array(
array(false, '', '', '', '', '') array(false, '', '', '', '', '')
); );
@ -313,9 +278,8 @@ class FrameTest extends TestCase {
/** /**
* @dataProvider UnframeMessageProvider * @dataProvider UnframeMessageProvider
* covers Ratchet\RFC6455\Messaging\Frame::getPayload
*/ */
public function testCheckPiecingTogetherMessage($msg, $encoded) { public function testCheckPiecingTogetherMessage(string $msg, string $encoded): void {
$framed = base64_decode($encoded); $framed = base64_decode($encoded);
for ($i = 0, $len = strlen($framed);$i < $len; $i++) { for ($i = 0, $len = strlen($framed);$i < $len; $i++) {
$this->_frame->addBuffer(substr($framed, $i, 1)); $this->_frame->addBuffer(substr($framed, $i, 1));
@ -323,12 +287,7 @@ class FrameTest extends TestCase {
$this->assertEquals($msg, $this->_frame->getPayload()); $this->assertEquals($msg, $this->_frame->getPayload());
} }
/** public function testLongCreate(): void {
* covers Ratchet\RFC6455\Messaging\Frame::__construct
* covers Ratchet\RFC6455\Messaging\Frame::getPayloadLength
* covers Ratchet\RFC6455\Messaging\Frame::getPayload
*/
public function testLongCreate() {
$len = 65525; $len = 65525;
$pl = $this->generateRandomString($len); $pl = $this->generateRandomString($len);
$frame = new Frame($pl, true, Frame::OP_PING); $frame = new Frame($pl, true, Frame::OP_PING);
@ -339,20 +298,13 @@ class FrameTest extends TestCase {
$this->assertEquals($pl, $frame->getPayload()); $this->assertEquals($pl, $frame->getPayload());
} }
/** public function testReallyLongCreate(): void {
* covers Ratchet\RFC6455\Messaging\Frame::__construct
* covers Ratchet\RFC6455\Messaging\Frame::getPayloadLength
*/
public function testReallyLongCreate() {
$len = 65575; $len = 65575;
$frame = new Frame($this->generateRandomString($len)); $frame = new Frame($this->generateRandomString($len));
$this->assertEquals($len, $frame->getPayloadLength()); $this->assertEquals($len, $frame->getPayloadLength());
} }
/**
* covers Ratchet\RFC6455\Messaging\Frame::__construct public function testExtractOverflow(): void {
* covers Ratchet\RFC6455\Messaging\Frame::extractOverflow
*/
public function testExtractOverflow() {
$string1 = $this->generateRandomString(); $string1 = $this->generateRandomString();
$frame1 = new Frame($string1); $frame1 = new Frame($string1);
$string2 = $this->generateRandomString(); $string2 = $this->generateRandomString();
@ -367,10 +319,7 @@ class FrameTest extends TestCase {
$this->assertEquals($string2, $uncat->getPayload()); $this->assertEquals($string2, $uncat->getPayload());
} }
/** public function testEmptyExtractOverflow(): void {
* covers Ratchet\RFC6455\Messaging\Frame::extractOverflow
*/
public function testEmptyExtractOverflow() {
$string = $this->generateRandomString(); $string = $this->generateRandomString();
$frame = new Frame($string); $frame = new Frame($string);
$this->assertEquals($string, $frame->getPayload()); $this->assertEquals($string, $frame->getPayload());
@ -378,10 +327,7 @@ class FrameTest extends TestCase {
$this->assertEquals($string, $frame->getPayload()); $this->assertEquals($string, $frame->getPayload());
} }
/** public function testGetContents(): void {
* covers Ratchet\RFC6455\Messaging\Frame::getContents
*/
public function testGetContents() {
$msg = 'The quick brown fox jumps over the lazy dog.'; $msg = 'The quick brown fox jumps over the lazy dog.';
$frame1 = new Frame($msg); $frame1 = new Frame($msg);
$frame2 = new Frame($msg); $frame2 = new Frame($msg);
@ -390,10 +336,7 @@ class FrameTest extends TestCase {
$this->assertEquals(strlen($frame1->getContents()) + 4, strlen($frame2->getContents())); $this->assertEquals(strlen($frame1->getContents()) + 4, strlen($frame2->getContents()));
} }
/** public function testMasking(): void {
* covers Ratchet\RFC6455\Messaging\Frame::maskPayload
*/
public function testMasking() {
$msg = 'The quick brown fox jumps over the lazy dog.'; $msg = 'The quick brown fox jumps over the lazy dog.';
$frame = new Frame($msg); $frame = new Frame($msg);
$frame->maskPayload(); $frame->maskPayload();
@ -401,10 +344,7 @@ class FrameTest extends TestCase {
$this->assertEquals($msg, $frame->getPayload()); $this->assertEquals($msg, $frame->getPayload());
} }
/** public function testUnMaskPayload(): void {
* covers Ratchet\RFC6455\Messaging\Frame::unMaskPayload
*/
public function testUnMaskPayload() {
$string = $this->generateRandomString(); $string = $this->generateRandomString();
$frame = new Frame($string); $frame = new Frame($string);
$frame->maskPayload()->unMaskPayload(); $frame->maskPayload()->unMaskPayload();
@ -412,10 +352,7 @@ class FrameTest extends TestCase {
$this->assertEquals($string, $frame->getPayload()); $this->assertEquals($string, $frame->getPayload());
} }
/** public function testGenerateMaskingKey(): void {
* covers Ratchet\RFC6455\Messaging\Frame::generateMaskingKey
*/
public function testGenerateMaskingKey() {
$dupe = false; $dupe = false;
$done = array(); $done = array();
for ($i = 0; $i < 10; $i++) { for ($i = 0; $i < 10; $i++) {
@ -429,27 +366,20 @@ class FrameTest extends TestCase {
$this->assertFalse($dupe); $this->assertFalse($dupe);
} }
/** public function testGivenMaskIsValid(): void {
* covers Ratchet\RFC6455\Messaging\Frame::maskPayload $this->expectException(\InvalidArgumentException::class);
*/
public function testGivenMaskIsValid() {
$this->setExpectedException('InvalidArgumentException');
$this->_frame->maskPayload('hello world'); $this->_frame->maskPayload('hello world');
} }
/** /**
* covers Ratchet\RFC6455\Messaging\Frame::maskPayload * @requires extension mbstring
*/ */
public function testGivenMaskIsValidAscii() { public function testGivenMaskIsValidAscii(): void {
if (!extension_loaded('mbstring')) { $this->expectException(\OutOfBoundsException::class);
$this->markTestSkipped("mbstring required for this test");
return;
}
$this->setExpectedException('OutOfBoundsException');
$this->_frame->maskPayload('x✖'); $this->_frame->maskPayload('x✖');
} }
protected function generateRandomString($length = 10, $addSpaces = true, $addNumbers = true) { protected function generateRandomString(int $length = 10, bool $addSpaces = true, bool $addNumbers = true): string {
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"$%&/()=[]{}'; // ยง $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"$%&/()=[]{}'; // ยง
$useChars = array(); $useChars = array();
for($i = 0; $i < $length; $i++) { for($i = 0; $i < $length; $i++) {
@ -473,11 +403,8 @@ class FrameTest extends TestCase {
* to set the payload length to 126 and then not recalculate it once the full length information was available. * to set the payload length to 126 and then not recalculate it once the full length information was available.
* *
* This is fixed by setting the defPayLen back to -1 before the underflow exception is thrown. * This is fixed by setting the defPayLen back to -1 before the underflow exception is thrown.
*
* covers Ratchet\RFC6455\Messaging\Frame::getPayloadLength
* covers Ratchet\RFC6455\Messaging\Frame::extractOverflow
*/ */
public function testFrameDeliveredOneByteAtATime() { public function testFrameDeliveredOneByteAtATime(): void {
$startHeader = "\x01\x7e\x01\x00"; // header for a text frame of 256 - non-final $startHeader = "\x01\x7e\x01\x00"; // header for a text frame of 256 - non-final
$framePayload = str_repeat("*", 256); $framePayload = str_repeat("*", 256);
$rawOverflow = "xyz"; $rawOverflow = "xyz";

View File

@ -9,13 +9,16 @@ use Ratchet\RFC6455\Messaging\MessageBuffer;
use React\EventLoop\Factory; use React\EventLoop\Factory;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/**
* @covers Ratchet\RFC6455\Messaging\MessageBuffer
*/
class MessageBufferTest extends TestCase class MessageBufferTest extends TestCase
{ {
/** /**
* This is to test that MessageBuffer can handle a large receive * This is to test that MessageBuffer can handle a large receive
* buffer with many many frames without blowing the stack (pre-v0.4 issue) * buffer with many many frames without blowing the stack (pre-v0.4 issue)
*/ */
public function testProcessingLotsOfFramesInASingleChunk() { public function testProcessingLotsOfFramesInASingleChunk(): void {
$frame = new Frame('a', true, Frame::OP_TEXT); $frame = new Frame('a', true, Frame::OP_TEXT);
$frameRaw = $frame->getContents(); $frameRaw = $frame->getContents();
@ -26,7 +29,7 @@ class MessageBufferTest extends TestCase
$messageBuffer = new MessageBuffer( $messageBuffer = new MessageBuffer(
new CloseFrameChecker(), new CloseFrameChecker(),
function (Message $message) use (&$messageCount) { function (Message $message) use (&$messageCount): void {
$messageCount++; $messageCount++;
$this->assertEquals('a', $message->getPayload()); $this->assertEquals('a', $message->getPayload());
}, },
@ -39,7 +42,7 @@ class MessageBufferTest extends TestCase
$this->assertEquals(1000, $messageCount); $this->assertEquals(1000, $messageCount);
} }
public function testProcessingMessagesAsynchronouslyWhileBlockingInMessageHandler() { public function testProcessingMessagesAsynchronouslyWhileBlockingInMessageHandler(): void {
$loop = Factory::create(); $loop = Factory::create();
$frameA = new Frame('a', true, Frame::OP_TEXT); $frameA = new Frame('a', true, Frame::OP_TEXT);
@ -49,7 +52,7 @@ class MessageBufferTest extends TestCase
$messageBuffer = new MessageBuffer( $messageBuffer = new MessageBuffer(
new CloseFrameChecker(), new CloseFrameChecker(),
function (Message $message) use (&$messageCount, &$bReceived, $loop) { static function (Message $message) use (&$messageCount, &$bReceived, $loop): void {
$payload = $message->getPayload(); $payload = $message->getPayload();
$bReceived = $payload === 'b'; $bReceived = $payload === 'b';
@ -61,7 +64,7 @@ class MessageBufferTest extends TestCase
false false
); );
$loop->addPeriodicTimer(0.1, function () use ($messageBuffer, $frameB, $loop) { $loop->addPeriodicTimer(0.1, static function () use ($messageBuffer, $frameB, $loop): void {
$loop->stop(); $loop->stop();
$messageBuffer->onData($frameB->getContents()); $messageBuffer->onData($frameB->getContents());
}); });
@ -71,7 +74,7 @@ class MessageBufferTest extends TestCase
$this->assertTrue($bReceived); $this->assertTrue($bReceived);
} }
public function testInvalidFrameLength() { public function testInvalidFrameLength(): void {
$frame = new Frame(str_repeat('a', 200), true, Frame::OP_TEXT); $frame = new Frame(str_repeat('a', 200), true, Frame::OP_TEXT);
$frameRaw = $frame->getContents(); $frameRaw = $frame->getContents();
@ -93,7 +96,7 @@ class MessageBufferTest extends TestCase
$messageBuffer = new MessageBuffer( $messageBuffer = new MessageBuffer(
new CloseFrameChecker(), new CloseFrameChecker(),
function (Message $message) use (&$messageCount) { static function (Message $message) use (&$messageCount): void {
$messageCount++; $messageCount++;
}, },
function (Frame $frame) use (&$controlFrame) { function (Frame $frame) use (&$controlFrame) {
@ -109,13 +112,13 @@ class MessageBufferTest extends TestCase
$messageBuffer->onData($frameRaw); $messageBuffer->onData($frameRaw);
$this->assertEquals(0, $messageCount); $this->assertEquals(0, $messageCount);
$this->assertTrue($controlFrame instanceof Frame); $this->assertInstanceOf(Frame::class, $controlFrame);
$this->assertEquals(Frame::OP_CLOSE, $controlFrame->getOpcode()); $this->assertEquals(Frame::OP_CLOSE, $controlFrame->getOpcode());
$this->assertEquals([Frame::CLOSE_PROTOCOL], array_merge(unpack('n*', substr($controlFrame->getPayload(), 0, 2)))); $this->assertEquals([Frame::CLOSE_PROTOCOL], array_merge(unpack('n*', substr($controlFrame->getPayload(), 0, 2))));
} }
public function testFrameLengthTooBig() { public function testFrameLengthTooBig(): void {
$frame = new Frame(str_repeat('a', 200), true, Frame::OP_TEXT); $frame = new Frame(str_repeat('a', 200), true, Frame::OP_TEXT);
$frameRaw = $frame->getContents(); $frameRaw = $frame->getContents();
@ -137,10 +140,10 @@ class MessageBufferTest extends TestCase
$messageBuffer = new MessageBuffer( $messageBuffer = new MessageBuffer(
new CloseFrameChecker(), new CloseFrameChecker(),
function (Message $message) use (&$messageCount) { static function (Message $message) use (&$messageCount): void {
$messageCount++; $messageCount++;
}, },
function (Frame $frame) use (&$controlFrame) { function (Frame $frame) use (&$controlFrame): void {
$this->assertNull($controlFrame); $this->assertNull($controlFrame);
$controlFrame = $frame; $controlFrame = $frame;
}, },
@ -153,12 +156,12 @@ class MessageBufferTest extends TestCase
$messageBuffer->onData($frameRaw); $messageBuffer->onData($frameRaw);
$this->assertEquals(0, $messageCount); $this->assertEquals(0, $messageCount);
$this->assertTrue($controlFrame instanceof Frame); $this->assertInstanceOf(Frame::class, $controlFrame);
$this->assertEquals(Frame::OP_CLOSE, $controlFrame->getOpcode()); $this->assertEquals(Frame::OP_CLOSE, $controlFrame->getOpcode());
$this->assertEquals([Frame::CLOSE_TOO_BIG], array_merge(unpack('n*', substr($controlFrame->getPayload(), 0, 2)))); $this->assertEquals([Frame::CLOSE_TOO_BIG], array_merge(unpack('n*', substr($controlFrame->getPayload(), 0, 2))));
} }
public function testFrameLengthBiggerThanMaxMessagePayload() { public function testFrameLengthBiggerThanMaxMessagePayload(): void {
$frame = new Frame(str_repeat('a', 200), true, Frame::OP_TEXT); $frame = new Frame(str_repeat('a', 200), true, Frame::OP_TEXT);
$frameRaw = $frame->getContents(); $frameRaw = $frame->getContents();
@ -169,10 +172,10 @@ class MessageBufferTest extends TestCase
$messageBuffer = new MessageBuffer( $messageBuffer = new MessageBuffer(
new CloseFrameChecker(), new CloseFrameChecker(),
function (Message $message) use (&$messageCount) { static function (Message $message) use (&$messageCount): void {
$messageCount++; $messageCount++;
}, },
function (Frame $frame) use (&$controlFrame) { function (Frame $frame) use (&$controlFrame): void {
$this->assertNull($controlFrame); $this->assertNull($controlFrame);
$controlFrame = $frame; $controlFrame = $frame;
}, },
@ -185,12 +188,12 @@ class MessageBufferTest extends TestCase
$messageBuffer->onData($frameRaw); $messageBuffer->onData($frameRaw);
$this->assertEquals(0, $messageCount); $this->assertEquals(0, $messageCount);
$this->assertTrue($controlFrame instanceof Frame); $this->assertInstanceOf(Frame::class, $controlFrame);
$this->assertEquals(Frame::OP_CLOSE, $controlFrame->getOpcode()); $this->assertEquals(Frame::OP_CLOSE, $controlFrame->getOpcode());
$this->assertEquals([Frame::CLOSE_TOO_BIG], array_merge(unpack('n*', substr($controlFrame->getPayload(), 0, 2)))); $this->assertEquals([Frame::CLOSE_TOO_BIG], array_merge(unpack('n*', substr($controlFrame->getPayload(), 0, 2))));
} }
public function testSecondFrameLengthPushesPastMaxMessagePayload() { public function testSecondFrameLengthPushesPastMaxMessagePayload(): void {
$frame = new Frame(str_repeat('a', 200), false, Frame::OP_TEXT); $frame = new Frame(str_repeat('a', 200), false, Frame::OP_TEXT);
$firstFrameRaw = $frame->getContents(); $firstFrameRaw = $frame->getContents();
$frame = new Frame(str_repeat('b', 200), true, Frame::OP_TEXT); $frame = new Frame(str_repeat('b', 200), true, Frame::OP_TEXT);
@ -202,10 +205,10 @@ class MessageBufferTest extends TestCase
$messageBuffer = new MessageBuffer( $messageBuffer = new MessageBuffer(
new CloseFrameChecker(), new CloseFrameChecker(),
function (Message $message) use (&$messageCount) { static function (Message $message) use (&$messageCount): void {
$messageCount++; $messageCount++;
}, },
function (Frame $frame) use (&$controlFrame) { function (Frame $frame) use (&$controlFrame): void {
$this->assertNull($controlFrame); $this->assertNull($controlFrame);
$controlFrame = $frame; $controlFrame = $frame;
}, },
@ -220,7 +223,7 @@ class MessageBufferTest extends TestCase
$messageBuffer->onData(substr($secondFrameRaw, 0, 150)); $messageBuffer->onData(substr($secondFrameRaw, 0, 150));
$this->assertEquals(0, $messageCount); $this->assertEquals(0, $messageCount);
$this->assertTrue($controlFrame instanceof Frame); $this->assertInstanceOf(Frame::class, $controlFrame);
$this->assertEquals(Frame::OP_CLOSE, $controlFrame->getOpcode()); $this->assertEquals(Frame::OP_CLOSE, $controlFrame->getOpcode());
$this->assertEquals([Frame::CLOSE_TOO_BIG], array_merge(unpack('n*', substr($controlFrame->getPayload(), 0, 2)))); $this->assertEquals([Frame::CLOSE_TOO_BIG], array_merge(unpack('n*', substr($controlFrame->getPayload(), 0, 2))));
} }
@ -258,15 +261,15 @@ class MessageBufferTest extends TestCase
* @param string $phpConfigurationValue * @param string $phpConfigurationValue
* @param int $expectedLimit * @param int $expectedLimit
*/ */
public function testMemoryLimits($phpConfigurationValue, $expectedLimit) { public function testMemoryLimits(string $phpConfigurationValue, int $expectedLimit): void {
$method = new \ReflectionMethod('Ratchet\RFC6455\Messaging\MessageBuffer', 'getMemoryLimit'); $method = new \ReflectionMethod(MessageBuffer::class, 'getMemoryLimit');
$method->setAccessible(true); $method->setAccessible(true);
$actualLimit = $method->invoke(null, $phpConfigurationValue); $actualLimit = $method->invoke(null, $phpConfigurationValue);
$this->assertSame($expectedLimit, $actualLimit); $this->assertSame($expectedLimit, $actualLimit);
} }
public function phpConfigurationProvider() { public function phpConfigurationProvider(): array {
return [ return [
'without unit type, just bytes' => ['500', 500], 'without unit type, just bytes' => ['500', 500],
'1 GB with big "G"' => ['1G', 1 * 1024 * 1024 * 1024], '1 GB with big "G"' => ['1G', 1 * 1024 * 1024 * 1024],
@ -281,32 +284,30 @@ class MessageBufferTest extends TestCase
]; ];
} }
/** public function testInvalidMaxFramePayloadSizes(): void {
* @expectedException \InvalidArgumentException $this->expectException(\InvalidArgumentException::class);
*/
public function testInvalidMaxFramePayloadSizes() { new MessageBuffer(
$messageBuffer = new MessageBuffer(
new CloseFrameChecker(), new CloseFrameChecker(),
function (Message $message) {}, static function (Message $message): void {},
function (Frame $frame) {}, static function (Frame $frame): void {},
false, false,
null, null,
0, 0,
0x8000000000000000 -1
); );
} }
/** public function testInvalidMaxMessagePayloadSizes(): void {
* @expectedException \InvalidArgumentException $this->expectException(\InvalidArgumentException::class);
*/
public function testInvalidMaxMessagePayloadSizes() { new MessageBuffer(
$messageBuffer = new MessageBuffer(
new CloseFrameChecker(), new CloseFrameChecker(),
function (Message $message) {}, static function (Message $message): void {},
function (Frame $frame) {}, static function (Frame $frame): void {},
false, false,
null, null,
0x8000000000000000, -1,
0 0
); );
} }
@ -318,9 +319,8 @@ class MessageBufferTest extends TestCase
* @param int $expectedLimit * @param int $expectedLimit
* *
* @runInSeparateProcess * @runInSeparateProcess
* @requires PHP 7.0
*/ */
public function testIniSizes($phpConfigurationValue, $expectedLimit) { public function testIniSizes(string $phpConfigurationValue, int $expectedLimit): void {
$value = @ini_set('memory_limit', $phpConfigurationValue); $value = @ini_set('memory_limit', $phpConfigurationValue);
if ($value === false) { if ($value === false) {
$this->markTestSkipped("Does not support setting the memory_limit lower than current memory_usage"); $this->markTestSkipped("Does not support setting the memory_limit lower than current memory_usage");
@ -328,8 +328,8 @@ class MessageBufferTest extends TestCase
$messageBuffer = new MessageBuffer( $messageBuffer = new MessageBuffer(
new CloseFrameChecker(), new CloseFrameChecker(),
function (Message $message) {}, static function (Message $message): void {},
function (Frame $frame) {}, static function (Frame $frame): void {},
false, false,
null null
); );
@ -349,9 +349,8 @@ class MessageBufferTest extends TestCase
/** /**
* @runInSeparateProcess * @runInSeparateProcess
* @requires PHP 7.0
*/ */
public function testInvalidIniSize() { public function testInvalidIniSize(): void {
$value = @ini_set('memory_limit', 'lots of memory'); $value = @ini_set('memory_limit', 'lots of memory');
if ($value === false) { if ($value === false) {
$this->markTestSkipped("Does not support setting the memory_limit lower than current memory_usage"); $this->markTestSkipped("Does not support setting the memory_limit lower than current memory_usage");

View File

@ -13,20 +13,20 @@ class MessageTest extends TestCase {
/** @var Message */ /** @var Message */
protected $message; protected $message;
public function setUp() { protected function setUp(): void {
$this->message = new Message; $this->message = new Message;
} }
public function testNoFrames() { public function testNoFrames(): void {
$this->assertFalse($this->message->isCoalesced()); $this->assertFalse($this->message->isCoalesced());
} }
public function testNoFramesOpCode() { public function testNoFramesOpCode(): void {
$this->setExpectedException('UnderflowException'); $this->expectException(\UnderflowException::class);
$this->message->getOpCode(); $this->message->getOpCode();
} }
public function testFragmentationPayload() { public function testFragmentationPayload(): void {
$a = 'Hello '; $a = 'Hello ';
$b = 'World!'; $b = 'World!';
$f1 = new Frame($a, false); $f1 = new Frame($a, false);
@ -36,13 +36,13 @@ class MessageTest extends TestCase {
$this->assertEquals($a . $b, $this->message->getPayload()); $this->assertEquals($a . $b, $this->message->getPayload());
} }
public function testUnbufferedFragment() { public function testUnbufferedFragment(): void {
$this->message->addFrame(new Frame('The quick brow', false)); $this->message->addFrame(new Frame('The quick brow', false));
$this->setExpectedException('UnderflowException'); $this->expectException(\UnderflowException::class);
$this->message->getPayload(); $this->message->getPayload();
} }
public function testGetOpCode() { public function testGetOpCode(): void {
$this->message $this->message
->addFrame(new Frame('The quick brow', false, Frame::OP_TEXT)) ->addFrame(new Frame('The quick brow', false, Frame::OP_TEXT))
->addFrame(new Frame('n fox jumps ov', false, Frame::OP_CONTINUE)) ->addFrame(new Frame('n fox jumps ov', false, Frame::OP_CONTINUE))
@ -51,7 +51,7 @@ class MessageTest extends TestCase {
$this->assertEquals(Frame::OP_TEXT, $this->message->getOpCode()); $this->assertEquals(Frame::OP_TEXT, $this->message->getOpCode());
} }
public function testGetUnBufferedPayloadLength() { public function testGetUnBufferedPayloadLength(): void {
$this->message $this->message
->addFrame(new Frame('The quick brow', false, Frame::OP_TEXT)) ->addFrame(new Frame('The quick brow', false, Frame::OP_TEXT))
->addFrame(new Frame('n fox jumps ov', false, Frame::OP_CONTINUE)) ->addFrame(new Frame('n fox jumps ov', false, Frame::OP_CONTINUE))