Skip to content

Commit d9a55ed

Browse files
committed
merged branch dlsniper/fix-expiry (PR symfony#6471)
This PR was squashed before being merged into the 2.1 branch (closes symfony#6471). Commits ------- 87b6cc2 Fix Expires when the header is -1 Discussion ---------- Fix Expires when the header is -1 This fixes the issue described in symfony#6469. Let me know if I've missed something. Thanks. --------------------------------------------------------------------------- by blaugueux at 2012-12-27T20:22:23Z @dlsniper how can i help here, i really need to fix this issue --------------------------------------------------------------------------- by dlsniper at 2012-12-27T20:28:53Z I think @fabpot needs to get me some feedback on this but meanwhile you should try the proposed patch and let me know if it fixes the problem for you or I should add more tests to this. Thanks! --------------------------------------------------------------------------- by blaugueux at 2012-12-30T14:37:58Z @dlsniper your patch is working great for me but it breaks tests. I agree that @fabpot point of view is required. --------------------------------------------------------------------------- by dlsniper at 2012-12-30T18:38:38Z @blaugueux Can you let me know what tests are failing? the one included in this PR or some custom ones from you? If it's the later, can you share some so that I can add them? Thanks! --------------------------------------------------------------------------- by arendjantetteroo at 2013-01-02T12:45:48Z The headerbag fix works here. I'm not sure if the extra check in Response is really necessary, it works here without it.
2 parents 36627c4 + 87b6cc2 commit d9a55ed

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)