Merge pull request #4673 from patricknelson/issue-4392-cache-headers-3.2

FIX #4392: Ensure headers are checked first before being clobbered by globally maintained state.
This commit is contained in:
Daniel Hensby 2015-10-12 00:11:19 +01:00
commit 592140364c
2 changed files with 28 additions and 3 deletions

View File

@ -448,8 +448,14 @@ class HTTP {
// Now that we've generated them, either output them or attach them to the SS_HTTPResponse as appropriate // Now that we've generated them, either output them or attach them to the SS_HTTPResponse as appropriate
foreach($responseHeaders as $k => $v) { foreach($responseHeaders as $k => $v) {
if($body) $body->addHeader($k, $v); if($body) {
else if(!headers_sent()) header("$k: $v"); // Set the header now if it's not already set.
if ($body->getHeader($k) === null) {
$body->addHeader($k, $v);
}
} elseif(!headers_sent()) {
header("$k: $v");
}
} }
} }

View File

@ -13,18 +13,37 @@ class HTTPTest extends FunctionalTest {
$this->assertEmpty($response->getHeader('Cache-Control')); $this->assertEmpty($response->getHeader('Cache-Control'));
HTTP::set_cache_age(30); HTTP::set_cache_age(30);
HTTP::add_cache_headers($response);
HTTP::add_cache_headers($response);
$this->assertNotEmpty($response->getHeader('Cache-Control')); $this->assertNotEmpty($response->getHeader('Cache-Control'));
// Ensure max-age is zero for development.
Config::inst()->update('Director', 'environment_type', 'dev'); Config::inst()->update('Director', 'environment_type', 'dev');
$response = new SS_HTTPResponse($body, 200);
HTTP::add_cache_headers($response); HTTP::add_cache_headers($response);
$this->assertContains('max-age=0', $response->getHeader('Cache-Control')); $this->assertContains('max-age=0', $response->getHeader('Cache-Control'));
// Ensure max-age setting is respected in production.
Config::inst()->update('Director', 'environment_type', 'live'); Config::inst()->update('Director', 'environment_type', 'live');
$response = new SS_HTTPResponse($body, 200);
HTTP::add_cache_headers($response); HTTP::add_cache_headers($response);
$this->assertContains('max-age=30', explode(', ', $response->getHeader('Cache-Control'))); $this->assertContains('max-age=30', explode(', ', $response->getHeader('Cache-Control')));
$this->assertNotContains('max-age=0', $response->getHeader('Cache-Control')); $this->assertNotContains('max-age=0', $response->getHeader('Cache-Control'));
// Still "live": Ensure header's aren't overridden if already set (using purposefully different values).
$headers = array(
'Vary' => '*',
'Pragma' => 'no-cache',
'Cache-Control' => 'max-age=0, no-cache, no-store',
);
$response = new SS_HTTPResponse($body, 200);
foreach($headers as $name => $value) {
$response->addHeader($name, $value);
}
HTTP::add_cache_headers($response);
foreach($headers as $name => $value) {
$this->assertEquals($value, $response->getHeader($name));
}
} }
/** /**