From b6ae251998201e27406008c5e8d43c4d9d1f5d3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rk=20S=C3=A1gi-Kaz=C3=A1r?= Date: Tue, 26 Jan 2016 16:54:59 +0100 Subject: [PATCH 1/2] Update package files --- .editorconfig | 3 -- .gitattributes | 9 +++- .gitignore | 4 +- .php_cs | 13 +++++ .scrutinizer.yml | 13 ++--- .styleci.yml | 10 ++++ .travis.yml | 49 ++++++++++--------- CONDUCT.md | 22 --------- CONTRIBUTING | 1 + CONTRIBUTING.md | 84 --------------------------------- LICENSE | 2 +- README.md | 19 ++++---- phpunit.xml => phpunit.xml.dist | 0 13 files changed, 79 insertions(+), 150 deletions(-) create mode 100644 .php_cs create mode 100644 .styleci.yml delete mode 100644 CONDUCT.md create mode 100644 CONTRIBUTING delete mode 100644 CONTRIBUTING.md rename phpunit.xml => phpunit.xml.dist (100%) diff --git a/.editorconfig b/.editorconfig index bb6d986..677e36e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,6 +7,3 @@ indent_size = 4 indent_style = space insert_final_newline = true trim_trailing_whitespace = true - -[*.yml*] -indent_size = 2 diff --git a/.gitattributes b/.gitattributes index daa7a29..5dd7941 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,6 +1,13 @@ +spec/ export-ignore +tests/ export-ignore .editorconfig export-ignore .gitattributes export-ignore .gitignore export-ignore +.php_cs export-ignore .scrutinizer.yml export-ignore +.styleci.yml export-ignore .travis.yml export-ignore -CONTRIBUTING.md export-ignore +CONTRIBUTING export-ignore +phpspec.yml.ci export-ignore +phpspec.yml.dist export-ignore +phpunit.xml.dist export-ignore diff --git a/.gitignore b/.gitignore index 02856bc..da734f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ -.puli +.puli/ build/ vendor/ composer.lock +phpspec.yml +phpunit.xml diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..23ba165 --- /dev/null +++ b/.php_cs @@ -0,0 +1,13 @@ + /dev/null 2>&1 & +before_script: + - vendor/bin/http_test_server > /dev/null 2>&1 & script: - - $TEST_COMMAND - - ./vendor/bin/phpunit tests/SocketClientFeatureTest.php --printer Http\\Client\\Tests\\FeatureTestListener || echo "" + - $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 + - 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/CONDUCT.md b/CONDUCT.md deleted file mode 100644 index 01b8644..0000000 --- a/CONDUCT.md +++ /dev/null @@ -1,22 +0,0 @@ -# Contributor Code of Conduct - -As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. - -We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality. - -Examples of unacceptable behavior by participants include: - -* The use of sexualized language or imagery -* Personal attacks -* Trolling or insulting/derogatory comments -* Public or private harassment -* Publishing other's private information, such as physical or electronic addresses, without explicit permission -* Other unethical or unprofessional conduct. - -Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. - -This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. - -Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. - -This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) diff --git a/CONTRIBUTING b/CONTRIBUTING new file mode 100644 index 0000000..841914a --- /dev/null +++ b/CONTRIBUTING @@ -0,0 +1 @@ +Please see http://docs.php-http.org/en/latest/development/contributing.html diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index ee0eb03..0000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,84 +0,0 @@ -# Contributing - -If you're here, you would like to contribute to this repository and you're really welcome! - - -## Bug reports - -If you find a bug or a documentation issue, please report it or even better: fix it :). If you report it, -please be as precise as possible. Here is a little list of required information: - - - Precise description of the bug - - Details of your environment (for example: OS, PHP version, installed extensions) - - Backtrace which might help identifing the bug - - -## Security issues - -If you discover any security related issues, please contact us at [security@php-http.org](mailto:security@php-http.org) instead of submitting an issue on Github. This allows us to fix the issue and release a security hotfix without publicly disclosing the vulnerability. - - -## Feature requests - -If you think a feature is missing, please report it or even better: implement it :). If you report it, describe the more -precisely what you would like to see implemented and we will discuss what is the best approach for it. If you can do -some research before submitting it and link the resources to your description, you're awesome! It will allow us to more -easily understood/implement it. - - -## Sending a Pull Request - -If you're here, you are going to fix a bug or implement a feature and you're the best! To do it, first fork the repository, clone it and create a new branch with the following commands: - -``` bash -$ git clone git@github.com:your-name/socket-client.git -$ git checkout -b feature-or-bug-fix-description -``` - -Then install the dependencies through [Composer](https://getcomposer.org/): - -``` bash -$ composer install -``` - -Write code and tests. When you are ready, run the tests. (This is usually [PHPUnit](http://phpunit.de/) or [PHPSpec](http://phpspec.net/)) - -``` bash -$ composer test -``` - -When you are ready with the code, tested it and documented it, you can commit and push it with the following commands: - -``` bash -$ git commit -m "Feature or bug fix description" -$ git push origin feature-or-bug-fix-description -``` - -**Note:** Please write your commit messages in the imperative and follow the [guidelines](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html) for clear and concise messages. - -Then [create a pull request](https://help.github.com/articles/creating-a-pull-request/) on GitHub. - -Please make sure that each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting with the following commands (here, we assume you would like to squash 3 commits in a single one): - -``` bash -$ git rebase -i HEAD~3 -``` - -If your branch conflicts with the master branch, you will need to rebase and repush it with the following commands: - -``` bash -$ git remote add upstream git@github.com:php-http/repo-name.git -$ git pull --rebase upstream master -$ git push -f origin feature-or-bug-fix-description -``` - - -## Coding standard - -This repository follows the [PSR-2 standard](http://www.php-fig.org/psr/psr-2/) and so, if you want to contribute, -you must follow these rules. - - -## Semver - -We are trying to follow [semver](http://semver.org/). When you are making BC breaking changes, please let us know why you think it is important. In this case, your patch can only be included in the next major version. diff --git a/LICENSE b/LICENSE index 8e2c4a0..4558d6f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2015 PHP HTTP Team +Copyright (c) 2015-2016 PHP HTTP Team Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 5f82bb6..e96ca90 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ $ composer require php-http/socket-client * TLS / SSL Encyrption * Client Certificate (only for php > 5.6) + ## Usage The SocketHttpClient class need a [message factory](https://github.com/php-http/message-factory) in order to work: @@ -35,27 +36,28 @@ $client = new new Http\Socket\SocketHttpClient($messageFactory, $options); The `$options` array allow to configure the socket client. + ## Options Here is the list of available options: * remote_socket: Specify the remote socket where the library should send the request to - + Can be a tcp remote : tcp://hostname:port - + Can be a unix remote : unix://hostname:port - + Do not use a tls / ssl scheme, this is handle by the ssl option. - + If not set, the client will try to determine it from the request uri or host header. - + * timeout : Timeout in __milliseconds__ for writing request and reading response on the remote * ssl : Activate or deactivate the ssl / tls encryption * stream_context_options : Custom options for the context of the stream, same as [PHP stream context options](http://php.net/manual/en/context.php) - + As an example someone may want to pass a client certificate when using the ssl, a valid configuration for this use case would be: - + ```php $options = [ 'stream_context_options' => [ @@ -71,6 +73,7 @@ Here is the list of available options: * write_buffer_size : When sending the request we need to bufferize the body, this option specify the size of this buffer, default is 8192, if you are sending big file with your client it may be interesting to have a bigger value in order to increase performance. + ## Testing First launch the http server: @@ -88,7 +91,7 @@ $ composer test ## Contributing -Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details. +Please see our [contributing guide](http://docs.php-http.org/en/latest/development/contributing.html). ## Security diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 100% rename from phpunit.xml rename to phpunit.xml.dist From 5583ef10fd14820299f773511773ce0ef1dca9ac Mon Sep 17 00:00:00 2001 From: sagikazarmark Date: Tue, 26 Jan 2016 15:27:33 -0500 Subject: [PATCH 2/2] Applied fixes from StyleCI --- src/Client.php | 43 +++++++++++++++++---------------- src/RequestWriter.php | 22 ++++++++--------- src/ResponseReader.php | 19 +++++++-------- src/Stream.php | 54 +++++++++++++++++++++--------------------- 4 files changed, 69 insertions(+), 69 deletions(-) diff --git a/src/Client.php b/src/Client.php index ba04f07..5460eb8 100644 --- a/src/Client.php +++ b/src/Client.php @@ -12,7 +12,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; /** - * Socket Http Client + * Socket Http Client. * * Use stream and socket capabilities of the core of PHP to send HTTP requests * @@ -24,20 +24,21 @@ class Client implements HttpClient use ResponseReader; private $config = [ - 'remote_socket' => null, - 'timeout' => null, - 'stream_context_options' => array(), - 'stream_context_param' => array(), - 'ssl' => null, - 'write_buffer_size' => 8192, - 'ssl_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT + 'remote_socket' => null, + 'timeout' => null, + 'stream_context_options' => [], + 'stream_context_param' => [], + 'ssl' => null, + 'write_buffer_size' => 8192, + 'ssl_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT, ]; /** * Constructor. * * @param ResponseFactory $responseFactory Response factory for creating response - * @param array $config { + * @param array $config { + * * @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 @@ -68,8 +69,8 @@ public function __construct(ResponseFactory $responseFactory = null, array $conf */ public function sendRequest(RequestInterface $request) { - $remote = $this->config['remote_socket']; - $useSsl = $this->config['ssl']; + $remote = $this->config['remote_socket']; + $useSsl = $this->config['ssl']; if (!$request->hasHeader('Connection')) { $request = $request->withHeader('Connection', 'close'); @@ -98,11 +99,11 @@ public function sendRequest(RequestInterface $request) } /** - * Create the socket to write request and read response on it + * Create the socket to write request and read response on it. * * @param RequestInterface $request Request for * @param string $remote Entrypoint for the connection - * @param boolean $useSsl Whether to use ssl or not + * @param bool $useSsl Whether to use ssl or not * * @throws NetworkException When the connection fail * @@ -110,7 +111,7 @@ public function sendRequest(RequestInterface $request) */ protected function createSocket(RequestInterface $request, $remote, $useSsl) { - $errNo = null; + $errNo = null; $errMsg = null; $socket = @stream_socket_client($remote, $errNo, $errMsg, floor($this->config['timeout'] / 1000), STREAM_CLIENT_CONNECT, $this->config['stream_context']); @@ -130,7 +131,7 @@ protected function createSocket(RequestInterface $request, $remote, $useSsl) } /** - * Close the socket, used when having an error + * Close the socket, used when having an error. * * @param resource $socket */ @@ -140,7 +141,7 @@ protected function closeSocket($socket) } /** - * Return configuration for the socket client + * Return configuration for the socket client. * * @param array $config Configuration from user * @@ -165,7 +166,7 @@ protected function configure(array $config = []) } /** - * Return remote socket from the request + * Return remote socket from the request. * * @param RequestInterface $request * @@ -175,13 +176,13 @@ protected function configure(array $config = []) */ 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->getUri()->getHost() == '' && !$request->hasHeader('Host')) { + throw new NetworkException('Cannot find connection endpoint for this request', $request); } $host = $request->getUri()->getHost(); - $port = $request->getUri()->getPort() ?: ($request->getUri()->getScheme() == "https" ? 443 : 80); - $endpoint = sprintf("%s:%s", $host, $port); + $port = $request->getUri()->getPort() ?: ($request->getUri()->getScheme() == 'https' ? 443 : 80); + $endpoint = sprintf('%s:%s', $host, $port); // If use the host header if present for the endpoint if (empty($host) && $request->hasHeader('Host')) { diff --git a/src/RequestWriter.php b/src/RequestWriter.php index 0a82950..57d6fa8 100644 --- a/src/RequestWriter.php +++ b/src/RequestWriter.php @@ -6,7 +6,7 @@ use Psr\Http\Message\RequestInterface; /** - * Method for writing request + * Method for writing request. * * Mainly used by SocketHttpClient * @@ -15,18 +15,18 @@ trait RequestWriter { /** - * Write a request to a socket + * Write a request to a socket. * * @param resource $socket * @param RequestInterface $request - * @param integer $bufferSize + * @param int $bufferSize * * @throws \Http\Client\Exception\NetworkException */ protected function writeRequest($socket, RequestInterface $request, $bufferSize = 8192) { if (false === $this->fwrite($socket, $this->transformRequestHeadersToString($request))) { - throw new NetworkException("Failed to send request, underlying socket not accessible, (BROKEN EPIPE)", $request); + throw new NetworkException('Failed to send request, underlying socket not accessible, (BROKEN EPIPE)', $request); } if ($request->getBody()->isReadable()) { @@ -35,11 +35,11 @@ protected function writeRequest($socket, RequestInterface $request, $bufferSize } /** - * Write Body of the request + * Write Body of the request. * * @param resource $socket * @param RequestInterface $request - * @param integer $bufferSize + * @param int $bufferSize * * @throws \Http\Client\Exception\NetworkException * @throws \Http\Client\Exception\RequestException @@ -56,13 +56,13 @@ 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 NetworkException('An error occur when writing request to client (BROKEN EPIPE)', $request); } } } /** - * Produce the header of request as a string based on a PSR Request + * Produce the header of request as a string based on a PSR Request. * * @param RequestInterface $request * @@ -70,10 +70,10 @@ protected function writeBody($socket, RequestInterface $request, $bufferSize = 8 */ protected function transformRequestHeadersToString(RequestInterface $request) { - $message = vsprintf('%s %s HTTP/%s', [ + $message = vsprintf('%s %s HTTP/%s', [ strtoupper($request->getMethod()), $request->getRequestTarget(), - $request->getProtocolVersion() + $request->getProtocolVersion(), ])."\r\n"; foreach ($request->getHeaders() as $name => $values) { @@ -86,7 +86,7 @@ protected function transformRequestHeadersToString(RequestInterface $request) } /** - * Replace fwrite behavior as api is broken in PHP + * Replace fwrite behavior as api is broken in PHP. * * @see https://secure.phabricator.com/rPHU69490c53c9c2ef2002bc2dd4cecfe9a4b080b497 * diff --git a/src/ResponseReader.php b/src/ResponseReader.php index c704821..202d1bd 100644 --- a/src/ResponseReader.php +++ b/src/ResponseReader.php @@ -3,13 +3,12 @@ namespace Http\Client\Socket; use Http\Client\Exception\NetworkException; -use Http\Message\MessageFactory; use Http\Message\ResponseFactory; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** - * Method for reading response + * Method for reading response. * * Mainly used by SocketHttpClient * @@ -23,7 +22,7 @@ trait ResponseReader protected $responseFactory; /** - * Read a response from a socket + * Read a response from a socket. * * @param RequestInterface $request * @param resource $socket @@ -34,8 +33,8 @@ trait ResponseReader */ protected function readResponse(RequestInterface $request, $socket) { - $headers = []; - $reason = null; + $headers = []; + $reason = null; while (($line = fgets($socket)) !== false) { if (rtrim($line) === '') { @@ -47,7 +46,7 @@ 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 NetworkException('Error while reading response, stream timed out', $request); } $parts = explode(' ', array_shift($headers), 3); @@ -57,7 +56,7 @@ protected function readResponse(RequestInterface $request, $socket) } $protocol = substr($parts[0], -3); - $status = $parts[1]; + $status = $parts[1]; if (isset($parts[2])) { $reason = $parts[2]; @@ -79,13 +78,13 @@ protected function readResponse(RequestInterface $request, $socket) } $response = $this->responseFactory->createResponse($status, $reason, $responseHeaders, null, $protocol); - $stream = $this->createStream($socket, $response); + $stream = $this->createStream($socket, $response); return $response->withBody($stream); } /** - * Create the stream + * Create the stream. * * @param $socket * @param ResponseInterface $response @@ -97,7 +96,7 @@ protected function createStream($socket, ResponseInterface $response) $size = null; if ($response->hasHeader('Content-Length')) { - $size = (int)$response->getHeaderLine('Content-Length'); + $size = (int) $response->getHeaderLine('Content-Length'); } return new Stream($socket, $size); diff --git a/src/Stream.php b/src/Stream.php index 8dd3674..062e784 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -7,7 +7,7 @@ use Psr\Http\Message\StreamInterface; /** - * Stream implementation for Socket Client + * Stream implementation for Socket Client. * * This implementation is used to have a Stream which react better to the Socket Client behavior. * @@ -43,27 +43,27 @@ class Stream implements StreamInterface private $readed = 0; /** - * Create the stream + * Create the stream. * * @param resource $socket - * @param integer $size + * @param int $size */ public function __construct($socket, $size = null) { $this->socket = $socket; - $this->size = $size; + $this->size = $size; } /** - * {@inheritDoc} + * {@inheritdoc} */ public function __toString() { - return (string)$this->getContents(); + return (string) $this->getContents(); } /** - * {@inheritDoc} + * {@inheritdoc} */ public function close() { @@ -71,7 +71,7 @@ public function close() } /** - * {@inheritDoc} + * {@inheritdoc} */ public function detach() { @@ -83,7 +83,7 @@ public function detach() } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getSize() { @@ -91,7 +91,7 @@ public function getSize() } /** - * {@inheritDoc} + * {@inheritdoc} */ public function tell() { @@ -99,7 +99,7 @@ public function tell() } /** - * {@inheritDoc} + * {@inheritdoc} */ public function eof() { @@ -107,7 +107,7 @@ public function eof() } /** - * {@inheritDoc} + * {@inheritdoc} */ public function isSeekable() { @@ -115,23 +115,23 @@ public function isSeekable() } /** - * {@inheritDoc} + * {@inheritdoc} */ public function seek($offset, $whence = SEEK_SET) { - throw new StreamException("This stream is not seekable"); + throw new StreamException('This stream is not seekable'); } /** - * {@inheritDoc} + * {@inheritdoc} */ public function rewind() { - throw new StreamException("This stream is not seekable"); + throw new StreamException('This stream is not seekable'); } /** - * {@inheritDoc} + * {@inheritdoc} */ public function isWritable() { @@ -139,15 +139,15 @@ public function isWritable() } /** - * {@inheritDoc} + * {@inheritdoc} */ public function write($string) { - throw new StreamException("This stream is not writable"); + throw new StreamException('This stream is not writable'); } /** - * {@inheritDoc} + * {@inheritdoc} */ public function isReadable() { @@ -155,7 +155,7 @@ public function isReadable() } /** - * {@inheritDoc} + * {@inheritdoc} */ public function read($length) { @@ -164,18 +164,18 @@ public function read($length) } if ($this->getSize() < ($this->readed + $length)) { - throw new StreamException("Cannot read more than %s", $this->getSize() - $this->readed); + throw new StreamException('Cannot read more than %s', $this->getSize() - $this->readed); } if ($this->getSize() === $this->readed) { - return ""; + return ''; } // Even if we request a length a non blocking stream can return less data than asked $read = fread($this->socket, $length); if ($this->getMetadata('timed_out')) { - throw new TimeoutException("Stream timed out while reading data"); + throw new TimeoutException('Stream timed out while reading data'); } $this->readed += strlen($read); @@ -184,7 +184,7 @@ public function read($length) } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getContents() { @@ -192,7 +192,7 @@ public function getContents() return stream_get_contents($this->socket); } - $contents = ""; + $contents = ''; do { $contents .= $this->read($this->getSize() - $this->readed); @@ -202,7 +202,7 @@ public function getContents() } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getMetadata($key = null) {