#!/usr/bin/perl -w # This script checks to see if we have a cached copy of the image. If one is available, # we send a server-side redirect to the image. If not, we redirect the the image # creation script. use HTTP::Cookies; use Mysql; use strict; my $qin = &get_qstring(); my $url = $qin->{'url'}{"="}; my $conn=Mysql->connect( "", "", "", "" ); # Check the cache. my $query=$conn->query( "select ContentType,LocalName from ImageCache where URL=\'$url\' AND TimeStamp>(DATE_SUB(NOW(), INTERVAL 365 DAY)) ORDER By TimeStamp DESC" ); # If a cached copy exists, redirect to it. if ( $query->numrows > 0 ) { my %record=$query->fetchhash; print "Status: 302 Moved\nLocation: $record{'LocalName'}\n\n"; # Otherwise, redirect to the image builder. } else { print "Status: 302 Moved\nLocation: /cgi-bin/projects/amazon/makeImage.pl?$ENV{QUERY_STRING}\n\n"; } sub get_qstring { my ($i, $op, $key, $val, $arglist ); my $qin= $ENV{'QUERY_STRING'}; my @qin = split(/[&;]/,$qin); my %qin; foreach $i (0 .. $#qin) { $qin[$i] =~ s/\+/ /g; ($key, $op, $val) = split(/((?:\%3C|\%3E|=){1,2})/,$qin[$i],3); # splits on the first =. $op =~ s/%(..)/pack("c",hex($1))/ge; $key =~ s/%(..)/pack("c",hex($1))/ge; $val =~ s/%(..)/pack("c",hex($1))/ge; if ( !( exists $qin{$key} ) ) { $qin{$key} = {}; } $qin{$key}->{$op} = $val; } return \%qin; }