. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; /** * Middleware to add a "Content-Length" header to a response. */ class ContentLength implements MiddlewareInterface { /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $response = $handler->handle($request); if ($response->hasHeader('content-length')) { return $response; } $content_length = $response->getBody()->getSize(); if ($content_length === null) { return $response; } return $response ->withHeader('content-length', (string) $content_length); } }