API Index — Namespaces, Packages, Reports, Indices
BodySizeLimitMiddleware
in package
implements
MiddlewareInterface
Body Size Limit Middleware — nginx client_max_body_size / Apache
LimitRequestBody / PHP post_max_size parity.
Rejects requests whose body exceeds a configured maximum with
413 Content Too Large (nginx returns 413 here too). OpenSwoole's
package_max_length is the transport-level hard cap; this is the
configurable application-level limit with the standard 413 response, so
legacy code that expects oversized uploads to be refused behaves as it would
under nginx/Apache.
The limit accepts a byte count (new BodySizeLimitMiddleware(10_485_760)) or
an nginx-style size string ('10m', '512k', '1g').
Content-Length requests: the declared length is checked before the body
is read — a fast, zero-copy path that mirrors Apache's ap_h1_body_in_filter
pre-read guard.
Chunked / no Content-Length requests: Apache enforces LimitRequestBody
against the decoded chunked byte count via the ctx->limit_used accumulator
(http_filters.c:671-686). In the OpenSwoole runtime the body is already
fully decoded and buffered by the time the PSR-15 middleware stack runs — the
Transfer-Encoding: chunked layer is owned by OpenSwoole's C parser and is
not re-exposed to PHP. This middleware therefore measures the length of the
buffered body string (via $request->getBody()->getSize() or a
strlen-equivalent fallback) and enforces the cap on that decoded size.
What this covers: any chunked upload that OpenSwoole accepted and decoded into
its internal buffer — package_max_length is still the outermost transport
guard for bodies so large they never reach PHP at all.
Usage in app.php:
$app->addMiddleware(new \ZealPHP\Middleware\BodySizeLimitMiddleware('10m'));
Table of Contents
Interfaces
- MiddlewareInterface
Properties
- $maxBytes : int
- $unlimited : bool
- True when the configured limit is explicitly
0(nginx unlimited semantics).
Methods
- __construct() : mixed
- process() : ResponseInterface
- parseSize() : int
- Parse an nginx-style size string (
10m,512k,1g) to bytes.
Properties
$maxBytes
private
int
$maxBytes
$unlimited
True when the configured limit is explicitly 0 (nginx unlimited semantics).
private
bool
$unlimited
Methods
__construct()
public
__construct(int|string $max) : mixed
Parameters
- $max : int|string
process()
public
process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
- $request : ServerRequestInterface
- $handler : RequestHandlerInterface
Return values
ResponseInterfaceparseSize()
Parse an nginx-style size string (10m, 512k, 1g) to bytes.
private
static parseSize(string $size) : int
Parameters
- $size : string