Skip to content

Commit

Permalink
Merge pull request #978 from nexcess/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
miguelbalparda committed Oct 12, 2015
2 parents f89ae64 + c5e7ba2 commit 35d44f5
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,10 @@ Magento CE 1.8+ or EE 1.13+, see [these instructions](https://github.com/nexcess

### RELEASE-0.6.5
* Varnish 4 (@aheadley @aricwatson)

### RELEASE-0.6.6
* [#923] DetermineVersion 3.0 to 3.0.3 bugfix (@timmuller)
* [#945] Send unmodified url to the backend server (@thampe)
* [#885] Add maintenance mode that will allow debug_ips through to visit the site, all other users get the landing page (@craigcarnell)
* [#955] Include theme specific handles to ESI data (@LyndonHook)
* [#972] Fixed bug with wrong return value of ->getSkinUrl() (@ceckoslab)
11 changes: 7 additions & 4 deletions app/code/community/Nexcessnet/Turpentine/Model/Observer/Esi.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ public function injectEsi( $eventObject ) {
if( $esiHelper->shouldResponseUseEsi() &&
$blockObject instanceof Mage_Core_Block_Template &&
$esiOptions = $blockObject->getEsiOptions() ) {

if ((isset($esiOptions['disableEsiInjection'])) && ($esiOptions['disableEsiInjection'] == 1)) {
return;
}

if( Mage::app()->getStore()->getCode() == 'admin' ) {
// admin blocks are not allowed to be cached for now
$debugHelper->logWarn(
Expand Down Expand Up @@ -320,8 +325,6 @@ protected function _getEsiData( $blockObject, $esiOptions ) {
$methodParam = $esiHelper->getEsiMethodParam();
$esiData = new Varien_Object();
$esiData->setStoreId( Mage::app()->getStore()->getId() );
$esiData->setDesignPackage( Mage::getDesign()->getPackageName() );
$esiData->setDesignTheme( Mage::getDesign()->getTheme( 'layout' ) );
$esiData->setNameInLayout( $blockObject->getNameInLayout() );
$esiData->setBlockType( get_class( $blockObject ) );
$esiData->setLayoutHandles( $this->_getBlockLayoutHandles( $blockObject ) );
Expand Down Expand Up @@ -413,9 +416,9 @@ protected function _getBlockLayoutHandles( $block ) {
// check if this handle has any block or reference tags that
// refer to this block or a child block, unless the handle name
// is blank
if( $handle !== '' &&
if( $handle !== '' && ( strpos($handle, 'THEME') === 0 ||
$layoutXml->xpath( sprintf(
'//%s//*[@name=\'%s\']', $handle, $blockName ) ) ) {
'//%s//*[@name=\'%s\']', $handle, $blockName ) ) ) ) {
$activeHandles[] = $handle;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,13 @@ protected function _determineVersion($bannerText) {
Mage::throwException('Varnish versions before 2.1 are not supported');
}
if ( count($bannerText)<7 ) {
// Varnish before 3.0 does not spit out a version number
// Varnish before 3.0.4 does not spit out a version number
$resp = $this->_write( 'help' )->_read();
if( strpos( $resp['text'], 'ban.url' ) !== false ) {
// Varnish versions 3.0 through 3.0.3 do not return a version banner.
// To differentiate between 2.1 and 3.0, we check the existence of the ban.url command.
return '3.0';
}
return '2.1';
} elseif ( preg_match(self::REGEXP_VARNISH_VERSION, $bannerText[4], $matches)===1 ) {
return $matches['vmajor'] . '.' . $matches['vminor'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,14 @@ protected function _getIgnoreGetParameters()
return implode( '|', $ignoredParameters);
}

/**
* @return boolean
*/
protected function _sendUnModifiedUrlToBackend()
{
return Mage::getStoreConfigFlag('turpentine_vcl/params/transfer_unmodified_url');
}

/**
* Get the Generate Session
*
Expand Down Expand Up @@ -406,8 +414,8 @@ protected function _getGenerateSessionEnd() {
* @return string
*/
protected function _getGenerateSession() {
return Mage::getStoreConfig( 'turpentine_varnish/general/vcl_fix' )
? '# call generate_session' : 'call generate_session;';
return Mage::getStoreConfigFlag( 'turpentine_varnish/general/vcl_fix' )
? 'return (pipe);' : 'call generate_session;';
}


Expand Down Expand Up @@ -825,6 +833,92 @@ protected function _getNormalizeCookieRegex() {
'turpentine_vcl/normalization/cookie_regex' ) );
}

/**
* Get the allowed IPs when in maintenance mode
*
* @return string
*/
protected function _vcl_sub_maintenance_allowed_ips() {
if((! $this->_getDebugIps()) || ! Mage::getStoreConfig( 'turpentine_vcl/maintenance/custom_vcl_synth' ) ) {
return false;
}

switch(Mage::getStoreConfig( 'turpentine_varnish/servers/version' )) {
case 4.0:
$tpl = <<<EOS
if (req.http.X-Forwarded-For) {
if (req.http.X-Forwarded-For !~ "{{debug_ips}}") {
return (synth(999, "Maintenance mode"));
}
}
else {
if (client.ip !~ debug_acl) {
return (synth(999, "Maintenance mode"));
}
}
EOS;
break;
default:
$tpl = <<<EOS
if (req.http.X-Forwarded-For) {
if(req.http.X-Forwarded-For !~ "{{debug_ips}}") {
error 503;
}
} else {
if (client.ip !~ debug_acl) {
error 503;
}
}
EOS;
}

return $this->_formatTemplate( $tpl, array(
'debug_ips' => Mage::getStoreConfig( 'dev/restrict/allow_ips' ) ) );
}

/**
* Get the allowed IPs when in maintenance mode
*
* @return string
*/
protected function _vcl_sub_synth()
{
if ((!$this->_getDebugIps()) || !Mage::getStoreConfig('turpentine_vcl/maintenance/custom_vcl_synth')) {
return false;
}

switch (Mage::getStoreConfig('turpentine_varnish/servers/version')) {
case 4.0:
$tpl = <<<EOS
sub vcl_synth {
if (resp.status == 999) {
set resp.status = 404;
set resp.http.Content-Type = "text/html; charset=utf-8";
synthetic({"{{vcl_synth_content}}"});
return (deliver);
}
return (deliver);
}
EOS;
break;
default:
$tpl = <<<EOS
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
synthetic {"{{vcl_synth_content}}"};
return (deliver);
}
EOS;
}

return $this->_formatTemplate($tpl, array(
'vcl_synth_content' => Mage::getStoreConfig('turpentine_vcl/maintenance/custom_vcl_synth')));
}



/**
* Build the list of template variables to apply to the VCL template
*
Expand All @@ -844,6 +938,7 @@ protected function _getTemplateVars() {
'default_ttl' => $this->_getDefaultTtl(),
'enable_get_excludes' => ($this->_getGetParamExcludes() ? 'true' : 'false'),
'enable_get_ignored' => ($this->_getIgnoreGetParameters() ? 'true' : 'false'),
'send_unmodified_url' => ($this->_sendUnModifiedUrlToBackend() ? 'true' : 'false'),
'debug_headers' => $this->_getEnableDebugHeaders(),
'grace_period' => $this->_getGracePeriod(),
'force_cache_static' => $this->_getForceCacheStatic(),
Expand Down Expand Up @@ -894,6 +989,13 @@ protected function _getTemplateVars() {
$vars['normalize_cookie_target'] = $this->_getNormalizeCookieTarget();
}

if( Mage::getStoreConfig( 'turpentine_vcl/maintenance/enable' ) ) {
// in vcl_recv set the allowed IPs otherwise load the vcl_error (v3)/vcl_synth (v4)
$vars['maintenance_allowed_ips'] = $this->_vcl_sub_maintenance_allowed_ips();
// set the vcl_error from Magento database
$vars['vcl_synth'] = $this->_vcl_sub_synth();
}

$customIncludeFile = $this->_getCustomIncludeFilename();
if( is_readable( $customIncludeFile ) ) {
$vars['custom_vcl_include'] = file_get_contents( $customIncludeFile );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,6 @@ protected function _getEsiBlock( $esiData ) {
}
}
$layout = Mage::getSingleton( 'core/layout' );
Mage::getSingleton( 'core/design_package' )
->setPackageName( $esiData->getDesignPackage() )
->setTheme( $esiData->getDesignTheme() );

// dispatch event for adding handles to layout update
Mage::dispatchEvent(
Expand Down
3 changes: 2 additions & 1 deletion app/code/community/Nexcessnet/Turpentine/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<config>
<modules>
<Nexcessnet_Turpentine>
<version>0.6.6</version>
<version>0.6.7</version>
</Nexcessnet_Turpentine>
</modules>
<default>
Expand Down Expand Up @@ -80,6 +80,7 @@
<params>
<get_params>__SID,XDEBUG_PROFILE</get_params>
<ignore_get_params>utm_source,utm_medium,utm_campaign,utm_content,utm_term,gclid,cx,ie,cof,siteurl</ignore_get_params>
<transfer_unmodified_url>0</transfer_unmodified_url>
</params>
<static>
<force_static>1</force_static>
Expand Down
43 changes: 43 additions & 0 deletions app/code/community/Nexcessnet/Turpentine/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,20 @@
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</ignore_get_params>
<transfer_unmodified_url translate="label" module="turpentine">
<label>Transfer unmodified URL to Backend Server</label>
<comment>
By default the backend server (webserver / magento) gets a modified URL (without ignored get parameters).
As a result the ignored parameters can not be used by the backend server for uncachable requests, for example a redirect.
By activating this option the backend server gets the the unmodified url, but the cache still uses the modified url for lookups.
</comment>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>21</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</transfer_unmodified_url>
</fields>
</params>
<static translate="label" module="turpentine">
Expand Down Expand Up @@ -575,6 +589,35 @@
</exts>
</fields>
</static>
<maintenance translate="label" module="turpentine">
<label>Maintenance mode</label>
<frontend_type>text</frontend_type>
<sort_order>46</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<fields>
<enable translate="label" module="turpentine">
<label>Enable Maintenance Mode</label>
<frontend_type>select</frontend_type>
<source_model>turpentine/config_select_toggle</source_model>
<comment>If enabled IP's not in 'developer client restrictions' will receive the following error page</comment>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</enable>
<custom_vcl_synth translate="label" module="turpentine">
<label>Custom HTML content of vcl synth (error) sub</label>
<frontend_type>textarea</frontend_type>
<comment>Enter full HTML page content</comment>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</custom_vcl_synth>
</fields>
</maintenance>
</groups>
</turpentine_vcl>
</sections>
Expand Down
23 changes: 22 additions & 1 deletion app/code/community/Nexcessnet/Turpentine/misc/version-2.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ sub vcl_recv {
set req.http.X-Opt-Enable-Caching = "{{enable_caching}}";
set req.http.X-Opt-Force-Static-Caching = "{{force_cache_static}}";
set req.http.X-Opt-Enable-Get-Excludes = "{{enable_get_excludes}}";
set req.http.X-Opt-Send-Unmodified-Url = "{{send_unmodified_url}}";


if(req.http.X-Opt-Send-Unmodified-Url == "true") {
# save unmodified url
set req.http.X-Varnish-Origin-Url = req.url;
}

# Normalize request data before potentially sending things off to the
# backend. This ensures all request types get the same information, most
Expand Down Expand Up @@ -200,7 +207,14 @@ sub vcl_recv {
set req.url = regsuball(req.url, "(?:(\?)?|&)(?:{{get_param_ignored}})=[^&]+", "\1");
set req.url = regsuball(req.url, "(?:(\?)&|\?$)", "\1");
}



if(req.http.X-Opt-Send-Unmodified-Url == "true") {
# change req.url back and save the modified for cache look-ups in a separate variable
set req.http.X-Varnish-Cache-Url = req.url;
set req.url = req.http.X-Varnish-Origin-Url;
unset req.http.X-Varnish-Origin-Url;
}

return (lookup);
}
Expand All @@ -220,6 +234,13 @@ sub vcl_pipe {
# }

sub vcl_hash {

if({{send_unmodified_url}} && req.http.X-Varnish-Cache-Url) {
set req.hash += req.http.X-Varnish-Cache-Url;
} else {
set req.hash += req.url;
}

set req.hash += req.url;
if (req.http.Host) {
set req.hash += req.http.Host;
Expand Down
24 changes: 21 additions & 3 deletions app/code/community/Nexcessnet/Turpentine/misc/version-3.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ sub generate_session_expires {
## Varnish Subroutines

sub vcl_recv {
{{maintenance_allowed_ips}}

# this always needs to be done so it's up at the top
if (req.restarts == 0) {
if (req.http.X-Forwarded-For) {
Expand All @@ -107,6 +109,11 @@ sub vcl_recv {
}
}

if({{send_unmodified_url}}) {
# save the unmodified url
set req.http.X-Varnish-Origin-Url = req.url;
}

# Normalize request data before potentially sending things off to the
# backend. This ensures all request types get the same information, most
# notably POST requests getting a normalized user agent string to empower
Expand Down Expand Up @@ -170,8 +177,7 @@ sub vcl_recv {
set req.http.Cookie = "frontend=crawler-session";
} else {
# it's a real user, make up a new session for them
{{generate_session}}# call generate_session;
return (pipe);
{{generate_session}}
}
}
if ({{force_cache_static}} &&
Expand Down Expand Up @@ -202,6 +208,11 @@ sub vcl_recv {
set req.url = regsuball(req.url, "(?:(\?)&|\?$)", "\1");
}

if({{send_unmodified_url}}) {
set req.http.X-Varnish-Cache-Url = req.url;
set req.url = req.http.X-Varnish-Origin-Url;
unset req.http.X-Varnish-Origin-Url;
}

# everything else checks out, try and pull from the cache
return (lookup);
Expand All @@ -222,7 +233,12 @@ sub vcl_pipe {
# }

sub vcl_hash {
hash_data(req.url);

if({{send_unmodified_url}} && req.http.X-Varnish-Cache-Url) {
hash_data(req.http.X-Varnish-Cache-Url);
} else {
hash_data(req.url);
}
if (req.http.Host) {
hash_data(req.http.Host);
} else {
Expand Down Expand Up @@ -350,6 +366,8 @@ sub vcl_fetch {
# else it's not part of Magento so use the default Varnish handling
}

{{vcl_synth}}

sub vcl_deliver {
if (req.http.X-Varnish-Faked-Session) {
# need to set the set-cookie header since we just made it out of thin air
Expand Down
Loading

0 comments on commit 35d44f5

Please sign in to comment.