From 66afa0105764bd42b13e29c140b02cce97c89c24 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 22 Jun 2016 15:14:54 +0200 Subject: [PATCH] [HttpFoundation] added private by default when setting Cache-Control to no-cache --- .../Component/HttpFoundation/Response.php | 2 +- .../HttpFoundation/ResponseHeaderBag.php | 2 +- .../HttpFoundation/Tests/HeaderBagTest.php | 9 ++++++ .../Tests/ResponseHeaderBagTest.php | 28 ++++++++++++------- .../HttpFoundation/Tests/ResponseTest.php | 2 +- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index 07a522e3ff..a5783da5b6 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -307,7 +307,7 @@ class Response } // Check if we need to send extra expire info headers - if ('1.0' == $this->getProtocolVersion() && 'no-cache' == $this->headers->get('Cache-Control')) { + if ('1.0' == $this->getProtocolVersion() && false !== strpos($this->headers->get('Cache-Control'), 'no-cache')) { $this->headers->set('pragma', 'no-cache'); $this->headers->set('expires', -1); } diff --git a/src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php b/src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php index 1d26e86b9b..95d0262062 100644 --- a/src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php +++ b/src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php @@ -281,7 +281,7 @@ class ResponseHeaderBag extends HeaderBag protected function computeCacheControlValue() { if (!$this->cacheControl && !$this->has('ETag') && !$this->has('Last-Modified') && !$this->has('Expires')) { - return 'no-cache'; + return 'no-cache, private'; } if (!$this->cacheControl) { diff --git a/src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php index d4d02d94fe..f9e5d7106b 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php @@ -171,6 +171,15 @@ class HeaderBagTest extends \PHPUnit_Framework_TestCase $this->assertEquals(10, $bag->getCacheControlDirective('max-age')); } + public function testCacheControlClone() + { + $headers = array('foo' => 'bar'); + $bag1 = new HeaderBag($headers); + $bag2 = new HeaderBag($bag1->all()); + + $this->assertEquals($bag1->all(), $bag2->all()); + } + public function testGetIterator() { $headers = array('foo' => 'bar', 'hello' => 'world', 'third' => 'charm'); diff --git a/src/Symfony/Component/HttpFoundation/Tests/ResponseHeaderBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/ResponseHeaderBagTest.php index 8e487d6127..1101b6ac26 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseHeaderBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseHeaderBagTest.php @@ -34,7 +34,7 @@ class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase return array( array( array('fOo' => 'BAR'), - array('fOo' => array('BAR'), 'Cache-Control' => array('no-cache')), + array('fOo' => array('BAR'), 'Cache-Control' => array('no-cache, private')), ), array( array('ETag' => 'xyzzy'), @@ -42,23 +42,23 @@ class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase ), array( array('Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ=='), - array('Content-MD5' => array('Q2hlY2sgSW50ZWdyaXR5IQ=='), 'Cache-Control' => array('no-cache')), + array('Content-MD5' => array('Q2hlY2sgSW50ZWdyaXR5IQ=='), 'Cache-Control' => array('no-cache, private')), ), array( array('P3P' => 'CP="CAO PSA OUR"'), - array('P3P' => array('CP="CAO PSA OUR"'), 'Cache-Control' => array('no-cache')), + array('P3P' => array('CP="CAO PSA OUR"'), 'Cache-Control' => array('no-cache, private')), ), array( array('WWW-Authenticate' => 'Basic realm="WallyWorld"'), - array('WWW-Authenticate' => array('Basic realm="WallyWorld"'), 'Cache-Control' => array('no-cache')), + array('WWW-Authenticate' => array('Basic realm="WallyWorld"'), 'Cache-Control' => array('no-cache, private')), ), array( array('X-UA-Compatible' => 'IE=edge,chrome=1'), - array('X-UA-Compatible' => array('IE=edge,chrome=1'), 'Cache-Control' => array('no-cache')), + array('X-UA-Compatible' => array('IE=edge,chrome=1'), 'Cache-Control' => array('no-cache, private')), ), array( array('X-XSS-Protection' => '1; mode=block'), - array('X-XSS-Protection' => array('1; mode=block'), 'Cache-Control' => array('no-cache')), + array('X-XSS-Protection' => array('1; mode=block'), 'Cache-Control' => array('no-cache, private')), ), ); } @@ -66,7 +66,7 @@ class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase public function testCacheControlHeader() { $bag = new ResponseHeaderBag(array()); - $this->assertEquals('no-cache', $bag->get('Cache-Control')); + $this->assertEquals('no-cache, private', $bag->get('Cache-Control')); $this->assertTrue($bag->hasCacheControlDirective('no-cache')); $bag = new ResponseHeaderBag(array('Cache-Control' => 'public')); @@ -111,6 +111,14 @@ class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control')); } + public function testCacheControlClone() + { + $headers = array('foo' => 'bar'); + $bag1 = new ResponseHeaderBag($headers); + $bag2 = new ResponseHeaderBag($bag1->allPreserveCase()); + $this->assertEquals($bag1->allPreserveCase(), $bag2->allPreserveCase()); + } + public function testToStringIncludesCookieHeaders() { $bag = new ResponseHeaderBag(array()); @@ -135,7 +143,7 @@ class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase public function testReplace() { $bag = new ResponseHeaderBag(array()); - $this->assertEquals('no-cache', $bag->get('Cache-Control')); + $this->assertEquals('no-cache, private', $bag->get('Cache-Control')); $this->assertTrue($bag->hasCacheControlDirective('no-cache')); $bag->replace(array('Cache-Control' => 'public')); @@ -146,12 +154,12 @@ class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase public function testReplaceWithRemove() { $bag = new ResponseHeaderBag(array()); - $this->assertEquals('no-cache', $bag->get('Cache-Control')); + $this->assertEquals('no-cache, private', $bag->get('Cache-Control')); $this->assertTrue($bag->hasCacheControlDirective('no-cache')); $bag->remove('Cache-Control'); $bag->replace(array()); - $this->assertEquals('no-cache', $bag->get('Cache-Control')); + $this->assertEquals('no-cache, private', $bag->get('Cache-Control')); $this->assertTrue($bag->hasCacheControlDirective('no-cache')); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php index 97674d48fd..2df2d61cad 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php @@ -33,7 +33,7 @@ class ResponseTest extends ResponseTestCase $response = new Response(); $response = explode("\r\n", $response); $this->assertEquals('HTTP/1.0 200 OK', $response[0]); - $this->assertEquals('Cache-Control: no-cache', $response[1]); + $this->assertEquals('Cache-Control: no-cache, private', $response[1]); } public function testClone()