Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/DocumentRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,10 @@ private function checkPreconditions(Request $request, int $mtime, string $etag):
return Precondition::NotModified;
}

// If-Modified-Since must be ignored when If-None-Match is present (RFC 9110 section 13.1.3)
$ifModifiedSince = $request->getHeader("If-Modified-Since");
$ifModifiedSince = $ifModifiedSince ? \strtotime($ifModifiedSince) : 0;
if ($ifModifiedSince && $mtime > $ifModifiedSince) {
$ifModifiedSince = $ifModifiedSince && !$ifNoneMatch ? \strtotime($ifModifiedSince) : 0;
if ($ifModifiedSince && $mtime <= $ifModifiedSince) {
return Precondition::NotModified;
}

Expand Down
57 changes: 56 additions & 1 deletion test/DocumentRootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function testPreconditionNotModified(): void

$request = new Request($this->createMock(Client::class), "GET", $this->createUri("/index.htm"), [
"if-match" => $etag,
"if-modified-since" => "2.1.1970",
"if-modified-since" => \gmdate("D, d M Y H:i:s", \filemtime($diskPath))." GMT",
]);

$response = $this->root->handleRequest($request);
Expand All @@ -260,6 +260,61 @@ public function testPreconditionNotModified(): void
$this->assertSame($etag, $response->getHeader("etag"));
}

public function testIfModifiedSinceOlderThanMtimeReturnsFile(): void
{
$diskPath = \realpath(self::fixturePath())."/index.htm";

$request = new Request($this->createMock(Client::class), "GET", $this->createUri("/index.htm"), [
"if-modified-since" => \gmdate("D, d M Y H:i:s", \filemtime($diskPath) - 3600)." GMT",
]);

$response = $this->root->handleRequest($request);

$this->assertSame(HttpStatus::OK, $response->getStatus());
$this->assertSame(\file_get_contents($diskPath), $response->getBody()->read());
}

public function testIfModifiedSinceEqualToMtimeReturnsNotModified(): void
{
$diskPath = \realpath(self::fixturePath())."/index.htm";

$request = new Request($this->createMock(Client::class), "GET", $this->createUri("/index.htm"), [
"if-modified-since" => \gmdate("D, d M Y H:i:s", \filemtime($diskPath))." GMT",
]);

$response = $this->root->handleRequest($request);

$this->assertSame(HttpStatus::NOT_MODIFIED, $response->getStatus());
}

public function testIfModifiedSinceNewerThanMtimeReturnsNotModified(): void
{
$diskPath = \realpath(self::fixturePath())."/index.htm";

$request = new Request($this->createMock(Client::class), "GET", $this->createUri("/index.htm"), [
"if-modified-since" => \gmdate("D, d M Y H:i:s", \filemtime($diskPath) + 3600)." GMT",
]);

$response = $this->root->handleRequest($request);

$this->assertSame(HttpStatus::NOT_MODIFIED, $response->getStatus());
}

public function testIfModifiedSinceIsIgnoredWhenIfNoneMatchDoesNotMatch(): void
{
$diskPath = \realpath(self::fixturePath())."/index.htm";

$request = new Request($this->createMock(Client::class), "GET", $this->createUri("/index.htm"), [
"if-none-match" => "\"not-the-etag\"",
"if-modified-since" => \gmdate("D, d M Y H:i:s", \filemtime($diskPath) + 3600)." GMT",
]);

$response = $this->root->handleRequest($request);

$this->assertSame(HttpStatus::OK, $response->getStatus());
$this->assertSame(\file_get_contents($diskPath), $response->getBody()->read());
}

public function testPreconditionRangeFail(): void
{
$this->root->setUseEtagInode(false);
Expand Down
Loading