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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.