From 723b5ca3de95db0ae0e9148bd0db5afbbad973ef Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Mon, 18 Nov 2019 13:24:53 +0000 Subject: [PATCH] [HttpFoundation] Use `Cache-Control: must-revalidate` only if explicit lifetime has been given --- ResponseHeaderBag.php | 10 +++++----- Tests/ResponseHeaderBagTest.php | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ResponseHeaderBag.php b/ResponseHeaderBag.php index 1dc8dc2c5..e11b98a10 100644 --- a/ResponseHeaderBag.php +++ b/ResponseHeaderBag.php @@ -309,13 +309,13 @@ public function makeDisposition($disposition, $filename, $filenameFallback = '') */ protected function computeCacheControlValue() { - if (!$this->cacheControl && !$this->has('ETag') && !$this->has('Last-Modified') && !$this->has('Expires')) { - return 'no-cache, private'; - } - if (!$this->cacheControl) { + if ($this->has('Last-Modified') || $this->has('Expires')) { + return 'private, must-revalidate'; // allows for heuristic expiration (RFC 7234 Section 4.2.2) in the case of "Last-Modified" + } + // conservative by default - return 'private, must-revalidate'; + return 'no-cache, private'; } $header = $this->getCacheControlHeader(); diff --git a/Tests/ResponseHeaderBagTest.php b/Tests/ResponseHeaderBagTest.php index d85f6e112..4e3a6c82b 100644 --- a/Tests/ResponseHeaderBagTest.php +++ b/Tests/ResponseHeaderBagTest.php @@ -51,9 +51,9 @@ public function testCacheControlHeader() $this->assertTrue($bag->hasCacheControlDirective('public')); $bag = new ResponseHeaderBag(['ETag' => 'abcde']); - $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control')); + $this->assertEquals('no-cache, private', $bag->get('Cache-Control')); $this->assertTrue($bag->hasCacheControlDirective('private')); - $this->assertTrue($bag->hasCacheControlDirective('must-revalidate')); + $this->assertTrue($bag->hasCacheControlDirective('no-cache')); $this->assertFalse($bag->hasCacheControlDirective('max-age')); $bag = new ResponseHeaderBag(['Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT']);