From b1cb966898aaf8df37280fde537a27b6724b3bc4 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 2 Oct 2020 15:32:43 +0200 Subject: [PATCH] [HttpClient] Always "buffer" empty responses --- Response/ResponseTrait.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Response/ResponseTrait.php b/Response/ResponseTrait.php index 3928343..9f457d3 100644 --- a/Response/ResponseTrait.php +++ b/Response/ResponseTrait.php @@ -115,15 +115,13 @@ public function getContent(bool $throw = true): string return $content; } - if ('HEAD' === $this->info['http_method'] || \in_array($this->info['http_code'], [204, 304], true)) { - return ''; + if (null === $this->content) { + throw new TransportException('Cannot get the content of the response twice: buffering is disabled.'); + } + } else { + foreach (self::stream([$this]) as $chunk) { + // Chunks are buffered in $this->content already } - - throw new TransportException('Cannot get the content of the response twice: buffering is disabled.'); - } - - foreach (self::stream([$this]) as $chunk) { - // Chunks are buffered in $this->content already } rewind($this->content); @@ -376,6 +374,10 @@ public static function stream(iterable $responses, float $timeout = null): \Gene $chunk = new ErrorChunk($response->offset, $e); } else { + if (0 === $response->offset && null === $response->content) { + $response->content = fopen('php://memory', 'w+'); + } + $chunk = new LastChunk($response->offset); } } elseif ($chunk instanceof ErrorChunk) {