Skip to content

Commit 150e602

Browse files
committed
Reverted body buffering introduced in aae037d.
1 parent 1324810 commit 150e602

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"license": "LGPL-3.0",
1111
"require": {
1212
"php": "^8.1",
13-
"amphp/http-client": "^5.1",
13+
"amphp/http-client": "^5.3.3",
1414
"amphp/http-client-cookies": "^2",
1515
"scriptfusion/porter": "^7|^8"
1616
},

src/HttpResponse.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace ScriptFUSION\Porter\Net\Http;
55

6+
use Amp\ByteStream\Payload;
67
use Amp\Http\Client\Response;
78

89
/**
@@ -15,7 +16,8 @@ final class HttpResponse
1516
private int $statusCode;
1617
private string $statusPhrase;
1718
private ?self $previous;
18-
private string $bufferedBody;
19+
private Payload $body;
20+
private string $bodyBuffer;
1921

2022
public function __construct(Response $ampResponse)
2123
{
@@ -24,8 +26,7 @@ public function __construct(Response $ampResponse)
2426
$this->statusCode = $ampResponse->getStatus();
2527
$this->statusPhrase = $ampResponse->getReason();
2628
$this->previous = $ampResponse->getPreviousResponse() ? new self($ampResponse->getPreviousResponse()) : null;
27-
// We must buffer body immediately, otherwise we get memory leaks.
28-
$this->bufferedBody = $ampResponse->getBody()->isReadable() ? $ampResponse->getBody()->buffer() : '';
29+
$this->body = $ampResponse->getBody();
2930
}
3031

3132
public function __toString(): string
@@ -59,7 +60,7 @@ public function getHeader(string $name): array
5960

6061
public function getBody(): string
6162
{
62-
return $this->bufferedBody;
63+
return $this->bodyBuffer ??= $this->body->buffer();
6364
}
6465

6566
public function getStatusCode(): int

test/Functional/HttpConnectorTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public function testDefaultBodyLengthTooLong(): void
209209
$this->expectException(StreamException::class);
210210

211211
try {
212-
$this->fetch(self::buildDataSource('big.php'));
212+
$this->fetch(self::buildDataSource('big.php'))->getBody();
213213
} finally {
214214
$this->stopServer($server);
215215
}
@@ -227,7 +227,7 @@ public function testCustomBodyLengthTooLong(): void
227227
$this->expectException(StreamException::class);
228228

229229
try {
230-
$this->fetch(self::buildDataSource());
230+
$this->fetch(self::buildDataSource())->getBody();
231231
} finally {
232232
$this->stopServer($server);
233233
}
@@ -243,12 +243,25 @@ public function testCustomBodyLengthOverride(): void
243243
$this->expectException(StreamException::class);
244244

245245
try {
246-
$this->fetch(self::buildDataSource()->setMaxBodyLength(1));
246+
$this->fetch(self::buildDataSource()->setMaxBodyLength(1))->getBody();
247247
} finally {
248248
$this->stopServer($server);
249249
}
250250
}
251251

252+
public function testUserAgentOverride(): void
253+
{
254+
$server = $this->startServer();
255+
256+
try {
257+
$response = $this->fetch(self::buildDataSource()->addHeader($h = 'user-agent', $v = 'Alfa'))->getBody();
258+
} finally {
259+
$this->stopServer($server);
260+
}
261+
262+
self::assertMatchesRegularExpression("[^$h: $v$]m", $response);
263+
}
264+
252265
private function startServer(): Process
253266
{
254267
$server = new Process([PHP_BINARY, '-S', self::HOST, '-t', self::DIR]);

0 commit comments

Comments
 (0)