Final Class Yiisoft\HttpMiddleware\RemoveBodyMiddleware
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Psr\ |
Removes the body from the response for specific HTTP status codes, along with headers that describe
the now-removed body (such as Content-Length and Transfer-Encoding).
For status codes such as 304 Not Modified, these headers are kept by default, since per RFC 9110 / RFC 9112
they are still allowed to describe the representation that would have been sent in a 200 OK response
to the same request, even though no body is actually sent.
See also:
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| process() | Yiisoft\ |
Method Details
| public mixed __construct ( \ 100, // Continue 101, // Switching Protocols 102, // Processing 103, // Early Hints 204, // No Content 205, // Reset Content 304, ], array $keepHeadersOnStatusCode = [304], array $removedHeaders = ['Content-Length', 'Transfer-Encoding'] ) | ||
| $streamFactory | \ |
Factory to create a stream. |
| $statusCodes | array |
List of HTTP status codes for which the body should be removed. |
| $keepHeadersOnStatusCode | array |
List of HTTP status codes for which {@see $removedHeaders} should be kept even though the body is removed. |
| $removedHeaders | array |
List of headers to remove together with the body, for status codes not listed in {@see $keepHeadersOnStatusCode}. |
public function __construct(
private readonly StreamFactoryInterface $streamFactory,
private readonly array $statusCodes = [
100, // Continue
101, // Switching Protocols
102, // Processing
103, // Early Hints
204, // No Content
205, // Reset Content
304, // Not Modified
],
private readonly array $keepHeadersOnStatusCode = [
304, // Not Modified
],
private readonly array $removedHeaders = [
'Content-Length',
'Transfer-Encoding',
],
) {}
| public \ | ||
| $request | \ |
|
| $handler | \ |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
return $this->shouldRemoveBody($response)
? $this->removeBody($response)
: $response;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.