Here's a snippet that will hopefully get you started:
#!/usr/bin/perl use HTTP::Proxy; use HTTP::Proxy::HeaderFilter::simple; use Digest::MD5 qw(md5_hex); my $proxy = HTTP::Proxy->new( port => 3128 ); my $filter = HTTP::Proxy::HeaderFilter::simple->new( sub { my ($self, $headers, $request) = @_; my $uri = $request->uri(); my $cache_path = md5_hex($uri); # (depending on the file system being used, you might want to # have the cache files spread across several directory levels; # you'd of course need to have stored them that way in the fir +st place...) substr($cache_path, $_, 0) = "/" for (2,5,8); # prepend base path $cache_path = "/path/to/cache/$cache_path"; # debug printf "URI = %s\n", $uri; printf "host = %s\n", $uri->host(); printf "path = %s\n", $uri->path(); printf "query = %s\n", $uri->query(); printf "cache_path = %s\n", $cache_path; if (-s $cache_path) { # is it in the cache? # this would of course be the content read from the cache +file... my $content = "<html><body>...yadda yadda...</body></html> +"; # create response my $res = HTTP::Response->new(200); $res->content_type('text/html'); $res->content($content); # send back (short-circuit normal content fetching) $self->proxy()->response($res); } } ); $proxy->push_filter( request => $filter ); $proxy->start;
For example, when fetching your PM node via that proxy, the debug output showing the request URI and cache path would look like:
URI = http://perlmonks.org/?node_id=799308 host = perlmonks.org path = / query = node_id=799308 cache_path = /path/to/cache/0c/12/7f/026c96a135e45706194ba5b1f8
And if you had stored associated content under the cache path 0c/12/7f/026c96a135e45706194ba5b1f8, it would be served instead of the real remote content...
In reply to Re: HTTP::Proxy filter for dishing out own files
by almut
in thread HTTP::Proxy filter for dishing out own files
by r1n0
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |