Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

April fool's annoying browser cache refuses to flush

I've spent my weekend programming a little April Fool's joke, but it doesn't work just the way I want it to yet.

I have a Drupal 6 based website on which I want to change as little as possible. The idea is that all images that are served from the /files directory are redirected to an external webserver (myserver) that flips the image upside down and then serves it to the browser.

To make the Drupal website (targetserver) redirect all requests for images to the other server, I set up a .htaccess as follows:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} !^aprilFool$
RewriteRule ^(.*)$        http://myserver/aprilFool/?url=http://targetserver/files/$1 [R=302,L]

And so far it works great! When I enable everything, the April Fool's trickery changes some of the images and they get displayed in the client's browser.

But when I disable the .htaccess @targetserver, my browser refuses to realize it was only meant to be a temporary joke and forget the edited images :(

Here is a sniplet of the Perl script on myserver/aprilFool:

my $ua = LWP::UserAgent->new;
# Identify ourselves through the useragent, to prevent endless redirect loops
$ua->agent( 'aprilFool' );

# Load remote file
my $response = $ua->get( $url );
if ( $response->is_error ) { die "Cannot retrieve document $url\n"; }
my $imageData = $response->content;


# Determine the file's mime type, need that to determine if we want to change it or not
my $ft = File::Type->new();
my $format = $ft->mime_type( $imageData );

# If the file is an image, flip it
if ( $format =~ m/^image\// ) {
        my $image=Image::Magick->new;
        $image->BlobToImage( $imageData );
        $image->Flip();
        $imageData = $image->ImageToBlob();
}

# Send HTTP headers
print "Content-type:$format\r\n";
print "\r\n";
print $imageData;

I've tried the following without success:

  1. Add an extra header in the script: print "Cache-Control: max-age=36\r\n";
  2. Add a line in .htaccess @targetserver: Header set Expires "Mon Mar 12 15:45:00 CET 2012"
  3. Change the name for image.jpg in various ways tmp-image.jpg, image.jpg.tmp by changing the RewriteRule in .htaccess @myserver

But after disabling the .htaccess the targetserver keeps sending 304 => 'Not Modified' until I manually flush the browser cache.

So my question is: How can I make April 1st last only one day, preferably until midnight ... How can I make the browser realize it has to reload the original image once the joke is over?

like image 388
jippie Avatar asked Nov 04 '22 03:11

jippie


1 Answers

This is a funny question :-)

Just disable the redirect when it isn't april first. 302's are not cached by the browser.

RewriteCond %{TIME_DAY} ^1$
RewriteCond %{TIME_MON} ^3$
RewriteCond %{HTTP_USER_AGENT} !^aprilFool$
RewriteRule ^(.*)$        http://myserver/aprilFool/?url=http://targetserver/files/$1 [R=302,L]

You'll have to check if the values 1 and 3 are correct to match april first, but I think TIME_MON is 0-11 and TIME_DAY is 1-31

like image 186
Gerben Avatar answered Nov 11 '22 13:11

Gerben