Browse Source

Added `yii\web\MultipartFormDataParser::$force` option allowing to enforce parsing even on 'POST' request

tags/2.0.13
Klimov Paul 7 years ago
parent
commit
578b2caf42
  1. 1
      framework/CHANGELOG.md
  2. 19
      framework/web/MultipartFormDataParser.php
  3. 35
      tests/framework/web/MultipartFormDataParserTest.php

1
framework/CHANGELOG.md

@ -3,6 +3,7 @@ Yii Framework 2 Change Log
2.0.13 under development
------------------------
- Bug #14523: Added `yii\web\MultipartFormDataParser::$force` option allowing to enforce parsing even on 'POST' request (klimov-paul)
- Bug #14449: Fix PHP 7.2 compatibility bugs and add explicit closure support in `yii\base\Application` (dynasource)
- Bug #7890: Allow `migrate/mark` to mark history at the point of the base migration (cebe)
- Bug #14206: `MySqlMutex`, `PgsqlMutex` and `OracleMutex` now use `useMaster()` to ensure lock is aquired on the same DB server (cebe, ryusoft)

19
framework/web/MultipartFormDataParser.php

@ -66,6 +66,15 @@ use yii\helpers\StringHelper;
class MultipartFormDataParser extends BaseObject implements RequestParserInterface
{
/**
* @var bool whether to parse raw body even for 'POST' request and `$_FILES` already populated.
* By default this option is disabled saving performance for 'POST' requests, which are already
* processed by PHP automatically.
* > Note: if this option is enabled, value of `$_FILES` will be reset on each parse.
* @since 2.0.13
*/
public $force = false;
/**
* @var int upload file max size in bytes.
*/
private $_uploadFileMaxSize;
@ -118,9 +127,13 @@ class MultipartFormDataParser extends BaseObject implements RequestParserInterfa
*/
public function parse($rawBody, $contentType)
{
if (!empty($_POST) || !empty($_FILES)) {
// normal POST request is parsed by PHP automatically
return $_POST;
if (!$this->force) {
if (!empty($_POST) || !empty($_FILES)) {
// normal POST request is parsed by PHP automatically
return $_POST;
}
} else {
$_FILES = [];
}
if (empty($rawBody)) {

35
tests/framework/web/MultipartFormDataParserTest.php

@ -128,4 +128,39 @@ class MultipartFormDataParserTest extends TestCase
$this->assertCount(3, $_FILES);
$this->assertEquals(UPLOAD_ERR_INI_SIZE, $_FILES['thirdFile']['error']);
}
/**
* @depends testNotEmptyPost
* @depends testNotEmptyFiles
*/
public function testForce()
{
$parser = new MultipartFormDataParser();
$parser->force = true;
$_POST = [
'existingName' => 'value',
];
$_FILES = [
'existingFile' => [
'name' => 'file.txt',
'type' => 'text/plain',
],
];
$boundary = '---------------------------22472926011618';
$contentType = 'multipart/form-data; boundary=' . $boundary;
$rawBody = "--{$boundary}\nContent-Disposition: form-data; name=\"title\"\r\n\r\ntest-title";
$rawBody .= "\r\n--{$boundary}\nContent-Disposition: form-data; name=\"someFile\"; filename=\"some-file.txt\"\nContent-Type: text/plain\r\n\r\nsome file content";
$rawBody .= "\r\n--{$boundary}--";
$bodyParams = $parser->parse($rawBody, $contentType);
$expectedBodyParams = [
'title' => 'test-title',
];
$this->assertEquals($expectedBodyParams, $bodyParams);
$this->assertNotEmpty($_FILES['someFile']);
$this->assertFalse(isset($_FILES['existingFile']));
}
}

Loading…
Cancel
Save