Skip to content

Commit b1cb966

Browse files
[HttpClient] Always "buffer" empty responses
1 parent 01e10c8 commit b1cb966

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

Response/ResponseTrait.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,13 @@ public function getContent(bool $throw = true): string
115115
return $content;
116116
}
117117

118-
if ('HEAD' === $this->info['http_method'] || \in_array($this->info['http_code'], [204, 304], true)) {
119-
return '';
118+
if (null === $this->content) {
119+
throw new TransportException('Cannot get the content of the response twice: buffering is disabled.');
120+
}
121+
} else {
122+
foreach (self::stream([$this]) as $chunk) {
123+
// Chunks are buffered in $this->content already
120124
}
121-
122-
throw new TransportException('Cannot get the content of the response twice: buffering is disabled.');
123-
}
124-
125-
foreach (self::stream([$this]) as $chunk) {
126-
// Chunks are buffered in $this->content already
127125
}
128126

129127
rewind($this->content);
@@ -376,6 +374,10 @@ public static function stream(iterable $responses, float $timeout = null): \Gene
376374

377375
$chunk = new ErrorChunk($response->offset, $e);
378376
} else {
377+
if (0 === $response->offset && null === $response->content) {
378+
$response->content = fopen('php://memory', 'w+');
379+
}
380+
379381
$chunk = new LastChunk($response->offset);
380382
}
381383
} elseif ($chunk instanceof ErrorChunk) {

0 commit comments

Comments
 (0)