From d0087aec29043b1608277834922c0ce22df6cade Mon Sep 17 00:00:00 2001 From: Phu Hoang Date: Fri, 18 Nov 2016 14:39:01 +0700 Subject: [PATCH 1/3] Allow to add custom cookie keys into varnish hash --- .../Model/Varnish/Configurator/Abstract.php | 17 +++++++++++++++++ .../Nexcessnet/Turpentine/etc/system.xml | 19 +++++++++++++++++++ .../Nexcessnet/Turpentine/misc/version-2.vcl | 2 ++ .../Nexcessnet/Turpentine/misc/version-3.vcl | 2 ++ .../Nexcessnet/Turpentine/misc/version-4.vcl | 3 +++ 5 files changed, 43 insertions(+) diff --git a/app/code/community/Nexcessnet/Turpentine/Model/Varnish/Configurator/Abstract.php b/app/code/community/Nexcessnet/Turpentine/Model/Varnish/Configurator/Abstract.php index 77e4ec675..0341b283a 100644 --- a/app/code/community/Nexcessnet/Turpentine/Model/Varnish/Configurator/Abstract.php +++ b/app/code/community/Nexcessnet/Turpentine/Model/Varnish/Configurator/Abstract.php @@ -139,6 +139,22 @@ protected function _getCustomIncludeFilename($position='') { } + /** + * Get custom cookie keys and add to varnish hash + * + * @return string + */ + protected function _getCustomCookieHash() { + $keys = Mage::getStoreConfig('turpentine_vcl/cookie/keys'); + if(!empty($keys)){ + $keys = implode('|',Mage::helper('turpentine/data')->cleanExplode(PHP_EOL, $keys)); + + return 'hash_data(regsub(req.http.Cookie, "^.*?('.$keys.')=([^;]*);*.*$", "\1"));'; + } + return null; + } + + /** * Get the custom VCL template, if it exists * Returns 'null' if the file doesn't exist @@ -1009,6 +1025,7 @@ protected function _getTemplateVars() { $this->_getVclTemplateFilename(self::VCL_CUSTOM_C_CODE_FILE) ), 'esi_private_ttl' => Mage::helper('turpentine/esi') ->getDefaultEsiTtl(), + 'custom_cookie_hash' => $this->_getCustomCookieHash(), ); if ((bool) Mage::getStoreConfig('turpentine_vcl/urls/bypass_cache_store_url')) { diff --git a/app/code/community/Nexcessnet/Turpentine/etc/system.xml b/app/code/community/Nexcessnet/Turpentine/etc/system.xml index 575a425fd..a22b378e6 100644 --- a/app/code/community/Nexcessnet/Turpentine/etc/system.xml +++ b/app/code/community/Nexcessnet/Turpentine/etc/system.xml @@ -579,6 +579,25 @@ + + + text + 35 + 1 + 0 + 0 + + + + textarea + List of cookie keys to include in caching hash key. + 10 + 1 + 0 + 0 + + + text diff --git a/app/code/community/Nexcessnet/Turpentine/misc/version-2.vcl b/app/code/community/Nexcessnet/Turpentine/misc/version-2.vcl index 2223bedbc..97fbcb42c 100644 --- a/app/code/community/Nexcessnet/Turpentine/misc/version-2.vcl +++ b/app/code/community/Nexcessnet/Turpentine/misc/version-2.vcl @@ -288,6 +288,8 @@ sub vcl_hash { set req.hash += regsub(req.http.Cookie, "^.*?customer_group=([^;]*);*.*$", "\1"); } + {{custom_cookie_hash}} + return (hash); } diff --git a/app/code/community/Nexcessnet/Turpentine/misc/version-3.vcl b/app/code/community/Nexcessnet/Turpentine/misc/version-3.vcl index b960df79a..f39bb7767 100644 --- a/app/code/community/Nexcessnet/Turpentine/misc/version-3.vcl +++ b/app/code/community/Nexcessnet/Turpentine/misc/version-3.vcl @@ -291,6 +291,8 @@ sub vcl_hash { hash_data(regsub(req.http.Cookie, "^.*?customer_group=([^;]*);*.*$", "\1")); } + {{custom_cookie_hash}} + return (hash); } diff --git a/app/code/community/Nexcessnet/Turpentine/misc/version-4.vcl b/app/code/community/Nexcessnet/Turpentine/misc/version-4.vcl index efef43060..0d784c9dd 100644 --- a/app/code/community/Nexcessnet/Turpentine/misc/version-4.vcl +++ b/app/code/community/Nexcessnet/Turpentine/misc/version-4.vcl @@ -318,6 +318,9 @@ sub vcl_hash { req.http.Cookie ~ "customer_group=") { hash_data(regsub(req.http.Cookie, "^.*?customer_group=([^;]*);*.*$", "\1")); } + + {{custom_cookie_hash}} + std.log("vcl_hash end return lookup"); return (lookup); } From 11c05709d23bc10bfb4d66d2c09d36e661f9e20b Mon Sep 17 00:00:00 2001 From: Phu Hoang Date: Fri, 18 Nov 2016 16:48:40 +0700 Subject: [PATCH 2/3] Allow to add custom cookie keys into varnish hash --- .../Turpentine/Model/Varnish/Configurator/Abstract.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/code/community/Nexcessnet/Turpentine/Model/Varnish/Configurator/Abstract.php b/app/code/community/Nexcessnet/Turpentine/Model/Varnish/Configurator/Abstract.php index 0341b283a..f15d745ba 100644 --- a/app/code/community/Nexcessnet/Turpentine/Model/Varnish/Configurator/Abstract.php +++ b/app/code/community/Nexcessnet/Turpentine/Model/Varnish/Configurator/Abstract.php @@ -147,9 +147,12 @@ protected function _getCustomIncludeFilename($position='') { protected function _getCustomCookieHash() { $keys = Mage::getStoreConfig('turpentine_vcl/cookie/keys'); if(!empty($keys)){ - $keys = implode('|',Mage::helper('turpentine/data')->cleanExplode(PHP_EOL, $keys)); - - return 'hash_data(regsub(req.http.Cookie, "^.*?('.$keys.')=([^;]*);*.*$", "\1"));'; + $tpl = ''; + $keys = Mage::helper('turpentine/data')->cleanExplode(PHP_EOL, $keys); + foreach($keys as $key){ + $tpl .= 'hash_data(regsub(req.http.Cookie, "^.*?'.$key.'=([^;]*);*.*$", "\1"));'.PHP_EOL; + } + return $tpl; } return null; } From 60ced2921a7ba1f4367f1c94de51b8e86407456f Mon Sep 17 00:00:00 2001 From: Phu Hoang Date: Fri, 18 Nov 2016 17:11:43 +0700 Subject: [PATCH 3/3] Correct _getCustomCookieHash function --- .../Turpentine/Model/Varnish/Configurator/Abstract.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/code/community/Nexcessnet/Turpentine/Model/Varnish/Configurator/Abstract.php b/app/code/community/Nexcessnet/Turpentine/Model/Varnish/Configurator/Abstract.php index f15d745ba..4eb803d5f 100644 --- a/app/code/community/Nexcessnet/Turpentine/Model/Varnish/Configurator/Abstract.php +++ b/app/code/community/Nexcessnet/Turpentine/Model/Varnish/Configurator/Abstract.php @@ -150,7 +150,9 @@ protected function _getCustomCookieHash() { $tpl = ''; $keys = Mage::helper('turpentine/data')->cleanExplode(PHP_EOL, $keys); foreach($keys as $key){ + $tpl .= 'if (req.http.Cookie ~ "'.$key.'=") {'.PHP_EOL; $tpl .= 'hash_data(regsub(req.http.Cookie, "^.*?'.$key.'=([^;]*);*.*$", "\1"));'.PHP_EOL; + $tpl .= '}'.PHP_EOL; } return $tpl; }