mirror of
https://github.com/AzuraCast/AzuraCast.git
synced 2024-06-14 21:26:37 +00:00
39 lines
1.1 KiB
PHP
39 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Middleware;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
/**
|
|
* Remove trailing slash from all URLs when routing.
|
|
*/
|
|
class RemoveSlashes implements MiddlewareInterface
|
|
{
|
|
/**
|
|
* @param ServerRequestInterface $request
|
|
* @param RequestHandlerInterface $handler
|
|
*/
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
{
|
|
$uri = $request->getUri();
|
|
$path = $uri->getPath();
|
|
|
|
if ($path !== '/' && substr($path, -1) === '/') {
|
|
// permanently redirect paths with a trailing slash
|
|
// to their non-trailing counterpart
|
|
$uri = $uri->withPath(substr($path, 0, -1));
|
|
|
|
/** @var ResponseInterface $response */
|
|
$response = new Response(308);
|
|
|
|
return $response->withHeader('Location', (string)$uri);
|
|
}
|
|
|
|
return $handler->handle($request);
|
|
}
|
|
}
|