Skip to content

Commit

Permalink
Fix Expires when the header is -1
Browse files Browse the repository at this point in the history
  • Loading branch information
dlsniper authored and fabpot committed Jan 2, 2013
1 parent 36627c4 commit 87b6cc2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpFoundation/HeaderBag.php
Expand Up @@ -235,6 +235,14 @@ public function getDate($key, \DateTime $default = null)
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));
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -693,6 +693,10 @@ public function getMaxAge()
}

if (null !== $this->getExpires()) {
if (!$this->getExpires() instanceof \DateTime) {
return 0;
}

return $this->getExpires()->format('U') - $this->getDate()->format('U');
}

Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Expand Up @@ -164,6 +164,11 @@ public function testGetMaxAge()
$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');
}
Expand Down Expand Up @@ -214,6 +219,11 @@ public function testExpire()
$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()
Expand Down

0 comments on commit 87b6cc2

Please sign in to comment.