Skip to content

Commit

Permalink
fix: Pass the ltrimed path to handleRoute/Middleware in Router
Browse files Browse the repository at this point in the history
Closes #35
  • Loading branch information
Gashmob committed Jul 3, 2024
1 parent b6567b9 commit c0b00bf
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions include/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public function route(ServerRequestInterface $request): void

$this->request = array_reduce(
$middlewares,
fn(ServerRequestInterface $mid_request, MiddlewareInformation $middleware) => $this->handleMiddleware($middleware, $mid_request),
fn(ServerRequestInterface $mid_request, MiddlewareInformation $middleware) => $this->handleMiddleware($middleware, $path, $mid_request),
$this->request,
);

$this->response = $this->handleRoute($route, $this->request);
$this->response = $this->handleRoute($route, $path, $this->request);
} catch (HTTPException $exception) {
$this->response = $exception->toResponse();
} catch (Throwable $throwable) {
Expand Down Expand Up @@ -96,10 +96,10 @@ public function response(): void
$final_handler->writeResponse($response);
}

private function handleMiddleware(MiddlewareInformation $middleware, ServerRequestInterface $request): ServerRequestInterface
private function handleMiddleware(MiddlewareInformation $middleware, string $path, ServerRequestInterface $request): ServerRequestInterface
{
$attributes = [];
assert(preg_match($middleware->route_regex, $request->getUri()->getPath(), $attributes) === 1);
assert(preg_match($middleware->route_regex, $path, $attributes) === 1);
foreach ($attributes as $key => $value) {
// preg_match array result should have int key for 'normal' groups and string key for named groups
if (is_string($key)) {
Expand All @@ -110,11 +110,11 @@ private function handleMiddleware(MiddlewareInformation $middleware, ServerReque
return $middleware->handler->process($request);
}

private function handleRoute(RouteInformation $route, ServerRequestInterface $request): ResponseInterface
private function handleRoute(RouteInformation $route, string $path, ServerRequestInterface $request): ResponseInterface
{
$factory = new HttpFactory();
$attributes = [];
assert(preg_match($route->route_regex, $request->getUri()->getPath(), $attributes) === 1);
assert(preg_match($route->route_regex, $path, $attributes) === 1);
foreach ($attributes as $key => $value) {
// preg_match array result should have int key for 'normal' groups and string key for named groups
if (is_string($key)) {
Expand Down

0 comments on commit c0b00bf

Please sign in to comment.