#!/usr/bin/perl #!/usr/bin/perl -I/kunden/homepages/23/d94990689/htdocs/perl $version = 0.1; use CGI; use CGI::Carp qw(fatalsToBrowser); use Cache::SizeAwareFileCache; use LWP::UserAgent; use threads; use Thread::Semaphore; use Thread::Queue; $cachesize = 1000000; $cachedir = '/usr/lib/cgi-bin/cache/'; @goodhosts = ('denney.homeip.net'); my $q = CGI->new; my %parm = $q->Vars; $req = HTTP::Request->new(HEAD => $parm{'url'}); unless (grep {$_ eq $req->uri->host()} @goodhosts) { print $q->header(-status => 406). $q->h1("Invalid Host Name"). $q->p("You requested an invalid host"); exit; } my $ua = LWP::UserAgent->new; $ua->agent("Bill's CGICache $version"); $res = $ua->request($req); # check the current file to see if we need to get a new version of it if ($res->is_error()) { print $r->error_as_HTML(); die; } else { # setup the cache my $cache = new Cache::SizeAwareFileCache({'namespace' => 'gallerycache', 'default_expires_in' => 'never', 'cache_root' => $cachedir}); #grab the cache object based on URL my $file = $cache->get($res->base); my $hit = 1; if (! defined $file) { $reqget = HTTP::Request->new(GET => $parm{'url'}); $queue = Thread::Queue->new; print $res->headers->as_string."\n"; $thr = threads->new(\&writer, $queue); #$queue->enqueue($res->headers->as_string."\n"); $resget = $ua->request($reqget, {$queue->enqueue(@_)}); $queue->enqueue(undef); $thr->join; if ($res->is_error()) { print $r->error_as_HTML(); exit; } $file = $resget; # get the file from your server $cache->set($res->base, $resget, $resget->freshness_lifetime); $hit = 0; $file->push_header(Bill_cache => 'Miss'); } $file->init_header(Bill_cache => 'Hit'); if ($hit) { print $file->headers->as_string."\n". $file->content; } # clean up the cache if we wrote to it. unless($hit) { limit_size($cachesize); } } sub writer { my($queue) = @_; while(my $mesg = $queue->dequeue) { print "test\n"; print $mesg; } }
In reply to Creating a CGI Caching Proxy by wsloand
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |