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

v0.4 (progress) #60

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
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
25 changes: 10 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ on:

jobs:
PHPUnit:
name: PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }})
runs-on: ubuntu-20.04
name: PHPUnit (PHP ${{ matrix.php }})(${{ matrix.env }}) on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-22.04
env:
- client
- server
php:
- 7.4
- 7.3
- 7.2
- 7.1
- 7.0
- 5.6
- 8.0
- 8.1
- 8.2
- 8.3
- 8.4
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
Expand All @@ -33,11 +35,4 @@ jobs:
- run: sh tests/ab/run_ab_tests.sh
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.phpunit.result.cache
composer.lock
vendor
tests/ab/reports
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
}
},
"require": {
"php": ">=5.4.2",
"guzzlehttp/psr7": "^2 || ^1.7"
"php": ">=7.4",
"psr/http-factory-implementation": "^1.0",
"symfony/polyfill-php80": "^1.15"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"react/socket": "^1.3"
"phpunit/phpunit": "^9.5",
"react/socket": "^1.3",
"guzzlehttp/psr7": "^2.7"
},
"scripts": {
"abtest-client": "ABTEST=client && sh tests/ab/run_ab_tests.sh",
Expand Down
19 changes: 7 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
forceCoversAnnotation="true"
mapTestClassNameToCoveredClassName="true"
bootstrap="tests/bootstrap.php"
colors="true"
backupGlobals="false"
backupStaticAttributes="false"
syntaxCheck="false"
stopOnError="false"
>

<testsuites>
<testsuite name="tests">
<directory>tests</directory>
<exclude>
<directory>test/ab</directory>
</exclude>
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>
51 changes: 24 additions & 27 deletions src/Handshake/ClientNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,35 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestFactoryInterface;

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

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

function __construct(PermessageDeflateOptions $perMessageDeflateOptions = null) {
private RequestFactoryInterface $requestFactory;

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

$this->defaultHeader = new Request('GET', '', [
'Connection' => 'Upgrade'
, 'Upgrade' => 'websocket'
, 'Sec-WebSocket-Version' => $this->getVersion()
, 'User-Agent' => "Ratchet"
]);
$this->defaultHeader = $this->requestFactory
->createRequest('GET', '')
->withHeader('Connection' , 'Upgrade')
->withHeader('Upgrade' , 'websocket')
->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=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 +42,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());
->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 +62,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;
}
Loading