Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpClient] Fix processing a NativeResponse after its client has been reset #59631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Symfony/Component/HttpClient/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 src/Symfony/Component/HttpClient/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);
}
}