From 552c70a5714842dcfc0b32b1e247ce9bc374591b Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Mon, 7 Nov 2011 00:21:05 -0430 Subject: [PATCH] Removing Pragma headers, implementing sharedMaxAge in CakeResponse --- lib/Cake/Network/CakeResponse.php | 34 ++++++++++-- .../Test/Case/Network/CakeResponseTest.php | 53 +++++++++++++++---- 2 files changed, 72 insertions(+), 15 deletions(-) diff --git a/lib/Cake/Network/CakeResponse.php b/lib/Cake/Network/CakeResponse.php index 17d59580d..be9d717a3 100644 --- a/lib/Cake/Network/CakeResponse.php +++ b/lib/Cake/Network/CakeResponse.php @@ -666,8 +666,7 @@ class CakeResponse { $this->header(array( 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', 'Last-Modified' => gmdate("D, d M Y H:i:s") . " GMT", - 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', - 'Pragma' => 'no-cache' + 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0' )); } @@ -699,9 +698,10 @@ class CakeResponse { * @param boolean $public if set to true, the Cache-Control header will be set as public * if set to false, the response will be set to private * if no value is provided, it will return whether the response is sharable or not + * @param int $time time in seconds after which the response should no longer be considered fresh * @return boolean */ - public function sharable($public = null) { + public function sharable($public = null, $time = null) { if ($public === null) { $public = array_key_exists('public', $this->_cacheDirectives); $private = array_key_exists('private', $this->_cacheDirectives); @@ -715,21 +715,45 @@ class CakeResponse { if ($public) { $this->_cacheDirectives['public'] = null; unset($this->_cacheDirectives['private']); + $this->sharedMaxAge($time); } else { $this->_cacheDirectives['private'] = null; unset($this->_cacheDirectives['public']); + $this->maxAge($time); + } + if ($time == null) { + $this->_setCacheControl(); } - $this->_setCacheControl(); return (bool) $public; } +/** + * Sets the Cache-Control s-maxage directive. + * The max-age is the number of seconds after which the response should no longer be considered + * a good candidate to be fetched from a shared cache (like in a proxy server). + * If called with no parameters, this function will return the current max-age value if any + * + * @param int $seconds if null, the method will return the current s-maxage value + * @return int + */ + public function sharedMaxAge($seconds = null) { + if ($seconds !== null) { + $this->_cacheDirectives['s-maxage'] = $seconds; + $this->_setCacheControl(); + } + if (isset($this->_cacheDirectives['s-maxage'])) { + return $this->_cacheDirectives['s-maxage']; + } + return null; + } + /** * Sets the Cache-Control max-age directive. * The max-age is the number of seconds after which the response should no longer be considered * a good candidate to be fetched from the local (client) cache. * If called with no parameters, this function will return the current max-age value if any * - * @param int $seconds + * @param int $seconds if null, the method will return the current max-age value * @return int */ public function maxAge($seconds = null) { diff --git a/lib/Cake/Test/Case/Network/CakeResponseTest.php b/lib/Cake/Test/Case/Network/CakeResponseTest.php index 03c58f6ec..0a6c0f92b 100644 --- a/lib/Cake/Test/Case/Network/CakeResponseTest.php +++ b/lib/Cake/Test/Case/Network/CakeResponseTest.php @@ -253,8 +253,7 @@ class CakeResponseTest extends CakeTestCase { $expected = array( 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', 'Last-Modified' => gmdate("D, d M Y H:i:s") . " GMT", - 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', - 'Pragma' => 'no-cache' + 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0' ); $response->disableCache(); $this->assertEquals($response->header(), $expected); @@ -273,8 +272,7 @@ class CakeResponseTest extends CakeTestCase { 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT', 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT', 'Expires' => $time->format('D, j M Y H:i:s') . ' GMT', - 'Cache-Control' => 'public, max-age=' . ($time->format('U') - time()), - 'Pragma' => 'cache' + 'Cache-Control' => 'public, max-age=' . ($time->format('U') - time()) ); $response->cache($since); $this->assertEquals($response->header(), $expected); @@ -286,8 +284,7 @@ class CakeResponseTest extends CakeTestCase { 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT', 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT', 'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT", - 'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()), - 'Pragma' => 'cache' + 'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()) ); $response->cache($since, $time); $this->assertEquals($response->header(), $expected); @@ -299,8 +296,7 @@ class CakeResponseTest extends CakeTestCase { 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT', 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT', 'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT", - 'Cache-Control' => 'public, max-age=0', - 'Pragma' => 'cache' + 'Cache-Control' => 'public, max-age=0' ); $response->cache($since, $time); $this->assertEquals($response->header(), $expected); @@ -673,6 +669,17 @@ class CakeResponseTest extends CakeTestCase { $this->assertFalse($response->sharable()); $response->sharable(true); $this->assertTrue($response->sharable()); + + $response = new CakeResponse; + $response->sharable(true, 3600); + $headers = $response->header(); + $this->assertEquals('public, s-maxage=3600', $headers['Cache-Control']); + + $response = new CakeResponse; + $response->sharable(false, 3600); + $headers = $response->header(); + $this->assertEquals('private, max-age=3600', $headers['Cache-Control']); + $response->send(); } /** @@ -693,11 +700,37 @@ class CakeResponseTest extends CakeTestCase { $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent')); $response->maxAge(3600); + $response->sharable(false); + $headers = $response->header(); + $this->assertEquals('max-age=3600, private', $headers['Cache-Control']); + $response->expects($this->at(1)) + ->method('_sendHeader')->with('Cache-Control', 'max-age=3600, private'); + $response->send(); + } + +/** + * Tests setting of s-maxage Cache-Control directive + * + * @return void + */ + public function testSharedMaxAge() { + $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent')); + $this->assertNull($response->maxAge()); + $response->sharedMaxAge(3600); + $this->assertEquals(3600, $response->sharedMaxAge()); + $headers = $response->header(); + $this->assertEquals('s-maxage=3600', $headers['Cache-Control']); + $response->expects($this->at(1)) + ->method('_sendHeader')->with('Cache-Control', 's-maxage=3600'); + $response->send(); + + $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent')); + $response->sharedMaxAge(3600); $response->sharable(true); $headers = $response->header(); - $this->assertEquals('max-age=3600, public', $headers['Cache-Control']); + $this->assertEquals('s-maxage=3600, public', $headers['Cache-Control']); $response->expects($this->at(1)) - ->method('_sendHeader')->with('Cache-Control', 'max-age=3600, public'); + ->method('_sendHeader')->with('Cache-Control', 's-maxage=3600, public'); $response->send(); } }