Skip to content

Commit 5433d18

Browse files
authored
优化http表单POST请求getParsedBody对原始请求体的加载:移除在getParsedBody对原始body加载的逻辑 (#718)
1 parent 12d16ea commit 5433d18

File tree

2 files changed

+5
-17
lines changed

2 files changed

+5
-17
lines changed

src/Util/Http/ServerRequest.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,6 @@ public function getParsedBody()
286286
$parsedBody = &$this->parsedBody;
287287
if (null === $parsedBody)
288288
{
289-
if (!$this->bodyInited)
290-
{
291-
$this->initBody();
292-
$this->bodyInited = true;
293-
}
294289
$contentType = $this->getHeaderLine(RequestHeader::CONTENT_TYPE);
295290
if ('' === $contentType)
296291
{
@@ -311,7 +306,7 @@ public function getParsedBody()
311306
// json
312307
elseif (MediaType::APPLICATION_JSON === $contentType)
313308
{
314-
$content = $this->body->getContents();
309+
$content = $this->getBody()->getContents();
315310
if ('' !== $content)
316311
{
317312
$parsedBody = json_decode($content, !Config::get('@currentServer.jsonBodyIsObject', false), 512, \JSON_THROW_ON_ERROR);
@@ -331,7 +326,7 @@ public function getParsedBody()
331326
]))
332327
{
333328
$this->post = $parsedBody = new \DOMDocument();
334-
$parsedBody->loadXML($this->body->getContents());
329+
$parsedBody->loadXML($this->getBody()->getContents());
335330
}
336331
// 其它
337332
else

tests/unit/Component/Tests/Util/Http/ServerRequestTest.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,14 @@ private function __testParsedBody(string $type): void
144144
$this->assertEquals(['name' => 'imi'], $request->getParsedBody());
145145

146146
// form
147-
$request = new class() extends ServerRequest {
148-
/**
149-
* {@inheritDoc}
150-
*/
151-
protected function initBody(): void
152-
{
153-
$this->post = ['name' => 'imi'];
154-
}
155-
};
147+
$request = new ServerRequest();
156148
foreach ([
157149
MediaType::APPLICATION_FORM_URLENCODED,
158150
MediaType::MULTIPART_FORM_DATA,
159151
] as $contentType)
160152
{
161-
$request->setHeader(RequestHeader::CONTENT_TYPE, $contentType);
153+
$request->setPost(['name' => 'imi'])
154+
->setHeader(RequestHeader::CONTENT_TYPE, $contentType);
162155
$requestTmp = $request->withMethod(RequestMethod::POST);
163156
$this->assertEquals(['name' => 'imi'], $requestTmp->getParsedBody());
164157
$requestTmp = $request->withMethod(RequestMethod::PUT);

0 commit comments

Comments
 (0)