Skip to content

Commit

Permalink
Added support for maximum body length to async connector options.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Feb 6, 2020
1 parent 5d46484 commit b45a6de
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/AsyncHttpOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
];
}
}
40 changes: 40 additions & 0 deletions test/Functional/HttpConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

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;
use ScriptFUSION\Porter\Connector\Connector;
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;
Expand Down Expand Up @@ -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.
*/
Expand Down
6 changes: 6 additions & 0 deletions test/Functional/servers/big.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
for ($i = 0; $i < 100000; ++$i) {
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
}

0 comments on commit b45a6de

Please sign in to comment.