Skip to content
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

Add internal IniUtil helper class to convert ini size settings to bytes #276

Merged
merged 1 commit into from
Dec 10, 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
48 changes: 48 additions & 0 deletions src/Io/IniUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace React\Http\Io;

/**
* @internal
*/
final class IniUtil
{
/**
* Convert a ini like size to a numeric size in bytes.
*
* @param string $iniSetting
* @return int
*/
public static function iniSizeToBytes($size)
{
if (is_numeric($size)) {
return $size;
}

$suffix = strtoupper(substr($size, -1));
$strippedSize = substr($size, 0, -1);

if (!is_numeric($strippedSize)) {
throw new \InvalidArgumentException('"' . $size . '" is not a valid ini size');
}

if ($strippedSize <= 0) {
throw new \InvalidArgumentException('Expect "' . $size . '" to be higher isn\'t zero or lower');
}

if ($suffix === 'K') {
return $strippedSize * 1024;
}
if ($suffix === 'M') {
return $strippedSize * 1024 * 1024;
}
if ($suffix === 'G') {
return $strippedSize * 1024 * 1024 * 1024;
}
if ($suffix === 'T') {
return $strippedSize * 1024 * 1024 * 1024 * 1024;
}

return $size;
}
}
30 changes: 5 additions & 25 deletions src/Io/MultipartParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ public function __construct($uploadMaxFilesize = null, $maxFileUploads = null)
$this->maxInputNestingLevel = (int)$var;
}

$this->uploadMaxFilesize = $uploadMaxFilesize === null ? $this->iniUploadMaxFilesize() : (int)$uploadMaxFilesize;
if ($uploadMaxFilesize === null) {
$uploadMaxFilesize = IniUtil::iniSizeToBytes(ini_get('upload_max_filesize'));
}

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

Expand Down Expand Up @@ -322,28 +326,4 @@ private function extractPost($postFields, $key, $value)

return $postFields;
}

/**
* Gets upload_max_filesize from PHP's configuration expressed in bytes
*
* @return int
* @link http://php.net/manual/en/ini.core.php#ini.upload-max-filesize
* @codeCoverageIgnore
*/
private function iniUploadMaxFilesize()
{
$size = ini_get('upload_max_filesize');
$suffix = strtoupper(substr($size, -1));
if ($suffix === 'K') {
return substr($size, 0, -1) * 1024;
}
if ($suffix === 'M') {
return substr($size, 0, -1) * 1024 * 1024;
}
if ($suffix === 'G') {
return substr($size, 0, -1) * 1024 * 1024 * 1024;
}

return $size;
}
}
27 changes: 2 additions & 25 deletions src/Middleware/RequestBodyBufferMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use OverflowException;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Io\IniUtil;
use React\Promise\Stream;
use React\Stream\ReadableStreamInterface;
use RingCentral\Psr7\BufferStream;
Expand All @@ -21,7 +22,7 @@ final class RequestBodyBufferMiddleware
public function __construct($sizeLimit = null)
{
if ($sizeLimit === null) {
$sizeLimit = $this->iniMaxPostSize();
$sizeLimit = IniUtil::iniSizeToBytes(ini_get('post_max_size'));
}

$this->sizeLimit = $sizeLimit;
Expand Down Expand Up @@ -66,28 +67,4 @@ public function __invoke(ServerRequestInterface $request, $stack)
throw $error;
});
}

/**
* Gets post_max_size from PHP's configuration expressed in bytes
*
* @return int
* @link http://php.net/manual/en/ini.core.php#ini.post-max-size
* @codeCoverageIgnore
*/
private function iniMaxPostSize()
{
$size = ini_get('post_max_size');
$suffix = strtoupper(substr($size, -1));
if ($suffix === 'K') {
return substr($size, 0, -1) * 1024;
}
if ($suffix === 'M') {
return substr($size, 0, -1) * 1024 * 1024;
}
if ($suffix === 'G') {
return substr($size, 0, -1) * 1024 * 1024 * 1024;
}

return $size;
}
}
77 changes: 77 additions & 0 deletions tests/Io/IniUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace React\Tests\Http\Io;

use React\Http\Io\IniUtil;
use React\Tests\Http\TestCase;

class IniUtilTest extends TestCase
{
public function provideIniSizes()
{
return array(
array(
'1',
1,
),
array(
'10',
10,
),
array(
'1024',
1024,
),
array(
'1K',
1024,
),
array(
'1.5M',
1572864,
),
array(
'64M',
67108864,
),
array(
'8G',
8589934592,
),
array(
'1T',
1099511627776,
),
);
}

/**
* @dataProvider provideIniSizes
*/
public function testIniSizeToBytes($input, $output)
{
$this->assertEquals($output, IniUtil::iniSizeToBytes($input));
}

public function provideInvalidInputIniSizeToBytes()
{
return array(
array('-1G'),
array('0G'),
array(null),
array('foo'),
array('fooK'),
array('1ooL'),
array('1ooL'),
);
}

/**
* @dataProvider provideInvalidInputIniSizeToBytes
* @expectedException InvalidArgumentException
*/
public function testInvalidInputIniSizeToBytes($input)
{
IniUtil::iniSizeToBytes($input);
}
}