diff --git a/src/Symfony/Component/HttpFoundation/HeaderBag.php b/src/Symfony/Component/HttpFoundation/HeaderBag.php index 2360e55bb4..64b8bd7c4e 100644 --- a/src/Symfony/Component/HttpFoundation/HeaderBag.php +++ b/src/Symfony/Component/HttpFoundation/HeaderBag.php @@ -235,6 +235,14 @@ class HeaderBag implements \IteratorAggregate, \Countable return $default; } + if (-1 === $value) { + /** + * Since we need to return a valid date time a older date has been chosen + * https://github.com/symfony/symfony/pull/6471#discussion_r2527156 + */ + $value = 'Sat, 01 Jan 00 00:00:00 +0000'; + } + if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) { throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value)); } diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index 7428b9a9c3..a25572138a 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -693,6 +693,10 @@ class Response } if (null !== $this->getExpires()) { + if (!$this->getExpires() instanceof \DateTime) { + return 0; + } + return $this->getExpires()->format('U') - $this->getDate()->format('U'); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php index 4fc93c33ef..38853ecc8c 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php @@ -164,6 +164,11 @@ class ResponseTest extends \PHPUnit_Framework_TestCase $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822)); $this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present'); + $response = new Response(); + $response->headers->set('Cache-Control', 'must-revalidate'); + $response->headers->set('Expires', -1); + $this->assertEquals('Sat, 01 Jan 00 00:00:00 +0000', $response->getExpires()->format(DATE_RFC822)); + $response = new Response(); $this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available'); } @@ -214,6 +219,11 @@ class ResponseTest extends \PHPUnit_Framework_TestCase $response = new Response(); $response->expire(); $this->assertFalse($response->headers->has('Age'), '->expire() does nothing when the response does not include freshness information'); + + $response = new Response(); + $response->headers->set('Expires', -1); + $response->expire(); + $this->assertEquals(0, $response->headers->get('Age'), '->expire() does not set the Age to 0'); } public function testGetTtl()