diff --git a/src/Client.php b/src/Client.php index 4ab7bc5..6422a11 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,8 +2,10 @@ namespace Http\Client\Socket; -use Http\Client\Exception\NetworkException; use Http\Client\HttpClient; +use Http\Client\Socket\Exception\ConnectionException; +use Http\Client\Socket\Exception\InvalidRequestException; +use Http\Client\Socket\Exception\SSLConnectionException; use Http\Discovery\MessageFactoryDiscovery; use Http\Message\ResponseFactory; use Psr\Http\Message\RequestInterface; @@ -98,7 +100,7 @@ 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 + * @throws ConnectionException|SSLConnectionException When the connection fail * * @return resource Socket resource */ @@ -109,15 +111,13 @@ protected function createSocket(RequestInterface $request, $remote, $useSsl) $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); + throw new ConnectionException($errMsg, $request); } stream_set_timeout($socket, 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']), $request); } return $socket; @@ -163,18 +163,18 @@ protected function configure(array $config = []) * * @param RequestInterface $request * - * @throws NetworkException When no remote can be determined from the request + * @throws InvalidRequestException When no remote can be determined from the request * * @return string */ 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() ?: ($request->getUri()->getScheme() === 'https' ? 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..58eb1bb --- /dev/null +++ b/src/Exception/BrokenPipeException.php @@ -0,0 +1,9 @@ +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()) { @@ -41,8 +41,7 @@ protected function writeRequest($socket, RequestInterface $request, $bufferSize * @param RequestInterface $request * @param int $bufferSize * - * @throws \Http\Client\Exception\NetworkException - * @throws \Http\Client\Exception\RequestException + * @throws BrokenPipeException */ protected function writeBody($socket, RequestInterface $request, $bufferSize = 8192) { @@ -56,7 +55,7 @@ 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); } } } diff --git a/src/ResponseReader.php b/src/ResponseReader.php index 202d1bd..6826078 100644 --- a/src/ResponseReader.php +++ b/src/ResponseReader.php @@ -2,7 +2,8 @@ namespace Http\Client\Socket; -use Http\Client\Exception\NetworkException; +use Http\Client\Socket\Exception\BrokenPipeException; +use Http\Client\Socket\Exception\TimeoutException; use Http\Message\ResponseFactory; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -27,7 +28,8 @@ trait ResponseReader * @param RequestInterface $request * @param resource $socket * - * @throws NetworkException When the response cannot be read + * @throws TimeoutException When the socket timed out + * @throws BrokenPipeException When the response cannot be read * * @return ResponseInterface */ @@ -46,13 +48,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); } $parts = explode(' ', array_shift($headers), 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);