bug #37506 [HttpClient] fix buffering AsyncResponse with no passthru (nicolas-grekas)

This PR was merged into the 5.2-dev branch.

Discussion
----------

[HttpClient] fix buffering AsyncResponse with no passthru

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Commits
-------

1ffeba3f09 [HttpClient] fix buffering AsyncResponse with no passthru
This commit is contained in:
Nicolas Grekas 2020-07-08 09:52:00 +02:00
commit e67c6d8886
2 changed files with 18 additions and 0 deletions

View File

@ -209,6 +209,10 @@ final class AsyncResponse implements ResponseInterface, StreamableInterface
if (!$r->passthru) {
if (null !== $chunk->getError() || $chunk->isLast()) {
unset($asyncMap[$response]);
} elseif (null !== $r->content && '' !== ($content = $chunk->getContent()) && \strlen($content) !== fwrite($r->content, $content)) {
$chunk = new ErrorChunk($r->offset, new TransportException(sprintf('Failed writing %d bytes to the response buffer.', \strlen($content))));
$r->info['error'] = $chunk->getError();
$r->response->cancel();
}
yield $r => $chunk;

View File

@ -182,4 +182,18 @@ class AsyncDecoratorTraitTest extends NativeHttpClientTest
$this->assertTrue($lastChunk->isLast());
}
public function testBufferPurePassthru()
{
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) {
$context->passthru();
yield $chunk;
});
$response = $client->request('GET', 'http://localhost:8057/');
$this->assertStringContainsString('SERVER_PROTOCOL', $response->getContent());
$this->assertStringContainsString('HTTP_HOST', $response->getContent());
}
}