Skip to content

Commit 87b6cc2

Browse files
dlsniperfabpot
authored andcommitted
Fix Expires when the header is -1
1 parent 36627c4 commit 87b6cc2

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/Symfony/Component/HttpFoundation/HeaderBag.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ public function getDate($key, \DateTime $default = null)
235235
return $default;
236236
}
237237

238+
if (-1 === $value) {
239+
/**
240+
* Since we need to return a valid date time a older date has been chosen
241+
* https://github.com/symfony/symfony/pull/6471#discussion_r2527156
242+
*/
243+
$value = 'Sat, 01 Jan 00 00:00:00 +0000';
244+
}
245+
238246
if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
239247
throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
240248
}

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,10 @@ public function getMaxAge()
693693
}
694694

695695
if (null !== $this->getExpires()) {
696+
if (!$this->getExpires() instanceof \DateTime) {
697+
return 0;
698+
}
699+
696700
return $this->getExpires()->format('U') - $this->getDate()->format('U');
697701
}
698702

src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ public function testGetMaxAge()
164164
$response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
165165
$this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
166166

167+
$response = new Response();
168+
$response->headers->set('Cache-Control', 'must-revalidate');
169+
$response->headers->set('Expires', -1);
170+
$this->assertEquals('Sat, 01 Jan 00 00:00:00 +0000', $response->getExpires()->format(DATE_RFC822));
171+
167172
$response = new Response();
168173
$this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
169174
}
@@ -214,6 +219,11 @@ public function testExpire()
214219
$response = new Response();
215220
$response->expire();
216221
$this->assertFalse($response->headers->has('Age'), '->expire() does nothing when the response does not include freshness information');
222+
223+
$response = new Response();
224+
$response->headers->set('Expires', -1);
225+
$response->expire();
226+
$this->assertEquals(0, $response->headers->get('Age'), '->expire() does not set the Age to 0');
217227
}
218228

219229
public function testGetTtl()

0 commit comments

Comments
 (0)