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

PHP8 update - incomplete #161

Open
wants to merge 3 commits into
base: master
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
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true

# Tab indentation (no size specified)
[Makefile]
indent_style = tab

[*.md]
max_line_length = off
trim_trailing_whitespace = false
88 changes: 88 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Test suite

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
- cron: '0 0 * * *'

jobs:
unit-test:
name: Unit tests
strategy:
matrix:
php: [ 7.4, 8.0 ]
os: [ ubuntu-latest ]
include:
- os: [ ubuntu-latest ]
php: 8.1
composer-flag: "'--ignore-platform-reqs'"

- os: [ ubuntu-latest ]
php: 7.3
composer-flag: "'--prefer-lowest'"

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- uses: actions/cache@v2
id: cache-composer
with:
path: ~/.composer/cache
key: composer-php-${{ matrix.php }}-${{ github.sha }}
restore-keys: composer-php-${{ matrix.php }}-

- name: Install dependencies
run: composer update --prefer-dist --no-progress --no-suggest ${{ matrix.composer-flag }}

# This part is commented for now but let's how being able to uncomment it soon!
# - name: Run PHP CS Fixer
# run: make test.phpcs

- name: Run PHPUnit tests
run: make test.phpunit

functional-test:
name: Functionnal tests
strategy:
matrix:
php: [ 7.4, 8.0 ]
os: [ ubuntu-latest ]
include:
- os: [ ubuntu-latest ]
php: 8.1
composer-flag: "'--ignore-platform-reqs'"

- os: [ ubuntu-latest ]
php: 7.3
composer-flag: "'--prefer-lowest'"

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- uses: actions/cache@v2
id: cache-composer
with:
path: ~/.composer/cache
key: composer-php-${{ matrix.php }}-${{ github.sha }}
restore-keys: composer-php-${{ matrix.php }}-


- name: Install dependencies
run: composer update --prefer-dist --no-progress --no-suggest ${{ matrix.composer-flag }}

- name: Run Behat tests
run: make test.behat
51 changes: 51 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
.DEFAULT_GOAL := help
COMPOSER = composer

#
### PROJECT
# --------
#

install: ## Install the project
${COMPOSER} install
.PHONY: install

reset: clean install ## Stop and start a fresh install of the project
.PHONY: reset

clean: ## Stop the project and remove generated files
rm -rf vendor
.PHONY: clean

#
### TESTS
# -------
#

test: test.composer test.phpcs test.phpunit ## Run all tests
.PHONY: test

test.composer: ## Validate composer.json
composer validate

test.phpcs: ## Run PHP CS Fixer in dry-run
composer run -- phpcs --dry-run -v

test.phpcs.fix: ## Run PHP CS Fixer and fix issues if possible
composer run -- phpcs -v

test.phpunit: ## Run PHPUnit tests
composer run tests

#
### OTHERS
# --------
#

