From 1ffeba3f09f226216cbd12e980224f34cbf79e6d Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 6 Jul 2020 20:00:25 +0200 Subject: [PATCH] [HttpClient] fix buffering AsyncResponse with no passthru --- .../HttpClient/Response/AsyncResponse.php | 4 ++++ .../HttpClient/Tests/AsyncDecoratorTraitTest.php | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Symfony/Component/HttpClient/Response/AsyncResponse.php b/src/Symfony/Component/HttpClient/Response/AsyncResponse.php index 3d84cc707e..664a273fef 100644 --- a/src/Symfony/Component/HttpClient/Response/AsyncResponse.php +++ b/src/Symfony/Component/HttpClient/Response/AsyncResponse.php @@ -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; diff --git a/src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php b/src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php index a2f43a8314..de65440e94 100644 --- a/src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php +++ b/src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php @@ -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()); + } }