diff --git a/HttplugClient.php b/HttplugClient.php index 67cf827..f717369 100644 --- a/HttplugClient.php +++ b/HttplugClient.php @@ -226,7 +226,11 @@ private function sendPsr7Request(RequestInterface $request, ?bool $buffer = null $body = $request->getBody(); if ($body->isSeekable()) { - $body->seek(0); + try { + $body->seek(0); + } catch (\RuntimeException) { + // ignore + } } $options = [ diff --git a/Internal/HttplugWaitLoop.php b/Internal/HttplugWaitLoop.php index aa172b8..36c4efc 100644 --- a/Internal/HttplugWaitLoop.php +++ b/Internal/HttplugWaitLoop.php @@ -140,7 +140,11 @@ public static function createPsr7Response(ResponseFactoryInterface $responseFact } if ($body->isSeekable()) { - $body->seek(0); + try { + $body->seek(0); + } catch (\RuntimeException) { + // ignore + } } return $psrResponse->withBody($body); diff --git a/Psr18Client.php b/Psr18Client.php index d46a7b1..f138f55 100644 --- a/Psr18Client.php +++ b/Psr18Client.php @@ -90,7 +90,11 @@ public function sendRequest(RequestInterface $request): ResponseInterface $body = $request->getBody(); if ($body->isSeekable()) { - $body->seek(0); + try { + $body->seek(0); + } catch (\RuntimeException) { + // ignore + } } $options = [ @@ -136,7 +140,11 @@ public function createStream(string $content = ''): StreamInterface $stream = $this->streamFactory->createStream($content); if ($stream->isSeekable()) { - $stream->seek(0); + try { + $stream->seek(0); + } catch (\RuntimeException) { + // ignore + } } return $stream; diff --git a/Response/CurlResponse.php b/Response/CurlResponse.php index 2be8fc8..e7f5529 100644 --- a/Response/CurlResponse.php +++ b/Response/CurlResponse.php @@ -316,7 +316,16 @@ private static function perform(ClientState $multi, ?array &$responses = null): } $multi->handlesActivity[$id][] = null; - $multi->handlesActivity[$id][] = \in_array($result, [\CURLE_OK, \CURLE_TOO_MANY_REDIRECTS], true) || '_0' === $waitFor || curl_getinfo($ch, \CURLINFO_SIZE_DOWNLOAD) === curl_getinfo($ch, \CURLINFO_CONTENT_LENGTH_DOWNLOAD) || (curl_error($ch) === 'OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 0' && -1.0 === curl_getinfo($ch, \CURLINFO_CONTENT_LENGTH_DOWNLOAD) && \in_array('close', array_map('strtolower', $responses[$id]->headers['connection']), true)) ? null : new TransportException(ucfirst(curl_error($ch) ?: curl_strerror($result)).sprintf(' for "%s".', curl_getinfo($ch, \CURLINFO_EFFECTIVE_URL))); + $multi->handlesActivity[$id][] = \in_array($result, [\CURLE_OK, \CURLE_TOO_MANY_REDIRECTS], true) + || '_0' === $waitFor + || curl_getinfo($ch, \CURLINFO_SIZE_DOWNLOAD) === curl_getinfo($ch, \CURLINFO_CONTENT_LENGTH_DOWNLOAD) + || ('C' === $waitFor[0] + && 'OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 0' === curl_error($ch) + && -1.0 === curl_getinfo($ch, \CURLINFO_CONTENT_LENGTH_DOWNLOAD) + && \in_array('close', array_map('strtolower', $responses[$id]->headers['connection'] ?? []), true) + ) + ? null + : new TransportException(ucfirst(curl_error($ch) ?: curl_strerror($result)).\sprintf(' for "%s".', curl_getinfo($ch, \CURLINFO_EFFECTIVE_URL))); } } finally { $multi->performing = false; diff --git a/Response/NativeResponse.php b/Response/NativeResponse.php index af7b25f..3ef310f 100644 --- a/Response/NativeResponse.php +++ b/Response/NativeResponse.php @@ -79,7 +79,7 @@ public function __construct( }; $this->canary = new Canary(static function () use ($multi, $id) { - if (null !== ($host = $multi->openHandles[$id][6] ?? null) && 0 >= --$multi->hosts[$host]) { + if (null !== ($host = $multi->openHandles[$id][6] ?? null) && isset($multi->hosts[$host]) && 0 >= --$multi->hosts[$host]) { unset($multi->hosts[$host]); } unset($multi->openHandles[$id], $multi->handlesActivity[$id]); @@ -123,7 +123,7 @@ private function open(): void throw new TransportException($msg); } - $this->logger?->info(sprintf('%s for "%s".', $msg, $url ?? $this->url)); + $this->logger?->info(\sprintf('%s for "%s".', $msg, $url ?? $this->url)); }); try { @@ -142,7 +142,7 @@ private function open(): void $this->info['request_header'] = $this->info['url']['path'].$this->info['url']['query']; } - $this->info['request_header'] = sprintf("> %s %s HTTP/%s \r\n", $context['http']['method'], $this->info['request_header'], $context['http']['protocol_version']); + $this->info['request_header'] = \sprintf("> %s %s HTTP/%s \r\n", $context['http']['method'], $this->info['request_header'], $context['http']['protocol_version']); $this->info['request_header'] .= implode("\r\n", $context['http']['header'])."\r\n\r\n"; if (\array_key_exists('peer_name', $context['ssl']) && null === $context['ssl']['peer_name']) { @@ -159,7 +159,7 @@ private function open(): void break; } - $this->logger?->info(sprintf('Redirecting: "%s %s"', $this->info['http_code'], $url ?? $this->url)); + $this->logger?->info(\sprintf('Redirecting: "%s %s"', $this->info['http_code'], $url ?? $this->url)); } } catch (\Throwable $e) { $this->close(); @@ -294,7 +294,7 @@ private static function perform(ClientState $multi, ?array &$responses = null): if (null === $e) { if (0 < $remaining) { - $e = new TransportException(sprintf('Transfer closed with %s bytes remaining to read.', $remaining)); + $e = new TransportException(\sprintf('Transfer closed with %s bytes remaining to read.', $remaining)); } elseif (-1 === $remaining && fwrite($buffer, '-') && '' !== stream_get_contents($buffer, -1, 0)) { $e = new TransportException('Transfer closed with outstanding data remaining from chunked response.'); } @@ -302,7 +302,7 @@ private static function perform(ClientState $multi, ?array &$responses = null): $multi->handlesActivity[$i][] = null; $multi->handlesActivity[$i][] = $e; - if (null !== ($host = $multi->openHandles[$i][6] ?? null) && 0 >= --$multi->hosts[$host]) { + if (null !== ($host = $multi->openHandles[$i][6] ?? null) && isset($multi->hosts[$host]) && 0 >= --$multi->hosts[$host]) { unset($multi->hosts[$host]); } unset($multi->openHandles[$i]); diff --git a/Tests/HttpClientTestCase.php b/Tests/HttpClientTestCase.php index 728f10d..32d9287 100644 --- a/Tests/HttpClientTestCase.php +++ b/Tests/HttpClientTestCase.php @@ -700,4 +700,17 @@ public function testPostToGetRedirect(int $status) $this->assertSame('GET', $body['REQUEST_METHOD']); $this->assertSame('/', $body['REQUEST_URI']); } + + public function testResponseCanBeProcessedAfterClientReset() + { + $client = $this->getHttpClient(__FUNCTION__); + $response = $client->request('GET', 'http://127.0.0.1:8057/timeout-body'); + $stream = $client->stream($response); + + $response->getStatusCode(); + $client->reset(); + $stream->current(); + + $this->addToAssertionCount(1); + } }