diff --git a/src/AsyncHttpOptions.php b/src/AsyncHttpOptions.php index 686ef3a..77e9fb6 100644 --- a/src/AsyncHttpOptions.php +++ b/src/AsyncHttpOptions.php @@ -19,6 +19,9 @@ final class AsyncHttpOptions // Automatically add a "Referer" header on redirect. private $autoReferrer = true; + // Maximum body length in bytes. Default 10MiB. + private $maxBodyLength = 0x100000 * 10; + public function getTransferTimeout(): int { return $this->transferTimeout; @@ -55,12 +58,25 @@ public function setAutoReferrer(bool $autoReferer): self return $this; } + public function getMaxBodyLength(): int + { + return $this->maxBodyLength; + } + + public function setMaxBodyLength($maxBodyLength): self + { + $this->maxBodyLength = $maxBodyLength; + + return $this; + } + public function extractArtaxOptions(): array { return [ Client::OP_AUTO_REFERER => $this->autoReferrer, Client::OP_MAX_REDIRECTS => $this->maxRedirects, Client::OP_TRANSFER_TIMEOUT => $this->transferTimeout, + Client::OP_MAX_BODY_BYTES => $this->maxBodyLength, ]; } } diff --git a/test/Functional/HttpConnectorTest.php b/test/Functional/HttpConnectorTest.php index f42f9b2..ce0b752 100644 --- a/test/Functional/HttpConnectorTest.php +++ b/test/Functional/HttpConnectorTest.php @@ -3,6 +3,8 @@ namespace ScriptFUSIONTest\Functional\Porter\Net\Http; +use Amp\Artax\HttpException; +use Amp\Artax\ParseException; use Amp\Artax\StringBody; use PHPUnit\Framework\TestCase; use ScriptFUSION\Porter\Connector\AsyncDataSource; @@ -10,6 +12,7 @@ use ScriptFUSION\Porter\Connector\DataSource; use ScriptFUSION\Porter\Net\Http\AsyncHttpConnector; use ScriptFUSION\Porter\Net\Http\AsyncHttpDataSource; +use ScriptFUSION\Porter\Net\Http\AsyncHttpOptions; use ScriptFUSION\Porter\Net\Http\HttpConnectionException; use ScriptFUSION\Porter\Net\Http\HttpConnector; use ScriptFUSION\Porter\Net\Http\HttpDataSource; @@ -153,6 +156,43 @@ public function testRedirect(): void self::assertSame(302, $prev->getStatusCode()); } + /** + * Tests that when the body length exceeds the default limit, an HTTP exception is thrown. + */ + public function testDefaultBodyLengthTooLong(): void + { + $server = $this->startServer(); + + $this->connector = new AsyncHttpConnector(); + + $this->expectException(HttpException::class); + + try { + $this->fetchAsync(self::buildAsyncDataSource('big.php')); + } finally { + $this->stopServer($server); + } + } + + /** + * Tests that when the body length exceeds a small custom limit, an HTTP exception is thrown. + */ + public function testCustomBodyLengthTooLong(): void + { + $server = $this->startServer(); + + $this->connector = new AsyncHttpConnector((new AsyncHttpOptions)->setMaxBodyLength(1)); + + // N.B. Actual type is Amp\Artax\ParseException. + $this->expectException(HttpException::class); + + try { + $this->fetchAsync(self::buildAsyncDataSource()); + } finally { + $this->stopServer($server); + } + } + /** * @return Process Server. */ diff --git a/test/Functional/servers/big.php b/test/Functional/servers/big.php new file mode 100644 index 0000000..f79e6de --- /dev/null +++ b/test/Functional/servers/big.php @@ -0,0 +1,6 @@ + $value) { + echo "$name: $value\n"; + } +}