1#!/usr/bin/perl
2#
3# Saves all PDF files, and just confirm saving to the client
4# (the PDF file never arrives to the client, but is replaced by
5# a simple HTML file)
6#
7# Based on a request by Emmanuel Di Pr�toro
8#
9use strict;
10use warnings;
11use HTTP::Proxy qw ( :log );
12use HTTP::Proxy::BodyFilter::save;
13use HTTP::Proxy::BodyFilter::simple;
14use HTTP::Proxy::HeaderFilter::simple;
15
16my $proxy = HTTP::Proxy->new( @ARGV );
17
18my $saved;
19$proxy->push_filter(
20    # you should probably restrict this to certain hosts as well
21    path => qr/\.pdf$/,
22    mime => 'application/pdf',
23    # save the PDF
24    response => HTTP::Proxy::BodyFilter::save->new(
25        template => "%f",
26        prefix   => 'pdf'
27    ),
28    # send a HTML message instead
29    response => HTTP::Proxy::BodyFilter::simple->new(
30        begin => sub {
31            my ( $self, $message ) = @_;    # for information, saorge
32            $saved = 0;
33        },
34        filter => sub {
35            my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
36            $$dataref = $saved++ ? ""
37              : sprintf '<p>Saving PDF file. Go <a href="%s">back</a></p>',
38                        $message->request->header('referer');
39        }
40    ),
41    # change the response Content-Type
42    response => HTTP::Proxy::HeaderFilter::simple->new(
43        sub {
44            my ( $self, $headers, $response ) = @_;
45            $headers->content_type('text/html');
46        }
47    ),
48);
49
50$proxy->start;
51
52