From f17fc1802037ef5f323206e4e891bf12f197154b Mon Sep 17 00:00:00 2001 From: "Phil E. Taylor" Date: Fri, 29 Nov 2024 16:35:04 +0000 Subject: [PATCH 1/3] [HttpClient] Fix Undefined array key "connection" #59044 Signed-off-by: Phil E. Taylor --- Response/CurlResponse.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Response/CurlResponse.php b/Response/CurlResponse.php index 4cb2a30..88cb764 100644 --- a/Response/CurlResponse.php +++ b/Response/CurlResponse.php @@ -312,7 +312,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; From 05f4f876ee0c42161f1155ee974a5c6604247494 Mon Sep 17 00:00:00 2001 From: Kurt Thiemann Date: Sat, 16 Nov 2024 19:21:25 +0100 Subject: [PATCH 2/3] [HttpClient] Ignore RuntimeExceptions thrown when rewinding the PSR-7 created in HttplugWaitLoop::createPsr7Response --- HttplugClient.php | 12 ++++++++++-- Internal/HttplugWaitLoop.php | 6 +++++- Psr18Client.php | 12 ++++++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/HttplugClient.php b/HttplugClient.php index b0a6d4a..b01579d 100644 --- a/HttplugClient.php +++ b/HttplugClient.php @@ -202,7 +202,11 @@ public function createStream($content = ''): StreamInterface } if ($stream->isSeekable()) { - $stream->seek(0); + try { + $stream->seek(0); + } catch (\RuntimeException) { + // ignore + } } return $stream; @@ -274,7 +278,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 bebe135..1412fcf 100644 --- a/Internal/HttplugWaitLoop.php +++ b/Internal/HttplugWaitLoop.php @@ -145,7 +145,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; From 394b440934056b8d9d6ba250001458e9d7998b7f Mon Sep 17 00:00:00 2001 From: "hubert.lenoir" Date: Mon, 27 Jan 2025 14:33:54 +0100 Subject: [PATCH 3/3] [HttpClient] Fix processing a NativeResponse after its client has been reset --- Response/NativeResponse.php | 12 ++++++------ Tests/HttpClientTestCase.php | 13 +++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Response/NativeResponse.php b/Response/NativeResponse.php index 7735070..9a5184e 100644 --- a/Response/NativeResponse.php +++ b/Response/NativeResponse.php @@ -79,7 +79,7 @@ public function __construct(NativeClientState $multi, $context, string $url, arr }; $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 79763bc..3b83d82 100644 --- a/Tests/HttpClientTestCase.php +++ b/Tests/HttpClientTestCase.php @@ -695,4 +695,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); + } }