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;
|
namespace Index\Http\ContentHandling;
|
||||||
|
|
||||||
use Index\Http\HttpResponseBuilder;
|
use Index\Http\HttpResponseBuilder;
|
||||||
|
|
||||||
interface IContentHandler {
|
interface IContentHandler {
|
||||||
function match(mixed $content): bool;
|
function match(mixed $content): bool;
|
||||||
function handle(HttpResponseBuilder $response, mixed $content): void;
|
function handle(HttpResponseBuilder $response, mixed $content): void;
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
namespace Index\Http\ErrorHandling;
|
namespace Index\Http\ErrorHandling;
|
||||||
|
|
||||||
use Index\Http\{HttpResponseBuilder,HttpRequest};
|
use Index\Http\{HttpResponseBuilder,HttpRequest};
|
||||||
|
|
||||||
class HtmlErrorHandler implements IErrorHandler {
|
class HtmlErrorHandler implements IErrorHandler {
|
||||||
private const TEMPLATE = <<<HTML
|
private const TEMPLATE = <<<HTML
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
namespace Index\Http\ErrorHandling;
|
namespace Index\Http\ErrorHandling;
|
||||||
|
|
||||||
use Index\Http\{HttpResponseBuilder,HttpRequest};
|
use Index\Http\{HttpResponseBuilder,HttpRequest};
|
||||||
|
|
||||||
class PlainErrorHandler implements IErrorHandler {
|
class PlainErrorHandler implements IErrorHandler {
|
||||||
public function handle(HttpResponseBuilder $response, HttpRequest $request, int $code, string $message): void {
|
public function handle(HttpResponseBuilder $response, HttpRequest $request, int $code, string $message): void {
|
||||||
$response->setTypePlain();
|
$response->setTypePlain();
|
||||||
|
|
|
@ -85,6 +85,10 @@ class HttpRouter implements IRouter {
|
||||||
if(!str_contains($path, '(') || !str_contains($path, ')'))
|
if(!str_contains($path, '(') || !str_contains($path, ')'))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
// make trailing slash optional
|
||||||
|
if(!$prefixMatch && str_ends_with($path, '/'))
|
||||||
|
$path .= '?';
|
||||||
|
|
||||||
return sprintf('#^%s%s#su', $path, $prefixMatch ? '' : '$');
|
return sprintf('#^%s%s#su', $path, $prefixMatch ? '' : '$');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,6 +115,9 @@ class HttpRouter implements IRouter {
|
||||||
|
|
||||||
$prepared = self::preparePath($path, false);
|
$prepared = self::preparePath($path, false);
|
||||||
if($prepared === false) {
|
if($prepared === false) {
|
||||||
|
if(str_ends_with($path, '/'))
|
||||||
|
$path = substr($path, 0, -1);
|
||||||
|
|
||||||
if(array_key_exists($path, $this->staticRoutes))
|
if(array_key_exists($path, $this->staticRoutes))
|
||||||
$this->staticRoutes[$path][$method] = $handler;
|
$this->staticRoutes[$path][$method] = $handler;
|
||||||
else
|
else
|
||||||
|
@ -124,6 +131,11 @@ class HttpRouter implements IRouter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resolve(string $method, string $path): ResolvedRouteInfo {
|
public function resolve(string $method, string $path): ResolvedRouteInfo {
|
||||||
|
if($method === 'head')
|
||||||
|
$method = 'get';
|
||||||
|
if(str_ends_with($path, '/'))
|
||||||
|
$path = substr($path, 0, -1);
|
||||||
|
|
||||||
$middlewares = [];
|
$middlewares = [];
|
||||||
|
|
||||||
foreach($this->middlewares as $mwInfo) {
|
foreach($this->middlewares as $mwInfo) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue