Skip to content

Allow ini-like filesize for RequestBodyBufferMiddleware and MultipartParser #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Io/MultipartParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class MultipartParser
private $emptyCount = 0;

/**
* @param int|null $uploadMaxFilesize
* @param int|string|null $uploadMaxFilesize
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, same change is required for RequestBodyParserMiddleware 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* @param int|null $maxFileUploads
*/
public function __construct($uploadMaxFilesize = null, $maxFileUploads = null)
Expand All @@ -83,10 +83,10 @@ public function __construct($uploadMaxFilesize = null, $maxFileUploads = null)
}

if ($uploadMaxFilesize === null) {
$uploadMaxFilesize = IniUtil::iniSizeToBytes(ini_get('upload_max_filesize'));
$uploadMaxFilesize = ini_get('upload_max_filesize');
}

$this->uploadMaxFilesize = (int)$uploadMaxFilesize;
$this->uploadMaxFilesize = IniUtil::iniSizeToBytes($uploadMaxFilesize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but doesn't match docblock/documentation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

$this->maxFileUploads = $maxFileUploads === null ? (ini_get('file_uploads') === '' ? 0 : (int)ini_get('max_file_uploads')) : (int)$maxFileUploads;
}

Expand Down
13 changes: 7 additions & 6 deletions src/Middleware/RequestBodyBufferMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ final class RequestBodyBufferMiddleware
private $sizeLimit;

/**
* @param int|null $sizeLimit Either an int with the max request body size
* or null to use post_max_size from PHP's
* configuration. (Note that the value from
* the CLI configuration will be used.)
* @param int|string|null $sizeLimit Either an int with the max request body size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "...body size in bytes..."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, done 👍

* in bytes or an ini like size string
* or null to use post_max_size from PHP's
* configuration. (Note that the value from
* the CLI configuration will be used.)
*/
public function __construct($sizeLimit = null)
{
if ($sizeLimit === null) {
$sizeLimit = IniUtil::iniSizeToBytes(ini_get('post_max_size'));
$sizeLimit = ini_get('post_max_size');
}

$this->sizeLimit = $sizeLimit;
$this->sizeLimit = IniUtil::iniSizeToBytes($sizeLimit);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but doesn't match docblock/documentation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

}

public function __invoke(ServerRequestInterface $request, $stack)
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/RequestBodyParserMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class RequestBodyParserMiddleware
private $multipart;

/**
* @param int|null $uploadMaxFilesize
* @param int|string|null $uploadMaxFilesize
* @param int|null $maxFileUploads
*/
public function __construct($uploadMaxFilesize = null, $maxFileUploads = null)
Expand Down
33 changes: 33 additions & 0 deletions tests/Io/MultipartParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,39 @@ public function testUploadTooLargeFile()
$this->assertSame(UPLOAD_ERR_INI_SIZE, $file->getError());
}

public function testUploadTooLargeFileWithIniLikeSize()
{
$boundary = "---------------------------12758086162038677464950549563";

$data = "--$boundary\r\n";
$data .= "Content-Disposition: form-data; name=\"file\"; filename=\"hello\"\r\n";
$data .= "Content-type: text/plain\r\n";
$data .= "\r\n";
$data .= str_repeat('world', 1024) . "\r\n";
$data .= "--$boundary--\r\n";

$request = new ServerRequest('POST', 'http://example.com/', array(
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
), $data, 1.1);

$parser = new MultipartParser('1K');
$parsedRequest = $parser->parse($request);

$files = $parsedRequest->getUploadedFiles();

$this->assertCount(1, $files);
$this->assertTrue(isset($files['file']));
$this->assertInstanceOf('Psr\Http\Message\UploadedFileInterface', $files['file']);

/* @var $file \Psr\Http\Message\UploadedFileInterface */
$file = $files['file'];

$this->assertSame('hello', $file->getClientFilename());
$this->assertSame('text/plain', $file->getClientMediaType());
$this->assertSame(5120, $file->getSize());
$this->assertSame(UPLOAD_ERR_INI_SIZE, $file->getError());
}

public function testUploadNoFile()
{
$boundary = "---------------------------12758086162038677464950549563";
Expand Down
29 changes: 28 additions & 1 deletion tests/Middleware/RequestBodyBufferMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testAlreadyBufferedResolvesImmediately()
{
$size = 1024;
$body = str_repeat('x', $size);
$stream = new BufferStream($size);
$stream = new BufferStream(1024);
$stream->write($body);
$serverRequest = new ServerRequest(
'GET',
Expand Down Expand Up @@ -93,6 +93,33 @@ function (ServerRequestInterface $request) {
$this->assertSame('', $response->getBody()->getContents());
}

public function testKnownExcessiveSizedWithIniLikeSize()
{
$loop = Factory::create();

$stream = new ThroughStream();
$loop->addTimer(0.001, function () use ($stream) {
$stream->end(str_repeat('a', 2048));
});
$serverRequest = new ServerRequest(
'GET',
'https://example.com/',
array(),
new HttpBodyStream($stream, 2048)
);

$buffer = new RequestBodyBufferMiddleware('1K');
$response = Block\await($buffer(
$serverRequest,
function (ServerRequestInterface $request) {
return new Response(200, array(), $request->getBody()->getContents());
}
), $loop);

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('', $response->getBody()->getContents());
}

public function testAlreadyBufferedExceedingSizeResolvesImmediatelyWithEmptyBody()
{
$serverRequest = new ServerRequest(
Expand Down