Skip to content

Commit 2af2aa4

Browse files
committed
refactor(HTTP): use ini_parse_quantity() to parse post_max_size
1 parent 383018c commit 2af2aa4

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

‎system/HTTP/IncomingRequest.php‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,9 @@ private function getPostMaxSize(): int
179179
*/
180180
private function parsePostMaxSize(string $postMaxSize): int
181181
{
182-
if ((int) $postMaxSize <= 0) {
183-
return PHP_INT_MAX;
184-
}
182+
$bytes = ini_parse_quantity($postMaxSize);
185183

186-
return match (strtoupper(substr($postMaxSize, -1))) {
187-
'G' => (int) str_replace('G', '', $postMaxSize) * 1024 ** 3,
188-
'M' => (int) str_replace('M', '', $postMaxSize) * 1024 ** 2,
189-
'K' => (int) str_replace('K', '', $postMaxSize) * 1024,
190-
default => (int) $postMaxSize,
191-
};
184+
return $bytes <= 0 ? PHP_INT_MAX : $bytes;
192185
}
193186

194187
/**

‎tests/system/HTTP/IncomingRequestTest.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,10 +1100,22 @@ public static function provideParsePostMaxSize(): iterable
11001100

11011101
yield 'gigabytes' => ['1G', 1024 ** 3];
11021102

1103+
yield 'lowercased gigabytes' => ['1g', 1024 ** 3];
1104+
11031105
yield 'megabytes' => ['8M', 8 * 1024 ** 2];
11041106

1107+
yield 'lowercased megabytes' => ['8m', 8 * 1024 ** 2];
1108+
11051109
yield 'kilobytes' => ['512K', 512 * 1024];
11061110

1111+
yield 'lowercased kilobytes' => ['512k', 512 * 1024];
1112+
1113+
yield 'hexadecimal' => ['0x10M', 16 * 1024 ** 2];
1114+
1115+
yield 'octal' => ['010M', 8 * 1024 ** 2];
1116+
1117+
yield 'binary' => ['0b10M', 2 * 1024 ** 2];
1118+
11071119
yield 'bytes' => ['12345', 12345];
11081120
}
11091121

0 commit comments

Comments
 (0)