Skip to content

Commit adb7c99

Browse files
bug #59689 [HttpClient] Fix buffering AsyncResponse with no passthru (nicolas-grekas)
This PR was merged into the 6.4 branch. Discussion ---------- [HttpClient] Fix buffering AsyncResponse with no passthru | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #59574 | License | MIT Commits ------- 19a50f7e5fb [HttpClient] Fix buffering AsyncResponse with no passthru
2 parents 144790f + cdc4788 commit adb7c99

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

Response/AsyncResponse.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\HttpClient\Response;
1313

1414
use Symfony\Component\HttpClient\Chunk\ErrorChunk;
15-
use Symfony\Component\HttpClient\Chunk\FirstChunk;
1615
use Symfony\Component\HttpClient\Chunk\LastChunk;
1716
use Symfony\Component\HttpClient\Exception\TransportException;
1817
use Symfony\Contracts\HttpClient\ChunkInterface;
@@ -245,7 +244,7 @@ public static function stream(iterable $responses, ?float $timeout = null, ?stri
245244
$wrappedResponses[] = $r->response;
246245

247246
if ($r->stream) {
248-
yield from self::passthruStream($response = $r->response, $r, new FirstChunk(), $asyncMap);
247+
yield from self::passthruStream($response = $r->response, $r, $asyncMap, new LastChunk());
249248

250249
if (!isset($asyncMap[$response])) {
251250
array_pop($wrappedResponses);
@@ -276,15 +275,9 @@ public static function stream(iterable $responses, ?float $timeout = null, ?stri
276275
}
277276

278277
if (!$r->passthru) {
279-
if (null !== $chunk->getError() || $chunk->isLast()) {
280-
unset($asyncMap[$response]);
281-
} elseif (null !== $r->content && '' !== ($content = $chunk->getContent()) && \strlen($content) !== fwrite($r->content, $content)) {
282-
$chunk = new ErrorChunk($r->offset, new TransportException(sprintf('Failed writing %d bytes to the response buffer.', \strlen($content))));
283-
$r->info['error'] = $chunk->getError();
284-
$r->response->cancel();
285-
}
278+
$r->stream = (static fn () => yield $chunk)();
279+
yield from self::passthruStream($response, $r, $asyncMap);
286280

287-
yield $r => $chunk;
288281
continue;
289282
}
290283

@@ -347,13 +340,13 @@ private static function passthru(HttpClientInterface $client, self $r, ChunkInte
347340
}
348341
$r->stream = $stream;
349342

350-
yield from self::passthruStream($response, $r, null, $asyncMap);
343+
yield from self::passthruStream($response, $r, $asyncMap);
351344
}
352345

353346
/**
354347
* @param \SplObjectStorage<ResponseInterface, AsyncResponse>|null $asyncMap
355348
*/
356-
private static function passthruStream(ResponseInterface $response, self $r, ?ChunkInterface $chunk, ?\SplObjectStorage $asyncMap): \Generator
349+
private static function passthruStream(ResponseInterface $response, self $r, ?\SplObjectStorage $asyncMap, ?ChunkInterface $chunk = null): \Generator
357350
{
358351
while (true) {
359352
try {

Tests/AsyncDecoratorTraitTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,20 @@ public function testBufferPurePassthru()
232232

233233
$this->assertStringContainsString('SERVER_PROTOCOL', $response->getContent());
234234
$this->assertStringContainsString('HTTP_HOST', $response->getContent());
235+
236+
$client = new class(parent::getHttpClient(__FUNCTION__)) implements HttpClientInterface {
237+
use AsyncDecoratorTrait;
238+
239+
public function request(string $method, string $url, array $options = []): ResponseInterface
240+
{
241+
return new AsyncResponse($this->client, $method, $url, $options);
242+
}
243+
};
244+
245+
$response = $client->request('GET', 'http://localhost:8057/');
246+
247+
$this->assertStringContainsString('SERVER_PROTOCOL', $response->getContent());
248+
$this->assertStringContainsString('HTTP_HOST', $response->getContent());
235249
}
236250

237251
public function testRetryTimeout()

0 commit comments

Comments
 (0)