From 0472625a00a193bd959a80a710d5a1e7985a4979 Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 28 Mar 2024 23:07:17 +0000 Subject: [PATCH] Attempt to make trailing slash optional. --- VERSION | 2 +- src/Http/ContentHandling/IContentHandler.php | 1 + src/Http/ErrorHandling/HtmlErrorHandler.php | 1 + src/Http/ErrorHandling/PlainErrorHandler.php | 1 + src/Http/Routing/HttpRouter.php | 12 ++++++++++++ 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 01bd5ce..0437a7a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2403.282229 +0.2403.282307 diff --git a/src/Http/ContentHandling/IContentHandler.php b/src/Http/ContentHandling/IContentHandler.php index 7dd9705..42d4146 100644 --- a/src/Http/ContentHandling/IContentHandler.php +++ b/src/Http/ContentHandling/IContentHandler.php @@ -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; diff --git a/src/Http/ErrorHandling/HtmlErrorHandler.php b/src/Http/ErrorHandling/HtmlErrorHandler.php index 0c3b5b4..2dffa58 100644 --- a/src/Http/ErrorHandling/HtmlErrorHandler.php +++ b/src/Http/ErrorHandling/HtmlErrorHandler.php @@ -6,6 +6,7 @@ namespace Index\Http\ErrorHandling; use Index\Http\{HttpResponseBuilder,HttpRequest}; + class HtmlErrorHandler implements IErrorHandler { private const TEMPLATE = << diff --git a/src/Http/ErrorHandling/PlainErrorHandler.php b/src/Http/ErrorHandling/PlainErrorHandler.php index 1d84252..7524bd4 100644 --- a/src/Http/ErrorHandling/PlainErrorHandler.php +++ b/src/Http/ErrorHandling/PlainErrorHandler.php @@ -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(); diff --git a/src/Http/Routing/HttpRouter.php b/src/Http/Routing/HttpRouter.php index 8b5a72d..4e100e7 100644 --- a/src/Http/Routing/HttpRouter.php +++ b/src/Http/Routing/HttpRouter.php @@ -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) {