0

Final Class Yiisoft\HttpMiddleware\RemoveBodyMiddleware

InheritanceYiisoft\HttpMiddleware\RemoveBodyMiddleware
ImplementsPsr\Http\Server\MiddlewareInterface

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:

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Psr\Http\Message\StreamFactoryInterface $streamFactory, array $statusCodes = [
    
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 \Psr\Http\Message\StreamFactoryInterface

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',
    ],
) {}

            
process() public method

public \Psr\Http\Message\ResponseInterface process ( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler )
$request \Psr\Http\Message\ServerRequestInterface
$handler \Psr\Http\Server\RequestHandlerInterface

                public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
    $response = $handler->handle($request);
    return $this->shouldRemoveBody($response)
        ? $this->removeBody($response)
        : $response;
}