Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.4] Code modernization #63

Open
wants to merge 1 commit into
base: v0.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"require": {
"php": ">=7.4",
"guzzlehttp/psr7": "^2 || ^1.7"
"guzzlehttp/psr7": "^2 || ^1.7",
"symfony/polyfill-php80": "^1.15"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
29 changes: 10 additions & 19 deletions src/Handshake/ClientNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@
use GuzzleHttp\Psr7\Request;

class ClientNegotiator {
/**
* @var ResponseVerifier
*/
private $verifier;
private ResponseVerifier $verifier;

/**
* @var \Psr\Http\Message\RequestInterface
*/
private $defaultHeader;
private RequestInterface $defaultHeader;

function __construct(PermessageDeflateOptions $perMessageDeflateOptions = null) {
public function __construct(PermessageDeflateOptions $perMessageDeflateOptions = null) {
$this->verifier = new ResponseVerifier;

$this->defaultHeader = new Request('GET', '', [
Expand All @@ -26,15 +20,12 @@ function __construct(PermessageDeflateOptions $perMessageDeflateOptions = null)
, '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=74240 - need >=7.1.4 or >=7.0.18
if ($perMessageDeflateOptions->isEnabled() &&
!PermessageDeflateOptions::permessageDeflateSupported()) {
trigger_error('permessage-deflate is being disabled because it is not support by your PHP version.', E_USER_NOTICE);
if ($perMessageDeflateOptions->isEnabled() && !PermessageDeflateOptions::permessageDeflateSupported()) {
trigger_error('permessage-deflate is being disabled because it is not supported by your PHP version.', E_USER_NOTICE);
$perMessageDeflateOptions = PermessageDeflateOptions::createDisabled();
}
if ($perMessageDeflateOptions->isEnabled() && !function_exists('deflate_add')) {
Expand All @@ -45,16 +36,16 @@ function __construct(PermessageDeflateOptions $perMessageDeflateOptions = null)
$this->defaultHeader = $perMessageDeflateOptions->addHeaderToRequest($this->defaultHeader);
}

public function generateRequest(UriInterface $uri) {
public function generateRequest(UriInterface $uri): RequestInterface {
return $this->defaultHeader->withUri($uri)
->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);
}

public function generateKey() {
public function generateKey(): string {
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwzyz1234567890+/=';
$charRange = strlen($chars) - 1;
$key = '';
Expand All @@ -65,7 +56,7 @@ public function generateKey() {
return base64_encode($key);
}

public function getVersion() {
public function getVersion(): int {
return 13;
}
}
15 changes: 8 additions & 7 deletions src/Handshake/NegotiatorInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Ratchet\RFC6455\Handshake;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* A standard interface for interacting with the various version of the WebSocket protocol
Expand All @@ -14,34 +15,34 @@ interface NegotiatorInterface {
* @param RequestInterface $request
* @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
* @return int
*/
function getVersionNumber();
public function getVersionNumber(): int;

/**
* Perform the handshake and return the response headers
* @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
* @param array $protocols
*/
function setSupportedSubProtocols(array $protocols);
public function setSupportedSubProtocols(array $protocols): void;

/**
* If enabled and support for a subprotocol has been added handshake
* will not upgrade if a match between request and supported subprotocols
* @param boolean $enable
* @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.
*/
function setStrictSubProtocolCheck($enable);
public function setStrictSubProtocolCheck(bool $enable): void;
}
75 changes: 39 additions & 36 deletions src/Handshake/PermessageDeflateOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

final class PermessageDeflateOptions
{
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'];
public const MAX_WINDOW_BITS = 15;

private $deflateEnabled = false;
private const VALID_BITS = [8, 9, 10, 11, 12, 13, 14, 15];

private $server_no_context_takeover;
private $client_no_context_takeover;
private $server_max_window_bits;
private $client_max_window_bits;
private bool $deflateEnabled = false;

private ?bool $server_no_context_takeover = null;
private ?bool $client_no_context_takeover = null;
private ?int $server_max_window_bits = null;
private ?int $client_max_window_bits = null;

private function __construct() { }

public static function createEnabled() {
$new = new static();
$new = new self();
$new->deflateEnabled = true;
$new->client_max_window_bits = self::MAX_WINDOW_BITS;
$new->client_no_context_takeover = false;
Expand All @@ -33,44 +33,44 @@ public static function createEnabled() {
}

public static function createDisabled() {
return new static();
return new self();
}

public function withClientNoContextTakeover() {
public function withClientNoContextTakeover(): self {
$new = clone $this;
$new->client_no_context_takeover = true;
return $new;
}

public function withoutClientNoContextTakeover() {
public function withoutClientNoContextTakeover(): self {
$new = clone $this;
$new->client_no_context_takeover = false;
return $new;
}

public function withServerNoContextTakeover() {
public function withServerNoContextTakeover(): self {
$new = clone $this;
$new->server_no_context_takeover = true;
return $new;
}

public function withoutServerNoContextTakeover() {
public function withoutServerNoContextTakeover(): self {
$new = clone $this;
$new->server_no_context_takeover = false;
return $new;
}

public function withServerMaxWindowBits($bits = self::MAX_WINDOW_BITS) {
if (!in_array($bits, self::$VALID_BITS)) {
public function withServerMaxWindowBits(int $bits = self::MAX_WINDOW_BITS): self {
if (!in_array($bits, self::VALID_BITS)) {
throw new \Exception('server_max_window_bits must have a value between 8 and 15.');
}
$new = clone $this;
$new->server_max_window_bits = $bits;
return $new;
}

public function withClientMaxWindowBits($bits = self::MAX_WINDOW_BITS) {
if (!in_array($bits, self::$VALID_BITS)) {
public function withClientMaxWindowBits(int $bits = self::MAX_WINDOW_BITS): self {
if (!in_array($bits, self::VALID_BITS)) {
throw new \Exception('client_max_window_bits must have a value between 8 and 15.');
}
$new = clone $this;
Expand All @@ -86,7 +86,7 @@ public function withClientMaxWindowBits($bits = self::MAX_WINDOW_BITS) {
* @return PermessageDeflateOptions[]
* @throws \Exception
*/
public static function fromRequestOrResponse(MessageInterface $requestOrResponse) {
public static function fromRequestOrResponse(MessageInterface $requestOrResponse): array {
$optionSets = [];

$extHeader = preg_replace('/\s+/', '', join(', ', $requestOrResponse->getHeader('Sec-Websocket-Extensions')));
Expand All @@ -103,7 +103,7 @@ public static function fromRequestOrResponse(MessageInterface $requestOrResponse
}

array_shift($parts);
$options = new static();
$options = new self();
$options->deflateEnabled = true;
foreach ($parts as $part) {
$kv = explode('=', $part);
Expand All @@ -119,15 +119,18 @@ public static function fromRequestOrResponse(MessageInterface $requestOrResponse
$value = true;
break;
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.');
}
break;
case "client_max_window_bits":
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.');
}
break;
Expand All @@ -154,47 +157,47 @@ public static function fromRequestOrResponse(MessageInterface $requestOrResponse
}

// always put a disabled on the end
$optionSets[] = new static();
$optionSets[] = new self();

return $optionSets;
}

/**
* @return mixed
* @return bool|null
*/
public function getServerNoContextTakeover()
public function getServerNoContextTakeover(): ?bool
{
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 mixed
* @return int|null
*/
public function getServerMaxWindowBits()
public function getServerMaxWindowBits(): ?int
{
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 bool
*/
public function isEnabled()
public function isEnabled(): bool
{
return $this->deflateEnabled;
}
Expand All @@ -203,7 +206,7 @@ public function isEnabled()
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function addHeaderToResponse(ResponseInterface $response)
public function addHeaderToResponse(ResponseInterface $response): ResponseInterface
{
if (!$this->deflateEnabled) {
return $response;
Expand All @@ -226,7 +229,7 @@ public function addHeaderToResponse(ResponseInterface $response)
return $response->withAddedHeader('Sec-Websocket-Extensions', $header);
}

public function addHeaderToRequest(RequestInterface $request) {
public function addHeaderToRequest(RequestInterface $request): RequestInterface {
if (!$this->deflateEnabled) {
return $request;
}
Expand All @@ -249,7 +252,7 @@ public function addHeaderToRequest(RequestInterface $request) {
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')) {
return false;
}
Expand Down
Loading