Skip to content

Commit

Permalink
Merge pull request #592 from getformwork/feature/allow-head-requests
Browse files Browse the repository at this point in the history
Allow HEAD requests
  • Loading branch information
giuscris authored Oct 25, 2024
2 parents 351ae93 + 570bfb8 commit f4da7ed
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
15 changes: 12 additions & 3 deletions formwork/src/Http/FileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public function send(): void

$this->sendHeaders();

$length = $this->length ?? $this->fileSize;

if ($length === 0) {
return;
}

$file = fopen($this->path, 'r');
$output = fopen('php://output', 'w');

Expand All @@ -58,8 +64,6 @@ public function send(): void
fseek($file, $this->offset);
}

$length = $this->length ?? $this->fileSize;

while ($length > 0 && !feof($file)) {
$read = fread($file, self::CHUNK_SIZE);

Expand All @@ -84,10 +88,15 @@ public function prepare(Request $request): static
{
parent::prepare($request);

if (!isset($this->headers['Accept-Ranges']) && $request->method() === RequestMethod::GET) {
if (!isset($this->headers['Accept-Ranges']) && in_array($request->method(), [RequestMethod::HEAD, RequestMethod::GET], true)) {
$this->headers['Accept-Ranges'] = 'bytes';
}

if ($request->method() === RequestMethod::HEAD || $this->requiresEmptyContent()) {
$this->length = 0;
return $this;
}

if ($request->method() === RequestMethod::GET && preg_match('/^bytes=(\d+)?-(\d+)?$/', $request->headers()->get('Range', ''), $matches, PREG_UNMATCHED_AS_NULL)) {
[, $start, $end] = $matches;

Expand Down
1 change: 1 addition & 0 deletions formwork/src/Http/RequestMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

enum RequestMethod: string
{
case HEAD = 'HEAD';
case GET = 'GET';
case POST = 'POST';
case PUT = 'PUT';
Expand Down
12 changes: 11 additions & 1 deletion formwork/src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Response implements ResponseInterface
public function __construct(protected string $content, protected ResponseStatus $responseStatus = ResponseStatus::OK, array $headers = [])
{
$headers += [
'Content-Type' => Header::make(['text/html', 'charset' => 'utf-8']),
'Content-Length' => (string) strlen($content),
'Content-Type' => Header::make(['text/html', 'charset' => 'utf-8']),
];
$this->headers = $headers;
}
Expand Down Expand Up @@ -61,6 +62,10 @@ public function headers(): array
*/
public function prepare(Request $request): static
{
if ($request->method() === RequestMethod::HEAD || $this->requiresEmptyContent()) {
$this->content = '';
}

return $this;
}

Expand Down Expand Up @@ -116,4 +121,9 @@ public static function cleanOutputBuffers(): void
ob_end_clean();
}
}

protected function requiresEmptyContent(): bool
{
return in_array($this->responseStatus, [ResponseStatus::NoContent, ResponseStatus::NotModified], true);
}
}
12 changes: 10 additions & 2 deletions formwork/src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Formwork\Http\Request;
use Formwork\Http\RequestMethod;
use Formwork\Http\Response;
use Formwork\Parsers\Php;
use Formwork\Router\Exceptions\InvalidRouteException;
Expand All @@ -26,7 +27,7 @@ class Router
/**
* Valid request methods
*/
protected const REQUEST_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
protected const REQUEST_METHODS = ['HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE'];

/**
* Valid param separators
Expand Down Expand Up @@ -449,7 +450,14 @@ protected function validateParamName(?string $param, array $params): string
*/
protected function matchMethods(array $methods): bool
{
return in_array($this->request->method()->value, $methods, true);
$method = $this->request->method();

// HEAD method is equivalent to GET method
if ($method === RequestMethod::HEAD) {
$method = RequestMethod::GET;
}

return in_array($method->value, $methods, true);
}

/**
Expand Down

0 comments on commit f4da7ed

Please sign in to comment.