Skip to content

Commit

Permalink
Merge pull request #984 from NatLibFi/fix-modified-date-format
Browse files Browse the repository at this point in the history
HTTP date format fixes
  • Loading branch information
osma authored May 12, 2020
2 parents 2f28514 + c24af1b commit 80fad12
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
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();
}
}

0 comments on commit 80fad12

Please sign in to comment.