diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..fb288d9 --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,3 @@ +# Contributing + +Please see our [contributing guide](http://docs.php-http.org/en/latest/development/contributing.html). diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..d4ecf20 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,27 @@ +| Q | A +| ------------ | --- +| Bug? | no|yes +| New Feature? | no|yes +| Version | Specific version or SHA of a commit + + +#### Actual Behavior + +What is the actual behavior? + + +#### Expected Behavior + +What is the behavior you expect? + + +#### Steps to Reproduce + +What are the steps to reproduce this bug? Please add code examples, +screenshots or links to GitHub repositories that reproduce the problem. + + +#### Possible Solutions + +If you have already ideas how to solve the issue, add them here. +(remove this section if not needed) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..323987b --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,43 @@ +| Q | A +| --------------- | --- +| Bug fix? | no|yes +| New feature? | no|yes +| BC breaks? | no|yes +| Deprecations? | no|yes +| Related tickets | fixes #X, partially #Y, mentioned in #Z +| Documentation | if this is a new feature, link to pull request in https://github.com/php-http/documentation that adds relevant documentation +| License | MIT + + +#### What's in this PR? + +Explain what the changes in this PR do. + + +#### Why? + +Which problem does the PR fix? (remove this section if you linked an issue above) + + +#### Example Usage + +``` php +// If you added new features, show examples of how to use them here +// (remove this section if not a new feature) + +$foo = new Foo(); + +// Now we can do +$foo->doSomething(); +``` + + +#### Checklist + +- [ ] Updated CHANGELOG.md to describe BC breaks / deprecations | new feature | bugfix +- [ ] Documentation pull request created (if not simply a bugfix) + + +#### To Do + +- [ ] If the PR is not complete but you want to discuss the approach, list what remains to be done here diff --git a/.github/workflows/.editorconfig b/.github/workflows/.editorconfig new file mode 100644 index 0000000..7bd3346 --- /dev/null +++ b/.github/workflows/.editorconfig @@ -0,0 +1,2 @@ +[*.yml] +indent_size = 2 diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml new file mode 100644 index 0000000..8e3f23e --- /dev/null +++ b/.github/workflows/checks.yml @@ -0,0 +1,20 @@ +name: Checks + +on: + push: + branches: + - 2.x + pull_request: + +jobs: + composer-normalize: + name: Composer Normalize + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Composer normalize + uses: docker://ergebnis/composer-normalize-action + diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..1231aa2 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,110 @@ +name: CI + +on: + push: + branches: + - '[0-9]+.x' + - '[0-9]+.[0-9]+' + - '[0-9]+.[0-9]+.x' + pull_request: + +jobs: + supported-versions-matrix: + name: Supported Versions Matrix + runs-on: ubuntu-latest + outputs: + version: ${{ steps.supported-versions-matrix.outputs.version }} + steps: + - uses: actions/checkout@v4 + - id: supported-versions-matrix + uses: WyriHaximus/github-action-composer-php-versions-in-range@v1 + latest: + name: PHP ${{ matrix.php }} Latest + runs-on: ubuntu-latest + needs: + - supported-versions-matrix + strategy: + matrix: + php: ${{ fromJson(needs.supported-versions-matrix.outputs.version) }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer + coverage: none + + - name: Install dependencies + run: composer update --prefer-dist --no-interaction --no-progress + + - name: generate ssl + run: cd ./tests/server/ssl && ./generate.sh && pwd && ls -la && cd ../../../ + + - name: boot test server + run: vendor/bin/http_test_server > /dev/null 2>&1 & + + - name: Execute tests + run: composer test + + lowest: + name: PHP ${{ matrix.php }} Lowest + runs-on: ubuntu-latest + needs: + - supported-versions-matrix + strategy: + matrix: + php: ${{ fromJson(needs.supported-versions-matrix.outputs.version) }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer + coverage: none + + - name: Install dependencies + run: composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress + + - name: generate ssl + run: cd ./tests/server/ssl && ./generate.sh && pwd && ls -la && cd ../../../ + + - name: boot test server + run: vendor/bin/http_test_server > /dev/null 2>&1 & + + - name: Execute tests + run: composer test + + coverage: + name: Code Coverage + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.3 + tools: composer + coverage: xdebug + + - name: Install dependencies + run: composer update --prefer-dist --no-interaction --no-progress + + - name: generate ssl + run: cd ./tests/server/ssl && ./generate.sh && pwd && ls -la && cd ../../../ + + - name: boot test server + run: vendor/bin/http_test_server > /dev/null 2>&1 & + + - name: Execute tests + run: composer test-ci diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml new file mode 100644 index 0000000..cfa74b1 --- /dev/null +++ b/.github/workflows/static.yml @@ -0,0 +1,36 @@ +name: Static analysis + +on: + push: + branches: + - 2.x + pull_request: + +jobs: + phpstan: + name: PHPStan + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: PHPStan + uses: docker://oskarstark/phpstan-ga + env: + REQUIRE_DEV: true + with: + args: analyze --no-progress + + php-cs-fixer: + name: PHP-CS-Fixer + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: PHP-CS-Fixer + uses: docker://oskarstark/php-cs-fixer-ga + with: + args: --dry-run --diff diff --git a/.gitignore b/.gitignore index da734f1..322ef74 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,10 @@ -.puli/ -build/ -vendor/ -composer.lock -phpspec.yml -phpunit.xml +/.php-cs-fixer.cache +/.puli/ +/build/ +/composer.lock +/phpstan.neon +/phpunit.xml +/tests/server/ssl/*.pem +/tests/server/ssl/*.key +/tests/server/ssl/*.req +/vendor/ diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..bcb9a69 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,16 @@ +exclude('vendor') + ->in(__DIR__) +; + +$config = (new PhpCsFixer\Config()) + ->setRiskyAllowed(true) + ->setRules([ + '@Symfony' => true, + ]) + ->setFinder($finder) +; + +return $config; diff --git a/.php_cs b/.php_cs deleted file mode 100644 index 23ba165..0000000 --- a/.php_cs +++ /dev/null @@ -1,13 +0,0 @@ - /dev/null 2>&1 & - -script: - - $TEST_COMMAND - - ./vendor/bin/phpunit tests/SocketClientFeatureTest.php --printer Http\\Client\\Tests\\FeatureTestListener || echo "" - -after_success: - - if [[ "$COVERAGE" = true ]]; then wget https://scrutinizer-ci.com/ocular.phar; fi - - if [[ "$COVERAGE" = true ]]; then php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml; fi diff --git a/CHANGELOG.md b/CHANGELOG.md index e59903e..b766856 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,57 @@ # Change Log -## Unreleased +## 2.3.0 + + * Fixed compatibility with `psr/http-message` v2 + * The `Http\Client\Socket\Stream` has BC breaks if you extended it. It is not meant to be extended, declaring it as `@internal` now. + +## 2.2.0 + + * Allow installation with Symfony 7 + +## 2.1.1 + + * Fixed constructor to work nicely with version 1 style arguments (e.g. HttplugBundle) + * Fixed PHP 8 compatibility for stream timeouts + * Renamed `master` branch to `2.x` for semantic branch naming. + * Add Symfony 6 compatibility + +## 2.1.0 + + * Add php 8 compatibility + +## 2.0.2 + + * Fixed composer "provide" section to say that we provide `psr/http-client-implementation` + +## 2.0.1 + + * Fix wrong call to trigger_error + +## 2.0.0 + + * Remove response and stream factory, use direct implementation of nyholm/psr7 + * PSR18 and HTTPlug 2 support + * Remove support for php 5.5, 5.6, 7.0 and 7.1 + * SSL Method now defaults to `STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT` + +## 1.4.0 + + * Support for Symfony 4 + +## 1.3.0 + + * Make sure `Stream::__toString` never throws exception + * Added more exception + * `BrokenPipeException` + * `ConnectionException` + * `InvalidRequestException` + * `SSLConnectionException` + +## 1.2.0 + + * Dropped PHP 5.4 support + * Using stable version of `php-http/discovery` ## 1.1.0 diff --git a/README.md b/README.md index 392f655..27be400 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Latest Version](https://img.shields.io/github/release/php-http/socket-client.svg?style=flat-square)](https://github.com/php-http/socket-client/releases) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) -[![Build Status](https://img.shields.io/travis/php-http/socket-client.svg?branch=master&style=flat-square)](https://travis-ci.org/php-http/socket-client) +[![Build Status](https://github.com/php-http/socket-client/actions/workflows/ci.yml/badge.svg?branch=2.x)](https://github.com/php-http/socket-client/actions/workflows/ci.yml) [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/php-http/socket-client.svg?style=flat-square)](https://scrutinizer-ci.com/g/php-http/socket-client) [![Quality Score](https://img.shields.io/scrutinizer/g/php-http/socket-client.svg?style=flat-square)](https://scrutinizer-ci.com/g/php-http/socket-client) [![Total Downloads](https://img.shields.io/packagist/dt/php-http/socket-client.svg?style=flat-square)](https://packagist.org/packages/php-http/socket-client) @@ -28,7 +28,15 @@ First launch the http server: $ ./vendor/bin/http_test_server > /dev/null 2>&1 & ``` -Then the test suite: +Then generate SSL certificates: + +```bash +$ composer gen-ssl +``` + +Note: If you are running this on macOS and get the following error: "Error opening CA Private Key privkey.pem", check [this](ssl-macOS.md) file. + +Now run the test suite: ``` bash $ composer test diff --git a/composer.json b/composer.json index 3f4e9bb..0689754 100644 --- a/composer.json +++ b/composer.json @@ -9,21 +9,24 @@ } ], "require": { - "php": "^5.5 || ^7.0", - "symfony/options-resolver": "^2.6 || ^3.0", - "php-http/httplug": "^1.0", - "php-http/message-factory": "^1.0.2", - "php-http/discovery": "^1.0" + "php": "^8.1", + "nyholm/psr7": "^1.8.1", + "php-http/httplug": "^2.4", + "psr/http-client": "^1.0", + "psr/http-message": "^1.0 || ^2.0", + "symfony/options-resolver": "^2.6 || ^3.4 || ^4.4 || ^5.0 || ^6.0 || ^7.0" }, "require-dev": { - "phpunit/phpunit": "^4.8", - "guzzlehttp/psr7": "^1.2", - "php-http/client-integration-tests": "^0.5.1", - "php-http/message": "^1.0", - "php-http/client-common": "^1.0" + "friendsofphp/php-cs-fixer": "^3.51", + "php-http/client-integration-tests": "^3.1.1", + "php-http/message": "^1.16", + "php-http/client-common": "^2.7", + "phpunit/phpunit": "^8.5.23 || ~9.5", + "php-http/message-factory": "^1.1" }, "provide": { - "php-http/client-implementation": "1.0" + "php-http/client-implementation": "1.0", + "psr/http-client-implementation": "1.0" }, "autoload": { "psr-4": { @@ -36,13 +39,11 @@ } }, "scripts": { + "cs-check": "vendor/bin/php-cs-fixer fix --dry-run", + "cs-fix": "vendor/bin/php-cs-fixer fix", "test": "vendor/bin/phpunit", - "test-ci": "vendor/bin/phpunit --coverage-clover build/coverage.xml" - }, - "extra": { - "branch-alias": { - "dev-master": "1.1-dev" - } + "test-ci": "vendor/bin/phpunit --coverage-clover build/coverage.xml", + "gen-ssl": "tests/server/ssl/generate.sh" }, "prefer-stable": true, "minimum-stability": "dev" diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..f88ed86 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,25 @@ +parameters: + level: max + paths: + - src + ignoreErrors: + # phpstan seems confused by passing a variable by reference to stream_select + - + message: '#^Negated boolean expression is always false.$#' + count: 1 + path: src/RequestWriter.php + + - + message: "#^Method Http\\\\Client\\\\Socket\\\\Client\\:\\:configure\\(\\) should return array\\{remote_socket\\: string\\|null, timeout\\: int, stream_context\\: resource, stream_context_options\\: array\\, stream_context_param\\: array\\, ssl\\: bool\\|null, write_buffer_size\\: int, ssl_method\\: int\\} but returns array\\.$#" + count: 1 + path: src/Client.php + + - + message: "#^Parameter \\#1 \\$options of function stream_context_create expects array\\|null, mixed given\\.$#" + count: 1 + path: src/Client.php + + - + message: "#^Parameter \\#2 \\$params of function stream_context_create expects array\\|null, mixed given\\.$#" + count: 1 + path: src/Client.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2ab0582..f12146d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,17 +1,16 @@ - - - - tests/ - tests/SocketClientFeatureTest.php - - - - - - - - src/ - - + + + + src/ + + + + + tests/ + + + + + diff --git a/src/Client.php b/src/Client.php index 4ab7bc5..116b621 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,11 +2,14 @@ namespace Http\Client\Socket; -use Http\Client\Exception\NetworkException; use Http\Client\HttpClient; -use Http\Discovery\MessageFactoryDiscovery; -use Http\Message\ResponseFactory; +use Http\Client\Socket\Exception\ConnectionException; +use Http\Client\Socket\Exception\InvalidRequestException; +use Http\Client\Socket\Exception\SSLConnectionException; +use Http\Client\Socket\Exception\TimeoutException; use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseFactoryInterface; +use Psr\Http\Message\ResponseInterface; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -16,51 +19,49 @@ * Use stream and socket capabilities of the core of PHP to send HTTP requests * * @author Joel Wurtz + * + * @final */ class Client implements HttpClient { use RequestWriter; use ResponseReader; - private $config = [ - 'remote_socket' => null, - 'timeout' => null, - 'stream_context_options' => [], - 'stream_context_param' => [], - 'ssl' => null, - 'write_buffer_size' => 8192, - 'ssl_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT, - ]; + /** + * @var array{remote_socket: string|null, timeout: int, stream_context: resource, stream_context_options: array, stream_context_param: array, ssl: ?boolean, write_buffer_size: int, ssl_method: int} + */ + private $config; /** * Constructor. * - * @param ResponseFactory $responseFactory Response factory for creating response - * @param array $config { + * @param array{remote_socket?: string|null, timeout?: int, stream_context?: resource, stream_context_options?: array, stream_context_param?: array, ssl?: ?boolean, write_buffer_size?: int, ssl_method?: int}|ResponseFactoryInterface $config1 + * @param array{remote_socket?: string|null, timeout?: int, stream_context?: resource, stream_context_options?: array, stream_context_param?: array, ssl?: ?boolean, write_buffer_size?: int, ssl_method?: int}|null $config2 Mistake when refactoring the constructor from version 1 to version 2 - used as $config if set and $configOrResponseFactory is a response factory instance + * @param array{remote_socket?: string|null, timeout?: int, stream_context?: resource, stream_context_options?: array, stream_context_param?: array, ssl?: ?boolean, write_buffer_size?: int, ssl_method?: int} $config intended for version 1 BC, used as $config if $config2 is not set and $configOrResponseFactory is a response factory instance * - * @var string $remote_socket Remote entrypoint (can be a tcp or unix domain address) - * @var int $timeout Timeout before canceling request - * @var array $stream_context_options Context options as defined in the PHP documentation - * @var array $stream_context_param Context params as defined in the PHP documentation - * @var bool $ssl Use ssl, default to scheme from request, false if not present - * @var int $write_buffer_size Buffer when writing the request body, defaults to 8192 - * @var int $ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLS_CLIENT - * } + * string|null remote_socket Remote entrypoint (can be a tcp or unix domain address) + * int timeout Timeout before canceling request + * stream resource The initialized stream context, if not set the context is created from the options and param. + * array stream_context_options Context options as defined in the PHP documentation + * array stream_context_param Context params as defined in the PHP documentation + * boolean ssl Use ssl, default to scheme from request, false if not present + * int write_buffer_size Buffer when writing the request body, defaults to 8192 + * int ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT */ - public function __construct(ResponseFactory $responseFactory = null, array $config = []) + public function __construct($config1 = [], $config2 = null, array $config = []) { - if (null === $responseFactory) { - $responseFactory = MessageFactoryDiscovery::find(); + if (\is_array($config1)) { + $this->config = $this->configure($config1); + + return; } - $this->responseFactory = $responseFactory; - $this->config = $this->configure($config); + @trigger_error('Passing a Psr\Http\Message\ResponseFactoryInterface to SocketClient is deprecated, and will be removed in 3.0, you should only pass config options.', E_USER_DEPRECATED); + + $this->config = $this->configure($config2 ?: $config); } - /** - * {@inheritdoc} - */ - public function sendRequest(RequestInterface $request) + public function sendRequest(RequestInterface $request): ResponseInterface { $remote = $this->config['remote_socket']; $useSsl = $this->config['ssl']; @@ -74,7 +75,7 @@ public function sendRequest(RequestInterface $request) } if (null === $useSsl) { - $useSsl = ($request->getUri()->getScheme() === 'https'); + $useSsl = ('https' === $request->getUri()->getScheme()); } $socket = $this->createSocket($request, $remote, $useSsl); @@ -98,26 +99,28 @@ public function sendRequest(RequestInterface $request) * @param string $remote Entrypoint for the connection * @param bool $useSsl Whether to use ssl or not * - * @throws NetworkException When the connection fail - * * @return resource Socket resource + * + * @throws ConnectionException|SSLConnectionException When the connection fail */ - protected function createSocket(RequestInterface $request, $remote, $useSsl) + protected function createSocket(RequestInterface $request, string $remote, bool $useSsl) { $errNo = null; $errMsg = null; $socket = @stream_socket_client($remote, $errNo, $errMsg, floor($this->config['timeout'] / 1000), STREAM_CLIENT_CONNECT, $this->config['stream_context']); if (false === $socket) { - throw new NetworkException($errMsg, $request); + if (110 === $errNo) { + throw new TimeoutException($errMsg, $request); + } + + throw new ConnectionException($errMsg, $request); } - stream_set_timeout($socket, floor($this->config['timeout'] / 1000), $this->config['timeout'] % 1000); + stream_set_timeout($socket, (int) floor($this->config['timeout'] / 1000), $this->config['timeout'] % 1000); - if ($useSsl) { - if (false === @stream_socket_enable_crypto($socket, true, $this->config['ssl_method'])) { - throw new NetworkException(sprintf('Cannot enable tls: %s', error_get_last()['message']), $request); - } + if ($useSsl && false === @stream_socket_enable_crypto($socket, true, $this->config['ssl_method'])) { + throw new SSLConnectionException(sprintf('Cannot enable tls: %s', error_get_last()['message'] ?? 'no error reported'), $request); } return $socket; @@ -127,6 +130,8 @@ protected function createSocket(RequestInterface $request, $remote, $useSsl) * Close the socket, used when having an error. * * @param resource $socket + * + * @return void */ protected function closeSocket($socket) { @@ -136,19 +141,26 @@ protected function closeSocket($socket) /** * Return configuration for the socket client. * - * @param array $config Configuration from user + * @param array{remote_socket?: string|null, timeout?: int, stream_context?: resource, stream_context_options?: array, stream_context_param?: array, ssl?: ?boolean, write_buffer_size?: int, ssl_method?: int} $config * - * @return array Configuration resolved + * @return array{remote_socket: string|null, timeout: int, stream_context: resource, stream_context_options: array, stream_context_param: array, ssl: ?boolean, write_buffer_size: int, ssl_method: int} */ protected function configure(array $config = []) { $resolver = new OptionsResolver(); - $resolver->setDefaults($this->config); + $resolver->setDefaults([ + 'remote_socket' => null, + 'timeout' => null, + 'stream_context_options' => [], + 'stream_context_param' => [], + 'ssl' => null, + 'write_buffer_size' => 8192, + 'ssl_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, + ]); $resolver->setDefault('stream_context', function (Options $options) { return stream_context_create($options['stream_context_options'], $options['stream_context_param']); }); - - $resolver->setDefault('timeout', ini_get('default_socket_timeout') * 1000); + $resolver->setDefault('timeout', ((int) ini_get('default_socket_timeout')) * 1000); $resolver->setAllowedTypes('stream_context_options', 'array'); $resolver->setAllowedTypes('stream_context_param', 'array'); @@ -161,20 +173,18 @@ protected function configure(array $config = []) /** * Return remote socket from the request. * - * @param RequestInterface $request - * - * @throws NetworkException When no remote can be determined from the request - * * @return string + * + * @throws InvalidRequestException When no remote can be determined from the request */ private function determineRemoteFromRequest(RequestInterface $request) { - if ($request->getUri()->getHost() == '' && !$request->hasHeader('Host')) { - throw new NetworkException('Cannot find connection endpoint for this request', $request); + if (!$request->hasHeader('Host') && '' === $request->getUri()->getHost()) { + throw new InvalidRequestException('Remote is not defined and we cannot determine a connection endpoint for this request (no Host header)', $request); } $host = $request->getUri()->getHost(); - $port = $request->getUri()->getPort() ?: ($request->getUri()->getScheme() == 'https' ? 443 : 80); + $port = $request->getUri()->getPort() ?: ('https' === $request->getUri()->getScheme() ? 443 : 80); $endpoint = sprintf('%s:%s', $host, $port); // If use the host header if present for the endpoint diff --git a/src/Exception/BrokenPipeException.php b/src/Exception/BrokenPipeException.php new file mode 100644 index 0000000..4cf4b62 --- /dev/null +++ b/src/Exception/BrokenPipeException.php @@ -0,0 +1,7 @@ +request = $request; + + parent::__construct($message, 0, $previous); + } + + public function getRequest(): RequestInterface + { + return $this->request; + } +} diff --git a/src/Exception/SSLConnectionException.php b/src/Exception/SSLConnectionException.php new file mode 100644 index 0000000..b1f85ef --- /dev/null +++ b/src/Exception/SSLConnectionException.php @@ -0,0 +1,7 @@ +fwrite($socket, $this->transformRequestHeadersToString($request))) { - throw new NetworkException('Failed to send request, underlying socket not accessible, (BROKEN EPIPE)', $request); + throw new BrokenPipeException('Failed to send request, underlying socket not accessible, (BROKEN EPIPE)', $request); } if ($request->getBody()->isReadable()) { @@ -37,14 +37,13 @@ protected function writeRequest($socket, RequestInterface $request, $bufferSize /** * Write Body of the request. * - * @param resource $socket - * @param RequestInterface $request - * @param int $bufferSize + * @param resource $socket + * + * @return void * - * @throws \Http\Client\Exception\NetworkException - * @throws \Http\Client\Exception\RequestException + * @throws BrokenPipeException */ - protected function writeBody($socket, RequestInterface $request, $bufferSize = 8192) + protected function writeBody($socket, RequestInterface $request, int $bufferSize = 8192) { $body = $request->getBody(); @@ -56,19 +55,15 @@ protected function writeBody($socket, RequestInterface $request, $bufferSize = 8 $buffer = $body->read($bufferSize); if (false === $this->fwrite($socket, $buffer)) { - throw new NetworkException('An error occur when writing request to client (BROKEN EPIPE)', $request); + throw new BrokenPipeException('An error occur when writing request to client (BROKEN EPIPE)', $request); } } } /** * Produce the header of request as a string based on a PSR Request. - * - * @param RequestInterface $request - * - * @return string */ - protected function transformRequestHeadersToString(RequestInterface $request) + protected function transformRequestHeadersToString(RequestInterface $request): string { $message = vsprintf('%s %s HTTP/%s', [ strtoupper($request->getMethod()), @@ -91,17 +86,16 @@ protected function transformRequestHeadersToString(RequestInterface $request) * @see https://secure.phabricator.com/rPHU69490c53c9c2ef2002bc2dd4cecfe9a4b080b497 * * @param resource $stream The stream resource - * @param string $bytes Bytes written in the stream * * @return bool|int false if pipe is broken, number of bytes written otherwise */ - private function fwrite($stream, $bytes) + private function fwrite($stream, string $bytes) { if (!strlen($bytes)) { return 0; } $result = @fwrite($stream, $bytes); - if ($result !== 0) { + if (0 !== $result) { // In cases where some bytes are witten (`$result > 0`) or // an error occurs (`$result === false`), the behavior of fwrite() is // correct. We can return the value as-is. @@ -125,10 +119,11 @@ private function fwrite($stream, $bytes) // perform a write. If the write also fails, conclude that these failures are // EPIPE or some other permanent failure. $result = @fwrite($stream, $bytes); - if ($result !== 0) { + if (0 !== $result) { // The write worked or failed explicitly. This value is fine to return. return $result; } + // We performed a 0-length write, were told that the stream was writable, and // then immediately performed another 0-length write. Conclude that the pipe // is broken and return `false`. diff --git a/src/ResponseReader.php b/src/ResponseReader.php index 202d1bd..d5beae6 100644 --- a/src/ResponseReader.php +++ b/src/ResponseReader.php @@ -2,8 +2,9 @@ namespace Http\Client\Socket; -use Http\Client\Exception\NetworkException; -use Http\Message\ResponseFactory; +use Http\Client\Socket\Exception\BrokenPipeException; +use Http\Client\Socket\Exception\TimeoutException; +use Nyholm\Psr7\Response; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -16,28 +17,21 @@ */ trait ResponseReader { - /** - * @var ResponseFactory For creating response - */ - protected $responseFactory; - /** * Read a response from a socket. * - * @param RequestInterface $request - * @param resource $socket - * - * @throws NetworkException When the response cannot be read + * @param resource $socket * - * @return ResponseInterface + * @throws TimeoutException When the socket timed out + * @throws BrokenPipeException When the response cannot be read */ - protected function readResponse(RequestInterface $request, $socket) + protected function readResponse(RequestInterface $request, $socket): ResponseInterface { $headers = []; $reason = null; - while (($line = fgets($socket)) !== false) { - if (rtrim($line) === '') { + while (false !== ($line = fgets($socket))) { + if ('' === rtrim($line)) { break; } $headers[] = trim($line); @@ -46,13 +40,13 @@ protected function readResponse(RequestInterface $request, $socket) $metadatas = stream_get_meta_data($socket); if (array_key_exists('timed_out', $metadatas) && true === $metadatas['timed_out']) { - throw new NetworkException('Error while reading response, stream timed out', $request); + throw new TimeoutException('Error while reading response, stream timed out', $request, null); } - - $parts = explode(' ', array_shift($headers), 3); + $header = array_shift($headers); + $parts = null !== $header ? explode(' ', $header, 3) : []; if (count($parts) <= 1) { - throw new NetworkException('Cannot read the response', $request); + throw new BrokenPipeException('Cannot read the response', $request); } $protocol = substr($parts[0], -3); @@ -77,8 +71,8 @@ protected function readResponse(RequestInterface $request, $socket) : ''; } - $response = $this->responseFactory->createResponse($status, $reason, $responseHeaders, null, $protocol); - $stream = $this->createStream($socket, $response); + $response = new Response((int) $status, $responseHeaders, null, $protocol, $reason); + $stream = $this->createStream($socket, $request, $response); return $response->withBody($stream); } @@ -86,19 +80,19 @@ protected function readResponse(RequestInterface $request, $socket) /** * Create the stream. * - * @param $socket - * @param ResponseInterface $response - * - * @return Stream + * @param resource $socket */ - protected function createStream($socket, ResponseInterface $response) + protected function createStream($socket, RequestInterface $request, ResponseInterface $response): Stream { $size = null; if ($response->hasHeader('Content-Length')) { $size = (int) $response->getHeaderLine('Content-Length'); } + if ($size < 0) { + $size = null; + } - return new Stream($socket, $size); + return new Stream($request, $socket, $size); } } diff --git a/src/Stream.php b/src/Stream.php index 9f5677f..e49844a 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -4,6 +4,7 @@ use Http\Client\Socket\Exception\StreamException; use Http\Client\Socket\Exception\TimeoutException; +use Psr\Http\Message\RequestInterface; use Psr\Http\Message\StreamInterface; /** @@ -20,11 +21,13 @@ * * Writing and seeking is disable to avoid weird behaviors. * - * @author Joel Wurtz + * @author Joel Wurtz */ class Stream implements StreamInterface { - /** @var resource Underlying socket */ + /** @var resource|null Underlying socket */ private $socket; /** @@ -33,48 +36,55 @@ class Stream implements StreamInterface private $isDetached = false; /** - * @var int|null Size of the stream, so we know what we must read, null if not available (i.e. a chunked stream) + * @var int<0, max>|null Size of the stream, so we know what we must read, null if not available (i.e. a chunked stream) */ private $size; /** - * @var int Size of the stream readed, to avoid reading more than available and have the user blocked + * @var int<0, max> Size of the stream readed, to avoid reading more than available and have the user blocked */ private $readed = 0; + /** + * @var RequestInterface request associated to this stream + */ + private $request; + /** * Create the stream. * - * @param resource $socket - * @param int $size + * @param resource $socket + * @param int<0, max>|null $size */ - public function __construct($socket, $size = null) + public function __construct(RequestInterface $request, $socket, ?int $size = null) { $this->socket = $socket; $this->size = $size; + $this->request = $request; } - /** - * {@inheritdoc} - */ - public function __toString() + public function __toString(): string { - return (string) $this->getContents(); + try { + return $this->getContents(); + } catch (\Exception $e) { + return ''; + } } - /** - * {@inheritdoc} - */ - public function close() + public function close(): void { + if ($this->isDetached || null === $this->socket) { + throw new StreamException('Stream is detached'); + } fclose($this->socket); } - /** - * {@inheritdoc} - */ public function detach() { + if ($this->isDetached) { + return null; + } $this->isDetached = true; $socket = $this->socket; $this->socket = null; @@ -83,84 +93,85 @@ public function detach() } /** - * {@inheritdoc} + * @return int<0, max>|null */ - public function getSize() + public function getSize(): ?int { return $this->size; } - /** - * {@inheritdoc} - */ - public function tell() + public function tell(): int { - return ftell($this->socket); + if ($this->isDetached || null === $this->socket) { + throw new StreamException('Stream is detached'); + } + $tell = ftell($this->socket); + if (false === $tell) { + throw new StreamException('ftell returned false'); + } + + return $tell; } - /** - * {@inheritdoc} - */ - public function eof() + public function eof(): bool { + if ($this->isDetached || null === $this->socket) { + throw new StreamException('Stream is detached'); + } + return feof($this->socket); } - /** - * {@inheritdoc} - */ - public function isSeekable() + public function isSeekable(): bool { return false; } - /** - * {@inheritdoc} - */ - public function seek($offset, $whence = SEEK_SET) + public function seek($offset, $whence = SEEK_SET): void { throw new StreamException('This stream is not seekable'); } - /** - * {@inheritdoc} - */ - public function rewind() + public function rewind(): void { throw new StreamException('This stream is not seekable'); } - /** - * {@inheritdoc} - */ - public function isWritable() + public function isWritable(): bool { return false; } - /** - * {@inheritdoc} - */ - public function write($string) + public function write($string): int { throw new StreamException('This stream is not writable'); } - /** - * {@inheritdoc} - */ - public function isReadable() + public function isReadable(): bool { return true; } /** - * {@inheritdoc} + * @param int<0, max> $length */ - public function read($length) + public function read($length): string { + if (0 === $length) { + return ''; + } + + if ($this->isDetached || null === $this->socket) { + throw new StreamException('Stream is detached'); + } + if (null === $this->getSize()) { - return fread($this->socket, $length); + $read = fread($this->socket, $length); + if (false === $read) { + throw new StreamException('Failed to read from stream'); + } + + return $read; } if ($this->getSize() === $this->readed) { @@ -169,9 +180,17 @@ public function read($length) // Even if we request a length a non blocking stream can return less data than asked $read = fread($this->socket, $length); + if (false === $read) { + // PHP 8 + if ($this->getMetadata('timed_out')) { + throw new TimeoutException('Stream timed out while reading data', $this->request); + } + throw new StreamException('Failed to read from stream'); + } + // PHP 7: fread does not return false when timing out if ($this->getMetadata('timed_out')) { - throw new TimeoutException('Stream timed out while reading data'); + throw new TimeoutException('Stream timed out while reading data', $this->request); } $this->readed += strlen($read); @@ -179,29 +198,38 @@ public function read($length) return $read; } - /** - * {@inheritdoc} - */ - public function getContents() + public function getContents(): string { + if ($this->isDetached || null === $this->socket) { + throw new StreamException('Stream is detached'); + } + if (null === $this->getSize()) { - return stream_get_contents($this->socket); + $contents = stream_get_contents($this->socket); + if (false === $contents) { + throw new StreamException('failed to get contents of stream'); + } + + return $contents; } $contents = ''; - do { - $contents .= $this->read($this->getSize() - $this->readed); - } while ($this->readed < $this->getSize()); + $toread = $this->getSize() - $this->readed; + while ($toread > 0) { + $contents .= $this->read($toread); + $toread = $this->getSize() - $this->readed; + } return $contents; } - /** - * {@inheritdoc} - */ public function getMetadata($key = null) { + if ($this->isDetached || null === $this->socket) { + throw new StreamException('Stream is detached'); + } + $meta = stream_get_meta_data($this->socket); if (null === $key) { diff --git a/ssl-macOS.md b/ssl-macOS.md new file mode 100644 index 0000000..c37468d --- /dev/null +++ b/ssl-macOS.md @@ -0,0 +1,58 @@ +# Generating SSL Certificates on macOS + +When generating SSL Certificates on macOS, you must ensure that you're using brew's openssl binary and not the one provided by the OS. + +To do that, find out where your openssl is installed by running: + +```bash +$ brew info openssl +``` + +You should see something like this: + +``` +openssl@1.1: stable 1.1.1i (bottled) [keg-only] +Cryptography and SSL/TLS Toolkit +https://openssl.org/ +/usr/local/Cellar/openssl@1.1/1.1.1i (8,067 files, 18.5MB) + Poured from bottle on 2020-12-11 at 11:31:46 +From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@1.1.rb +License: OpenSSL +==> Caveats +A CA file has been bootstrapped using certificates from the system +keychain. To add additional certificates, place .pem files in + /usr/local/etc/openssl@1.1/certs + +and run + /usr/local/opt/openssl@1.1/bin/c_rehash + +openssl@1.1 is keg-only, which means it was not symlinked into /usr/local, +because macOS provides LibreSSL. + +If you need to have openssl@1.1 first in your PATH run: + echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> /Users/flavio/.bash_profile + +For compilers to find openssl@1.1 you may need to set: + export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib" + export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include" + +For pkg-config to find openssl@1.1 you may need to set: + export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig" + +==> Analytics +install: 855,315 (30 days), 2,356,331 (90 days), 7,826,269 (365 days) +install-on-request: 139,236 (30 days), 373,801 (90 days), 1,120,685 (365 days) +build-error: 0 (30 days) +``` + +The important part is this: + +> echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> /Users/flavio/.bash_profile + +Instead of running `./tests/server/ssl/generate.sh`, you should instead run: + +```bash +$ PATH="/usr/local/opt/openssl@1.1/bin ./tests/server/ssl/generate.sh +``` + +You should now be good to go. diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index d049251..43d26a6 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -2,16 +2,23 @@ namespace Http\Client\Socket\Tests; -class BaseTestCase extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class BaseTestCase extends TestCase { private $servers = []; public function startServer($name) { - $filename = __DIR__ . '/server/' . $name . '.php'; - $pipes = []; - $this->servers[$name] = proc_open('php '. $filename, [], $pipes); - usleep(30000); + $filename = __DIR__.'/server/'.$name.'.php'; + $pipes = []; + + if (!Semaphore::acquire()) { + $this->fail('Could not connect to server'); + } + + $this->servers[$name] = proc_open('php '.$filename, [], $pipes); + sleep(1); } public function stopServer($name) @@ -21,10 +28,12 @@ public function stopServer($name) } } - public function tearDown() + public function tearDown(): void { foreach (array_keys($this->servers) as $name) { $this->stopServer($name); } + + Semaphore::release(); } } diff --git a/tests/Semaphore.php b/tests/Semaphore.php new file mode 100644 index 0000000..9ff92a7 --- /dev/null +++ b/tests/Semaphore.php @@ -0,0 +1,40 @@ + + */ +final class Semaphore +{ + private static $openConnections = 0; + + /** + * Busy waiting for my turn to go. + * + * @return bool + */ + public static function acquire() + { + $tries = 1; + while (self::$openConnections > 0) { + sleep($tries++); + if ($tries > 5) { + return false; + } + } + ++self::$openConnections; + + return true; + } + + /** + * Signal that you are done. + */ + public static function release() + { + // Do no be too quick + usleep(500000); + --self::$openConnections; + } +} diff --git a/tests/SocketClientFeatureTest.php b/tests/SocketClientFeatureTest.php index 487944b..f1fe854 100644 --- a/tests/SocketClientFeatureTest.php +++ b/tests/SocketClientFeatureTest.php @@ -2,14 +2,39 @@ namespace Http\Client\Socket\Tests; -use Http\Client\Tests\HttpFeatureTest; -use Http\Message\MessageFactory\GuzzleMessageFactory; use Http\Client\Socket\Client as SocketHttpClient; +use Http\Client\Tests\HttpFeatureTest; +use Psr\Http\Client\ClientInterface; class SocketClientFeatureTest extends HttpFeatureTest { - protected function createClient() + protected function createClient(): ClientInterface { return new SocketHttpClient(); } + + public function testAutoSetContentLength(): void + { + $this->markTestSkipped('Feature is unsupported'); + } + + public function testGzip(): void + { + $this->markTestSkipped('Feature is unsupported'); + } + + public function testDeflate(): void + { + $this->markTestSkipped('Feature is unsupported'); + } + + public function testChunked(): void + { + $this->markTestSkipped('Feature is unsupported'); + } + + public function testRedirect(): void + { + $this->markTestSkipped('Feature is unsupported'); + } } diff --git a/tests/SocketHttpAdapterTest.php b/tests/SocketHttpAdapterTest.php index 3d90e70..72ed4bb 100644 --- a/tests/SocketHttpAdapterTest.php +++ b/tests/SocketHttpAdapterTest.php @@ -2,16 +2,13 @@ namespace Http\Client\Socket\Tests; -use Http\Client\Tests\HttpClientTest; -use Http\Message\MessageFactory\GuzzleMessageFactory; use Http\Client\Socket\Client as SocketHttpClient; +use Http\Client\Tests\HttpClientTest; +use Psr\Http\Client\ClientInterface; class SocketHttpAdapterTest extends HttpClientTest { - /** - * {@inheritdoc} - */ - protected function createHttpAdapter() + protected function createHttpAdapter(): ClientInterface { return new SocketHttpClient(); } diff --git a/tests/SocketHttpClientTest.php b/tests/SocketHttpClientTest.php index c3660ec..657f2ff 100644 --- a/tests/SocketHttpClientTest.php +++ b/tests/SocketHttpClientTest.php @@ -3,103 +3,96 @@ namespace Http\Client\Socket\Tests; use Http\Client\Common\HttpMethodsClient; -use Http\Message\MessageFactory\GuzzleMessageFactory; use Http\Client\Socket\Client as SocketHttpClient; +use Http\Client\Socket\Exception\NetworkException; +use Http\Client\Socket\Exception\TimeoutException; +use Nyholm\Psr7\Factory\Psr17Factory; class SocketHttpClientTest extends BaseTestCase { - public function createClient($options = array()) + public function createClient($options = []) { - $messageFactory = new GuzzleMessageFactory(); - - return new HttpMethodsClient(new SocketHttpClient($messageFactory, $options), $messageFactory); + return new HttpMethodsClient(new SocketHttpClient($options), new Psr17Factory()); } public function testTcpSocketDomain() { $this->startServer('tcp-server'); - $client = $this->createClient(['remote_socket' => '127.0.0.1:19999']); + $client = $this->createClient(['remote_socket' => '127.0.0.1:19999']); $response = $client->get('/', []); - $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); } - /** - * @expectedException \Http\Client\Exception\NetworkException - */ - public function testNoRemote() + public function testNoRemote(): void { - $client = $this->createClient(); + $client = $this->createClient(); + $this->expectException(NetworkException::class); $client->get('/', []); } - public function testRemoteInUri() + public function testRemoteInUri(): void { $this->startServer('tcp-server'); - $client = $this->createClient(); + $client = $this->createClient(); $response = $client->get('http://127.0.0.1:19999/', []); - $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); } - public function testRemoteInHostHeader() + public function testRemoteInHostHeader(): void { $this->startServer('tcp-server'); - $client = $this->createClient(); + $client = $this->createClient(); $response = $client->get('/', ['Host' => '127.0.0.1:19999']); $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); } - /** - * @expectedException \Http\Client\Exception\NetworkException - */ - public function testBrokenSocket() + public function testBrokenSocket(): void { $this->startServer('tcp-bugous-server'); $client = $this->createClient(['remote_socket' => '127.0.0.1:19999']); + $this->expectException(NetworkException::class); $client->get('/', []); } - public function testSslRemoteInUri() + public function testSslRemoteInUri(): void { $this->startServer('tcp-ssl-server'); - $client = $this->createClient([ + $client = $this->createClient([ + 'remote_socket' => 'tcp://127.0.0.1:19999', + 'ssl' => true, 'stream_context_options' => [ 'ssl' => [ 'peer_name' => 'socket-adapter', - 'cafile' => __DIR__ . '/server/ssl/ca.pem' - ] - ] + 'cafile' => __DIR__.'/server/ssl/ca.pem', + ], + ], ]); - $response = $client->get('https://127.0.0.1:19999/', []); + $response = $client->get('/', []); $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); } - public function testUnixSocketDomain() + public function testUnixSocketDomain(): void { $this->startServer('unix-domain-server'); - $client = $this->createClient([ - 'remote_socket' => 'unix://'.__DIR__.'/server/server.sock' + $client = $this->createClient([ + 'remote_socket' => 'unix://'.__DIR__.'/server/server.sock', ]); $response = $client->get('/', []); - $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); } - /** - * @expectedException \Http\Client\Exception\NetworkException - */ - public function testNetworkExceptionOnConnectError() + public function testNetworkExceptionOnConnectError(): void { - $client = $this->createClient(['remote_socket' => '127.0.0.1:19999']); + $client = $this->createClient(['remote_socket' => '127.0.0.1:19999']); + $this->expectException(NetworkException::class); $client->get('/', []); } @@ -107,88 +100,74 @@ public function testSslConnection() { $this->startServer('tcp-ssl-server'); - $client = $this->createClient([ + $client = $this->createClient([ 'remote_socket' => '127.0.0.1:19999', - 'ssl' => true, + 'ssl' => true, 'stream_context_options' => [ 'ssl' => [ 'peer_name' => 'socket-adapter', - 'cafile' => __DIR__ . '/server/ssl/ca.pem' - ] - ] + 'cafile' => __DIR__.'/server/ssl/ca.pem', + ], + ], ]); $response = $client->get('/', []); - $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); } - public function testSslConnectionWithClientCertificate() + public function testSslConnectionWithClientCertificate(): void { - if (version_compare(PHP_VERSION, '5.6', '<')) { - $this->markTestSkipped('Test can only run on php 5.6 and superior (for capturing peer certificate)'); - } - $this->startServer('tcp-ssl-server-client'); - $client = $this->createClient([ + $client = $this->createClient([ 'remote_socket' => '127.0.0.1:19999', - 'ssl' => true, + 'ssl' => true, 'stream_context_options' => [ 'ssl' => [ - 'peer_name' => 'socket-adapter', - 'cafile' => __DIR__ . '/server/ssl/ca.pem', - 'local_cert' => __DIR__ . '/server/ssl/client-and-key.pem' - ] - ] + 'peer_name' => 'socket-adapter', + 'cafile' => __DIR__.'/server/ssl/ca.pem', + 'local_cert' => __DIR__.'/server/ssl/client-and-key.pem', + ], + ], ]); $response = $client->get('/', []); - $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); } - public function testInvalidSslConnectionWithClientCertificate() + public function testInvalidSslConnectionWithClientCertificate(): void { - if (version_compare(PHP_VERSION, '5.6', '<')) { - $this->markTestSkipped('Test can only run on php 5.6 and superior (for capturing peer certificate)'); - } - $this->startServer('tcp-ssl-server-client'); - $client = $this->createClient([ + $client = $this->createClient([ 'remote_socket' => '127.0.0.1:19999', - 'ssl' => true, + 'ssl' => true, 'stream_context_options' => [ 'ssl' => [ - 'peer_name' => 'socket-adapter', - 'cafile' => __DIR__ . '/server/ssl/ca.pem' - ] - ] + 'peer_name' => 'socket-adapter', + 'cafile' => __DIR__.'/server/ssl/ca.pem', + ], + ], ]); $response = $client->get('/', []); - $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); $this->assertEquals(403, $response->getStatusCode()); } - /** - * @expectedException \Http\Client\Exception\NetworkException - */ - public function testNetworkExceptionOnSslError() + public function testNetworkExceptionOnSslError(): void { $this->startServer('tcp-server'); - $client = $this->createClient(['remote_socket' => '127.0.0.1:19999', 'ssl' => true]); + $client = $this->createClient(['remote_socket' => '127.0.0.1:19999', 'ssl' => true]); + $this->expectException(NetworkException::class); $client->get('/', []); } - /** - * @expectedException \Http\Client\Exception\NetworkException - */ - public function testNetworkExceptionOnTimeout() + public function testNetworkExceptionOnTimeout(): void { - $client = $this->createClient(['timeout' => 10]); - $client->get('http://php.net', []); + $client = $this->createClient(['timeout' => 1]); + $this->expectException(TimeoutException::class); + $response = $client->get('https://php.net', []); + $response->getBody()->getContents(); } } diff --git a/tests/StreamTest.php b/tests/StreamTest.php index 9d5ea10..ff40991 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -2,155 +2,146 @@ namespace Http\Client\Socket\Tests; +use Http\Client\Socket\Exception\StreamException; +use Http\Client\Socket\Exception\TimeoutException; use Http\Client\Socket\Stream; +use Nyholm\Psr7\Request; +use PHPUnit\Framework\TestCase; -class StreamTest extends \PHPUnit_Framework_TestCase +class StreamTest extends TestCase { - public function createSocket($body, $useSize = true) + public function createSocket($body, $useSize = true): Stream { - $socket = fopen('php://memory', 'rw'); + $socket = fopen('php://memory', 'rwb'); fwrite($socket, $body); fseek($socket, 0); - return new Stream($socket, $useSize ? strlen($body) : null); + return new Stream(new Request('GET', '/'), $socket, $useSize ? strlen($body) : null); } - public function testToString() + public function testToString(): void { - $stream = $this->createSocket("Body"); + $stream = $this->createSocket('Body'); - $this->assertEquals("Body", $stream->__toString()); + $this->assertEquals('Body', $stream->__toString()); $stream->close(); } - public function testSubsequentCallIsEmpty() + public function testSubsequentCallIsEmpty(): void { - $stream = $this->createSocket("Body"); + $stream = $this->createSocket('Body'); - $this->assertEquals("Body", $stream->getContents()); + $this->assertEquals('Body', $stream->getContents()); $this->assertEmpty($stream->getContents()); $stream->close(); } - public function testDetach() + public function testDetach(): void { - $stream = $this->createSocket("Body"); + $stream = $this->createSocket('Body'); $socket = $stream->detach(); - $this->assertTrue(is_resource($socket)); + $this->assertIsResource($socket); $this->assertNull($stream->detach()); } - public function testTell() + public function testTell(): void { - $stream = $this->createSocket("Body"); + $stream = $this->createSocket('Body'); $this->assertEquals(0, $stream->tell()); - $this->assertEquals("Body", $stream->getContents()); + $this->assertEquals('Body', $stream->getContents()); $this->assertEquals(4, $stream->tell()); } - public function testEof() + public function testEof(): void { - $socket = fopen('php://memory', 'rw+'); - fwrite($socket, "Body"); + $socket = fopen('php://memory', 'rwb+'); + fwrite($socket, 'Body'); fseek($socket, 0); - $stream = new Stream($socket); + $stream = new Stream(new Request('GET', '/'), $socket); - $this->assertEquals("Body", $stream->getContents()); + $this->assertEquals('Body', $stream->getContents()); fwrite($socket, "\0"); $this->assertTrue($stream->eof()); $stream->close(); } - public function testNotSeekable() + public function testNotSeekable(): void { - $stream = $this->createSocket("Body"); + $stream = $this->createSocket('Body'); $this->assertFalse($stream->isSeekable()); - try { - $stream->seek(0); - } catch (\Exception $e) { - $this->assertInstanceOf('Http\Client\Socket\Exception\StreamException', $e); - } + $this->expectException(StreamException::class); + $stream->seek(0); } - public function testNoRewing() + public function testNoRewind(): void { - $stream = $this->createSocket("Body"); + $stream = $this->createSocket('Body'); - try { - $stream->rewind(); - } catch (\Exception $e) { - $this->assertInstanceOf('Http\Client\Socket\Exception\StreamException', $e); - } + $this->expectException(StreamException::class); + $stream->rewind(); } - public function testNotWritable() + public function testNotWritable(): void { - $stream = $this->createSocket("Body"); + $stream = $this->createSocket('Body'); $this->assertFalse($stream->isWritable()); - try { - $stream->write("Test"); - } catch (\Exception $e) { - $this->assertInstanceOf('Http\Client\Socket\Exception\StreamException', $e); - } + $this->expectException(StreamException::class); + $stream->write('Test'); } - public function testIsReadable() + public function testIsReadable(): void { - $stream = $this->createSocket("Body"); + $stream = $this->createSocket('Body'); $this->assertTrue($stream->isReadable()); } - public function testTimeout() + public function testTimeout(): void { - $socket = fsockopen("php.net", 80); - socket_set_timeout($socket, 0, 100); + $socket = fsockopen('php.net', 80); + stream_set_timeout($socket, 0, 100); - $stream = new Stream($socket); - - try { - $stream->getContents(); - } catch (\Exception $e) { - $this->assertInstanceOf('Http\Socket\Exception\TimeoutException', $e); - } + $stream = new Stream(new Request('GET', '/'), $socket, 50); + $this->expectException(TimeoutException::class); + $stream->getContents(); } - public function testMetadatas() + public function testMetadatas(): void { - $stream = $this->createSocket("Body", false); - - $this->assertEquals("PHP", $stream->getMetadata("wrapper_type")); - $this->assertEquals("MEMORY", $stream->getMetadata("stream_type")); - $this->assertEquals("php://memory", $stream->getMetadata("uri")); - $this->assertFalse($stream->getMetadata("timed_out")); - $this->assertFalse($stream->getMetadata("eof")); - $this->assertTrue($stream->getMetadata("blocked")); + $stream = $this->createSocket('Body', false); + + $this->assertEquals('PHP', $stream->getMetadata('wrapper_type')); + $this->assertEquals('MEMORY', $stream->getMetadata('stream_type')); + $this->assertEquals('php://memory', $stream->getMetadata('uri')); + $this->assertFalse($stream->getMetadata('timed_out')); + $this->assertFalse($stream->getMetadata('eof')); + $this->assertTrue($stream->getMetadata('blocked')); } - public function testClose() + public function testClose(): void { - $socket = fopen('php://memory', 'rw+'); - fwrite($socket, "Body"); + $socket = fopen('php://memory', 'rwb+'); + fwrite($socket, 'Body'); fseek($socket, 0); - $stream = new Stream($socket); + $stream = new Stream(new Request('GET', '/'), $socket); $stream->close(); - $this->assertFalse(is_resource($socket)); + $this->assertFalse(is_resource($socket)); // phpstorm thinks we could assertNotIsResource, but closed resources seem to behave differently } - public function testRead() + public function testRead(): void { - $stream = $this->createSocket("Body"); + $stream = $this->createSocket('Body'); - $this->assertEquals("Bod", $stream->read(3)); - $this->assertEquals("y", $stream->read(3)); + $this->assertEquals('Bod', $stream->read(3)); + $this->assertEquals('y', $stream->read(3)); $stream->close(); } diff --git a/tests/server/ssl/ca.pem b/tests/server/ssl/ca.pem deleted file mode 100644 index aa477d0..0000000 --- a/tests/server/ssl/ca.pem +++ /dev/null @@ -1,19 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIDBDCCAm2gAwIBAgIJAKf70F8G74WBMA0GCSqGSIb3DQEBBQUAMGAxCzAJBgNV -BAYTAkZSMRYwFAYDVQQIEw1JbGUtZGUtRnJhbmNlMQ4wDAYDVQQHEwVQYXJpczER -MA8GA1UEChMIUEhQLUhUVFAxFjAUBgNVBAMTDXNvY2tldC1zZXJ2ZXIwHhcNMTYw -NzE4MTAwMDIzWhcNMTYwODE3MTAwMDIzWjBgMQswCQYDVQQGEwJGUjEWMBQGA1UE -CBMNSWxlLWRlLUZyYW5jZTEOMAwGA1UEBxMFUGFyaXMxETAPBgNVBAoTCFBIUC1I -VFRQMRYwFAYDVQQDEw1zb2NrZXQtc2VydmVyMIGfMA0GCSqGSIb3DQEBAQUAA4GN -ADCBiQKBgQDZbuJ4bebB+9mER7bHl2kizLWP0gWwv5l0Kvr2m0ajZ7MZpxH19Hmo -I5XXIdnM3JSIajW8+FZ23z4s1FawRrn9EnJc/o0KP7NB0yREcO3XiuqAo5K8gkFA -wQPeaxl1JDpAVjKPAfP35bnMoIz55MW/UJXH+QBv1/95T3r9xvX+PQIDAQABo4HF -MIHCMB0GA1UdDgQWBBQpMG5EbNuv8sNLO/MbPbyp3DMLezCBkgYDVR0jBIGKMIGH -gBQpMG5EbNuv8sNLO/MbPbyp3DMLe6FkpGIwYDELMAkGA1UEBhMCRlIxFjAUBgNV -BAgTDUlsZS1kZS1GcmFuY2UxDjAMBgNVBAcTBVBhcmlzMREwDwYDVQQKEwhQSFAt -SFRUUDEWMBQGA1UEAxMNc29ja2V0LXNlcnZlcoIJAKf70F8G74WBMAwGA1UdEwQF -MAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAFfJlrEhQ4o1sroNsq/gWBP9rg4jzU4Bm -HcWieWQyWg5sXNCJgCr9ntZ4w29POtlIpHXCoH3UnEDEq7BXGiVRd7vAbxKn6Yt/ -+oKTxvcmBNXkkOgEbH/3uD2sGkcfndYkJieRGcUr69wThhjSxfgUE1Gnynjl7SIF -UlZlufPto3I= ------END CERTIFICATE----- diff --git a/tests/server/ssl/client-and-key.pem b/tests/server/ssl/client-and-key.pem deleted file mode 100644 index fb27231..0000000 --- a/tests/server/ssl/client-and-key.pem +++ /dev/null @@ -1,29 +0,0 @@ ------BEGIN CERTIFICATE----- -MIICNzCCAaACASowDQYJKoZIhvcNAQEFBQAwYDELMAkGA1UEBhMCRlIxFjAUBgNV -BAgTDUlsZS1kZS1GcmFuY2UxDjAMBgNVBAcTBVBhcmlzMREwDwYDVQQKEwhQSFAt -SFRUUDEWMBQGA1UEAxMNc29ja2V0LXNlcnZlcjAeFw0xNjA3MTgxMDAwMzFaFw0x -NjA4MTcxMDAwMzFaMGgxCzAJBgNVBAYTAkZSMRYwFAYDVQQIEw1JbGUtZGUtRnJh -bmNlMQ4wDAYDVQQHEwVQYXJpczERMA8GA1UEChMIUEhQLUhUVFAxHjAcBgNVBAMT -FXNvY2tldC1hZGFwdGVyLWNsaWVudDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkC -gYEA4iXSjiZWYKmGfEluM0z+bY6r2AGaZgApvOehQMjom1mORllMVkfIanuenbQE -ay9UIihRCO9gYZpFTSDt8Mgobl8x1i9gGd1bFf5Vy/9fubBD0k8Sss3GHNfTe5az -mls7D8pqTD7KOdBe1000DmpNKbV4b5WZGI/dsliUqvEQJDcCAwEAATANBgkqhkiG -9w0BAQUFAAOBgQDRjvy010S62JD20ZrK60svmqI6Z9a3jJ6dVmWS1YM1u8DRrUKf -StsEbIuNMuLmdGQ9FDoLuURuzuqtYlsKyjnUq7oyjq8xeIKr2oAPxftwBBeEydQF -7GBcdokMOMWl9bGLI7YMbRiqyshw8y/rqVCAF+5/65jhPMJuPEPVa2pCJQ== ------END CERTIFICATE----- ------BEGIN RSA PRIVATE KEY----- -MIICXgIBAAKBgQDiJdKOJlZgqYZ8SW4zTP5tjqvYAZpmACm856FAyOibWY5GWUxW -R8hqe56dtARrL1QiKFEI72BhmkVNIO3wyChuXzHWL2AZ3VsV/lXL/1+5sEPSTxKy -zcYc19N7lrOaWzsPympMPso50F7XTTQOak0ptXhvlZkYj92yWJSq8RAkNwIDAQAB -AoGBAKR3eU+kFgGqiIHvLVPl1Mt8jQFE+TLhP3mAw0dIVdJVKSGEY/4Dqn+2c76h -Yu1IuFk3FOf4nxGcjnXvUtxf1PZB/t1xHMHuspYHkc9ElDQDfuYqgimAPzMHsUfm -St47YNNLA6pzPUtpaw7qRA7v54y8qi3GdHf4pLW1/PSuuoLhAkEA9T62mody3EDG -33jks1vCGoFtADlU3J2ShJiFG44HsWueJ933KRgP2NlAwtumO+Om4CaUUzIiOSzT -uibjOEyl7wJBAOwQtfERZfvpVLoqVF9NADvy4KpGbv+lXq9gTia9hSMumH2h5+Jh -nVXhN9m/TRQiQ74XB3tpC0I5H4fozCj17jkCQQDY/gNN2Vjc3wfYBTy+IgstrqTG -VPPsPBO+9MCZr+LIrgMDHVeqaZrj8N5nSzfbt8MYg6orrqkvU6WHCQ906gTLAkAc -6dMPCr1+BE8vWh3pakn8Dbx8vpG72Cpnupj338Cd1Ygx4xG+xYv6x9IKZobvT18S -ukldUTlizBJTmCzhW3XpAkEA5tYfC72Ia761g6ild8HaILMK30+wYCWIu3Ieb+G1 -uaJWO+VDKKtWVntkPfnckVlfPQMmuMDaIGh9G2vWRYdtDQ== ------END RSA PRIVATE KEY----- diff --git a/tests/server/ssl/client.key b/tests/server/ssl/client.key deleted file mode 100644 index 8f7d7c7..0000000 --- a/tests/server/ssl/client.key +++ /dev/null @@ -1,15 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIICXgIBAAKBgQDiJdKOJlZgqYZ8SW4zTP5tjqvYAZpmACm856FAyOibWY5GWUxW -R8hqe56dtARrL1QiKFEI72BhmkVNIO3wyChuXzHWL2AZ3VsV/lXL/1+5sEPSTxKy -zcYc19N7lrOaWzsPympMPso50F7XTTQOak0ptXhvlZkYj92yWJSq8RAkNwIDAQAB -AoGBAKR3eU+kFgGqiIHvLVPl1Mt8jQFE+TLhP3mAw0dIVdJVKSGEY/4Dqn+2c76h -Yu1IuFk3FOf4nxGcjnXvUtxf1PZB/t1xHMHuspYHkc9ElDQDfuYqgimAPzMHsUfm -St47YNNLA6pzPUtpaw7qRA7v54y8qi3GdHf4pLW1/PSuuoLhAkEA9T62mody3EDG -33jks1vCGoFtADlU3J2ShJiFG44HsWueJ933KRgP2NlAwtumO+Om4CaUUzIiOSzT -uibjOEyl7wJBAOwQtfERZfvpVLoqVF9NADvy4KpGbv+lXq9gTia9hSMumH2h5+Jh -nVXhN9m/TRQiQ74XB3tpC0I5H4fozCj17jkCQQDY/gNN2Vjc3wfYBTy+IgstrqTG -VPPsPBO+9MCZr+LIrgMDHVeqaZrj8N5nSzfbt8MYg6orrqkvU6WHCQ906gTLAkAc -6dMPCr1+BE8vWh3pakn8Dbx8vpG72Cpnupj338Cd1Ygx4xG+xYv6x9IKZobvT18S -ukldUTlizBJTmCzhW3XpAkEA5tYfC72Ia761g6ild8HaILMK30+wYCWIu3Ieb+G1 -uaJWO+VDKKtWVntkPfnckVlfPQMmuMDaIGh9G2vWRYdtDQ== ------END RSA PRIVATE KEY----- diff --git a/tests/server/ssl/client.pem b/tests/server/ssl/client.pem deleted file mode 100644 index 801a918..0000000 --- a/tests/server/ssl/client.pem +++ /dev/null @@ -1,14 +0,0 @@ ------BEGIN CERTIFICATE----- -MIICNzCCAaACASowDQYJKoZIhvcNAQEFBQAwYDELMAkGA1UEBhMCRlIxFjAUBgNV -BAgTDUlsZS1kZS1GcmFuY2UxDjAMBgNVBAcTBVBhcmlzMREwDwYDVQQKEwhQSFAt -SFRUUDEWMBQGA1UEAxMNc29ja2V0LXNlcnZlcjAeFw0xNjA3MTgxMDAwMzFaFw0x -NjA4MTcxMDAwMzFaMGgxCzAJBgNVBAYTAkZSMRYwFAYDVQQIEw1JbGUtZGUtRnJh -bmNlMQ4wDAYDVQQHEwVQYXJpczERMA8GA1UEChMIUEhQLUhUVFAxHjAcBgNVBAMT -FXNvY2tldC1hZGFwdGVyLWNsaWVudDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkC -gYEA4iXSjiZWYKmGfEluM0z+bY6r2AGaZgApvOehQMjom1mORllMVkfIanuenbQE -ay9UIihRCO9gYZpFTSDt8Mgobl8x1i9gGd1bFf5Vy/9fubBD0k8Sss3GHNfTe5az -mls7D8pqTD7KOdBe1000DmpNKbV4b5WZGI/dsliUqvEQJDcCAwEAATANBgkqhkiG -9w0BAQUFAAOBgQDRjvy010S62JD20ZrK60svmqI6Z9a3jJ6dVmWS1YM1u8DRrUKf -StsEbIuNMuLmdGQ9FDoLuURuzuqtYlsKyjnUq7oyjq8xeIKr2oAPxftwBBeEydQF -7GBcdokMOMWl9bGLI7YMbRiqyshw8y/rqVCAF+5/65jhPMJuPEPVa2pCJQ== ------END CERTIFICATE----- diff --git a/tests/server/ssl/client.req b/tests/server/ssl/client.req deleted file mode 100644 index 545173d..0000000 --- a/tests/server/ssl/client.req +++ /dev/null @@ -1,11 +0,0 @@ ------BEGIN CERTIFICATE REQUEST----- -MIIBqDCCARECAQAwaDELMAkGA1UEBhMCRlIxFjAUBgNVBAgTDUlsZS1kZS1GcmFu -Y2UxDjAMBgNVBAcTBVBhcmlzMREwDwYDVQQKEwhQSFAtSFRUUDEeMBwGA1UEAxMV -c29ja2V0LWFkYXB0ZXItY2xpZW50MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB -gQDiJdKOJlZgqYZ8SW4zTP5tjqvYAZpmACm856FAyOibWY5GWUxWR8hqe56dtARr -L1QiKFEI72BhmkVNIO3wyChuXzHWL2AZ3VsV/lXL/1+5sEPSTxKyzcYc19N7lrOa -WzsPympMPso50F7XTTQOak0ptXhvlZkYj92yWJSq8RAkNwIDAQABoAAwDQYJKoZI -hvcNAQEFBQADgYEA3G8c/4gB/eEme+1U99SKZlxHGzKhj28Qz9aiunfGRVnPDgnF -wFrnah8KjBwAv2Eh58SwTomenSYtpuuuT6W3YQ4YGn5BtoTb2l4qpW8cncqQR5Bq -g91l/fXtuZKU5hSOkI5vAKCsHY3Z+R8Raxxd0s6pzeca+qkkEmUmKedEnPM= ------END CERTIFICATE REQUEST----- diff --git a/tests/server/ssl/file.srl b/tests/server/ssl/file.srl index 8676c24..a787364 100644 --- a/tests/server/ssl/file.srl +++ b/tests/server/ssl/file.srl @@ -1 +1 @@ -2A +34 diff --git a/tests/server/ssl/generate.sh b/tests/server/ssl/generate.sh index b99f4b4..8f1be48 100755 --- a/tests/server/ssl/generate.sh +++ b/tests/server/ssl/generate.sh @@ -1,4 +1,8 @@ -#/bin/bash +#!/bin/bash + +set -eo pipefail + +cd $(dirname $0) C=FR ST=Ile-de-France @@ -6,15 +10,14 @@ L=Paris O="PHP-HTTP" CN="socket-adapter" -openssl req -out ca.pem -new -x509 -subj "/C=$C/ST=$ST/L=$L/O=$O/CN=socket-server" - -openssl genrsa -out server.key 1024 -subj "/C=$C/ST=$ST/L=$L/O=$O/CN=socket-adapter" -openssl req -key server.key -new -out server.req -subj "/C=$C/ST=$ST/L=$L/O=$O/CN=socket-adapter" -openssl x509 -req -in server.req -CA ca.pem -CAkey privkey.pem -CAserial file.srl -out server.pem +openssl req -out ca.pem -new -x509 -subj "/C=$C/ST=$ST/L=$L/O=$O/CN=socket-server" -passout pass:password +openssl genrsa -out server.key +openssl req -key server.key -new -out server.req -subj "/C=$C/ST=$ST/L=$L/O=$O/CN=socket-adapter" -passout pass:password +openssl x509 -req -in server.req -CA ca.pem -CAkey privkey.pem -CAserial file.srl -out server.pem -passin pass:password -openssl genrsa -out client.key 1024 -subj "/C=$C/ST=$ST/L=$L/O=$O/CN=socket-adapter-client" -openssl req -key client.key -new -out client.req -subj "/C=$C/ST=$ST/L=$L/O=$O/CN=socket-adapter-client" -openssl x509 -req -in client.req -CA ca.pem -CAkey privkey.pem -CAserial file.srl -out client.pem +openssl genrsa -out client.key +openssl req -key client.key -new -out client.req -subj "/C=$C/ST=$ST/L=$L/O=$O/CN=socket-adapter-client" -passout pass:password +openssl x509 -req -in client.req -CA ca.pem -CAkey privkey.pem -CAserial file.srl -out client.pem -passin pass:password cat client.pem client.key > client-and-key.pem cat server.pem server.key > server-and-key.pem diff --git a/tests/server/ssl/privkey.pem b/tests/server/ssl/privkey.pem deleted file mode 100644 index 31c9834..0000000 --- a/tests/server/ssl/privkey.pem +++ /dev/null @@ -1,18 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -Proc-Type: 4,ENCRYPTED -DEK-Info: DES-EDE3-CBC,7BB9811913388905 - -UHN46XyFU48x2IQAkYjrLDzcFLU6xZh1rPGYgIEy6Dacg6QqxO90Nlm2GD8phvY6 -UVj2E2S3GlviBegs/8AJtJ3Ly9LFGKoM5zFrQ24zPp1QAKqMhLBpbq/iFi6oyWv4 -cEB2d7fBZETtg1ZXhvjzS9muvc3uZfIALmoPxWlTYv0m4ErE4GBezKO+Y7Mexl6U -K9Mg0fAXNW8GQsUpqjs+9IA/COS7GKsHRKV7Bk0Ar/Kd/xjFBv0Vf4mdyYe/fAu1 -ary20XZsYNsO/RdfUtWMzsTKfnMgG1csQcO7xgYHGKHC6U+35yjxwmQxw7F3xP4W -8i5t2J3QfnX9PlnYOLTYucj74na+U6raE2S+ejO3YDF8nIpJaDvzpA4N35T4hDDL -Z/SpFtol8Uf33GuH/uiz7x2ss7vyPe1hk96nirT14Bsx1ZEczua/CCTs+iQtoB5F -breSVhlyMCq6rC47xd69IYVnyEe11EigHn/sQkdUae3kjqL4pgMGBdlONuV63gmX -yNSFIaE+gh0mFnOVdCtiFxS8XP5BkJ11IcSLIG4ZygycX/IOTRQFh/TTJ4HYDcPZ -j3zJvZm/OI8xiq5jhEqfkgJElQd/pFuLHESEa8+Q4wiOhYwQEBjKDIBboWXF8CDE -QFIVBf/o+w3YN700p0xKjGqhaPnKQwCEfY5Lh44pPf7yQX7LiE8tG9kqAfEu25Dc -kYl1Rw8SFP6OVLqCNMjRTKeyFubkoAFXz828dzZdGd6H8zPH51FqsxIqJepwH6sT -NWHvnX5LwC5mblF4+VSc7TTgaE16Nq/g1oEXNBuZS6oJq2OnwtH4yQ== ------END RSA PRIVATE KEY----- diff --git a/tests/server/ssl/server-and-key.pem b/tests/server/ssl/server-and-key.pem deleted file mode 100644 index bd112d2..0000000 --- a/tests/server/ssl/server-and-key.pem +++ /dev/null @@ -1,29 +0,0 @@ ------BEGIN CERTIFICATE----- -MIICMDCCAZkCASkwDQYJKoZIhvcNAQEFBQAwYDELMAkGA1UEBhMCRlIxFjAUBgNV -BAgTDUlsZS1kZS1GcmFuY2UxDjAMBgNVBAcTBVBhcmlzMREwDwYDVQQKEwhQSFAt -SFRUUDEWMBQGA1UEAxMNc29ja2V0LXNlcnZlcjAeFw0xNjA3MTgxMDAwMjhaFw0x -NjA4MTcxMDAwMjhaMGExCzAJBgNVBAYTAkZSMRYwFAYDVQQIEw1JbGUtZGUtRnJh -bmNlMQ4wDAYDVQQHEwVQYXJpczERMA8GA1UEChMIUEhQLUhUVFAxFzAVBgNVBAMT -DnNvY2tldC1hZGFwdGVyMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZHYLV -1mpoiG2AW5VebIB0nCaT8EIryeUk57swa5nqW9fj/KD4hIaoz19Ivt7/LrVTCOag -jFiwEcm4kEC8h0DJvW2IKNDWIpObHBSFFFOlLxhJkq+US1CJjKgSDvU5X3ElXb99 -L1EHN/+M8ZA4Qw3fFAbO2ajENeoxHH9KKZCQLQIDAQABMA0GCSqGSIb3DQEBBQUA -A4GBAM1TFIPTwbG0x//yMhPD9ZGWb2mYfA00Lri198YVmG61PtgDI5hG2dxaDVDa -6Ig6DnrwTBMc51p+NiTgci3OqnJ4FuwBdY5d1moyQre0MHr+sg8STJr5tmAEithL -GUGq3Uv3RuN3OetaPrlxaccJM1nB63caoH5Rl+VqAsJ4S4LI ------END CERTIFICATE----- ------BEGIN RSA PRIVATE KEY----- -MIICXQIBAAKBgQDZHYLV1mpoiG2AW5VebIB0nCaT8EIryeUk57swa5nqW9fj/KD4 -hIaoz19Ivt7/LrVTCOagjFiwEcm4kEC8h0DJvW2IKNDWIpObHBSFFFOlLxhJkq+U -S1CJjKgSDvU5X3ElXb99L1EHN/+M8ZA4Qw3fFAbO2ajENeoxHH9KKZCQLQIDAQAB -AoGAHcaqVuFdHpD3U4874QNIarWvpMJikNbTkkglp/JZ3YZTCXVwLiUewrAOBVAI -DT8+pvClkIcu8pa97435EuIJxGB9C0Nudzpknm3xucc1pfYsq9fIa6F/+xTBIaTu -3FcA1vJ/fodhcPPwp6CAQJV7pLfQ5m+DLuwp5ICyy5v2yRECQQD0YgkbGLyKIM0K -qcNbDE3fNrRQ99ydQ1QXrB+4M8Lc/49XLvd5l9QwMN6BHvEpGf4qvBUYBFYroSVs -wogOWfmvAkEA42+lxrETE7mIJ/h+QDAQ6JX9fUuda96YU7Xs7OaRWAc77RazxP2p -XTV1U9l+FHpAvdauWgi7h+0zCEfbjEb24wJBALDJ4vB5OptqEyqtmKdbBVJ+zn7s -b+pMdJ8TY3br6wHY1qcpUNnjnhqmn7Ak5CzpotbjbRcMvBGf5+T/7VEEgf0CQGlE -/hgvUcMpdun5TIZtOQZOllvuKdkJ2Lu2Uwl71v5FqukcPu0I8+FeowXCf2b/BwRX -X6/wpsxNb/d/OxF8rz0CQQDPPxYM5+FSFzun831QSfxWwGx2LLFPQKYmixKfwpQB -3eIq3gPYN3A/qlVCZlGhyKLH2WiW/bY3HTQ4ZSCgTYzd ------END RSA PRIVATE KEY----- diff --git a/tests/server/ssl/server.key b/tests/server/ssl/server.key deleted file mode 100644 index 5a49a70..0000000 --- a/tests/server/ssl/server.key +++ /dev/null @@ -1,15 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIICXQIBAAKBgQDZHYLV1mpoiG2AW5VebIB0nCaT8EIryeUk57swa5nqW9fj/KD4 -hIaoz19Ivt7/LrVTCOagjFiwEcm4kEC8h0DJvW2IKNDWIpObHBSFFFOlLxhJkq+U -S1CJjKgSDvU5X3ElXb99L1EHN/+M8ZA4Qw3fFAbO2ajENeoxHH9KKZCQLQIDAQAB -AoGAHcaqVuFdHpD3U4874QNIarWvpMJikNbTkkglp/JZ3YZTCXVwLiUewrAOBVAI -DT8+pvClkIcu8pa97435EuIJxGB9C0Nudzpknm3xucc1pfYsq9fIa6F/+xTBIaTu -3FcA1vJ/fodhcPPwp6CAQJV7pLfQ5m+DLuwp5ICyy5v2yRECQQD0YgkbGLyKIM0K -qcNbDE3fNrRQ99ydQ1QXrB+4M8Lc/49XLvd5l9QwMN6BHvEpGf4qvBUYBFYroSVs -wogOWfmvAkEA42+lxrETE7mIJ/h+QDAQ6JX9fUuda96YU7Xs7OaRWAc77RazxP2p -XTV1U9l+FHpAvdauWgi7h+0zCEfbjEb24wJBALDJ4vB5OptqEyqtmKdbBVJ+zn7s -b+pMdJ8TY3br6wHY1qcpUNnjnhqmn7Ak5CzpotbjbRcMvBGf5+T/7VEEgf0CQGlE -/hgvUcMpdun5TIZtOQZOllvuKdkJ2Lu2Uwl71v5FqukcPu0I8+FeowXCf2b/BwRX -X6/wpsxNb/d/OxF8rz0CQQDPPxYM5+FSFzun831QSfxWwGx2LLFPQKYmixKfwpQB -3eIq3gPYN3A/qlVCZlGhyKLH2WiW/bY3HTQ4ZSCgTYzd ------END RSA PRIVATE KEY----- diff --git a/tests/server/ssl/server.pem b/tests/server/ssl/server.pem deleted file mode 100644 index 5c9542a..0000000 --- a/tests/server/ssl/server.pem +++ /dev/null @@ -1,14 +0,0 @@ ------BEGIN CERTIFICATE----- -MIICMDCCAZkCASkwDQYJKoZIhvcNAQEFBQAwYDELMAkGA1UEBhMCRlIxFjAUBgNV -BAgTDUlsZS1kZS1GcmFuY2UxDjAMBgNVBAcTBVBhcmlzMREwDwYDVQQKEwhQSFAt -SFRUUDEWMBQGA1UEAxMNc29ja2V0LXNlcnZlcjAeFw0xNjA3MTgxMDAwMjhaFw0x -NjA4MTcxMDAwMjhaMGExCzAJBgNVBAYTAkZSMRYwFAYDVQQIEw1JbGUtZGUtRnJh -bmNlMQ4wDAYDVQQHEwVQYXJpczERMA8GA1UEChMIUEhQLUhUVFAxFzAVBgNVBAMT -DnNvY2tldC1hZGFwdGVyMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZHYLV -1mpoiG2AW5VebIB0nCaT8EIryeUk57swa5nqW9fj/KD4hIaoz19Ivt7/LrVTCOag -jFiwEcm4kEC8h0DJvW2IKNDWIpObHBSFFFOlLxhJkq+US1CJjKgSDvU5X3ElXb99 -L1EHN/+M8ZA4Qw3fFAbO2ajENeoxHH9KKZCQLQIDAQABMA0GCSqGSIb3DQEBBQUA -A4GBAM1TFIPTwbG0x//yMhPD9ZGWb2mYfA00Lri198YVmG61PtgDI5hG2dxaDVDa -6Ig6DnrwTBMc51p+NiTgci3OqnJ4FuwBdY5d1moyQre0MHr+sg8STJr5tmAEithL -GUGq3Uv3RuN3OetaPrlxaccJM1nB63caoH5Rl+VqAsJ4S4LI ------END CERTIFICATE----- diff --git a/tests/server/ssl/server.req b/tests/server/ssl/server.req deleted file mode 100644 index cc19ec7..0000000 --- a/tests/server/ssl/server.req +++ /dev/null @@ -1,11 +0,0 @@ ------BEGIN CERTIFICATE REQUEST----- -MIIBoTCCAQoCAQAwYTELMAkGA1UEBhMCRlIxFjAUBgNVBAgTDUlsZS1kZS1GcmFu -Y2UxDjAMBgNVBAcTBVBhcmlzMREwDwYDVQQKEwhQSFAtSFRUUDEXMBUGA1UEAxMO -c29ja2V0LWFkYXB0ZXIwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANkdgtXW -amiIbYBblV5sgHScJpPwQivJ5STnuzBrmepb1+P8oPiEhqjPX0i+3v8utVMI5qCM -WLARybiQQLyHQMm9bYgo0NYik5scFIUUU6UvGEmSr5RLUImMqBIO9TlfcSVdv30v -UQc3/4zxkDhDDd8UBs7ZqMQ16jEcf0opkJAtAgMBAAGgADANBgkqhkiG9w0BAQUF -AAOBgQCWD+DqC2HAiKCZh5j1QdKbzVF2rNV0Gfn2RM/cswMGHEGQyqeI1JH6R3ec -Reo7XG7Sx/qswpr8SBe7TrgGhgHm1U9757+Vw2U64Mvm0m6MvW5+PIwvr1k+f1iD -zJsvFhdS0ijRKTerAhBO1rpw1A1O7a8zJP/g0mie2OE8PnWQPQ== ------END CERTIFICATE REQUEST----- diff --git a/tests/server/tcp-bugous-server.php b/tests/server/tcp-bugous-server.php index 72b0946..723a6eb 100644 --- a/tests/server/tcp-bugous-server.php +++ b/tests/server/tcp-bugous-server.php @@ -1,6 +1,9 @@ [ - 'local_cert' => __DIR__ . '/ssl/server-and-key.pem', - 'cafile' => __DIR__ . '/ssl/ca.pem', - 'capture_peer_cert' => true - ] + 'local_cert' => __DIR__.'/ssl/server-and-key.pem', + 'cafile' => __DIR__.'/ssl/ca.pem', + 'capture_peer_cert' => true, + ], ]); -$socketServer = stream_socket_server('tcp://127.0.0.1:19999', $errNo, $errStr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context); +$socketServer = stream_socket_server('tcp://127.0.0.1:19999', $errNo, $errStr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context); stream_socket_enable_crypto($socketServer, false); -$client = stream_socket_accept($socketServer); +$client = stream_socket_accept($socketServer); stream_set_blocking($client, true); -stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_TLS_SERVER); +stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_TLSv1_2_SERVER); // Verify client certificate $name = null; if (isset(stream_context_get_options($context)['ssl']['peer_certificate'])) { $client_cert = stream_context_get_options($context)['ssl']['peer_certificate']; - $name = openssl_x509_parse($client_cert)["subject"]["CN"]; + $name = openssl_x509_parse($client_cert)['subject']['CN']; } -if ($name == "socket-adapter-client") { +if ('socket-adapter-client' == $name) { fwrite($client, str_replace("\n", "\r\n", << [ - 'local_cert' => __DIR__ . '/ssl/server-and-key.pem' - ] + 'local_cert' => __DIR__.'/ssl/server-and-key.pem', + ], ]); -$socketServer = stream_socket_server('tcp://127.0.0.1:19999', $errNo, $errStr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context); +$socketServer = stream_socket_server('tcp://127.0.0.1:19999', $errNo, $errStr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context); stream_socket_enable_crypto($socketServer, false); -$client = stream_socket_accept($socketServer); +$client = stream_socket_accept($socketServer); stream_set_blocking($client, true); -if (@stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_TLS_SERVER)) { +if (@stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_TLSv1_2_SERVER)) { fwrite($client, str_replace("\n", "\r\n", <<