. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use function extension_loaded; use function gzdeflate; use function gzencode; use function in_array; use function str_contains; use function strlen; use function strstr; use function strtolower; use function strtr; /** * 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); } }