From f7c270a3bad984910fa84f552dfa8b99324afb16 Mon Sep 17 00:00:00 2001 From: Marcus Nyeholt Date: Wed, 2 Dec 2015 10:24:17 +1100 Subject: [PATCH 1/2] NEW Use Config for determining Vary header Existing implementation hardcodes the Vary header; swap to using Config layer instead Added test for changing the variable from config --- _config/config.yml | 3 ++- control/HTTP.php | 5 +++- .../08_Performance/02_HTTP_Cache_Headers.md | 20 +++++++++++++++ tests/control/HTTPTest.php | 25 +++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/_config/config.yml b/_config/config.yml index 31dbd1682..8d7eaef34 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -10,4 +10,5 @@ HTTP: cache_control: max-age: 0 must-revalidate: "true" - no-transform: "true" \ No newline at end of file + no-transform: "true" + vary: "Cookie, X-Forwarded-Protocol, User-Agent, Accept" \ No newline at end of file diff --git a/control/HTTP.php b/control/HTTP.php index b1b2b3cae..6b23b3659 100644 --- a/control/HTTP.php +++ b/control/HTTP.php @@ -358,7 +358,10 @@ class HTTP { // To do: User-Agent should only be added in situations where you *are* actually // varying according to user-agent. - $responseHeaders['Vary'] = 'Cookie, X-Forwarded-Protocol, User-Agent, Accept'; + $vary = $config->get('HTTP', 'vary'); + if ($vary && strlen($vary)) { + $responseHeaders['Vary'] = $vary; + } } else { if($body) { diff --git a/docs/en/02_Developer_Guides/08_Performance/02_HTTP_Cache_Headers.md b/docs/en/02_Developer_Guides/08_Performance/02_HTTP_Cache_Headers.md index 455a3c4e9..268a45a0f 100644 --- a/docs/en/02_Developer_Guides/08_Performance/02_HTTP_Cache_Headers.md +++ b/docs/en/02_Developer_Guides/08_Performance/02_HTTP_Cache_Headers.md @@ -31,3 +31,23 @@ clause in `Cache-Control` and `Pragma` will be included. Used to set the modification date to something more recent than the default. [api:DataObject::__construct] calls [api:HTTP::register_modification_date(] whenever a record comes from the database ensuring the newest date is present. + +### Vary: cache header + +By default, SilverStripe will output a `Vary` header (used by upstream caches for determining uniqueness) +that looks like + +``` +Cookie, X-Forwarded-Protocol, User-Agent, Accept +``` + +To change the value of the `Vary` header, you can change this value by specifying the header in configuration + +```yml +HTTP: + vary: "" +``` + + + + diff --git a/tests/control/HTTPTest.php b/tests/control/HTTPTest.php index b0bd98703..326aca06e 100644 --- a/tests/control/HTTPTest.php +++ b/tests/control/HTTPTest.php @@ -46,6 +46,31 @@ class HTTPTest extends FunctionalTest { } } + + public function testConfigVary() { + $body = "

Mysite

"; + $response = new SS_HTTPResponse($body, 200); + Config::inst()->update('Director', 'environment_type', 'live'); + HTTP::set_cache_age(30); + HTTP::add_cache_headers($response); + + $v = $response->getHeader('Vary'); + $this->assertNotEmpty($v); + + $this->assertContains("Cookie", $v); + $this->assertContains("X-Forwarded-Protocol", $v); + $this->assertContains("User-Agent", $v); + $this->assertContains("Accept", $v); + + Config::inst()->update('HTTP', 'vary', ''); + + $response = new SS_HTTPResponse($body, 200); + HTTP::add_cache_headers($response); + + $v = $response->getHeader('Vary'); + $this->assertEmpty($v); + } + /** * Tests {@link HTTP::getLinksIn()} */ From fc5e584201dce50eb6a996decfaafd15fbdd4a68 Mon Sep 17 00:00:00 2001 From: Marcus Nyeholt Date: Tue, 8 Dec 2015 15:19:24 +1100 Subject: [PATCH 2/2] Format for SS3 using tabs instead of spaces --- control/HTTP.php | 8 ++++---- tests/control/HTTPTest.php | 26 +++++++++++++------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/control/HTTP.php b/control/HTTP.php index 6b23b3659..2359d5836 100644 --- a/control/HTTP.php +++ b/control/HTTP.php @@ -358,10 +358,10 @@ class HTTP { // To do: User-Agent should only be added in situations where you *are* actually // varying according to user-agent. - $vary = $config->get('HTTP', 'vary'); - if ($vary && strlen($vary)) { - $responseHeaders['Vary'] = $vary; - } + $vary = $config->get('HTTP', 'vary'); + if ($vary && strlen($vary)) { + $responseHeaders['Vary'] = $vary; + } } else { if($body) { diff --git a/tests/control/HTTPTest.php b/tests/control/HTTPTest.php index 326aca06e..d15bc3655 100644 --- a/tests/control/HTTPTest.php +++ b/tests/control/HTTPTest.php @@ -48,28 +48,28 @@ class HTTPTest extends FunctionalTest { public function testConfigVary() { - $body = "

Mysite

"; + $body = "

Mysite

"; $response = new SS_HTTPResponse($body, 200); - Config::inst()->update('Director', 'environment_type', 'live'); - HTTP::set_cache_age(30); + Config::inst()->update('Director', 'environment_type', 'live'); + HTTP::set_cache_age(30); HTTP::add_cache_headers($response); - $v = $response->getHeader('Vary'); + $v = $response->getHeader('Vary'); $this->assertNotEmpty($v); - $this->assertContains("Cookie", $v); - $this->assertContains("X-Forwarded-Protocol", $v); - $this->assertContains("User-Agent", $v); - $this->assertContains("Accept", $v); + $this->assertContains("Cookie", $v); + $this->assertContains("X-Forwarded-Protocol", $v); + $this->assertContains("User-Agent", $v); + $this->assertContains("Accept", $v); - Config::inst()->update('HTTP', 'vary', ''); + Config::inst()->update('HTTP', 'vary', ''); - $response = new SS_HTTPResponse($body, 200); - HTTP::add_cache_headers($response); + $response = new SS_HTTPResponse($body, 200); + HTTP::add_cache_headers($response); - $v = $response->getHeader('Vary'); + $v = $response->getHeader('Vary'); $this->assertEmpty($v); - } + } /** * Tests {@link HTTP::getLinksIn()}