Skip to content

Commit

Permalink
[HttpClient] Fix processing a NativeResponse after its client has bee…
Browse files Browse the repository at this point in the history
…n reset
  • Loading branch information
Jean-Beru authored and nicolas-grekas committed Jan 28, 2025
1 parent 05f4f87 commit 394b440
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Response/NativeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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 {
Expand All @@ -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']) {
Expand All @@ -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();
Expand Down Expand Up @@ -294,15 +294,15 @@ 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.');
}
}

$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]);
Expand Down
13 changes: 13 additions & 0 deletions Tests/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 394b440

Please sign in to comment.