use strict;
use warnings;
use HTTP::Proxy qw ( :log );
use HTTP::Proxy::BodyFilter::save;
use HTTP::Proxy::BodyFilter::simple;
use HTTP::Proxy::HeaderFilter::simple;
my $proxy = HTTP::Proxy->new( @ARGV );
my $saved;
$proxy->push_filter(
path => qr/\.pdf$/,
mime => 'application/pdf',
response => HTTP::Proxy::BodyFilter::save->new(
template => "%f",
prefix => 'pdf'
),
response => HTTP::Proxy::BodyFilter::simple->new(
begin => sub {
my ( $self, $message ) = @_; $saved = 0;
},
filter => sub {
my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
$$dataref = $saved++ ? ""
: sprintf '<p>Saving PDF file. Go <a href="%s">back</a></p>',
$message->request->header('referer');
}
),
response => HTTP::Proxy::HeaderFilter::simple->new(
sub {
my ( $self, $headers, $response ) = @_;
$headers->content_type('text/html');
}
),
);
$proxy->start;