diff --git a/src/HttpServer.php b/src/HttpServer.php index 24168cc5..31a0d4f5 100644 --- a/src/HttpServer.php +++ b/src/HttpServer.php @@ -230,13 +230,8 @@ public function __construct($requestHandlerOrLoop) $middleware[] = new LimitConcurrentRequestsMiddleware($concurrency); } $middleware[] = new RequestBodyBufferMiddleware($maxSize); - // Checking for an empty string because that is what a boolean - // false is returned as by ini_get depending on the PHP version. // @link http://php.net/manual/en/ini.core.php#ini.enable-post-data-reading - // @link http://php.net/manual/en/function.ini-get.php#refsect1-function.ini-get-notes - // @link https://3v4l.org/qJtsa - $enablePostDataReading = \ini_get('enable_post_data_reading'); - if ($enablePostDataReading !== '') { + if (IniUtil::iniBoolean(\ini_get('enable_post_data_reading'))) { $middleware[] = new RequestBodyParserMiddleware(); } } diff --git a/src/Io/IniUtil.php b/src/Io/IniUtil.php index 612aae29..002b7de5 100644 --- a/src/Io/IniUtil.php +++ b/src/Io/IniUtil.php @@ -45,4 +45,16 @@ public static function iniSizeToBytes($size) return (int)$size; } + + /** + * Convert a ini like boolean to a boolean value just like PHP does. + * + * @param string $value + * @return bool + * @link https://github.com/php/php-src/blob/master/Zend/zend_ini.c + */ + public static function iniBoolean($value) + { + return (int) $value !== 0 || \in_array(\strtolower($value), ['on', 'yes', 'true'], true); + } } diff --git a/src/Io/MultipartParser.php b/src/Io/MultipartParser.php index cdfe189b..feec3ba0 100644 --- a/src/Io/MultipartParser.php +++ b/src/Io/MultipartParser.php @@ -89,7 +89,7 @@ public function __construct($uploadMaxFilesize = null, $maxFileUploads = null) } $this->uploadMaxFilesize = IniUtil::iniSizeToBytes($uploadMaxFilesize); - $this->maxFileUploads = $maxFileUploads === null ? (\ini_get('file_uploads') === '' ? 0 : (int)\ini_get('max_file_uploads')) : (int)$maxFileUploads; + $this->maxFileUploads = $maxFileUploads === null ? (!IniUtil::iniBoolean(\ini_get('file_uploads')) ? 0 : (int) \ini_get('max_file_uploads')) : (int) $maxFileUploads; $this->maxMultipartBodyParts = $this->maxInputVars + $this->maxFileUploads; } diff --git a/tests/Io/IniUtilTest.php b/tests/Io/IniUtilTest.php index 2e9f99c9..6d663fce 100644 --- a/tests/Io/IniUtilTest.php +++ b/tests/Io/IniUtilTest.php @@ -74,4 +74,46 @@ public function testInvalidInputIniSizeToBytes($input) $this->expectException(\InvalidArgumentException::class); IniUtil::iniSizeToBytes($input); } + + public static function provideIniBooleansEnabled() + { + yield ['1']; + yield ['On']; + yield ['on']; + yield ['yes']; + yield ['true']; + yield ['TRUE']; + yield ['2']; + yield ['-1']; + yield ['1abc']; + } + + /** + * @dataProvider provideIniBooleansEnabled + */ + public function testIniBooleanReturnsTrueForEnabledValue($input) + { + $this->assertTrue(IniUtil::iniBoolean($input)); + } + + public static function provideIniBooleansDisabled() + { + yield ['']; + yield ['0']; + yield ['Off']; + yield ['off']; + yield ['no']; + yield ['false']; + yield ['none']; + yield ['00']; + yield ['abc']; + } + + /** + * @dataProvider provideIniBooleansDisabled + */ + public function testIniBooleanReturnsFalseForDisabledValue($input) + { + $this->assertFalse(IniUtil::iniBoolean($input)); + } }