Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 42712ee

Browse filesBrowse files
bug #34438 [HttpFoundation] Use Cache-Control: must-revalidate only if explicit lifetime has been given (mpdude)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpFoundation] Use `Cache-Control: must-revalidate` only if explicit lifetime has been given | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | This is really nit-picking: The conservative, safe default for `Cache-Control` is `private, no-cache` which means the response must not be served from cache unless it has been validated. If `Last-Modified` or `Expires` are present, we can relax `no-cache` to be `must-revalidate`, which means that _once the response has become stale_, it must be revalidated. An `ETag` alone does not give the response a lifetime, so IMO sticking with `no-cache` in this case would be more consistent. Commits ------- 1b1002b [HttpFoundation] Use `Cache-Control: must-revalidate` only if explicit lifetime has been given
2 parents 035d8a3 + 1b1002b commit 42712ee
Copy full SHA for 42712ee

File tree

Expand file treeCollapse file tree

4 files changed

+7
-11
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+7
-11
lines changed

‎src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,13 @@ public function makeDisposition($disposition, $filename, $filenameFallback = '')
309309
*/
310310
protected function computeCacheControlValue()
311311
{
312-
if (!$this->cacheControl && !$this->has('ETag') && !$this->has('Last-Modified') && !$this->has('Expires')) {
313-
return 'no-cache, private';
314-
}
315-
316312
if (!$this->cacheControl) {
313+
if ($this->has('Last-Modified') || $this->has('Expires')) {
314+
return 'private, must-revalidate'; // allows for heuristic expiration (RFC 7234 Section 4.2.2) in the case of "Last-Modified"
315+
}
316+
317317
// conservative by default
318-
return 'private, must-revalidate';
318+
return 'no-cache, private';
319319
}
320320

321321
$header = $this->getCacheControlHeader();

‎src/Symfony/Component/HttpFoundation/Tests/ResponseHeaderBagTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/ResponseHeaderBagTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public function testCacheControlHeader()
5151
$this->assertTrue($bag->hasCacheControlDirective('public'));
5252

5353
$bag = new ResponseHeaderBag(['ETag' => 'abcde']);
54-
$this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
54+
$this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
5555
$this->assertTrue($bag->hasCacheControlDirective('private'));
56-
$this->assertTrue($bag->hasCacheControlDirective('must-revalidate'));
56+
$this->assertTrue($bag->hasCacheControlDirective('no-cache'));
5757
$this->assertFalse($bag->hasCacheControlDirective('max-age'));
5858

5959
$bag = new ResponseHeaderBag(['Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT']);

‎src/Symfony/Component/HttpKernel/HttpCache/ResponseCacheStrategy.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/HttpCache/ResponseCacheStrategy.php
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ public function update(Response $response)
110110
$response->headers->set('Age', $this->age);
111111

112112
if ($this->isNotCacheableResponseEmbedded) {
113-
$response->setExpires($response->getDate());
114-
115113
if ($this->flagDirectives['no-store']) {
116114
$response->headers->set('Cache-Control', 'no-cache, no-store, must-revalidate');
117115
} else {

‎src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,6 @@ public function testEsiCacheForceValidation()
12421242
$this->request('GET', '/', [], [], true);
12431243
$this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
12441244
$this->assertNull($this->response->getTtl());
1245-
$this->assertTrue($this->response->mustRevalidate());
12461245
$this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
12471246
$this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
12481247
}
@@ -1273,7 +1272,6 @@ public function testEsiCacheForceValidationForHeadRequests()
12731272
// This can neither be cached nor revalidated, so it should be private/no cache
12741273
$this->assertEmpty($this->response->getContent());
12751274
$this->assertNull($this->response->getTtl());
1276-
$this->assertTrue($this->response->mustRevalidate());
12771275
$this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
12781276
$this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
12791277
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.