in reply to HTTP::Proxy filter for dishing out own files

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...

Replies are listed 'Best First'.
Re^2: HTTP::Proxy filter for dishing out own files
by r1n0 (Beadle) on Oct 06, 2009 at 15:09 UTC
    Thank you very much for the response! :-)

    I have a follow on question before I try what you sent. For the response code:
    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); }

    I would like to know how I can assign the contents of an entire file to the HTTP::Reponse. The response will have the necessary header fields in it if I can just throw the entire file back to the client.
    I might need to through my own response code, but that will be easy enough.
    I would prefer not to have to read in all of the original headers and assign to each component of HTTP::Response, if I can avoid it.

    Thanks in advance for your help.