xref: /webtrees/app/Http/Middleware/ContentLength.php (revision d11be7027e34e3121be11cc025421873364403f9)
1d1da5ba4SGreg Roach<?php
2d1da5ba4SGreg Roach
3d1da5ba4SGreg Roach/**
4d1da5ba4SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6d1da5ba4SGreg Roach * This program is free software: you can redistribute it and/or modify
7d1da5ba4SGreg Roach * it under the terms of the GNU General Public License as published by
8d1da5ba4SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9d1da5ba4SGreg Roach * (at your option) any later version.
10d1da5ba4SGreg Roach * This program is distributed in the hope that it will be useful,
11d1da5ba4SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12d1da5ba4SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13d1da5ba4SGreg Roach * GNU General Public License for more details.
14d1da5ba4SGreg Roach * You should have received a copy of the GNU General Public License
15d1da5ba4SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16d1da5ba4SGreg Roach */
17d1da5ba4SGreg Roach
18d1da5ba4SGreg Roachdeclare(strict_types=1);
19d1da5ba4SGreg Roach
20d1da5ba4SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
21d1da5ba4SGreg Roach
22d1da5ba4SGreg Roachuse Psr\Http\Message\ResponseInterface;
23d1da5ba4SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
24d1da5ba4SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
25d1da5ba4SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
26d1da5ba4SGreg Roach
27d1da5ba4SGreg Roach/**
28d1da5ba4SGreg Roach * Middleware to add a "Content-Length" header to a response.
29d1da5ba4SGreg Roach */
30d1da5ba4SGreg Roachclass ContentLength implements MiddlewareInterface
31d1da5ba4SGreg Roach{
32d1da5ba4SGreg Roach    /**
33d1da5ba4SGreg Roach     * @param ServerRequestInterface  $request
34d1da5ba4SGreg Roach     * @param RequestHandlerInterface $handler
35d1da5ba4SGreg Roach     *
36d1da5ba4SGreg Roach     * @return ResponseInterface
37d1da5ba4SGreg Roach     */
38d1da5ba4SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
39d1da5ba4SGreg Roach    {
40d1da5ba4SGreg Roach        $response = $handler->handle($request);
41d1da5ba4SGreg Roach
42d1da5ba4SGreg Roach        if ($response->hasHeader('content-length')) {
43d1da5ba4SGreg Roach            return $response;
44d1da5ba4SGreg Roach        }
45d1da5ba4SGreg Roach
46d1da5ba4SGreg Roach        $content_length = $response->getBody()->getSize();
47d1da5ba4SGreg Roach
48d1da5ba4SGreg Roach        if ($content_length === null) {
49d1da5ba4SGreg Roach            return $response;
50d1da5ba4SGreg Roach        }
51d1da5ba4SGreg Roach
52d1da5ba4SGreg Roach        return $response
53d1da5ba4SGreg Roach            ->withHeader('content-length', (string) $content_length);
54d1da5ba4SGreg Roach    }
55d1da5ba4SGreg Roach}
56