WP Super Cache protocol-aware caching patch (11 posts)

  1. 0blivian
    Posted 5 years ago #

    I collaborate in managing a large multisite installation. We are using wp-super-cache, but we ran into an issue: since we allow our users to browse the blogs both in http and in https, we had issues with the fact that the cache does not divide cached contents for the two cases. We made a simple patch to allow for caching the two protocols. The separate caching allows for https users to be served pages with all links in https, so that they do not get any security warning.

    You may consider this patch as a POC, since I'm relatively new to wp-super-cache, but I think it does his job.

    diff -rupN wp-super-cache.orig/wp-cache-phase1.php wp-super-cache/wp-cache-phase1.php
    --- wp-super-cache.orig/wp-cache-phase1.php	2011-03-16 00:07:38.000000000 +0100
    +++ wp-super-cache/wp-cache-phase1.php	2011-03-16 08:44:09.339082990 +0100
    @@ -459,7 +459,10 @@ function get_current_url_supercache_dir(
     	$uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', $uri ) ) ) );
     	$uri = str_replace( '\\', '', $uri );
    -	$dir = preg_replace( '/:.*$/', '',  $_SERVER["HTTP_HOST"] ) . $uri; // To avoid XSS attacks
    +	//Add support for https and http caching
    +	$is_https = ('on' ==  strtolower($_SERVER['HTTPS'])  || 'https' == strtolower($_SERVER['HTTP_X_FORWARDED_PROTO'])); //Also supports https requests coming from an nginx reverse proxy
    +	$schema_dir = $is_https ? '/https' : '/http';
    +	$dir = preg_replace( '/:.*$/', '',  $_SERVER["HTTP_HOST"] ) . $schema_dir . $uri; // To avoid XSS attacks
     	if ( function_exists( "apply_filters" ) )
     		$dir = apply_filters( 'supercache_dir', $dir );
     	$dir = $cache_path . 'supercache/' . $dir . '/';
    diff -rupN wp-super-cache.orig/wp-cache.php wp-super-cache/wp-cache.php
    --- wp-super-cache.orig/wp-cache.php	2011-03-16 00:07:38.000000000 +0100
    +++ wp-super-cache/wp-cache.php	2011-03-16 09:00:44.239082997 +0100
    @@ -2450,14 +2450,24 @@ function wpsc_get_htaccess_info() {
     	$rules .= "RewriteBase $home_root\n"; // props Chris Messina
     	$charset = get_option('blog_charset') == '' ? 'UTF-8' : get_option('blog_charset');
     	$rules .= "AddDefaultCharset {$charset}\n";
    +	$rules .= "#If you serve pages from behind a proxy you may want to change RewriteCond %{HTTPS} on to something more sensible\n";
     	$rules .= "CONDITION_RULES";
     	$rules .= "RewriteCond %{HTTP:Accept-Encoding} gzip\n";
    -	$rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html.gz -f\n";
    -	$rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html.gz\" [L]\n\n";
    +	$rules .= "RewriteCond %{HTTPS} on\n";
    +	$rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}/https{$home_root}$1/index.html.gz -f\n";
    +	$rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{HTTP_HOST}/https{$home_root}$1/index.html.gz\" [L]\n\n";
    +	$rules .= "RewriteCond %{HTTP:Accept-Encoding} gzip\n";
    +	$rules .= "RewriteCond %{HTTPS} !on\n";
    +	$rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}/http{$home_root}$1/index.html.gz -f\n";
    +	$rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{HTTP_HOST}/http{$home_root}$1/index.html.gz\" [L]\n\n";
     	$rules .= "CONDITION_RULES";
    -	$rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html -f\n";
    -	$rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html\" [L]\n";
    +	$rules .= "RewriteCond %{HTTPS} on\n";
    +	$rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}/https{$home_root}$1/index.html -f\n";
    +	$rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{HTTP_HOST}/https{$home_root}$1/index.html\" [L]\n";
    +	$rules .= "RewriteCond %{HTTPS} !on\n";
    +	$rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}/http{$home_root}$1/index.html -f\n";
    +	$rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{HTTP_HOST}/http{$home_root}$1/index.html\" [L]\n";
     	$rules .= "</IfModule>\n";
     	$rules = apply_filters( 'supercacherewriterules', $rules );

    Hope this helps.

  2. Donncha O Caoimh
    Posted 5 years ago #

    It looks good! Hopefully I'll get that in and working before 1.0 (the next release) if you can test it?

  3. Donncha O Caoimh
    Posted 5 years ago #

    Actually, those rewrite rules won't be correct. The CONDITION_RULES string needs to be in both chunks of rules. Fixing it while merging!

  4. Donncha O Caoimh
    Posted 5 years ago #

    Checked that patch (and a slight mod fixing the above problem) into trunk. Should be in the development version in about 15 minutes.

  5. Eyecool
    Posted 5 years ago #

    Happy!! :)

    I love it when a plan comes together.

  6. Donncha O Caoimh
    Posted 5 years ago #

    Hmm, I just realised this is incomplete. It doesn't handle deleting cache files at all as the supercache directory is referenced in other places too.

  7. 0blivian
    Posted 5 years ago #

    Sorry for the incomplete patch to the rewrite rules, I wrote it afterwards as I changed the rewrite rules by hand while developing.

    As of deleting cache files, my cache files do get pruned, but I suspect I have some issues with pruning in my installation (it happens way too often, I have to look into that). If you point me to where I should correct the deletion of the cache files, I'd be happy to help with a patch.

    Also, I've seen that the pruning function is called recursively. If I understood its working correctly, this can be a problem on large installations (with thousands of blogs) as you may risk encountering the max recursion limit of php (100 by default) when pruning directories recursively.

  8. Donncha O Caoimh
    Posted 5 years ago #

    I think I've patched the final bit of code and tested it by leaving a comment on one of my blogs running it. Can you give the development version a go shortly?

  9. 0blivian
    Posted 5 years ago #

    this is great! we'll test the development version asap. We have quite a large userbase, so we'll test it both off and online. I'll let you know if it works as intended.

  10. Donncha O Caoimh
    Posted 5 years ago #

    Any luck with this?

  11. rhertzog
    Posted 4 years ago #

    I tested it here and it seems to work fine for me.

Topic Closed

This topic has been closed to new replies.

About this Topic