Attempt to make trailing slash optional.

This commit is contained in:
flash 2024-03-28 23:07:17 +00:00
parent 77ab1e7f9f
commit 0472625a00
5 changed files with 16 additions and 1 deletions

View file

@ -1 +1 @@
0.2403.282229
0.2403.282307

View file

@ -6,6 +6,7 @@
namespace Index\Http\ContentHandling;
use Index\Http\HttpResponseBuilder;
interface IContentHandler {
function match(mixed $content): bool;
function handle(HttpResponseBuilder $response, mixed $content): void;

View file

@ -6,6 +6,7 @@
namespace Index\Http\ErrorHandling;
use Index\Http\{HttpResponseBuilder,HttpRequest};
class HtmlErrorHandler implements IErrorHandler {
private const TEMPLATE = <<<HTML
<!doctype html>

View file

@ -6,6 +6,7 @@
namespace Index\Http\ErrorHandling;
use Index\Http\{HttpResponseBuilder,HttpRequest};
class PlainErrorHandler implements IErrorHandler {
public function handle(HttpResponseBuilder $response, HttpRequest $request, int $code, string $message): void {
$response->setTypePlain();

View file

@ -85,6 +85,10 @@ class HttpRouter implements IRouter {
if(!str_contains($path, '(') || !str_contains($path, ')'))
return false;
// make trailing slash optional
if(!$prefixMatch && str_ends_with($path, '/'))
$path .= '?';
return sprintf('#^%s%s#su', $path, $prefixMatch ? '' : '$');
}
@ -111,6 +115,9 @@ class HttpRouter implements IRouter {
$prepared = self::preparePath($path, false);
if($prepared === false) {
if(str_ends_with($path, '/'))
$path = substr($path, 0, -1);
if(array_key_exists($path, $this->staticRoutes))
$this->staticRoutes[$path][$method] = $handler;
else
@ -124,6 +131,11 @@ class HttpRouter implements IRouter {
}
public function resolve(string $method, string $path): ResolvedRouteInfo {
if($method === 'head')
$method = 'get';
if(str_ends_with($path, '/'))
$path = substr($path, 0, -1);
$middlewares = [];
foreach($this->middlewares as $mwInfo) {