From 3a7fa7ede265d4e2d06c8c4030a1c6ddd8168669 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Fri, 17 Mar 2017 12:57:37 +0100 Subject: [PATCH] Set Date header in Response constructor already --- src/Symfony/Component/HttpFoundation/Response.php | 11 +++++++++++ .../Component/HttpFoundation/Tests/ResponseTest.php | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index 9d112d15785d..3eb8bd289ce0 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -201,6 +201,11 @@ public function __construct($content = '', $status = 200, $headers = array()) $this->setContent($content); $this->setStatusCode($status); $this->setProtocolVersion('1.0'); + + /* RFC2616 - 14.18 says all Responses need to have a Date */ + if (!$this->headers->has('Date')) { + $this->setDate(new \DateTime(null, new \DateTimeZone('UTC'))); + } } /** @@ -329,6 +334,7 @@ public function sendHeaders() return $this; } + /* RFC2616 - 14.18 says all Responses need to have a Date */ if (!$this->headers->has('Date')) { $this->setDate(\DateTime::createFromFormat('U', time())); } @@ -612,6 +618,11 @@ public function mustRevalidate() */ public function getDate() { + /* + RFC2616 - 14.18 says all Responses need to have a Date. + Make sure we provide one even if it the header + has been removed in the meantime. + */ if (!$this->headers->has('Date')) { $this->setDate(\DateTime::createFromFormat('U', time())); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php index d73dd3d344c3..453a05a9b022 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php @@ -276,8 +276,10 @@ public function testGetDate() $this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the date when the header has been modified'); $response = new Response('', 200); + $now = $this->createDateTimeNow(); $response->headers->remove('Date'); - $this->assertInstanceOf('\DateTime', $response->getDate()); + $date = $response->getDate(); + $this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the current Date when the header has previously been removed'); } public function testGetMaxAge()