Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP date format fixes #984

Merged
merged 1 commit into from
May 12, 2020
Merged
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
9 changes: 5 additions & 4 deletions controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ protected function sendNotModifiedHeader($modifiedDate): bool
{
if ($modifiedDate) {
$ifModifiedSince = $this->getIfModifiedSince();
$this->sendHeader("Last-Modified: " . $modifiedDate->format('Y-m-d H:i:s'));
$this->sendHeader("Last-Modified: " . $modifiedDate->format('D, d M Y H:i:s \G\M\T'));
if ($ifModifiedSince !== null && $ifModifiedSince >= $modifiedDate) {
$this->sendHeader("HTTP/1.0 304 Not Modified");
return true;
Expand All @@ -308,9 +308,10 @@ protected function sendNotModifiedHeader($modifiedDate): bool
protected function getIfModifiedSince()
{
$ifModifiedSince = null;
if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
// example value set by a browser: "2019-04-13 08:28:23"
$ifModifiedSince = DateTime::createFromFormat("Y-m-d H:i:s", $_SERVER["HTTP_IF_MODIFIED_SINCE"]);
$ifModSinceHeader = filter_input(INPUT_SERVER, 'HTTP_IF_MODIFIED_SINCE', FILTER_SANITIZE_STRING);
if ($ifModSinceHeader) {
// example value set by a browser: "Mon, 11 May 2020 10:46:57 GMT"
$ifModifiedSince = new DateTime($ifModSinceHeader);
}
return $ifModifiedSince;
}
Expand Down
16 changes: 14 additions & 2 deletions tests/Http304Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ public function testHttp304FirstEverRequest()
$modifiedDate = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
$this->controller
->shouldReceive("getModifiedDate")
->once()
->andReturn($modifiedDate);
$this->controller
->shouldReceive("sendHeader")
->withArgs(["Last-Modified: " . $modifiedDate->format('Y-m-d H:i:s')])
->once()
->withArgs(["Last-Modified: " . $modifiedDate->format('D, d M Y H:i:s \G\M\T')])
->andReturn(true);
}

Expand Down Expand Up @@ -204,16 +206,20 @@ public function testHttp304()
$ifModifiedSince = DateTime::createFromFormat('j-M-Y', '15-Feb-2019');
$this->controller
->shouldReceive("getModifiedDate")
->once()
->andReturn($modifiedDate);
$this->controller
->shouldReceive("getIfModifiedSince")
->once()
->andReturn($ifModifiedSince);
$this->controller
->shouldReceive("sendHeader")
->withArgs(["Last-Modified: " . $modifiedDate->format('Y-m-d H:i:s')])
->once()
->withArgs(["Last-Modified: " . $modifiedDate->format('D, d M Y H:i:s \G\M\T')])
->andReturn(true);
$this->controller
->shouldReceive("sendHeader")
->once()
->withArgs(["HTTP/1.0 304 Not Modified"])
->andReturn(true);
}
Expand All @@ -223,4 +229,10 @@ public function testHttp304()
$content = ob_get_clean();
$this->assertEquals("", $content);
}

public function tearDown()
{
parent::tearDown();
\Mockery::close();
}
}