Skip to content
Closed
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
9 changes: 5 additions & 4 deletions src/Middleware/RequestBodyBufferMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ final class RequestBodyBufferMiddleware
public function __construct($sizeLimit = null)
{
if ($sizeLimit === null) {
$sizeLimit = \ini_get('post_max_size');
$iniSizeLimit = \ini_get('post_max_size');
$sizeLimit = $iniSizeLimit !== '0' ? $iniSizeLimit : null;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, a zero value will only mean "unlimited" when not given explicitly and read from post_max_size. This makes it a bit inconsistent with passing $sizeLimit explicitly and also makes it harder to test. Should both paths be handled consistently here? Among others, this should make testing this feature much easier.


$this->sizeLimit = IniUtil::iniSizeToBytes($sizeLimit);
$this->sizeLimit = $sizeLimit !== null ? IniUtil::iniSizeToBytes($sizeLimit) : null;
}

public function __invoke(ServerRequestInterface $request, $stack)
Expand All @@ -37,7 +38,7 @@ public function __invoke(ServerRequestInterface $request, $stack)
// happy path: skip if body is known to be empty (or is already buffered)
if ($size === 0 || !$body instanceof ReadableStreamInterface) {
// replace with empty body if body is streaming (or buffered size exceeds limit)
if ($body instanceof ReadableStreamInterface || $size > $this->sizeLimit) {
if ($body instanceof ReadableStreamInterface || ($this->sizeLimit !== null && $size > $this->sizeLimit)) {
$request = $request->withBody(new BufferedBody(''));
}

Expand All @@ -46,7 +47,7 @@ public function __invoke(ServerRequestInterface $request, $stack)

// request body of known size exceeding limit
$sizeLimit = $this->sizeLimit;
if ($size > $this->sizeLimit) {
if ($this->sizeLimit !== null && $size > $this->sizeLimit) {
$sizeLimit = 0;
}

Expand Down