From 9d5b050b8928435416a7efbebe2a19ae8e626224 Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 28 Mar 2024 23:27:04 +0000 Subject: [PATCH] Revert to implicitly supporting HEAD.. --- VERSION | 2 +- src/Http/Routing/HttpRouter.php | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/VERSION b/VERSION index 0437a7a..fe1507c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2403.282307 +0.2403.282326 diff --git a/src/Http/Routing/HttpRouter.php b/src/Http/Routing/HttpRouter.php index e44fa87..0d4e084 100644 --- a/src/Http/Routing/HttpRouter.php +++ b/src/Http/Routing/HttpRouter.php @@ -113,9 +113,6 @@ class HttpRouter implements IRouter { if(trim($method) !== $method) throw new InvalidArgumentException('$method may start or end with whitespace'); - if($method === 'GET') - $this->add('HEAD', $path, $handler); - $prepared = self::preparePath($path, false); if($prepared === false) { if(str_ends_with($path, '/')) @@ -173,6 +170,8 @@ class HttpRouter implements IRouter { $method = strtoupper($method); if(array_key_exists($method, $methods)) $handler = $methods[$method]; + elseif($method === 'HEAD' && array_key_exists('GET', $methods)) + $handler = $methods['GET']; return new ResolvedRouteInfo($middlewares, array_keys($methods), $handler, $args); } @@ -228,7 +227,7 @@ class HttpRouter implements IRouter { } } - self::output($response->toResponse()); + self::output($response->toResponse(), $request->getMethod() !== 'HEAD'); } public function writeErrorPage(HttpResponseBuilder $response, HttpRequest $request, int $statusCode): void { @@ -236,7 +235,7 @@ class HttpRouter implements IRouter { $this->errorHandler->handle($response, $request, $response->getStatusCode(), $response->getStatusText()); } - public static function output(HttpResponse $response): void { + public static function output(HttpResponse $response, bool $includeBody): void { $version = $response->getHttpVersion(); header(sprintf( 'HTTP/%d.%d %03d %s', @@ -255,7 +254,7 @@ class HttpRouter implements IRouter { header(sprintf('%s: %s', $name, (string)$line)); } - if($response->hasContent()) + if($includeBody && $response->hasContent()) echo (string)$response->getContent(); } }