help: SHELL=/bin/bash
help: ## Dislay this help
@IFS=$$'\n'; for line in `grep -h -E '^[a-zA-Z_#-]+:?.*?## .*$$' $(MAKEFILE_LIST)`; do if [ "$${line:0:2}" = "##" ]; then \
echo $$line | awk 'BEGIN {FS = "## "}; {printf "\n\033[33m%s\033[0m\n", $$2}'; else \
echo $$line | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'; fi; \
done; unset IFS;
.PHONY: help
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"license": "MIT",
"type": "library",
"description": "A WebSocket library for PHP",
"scripts": {
"tests": "phpunit"
},
"autoload": {
"psr-4": {
"Nekland\\Woketo\\": "src/"
Expand All @@ -14,7 +17,7 @@
}
},
"require": {
"php": "^7.0",
"php": ">=7.0",
"react/event-loop": "^1.0.0",
"react/socket": "^1.0.0",
"nekland/tools": "^1.0 || ^2.0",
Expand Down
210 changes: 105 additions & 105 deletions src/Rfc6455/Handshake/ServerHandshake.php
Original file line number Diff line number Diff line change
@@ -1,105 +1,105 @@
<?php
/**
* This file is a part of Woketo package.
*
* (c) Nekland <[email protected]>
*
* For the full license, take a look to the LICENSE file
* on the root directory of this project
*/
namespace Nekland\Woketo\Rfc6455\Handshake;
use Nekland\Woketo\Exception\RuntimeException;
use Nekland\Woketo\Exception\WebsocketException;
use Nekland\Woketo\Exception\WebsocketVersionException;
use Nekland\Woketo\Http\AbstractHttpMessage;
use Nekland\Woketo\Http\Request;
use Nekland\Woketo\Http\Response;
/**
* Class ServerHandshake
*
* This class is highly inspired by ratchetphp/RFC6455.
*/
class ServerHandshake implements HandshakeInterface
{
public function sign(AbstractHttpMessage $response = null, string $key = null)
{
$signature = \base64_encode(\sha1($key . HandshakeInterface::GUID, true));
if (null !== $response) {
$response->addHeader('Sec-WebSocket-Accept', $signature);
}
return $signature;
}
/**
* https://tools.ietf.org/html/rfc6455#section-4.2
*
* @param AbstractHttpMessage $request
* @param string|null $key
* @return bool
* @throws RuntimeException
* @throws WebsocketException
* @throws WebsocketVersionException
*/
public function verify(AbstractHttpMessage $request, string $key = null)
{
if (!$request instanceof Request) {
throw new RuntimeException(
sprintf('The client handshake cannot verify something else than a Response object, %s given.', get_class($response))
);
}
if ($request->getHttpVersion() !== 'HTTP/1.1') {
throw new WebsocketException(
\sprintf('Wrong http version, HTTP/1.1 expected, "%s" received.', $request->getHttpVersion())
);
}
if ($request->getMethod() !== 'GET') {
throw new WebsocketException(
\sprintf('Wrong http method, GET expected, "%" received.', $request->getMethod())
);
}
$headers = $request->getHeaders();
if (empty($headers['Sec-WebSocket-Key'])) {
throw new WebsocketException(
\sprintf('Missing websocket key header.')
);
}
if (empty($headers['Upgrade']) || 'websocket' !== \strtolower($headers['Upgrade'])) {
throw new WebsocketException(
\sprintf('Wrong or missing upgrade header.')
);
}
$version = $headers->get('Sec-WebSocket-Version');
if (!\in_array($version, ServerHandshake::SUPPORTED_VERSIONS)) {
throw new WebsocketVersionException(sprintf('Version %s not supported by Woketo for now.', $version));
}
return true;
}
/**
* @param Request $request
* @return string
* @throws WebsocketException
*/
public function extractKeyFromRequest(Request $request)
{
$key = $request->getHeader('Sec-WebSocket-Key');
if (empty($key)) {
throw new WebsocketException('No key found in the request.');
}
return $key;
}
}
<?php

/**
* This file is a part of Woketo package.
*
* (c) Nekland <[email protected]>
*
* For the full license, take a look to the LICENSE file
* on the root directory of this project
*/

namespace Nekland\Woketo\Rfc6455\Handshake;
use Nekland\Woketo\Exception\RuntimeException;
use Nekland\Woketo\Exception\WebsocketException;
use Nekland\Woketo\Exception\WebsocketVersionException;
use Nekland\Woketo\Http\AbstractHttpMessage;
use Nekland\Woketo\Http\Request;
use Nekland\Woketo\Http\Response;


/**
* Class ServerHandshake
*
* This class is highly inspired by ratchetphp/RFC6455.
*/
class ServerHandshake implements HandshakeInterface
{
public function sign(AbstractHttpMessage $response = null, string $key = null)
{
$signature = \base64_encode(\sha1($key . HandshakeInterface::GUID, true));

if (null !== $response) {
$response->addHeader('Sec-WebSocket-Accept', $signature);
}

return $signature;
}

/**
* https://tools.ietf.org/html/rfc6455#section-4.2
*
* @param AbstractHttpMessage $request
* @param string|null $key
* @return bool
* @throws RuntimeException
* @throws WebsocketException
* @throws WebsocketVersionException
*/
public function verify(AbstractHttpMessage $request, string $key = null)
{
if (!$request instanceof Request) {
throw new RuntimeException(
sprintf('The client handshake cannot verify something else than a Response object, %s given.', get_class($response))
);
}

if ($request->getHttpVersion() !== 'HTTP/1.1') {
throw new WebsocketException(
\sprintf('Wrong http version, HTTP/1.1 expected, "%s" received.', $request->getHttpVersion())
);
}

if ($request->getMethod() !== 'GET') {
throw new WebsocketException(
\sprintf('Wrong http method, GET expected, "%s" received.', $request->getMethod())
);
}

$headers = $request->getHeaders();
if (empty($headers['Sec-WebSocket-Key'])) {
throw new WebsocketException(
\sprintf('Missing websocket key header.')
);
}

if (empty($headers['Upgrade']) || 'websocket' !== \strtolower($headers['Upgrade'])) {
throw new WebsocketException(
\sprintf('Wrong or missing upgrade header.')
);
}

$version = $headers->get('Sec-WebSocket-Version');
if (!\in_array($version, ServerHandshake::SUPPORTED_VERSIONS)) {
throw new WebsocketVersionException(sprintf('Version %s not supported by Woketo for now.', $version));
}

return true;
}

/**
* @param Request $request
* @return string
* @throws WebsocketException
*/
public function extractKeyFromRequest(Request $request)
{
$key = $request->getHeader('Sec-WebSocket-Key');

if (empty($key)) {
throw new WebsocketException('No key found in the request.');
}

return $key;
}
}
Loading