diff --git a/controller/Controller.php b/controller/Controller.php index 115251dae..ca50c0959 100644 --- a/controller/Controller.php +++ b/controller/Controller.php @@ -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; @@ -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; } diff --git a/tests/Http304Test.php b/tests/Http304Test.php index 6eb6eb590..f3d683b59 100644 --- a/tests/Http304Test.php +++ b/tests/Http304Test.php @@ -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); } @@ -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); } @@ -223,4 +229,10 @@ public function testHttp304() $content = ob_get_clean(); $this->assertEquals("", $content); } + + public function tearDown() + { + parent::tearDown(); + \Mockery::close(); + } }