Attempt to make trailing slash optional.
This commit is contained in:
parent
77ab1e7f9f
commit
0472625a00
5 changed files with 16 additions and 1 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
|||
0.2403.282229
|
||||
0.2403.282307
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
namespace Index\Http\ErrorHandling;
|
||||
|
||||
use Index\Http\{HttpResponseBuilder,HttpRequest};
|
||||
|
||||
class HtmlErrorHandler implements IErrorHandler {
|
||||
private const TEMPLATE = <<<HTML
|
||||
<!doctype html>
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue