From a0bae94f882966cc60707b8a0b55ca1cd6e6029c Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Tue, 15 Feb 2011 11:00:52 -0800 Subject: [PATCH 1/3] [HttpFoundation] updated ResponseHeaderBag to compute Cache-Control whenever any of the headers it considers changes --- src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php | 2 +- .../Tests/Component/HttpFoundation/ResponseHeaderBagTest.php | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php b/src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php index 42e6bf2b8c634..c0fe501eeaac2 100644 --- a/src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php +++ b/src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php @@ -54,7 +54,7 @@ public function set($key, $values, $replace = true) parent::set($key, $values, $replace); // ensure the cache-control header has sensible defaults - if ('cache-control' === strtr(strtolower($key), '_', '-')) { + if (in_array(strtr(strtolower($key), '_', '-'), array('cache-control', 'etag', 'last-modified', 'expires'))) { $computed = $this->computeCacheControlValue(); $this->headers['cache-control'] = array($computed); $this->computedCacheControl = $this->parseCacheControl($computed); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/ResponseHeaderBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/ResponseHeaderBagTest.php index ba15ff37c490a..1b0551531dc54 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/ResponseHeaderBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/ResponseHeaderBagTest.php @@ -57,5 +57,9 @@ public function testCacheControlHeader() $bag = new ResponseHeaderBag(array('cache-control' => 'public, max-age=100')); $this->assertEquals('max-age=100, public', $bag->get('Cache-Control')); + + $bag = new ResponseHeaderBag(); + $bag->set('Last-Modified', 'abcde'); + $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control')); } } From 3e131f5b709c5f32d3f1057fe9eb3ad3d1344efd Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Mon, 21 Feb 2011 19:16:55 -0800 Subject: [PATCH 2/3] [HttpKernel] fixed invalid test According to ResponseHeaderBag::computeCacheControlValue(), a response with an ETag but no explicit Cache-Control header should have a sensible Cache-Control of "private, must-revalidate" set. According to Response::isCacheable(), a response that includes a private Cache-Controls is not considered cacheable. Therefore, in order for this test response to be cacheable and stored, it requires an explicit Cache-Control of public. --- .../Tests/Component/HttpKernel/HttpCache/HttpCacheTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Symfony/Tests/Component/HttpKernel/HttpCache/HttpCacheTest.php b/tests/Symfony/Tests/Component/HttpKernel/HttpCache/HttpCacheTest.php index d6a5ba7f2afaf..a1c113a8fc61d 100644 --- a/tests/Symfony/Tests/Component/HttpKernel/HttpCache/HttpCacheTest.php +++ b/tests/Symfony/Tests/Component/HttpKernel/HttpCache/HttpCacheTest.php @@ -168,6 +168,7 @@ public function testValidatesPrivateResponsesCachedOnTheClient() $response->setContent('private data'); } } else { + $response->headers->set('Cache-Control', 'public'); $response->setETag('"public tag"'); if (in_array('"public tag"', $etags)) { $response->setStatusCode(304); From fb005396dcb36ac1600ca1b17990cc73e4fd9f8f Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Mon, 21 Feb 2011 21:53:04 -0800 Subject: [PATCH 3/3] [HttpKernel] fixed another test where an explicit cache-control header is necessary --- .../Tests/Component/HttpKernel/HttpCache/HttpCacheTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Symfony/Tests/Component/HttpKernel/HttpCache/HttpCacheTest.php b/tests/Symfony/Tests/Component/HttpKernel/HttpCache/HttpCacheTest.php index a1c113a8fc61d..28ad9f60c6264 100644 --- a/tests/Symfony/Tests/Component/HttpKernel/HttpCache/HttpCacheTest.php +++ b/tests/Symfony/Tests/Component/HttpKernel/HttpCache/HttpCacheTest.php @@ -693,6 +693,7 @@ public function testReplacesCachedResponsesWhenValidationResultsInNon304Response $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time, &$count) { $response->headers->set('Last-Modified', $time->format(DATE_RFC2822)); + $response->headers->set('Cache-Control', 'public'); switch (++$count) { case 1: $response->setContent('first response');