Browse Source

fix `web\Request::getParsedBody()` throws exception for empty body without content-type

tags/3.0.0-alpha1
Klimov Paul 7 years ago
parent
commit
854ae7127c
  1. 11
      framework/web/Request.php
  2. 21
      tests/framework/web/RequestTest.php

11
framework/web/Request.php

@ -652,7 +652,7 @@ class Request extends \yii\base\Request implements ServerRequestInterface
$this->setBody($body);
}
private $_parsedBody;
private $_parsedBody = false;
/**
* Returns the request parameters given in the request body.
@ -663,7 +663,8 @@ class Request extends \yii\base\Request implements ServerRequestInterface
*
* Since 2.1.0 body params also include result of [[getUploadedFiles()]].
*
* @return array the request parameters given in the request body.
* @return array|null the request parameters given in the request body. A `null` value indicates
* the absence of body content.
* @throws InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]].
* @throws UnsupportedMediaTypeHttpException if unable to parse raw body.
* @see getMethod()
@ -672,7 +673,7 @@ class Request extends \yii\base\Request implements ServerRequestInterface
*/
public function getParsedBody()
{
if ($this->_parsedBody === null) {
if ($this->_parsedBody === false) {
if (isset($_POST[$this->methodParam])) {
$this->_parsedBody = $_POST;
unset($this->_parsedBody[$this->methodParam]);
@ -707,6 +708,8 @@ class Request extends \yii\base\Request implements ServerRequestInterface
if ($contentType === 'multipart/form-data') {
$this->_parsedBody = ArrayHelper::merge($this->_parsedBody, $this->getUploadedFiles());
}
} elseif (empty($contentType) && $this->getBody()->getSize() === 0) {
$this->_parsedBody = null;
} else {
if ($contentType !== 'application/x-www-form-urlencoded') {
throw new UnsupportedMediaTypeHttpException();
@ -2226,5 +2229,7 @@ class Request extends \yii\base\Request implements ServerRequestInterface
if (is_object($this->_cookies)) {
$this->_cookies = clone $this->_cookies;
}
$this->_parsedBody = false;
}
}

21
tests/framework/web/RequestTest.php

@ -662,7 +662,7 @@ class RequestTest extends TestCase
$_SERVER = $original;
}
public function testGetBodyParams()
public function testGetParsedBody()
{
$body = new MemoryStream();
$body->write('name=value');
@ -688,6 +688,23 @@ class RequestTest extends TestCase
}
/**
* @depends testGetParsedBody
*/
public function testParseEmptyBody()
{
$request = new Request();
$request->setMethod('GET');
$request->setBody(new MemoryStream());
$this->assertNull($request->getParsedBody());
try {
$request->withHeader('Content-Type', 'test/json')->getParsedBody();
} catch (UnsupportedMediaTypeHttpException $noContentException) {}
$this->assertTrue(isset($noContentException));
}
/**
* Data provider for [[testDefaultUploadedFiles()]]
* @return array test data.
*/
@ -810,7 +827,7 @@ class RequestTest extends TestCase
}
/**
* @depends testGetBodyParams
* @depends testGetParsedBody
* @dataProvider dataProviderDefaultUploadedFiles
*
* @param array $rawFiles

Loading…
Cancel
Save