Skip to content

Commit

Permalink
Merge pull request #30 from nox7/support-http-head-requests-on-static…
Browse files Browse the repository at this point in the history
…-files

Handle HEAD request on a static file
  • Loading branch information
nox7 authored Aug 23, 2022
2 parents 10b063f + 558d6b6 commit e797a71
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(
* @return void
*/
public function processRequestAsStaticFile(): void{
if ($this->requestMethod === "get") {
if ($this->requestMethod === "get" || $this->requestMethod === "head") {
$mimeType = $this->noxInstance->staticFileHandler->getStaticFileMime($this->requestPath);
// Do not serve unknown mime types
if ($mimeType !== null) {
Expand All @@ -70,8 +70,15 @@ public function processRequestAsStaticFile(): void{
header(sprintf("cache-control: max-age=%d", $cacheTime));
}

$fileContents = file_get_contents(realpath($staticFilePath));
header("content-type: $mimeType");
print(file_get_contents(realpath($staticFilePath)));
header("content-length: " . strlen($fileContents));

// Only output for GET methods and not HEAD
if ($this->requestMethod === "get") {
print($fileContents);
}

exit();
}
}
Expand Down

0 comments on commit e797a71

Please sign in to comment.