#!/usr/bin/perl
#
# http proxy for archives
#
use strict;
use warnings;
use HTTP::Proxy qw ( :log );
use HTTP::Proxy::BodyFilter::save;
use HTTP::Proxy::BodyFilter::simple;
use HTTP::Proxy::HeaderFilter::simple;

our $info = {
	debug => 1,
	direct => [ qw/
imageserver.ebscohost.com
	/ ],
};
use Data::Dump qw(dump);

#
# logger.pl
#

use CGI::Util qw( unescape );

$|++; # STDOUT

# get the command-line parameters
my %args = (
   peek    => [],
   header  => [],
   mime    => 'text/*',
);
{
    my $args = '(' . join( '|', keys %args ) . ')';
    for ( my $i = 0 ; $i < @ARGV ; $i += 2 ) {
        if ( $ARGV[$i] =~ /$args/o ) {
            if ( ref $args{$1} ) {
                push @{ $args{$1} }, $ARGV[ $i + 1 ];
            }
            else {
                $args{$1} = $ARGV[ $i + 1 ];
            }
            splice( @ARGV, $i, 2 );
            redo if $i < @ARGV;
        }
    }
}

# the headers we want to see
my @srv_hdr = (
    qw( Content-Type Set-Cookie Set-Cookie2 WWW-Authenticate Location ),
    @{ $args{header} }
);
my @clt_hdr =
  ( qw( Cookie Cookie2 Referer Referrer Authorization ), @{ $args{header} } );

# NOTE: Body request filters always receive the request body in one pass
my $post_filter = HTTP::Proxy::BodyFilter::simple->new(
    begin  => sub { $_[0]->{binary} = 0; },
    filter => sub {
        my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
        print STDOUT "\n", $message->method, " ", $message->uri, "\n";
        print_headers( $message, @clt_hdr );

        if ( $self->{binary} || $$dataref =~ /\0/ ) {
            $self->{binary} = 1;
            print STDOUT "    (not printing binary data)\n";
            return;
        }

        # this is from CGI.pm, method parse_params()
        my (@pairs) = split( /[&;]/, $$dataref );
        for (@pairs) {
            my ( $param, $value ) = split( '=', $_, 2 );
            $param = unescape($param);
            $value = unescape($value);
            printf STDOUT "    %-20s => %s\n", $param, $value;
        }
    }
);

my $get_filter = HTTP::Proxy::HeaderFilter::simple->new(
    sub {
        my ( $self, $headers, $message ) = @_;
        my $req = $message->request;
        if ( $req->method ne 'POST' ) {
            print STDOUT "\n", $req->method, " ", $req->uri, "\n";
            print_headers( $req, @clt_hdr );
        }
        print STDOUT $message->status_line, "\n";
        print_headers( $message, @srv_hdr );
    }
);

sub print_headers {
    my $message = shift;
    for my $h (@_) {
        if ( $message->header($h) ) {
            print STDOUT "    $h: $_\n" for ( $message->header($h) );
        }
    }
}

# create and start the proxy
my $proxy = HTTP::Proxy->new(@ARGV);

# if we want to look at SOME sites
if (@{$args{peek}}) {
    for (@{$args{peek}}) {
        $proxy->push_filter(
            host    => $_,
            method  => 'POST',
            request => $post_filter
        );
        $proxy->push_filter(
            host     => $_,
            response => $get_filter,
            mime     => $args{mime},
        );
    }
}
# otherwise, peek at all sites
else {
    $proxy->push_filter(
        method  => 'POST',
        request => $post_filter
    );
    $proxy->push_filter( response => $get_filter, mime => $args{mime} );
}

#
# pdf.pl
#

my $saved;
$proxy->push_filter(
    # you should probably restrict this to certain hosts as well
    path => qr/\.pdf$/,
    mime => 'application/pdf',
    # save the PDF
    response => HTTP::Proxy::BodyFilter::save->new(
        template => "%f",
        prefix   => 'pdf'
    ),
    # send a HTML message instead
    response => HTTP::Proxy::BodyFilter::simple->new(
        begin => sub {
            my ( $self, $message ) = @_;    # for information, saorge
            $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');
        }
    ),
    # change the response Content-Type
    response => HTTP::Proxy::HeaderFilter::simple->new(
        sub {
            my ( $self, $headers, $response ) = @_;
            $headers->content_type('text/html');
        }
    ),
);

#
# admin interface
#

my $admin_filter = HTTP::Proxy::HeaderFilter::simple->new( sub {
   my ( $self, $headers, $message ) = @_;
warn "XXX ", $headers->header('x-forwarded-for'), ' ', $message->uri, "\n";

	return unless $message->uri->host eq $proxy->host;

	$info->{debug} = not $info->{debug} if $message->uri->query =~ m{debug};
	warn "## ", dump( $headers, $message ) if $info->{debug};

	my $host_port = $proxy->host . ':' . $proxy->port;

	my $res = HTTP::Response->new( 200 );

	if ( $message->uri->path =~ m/(proxy.pac|wpad.dat)/ ) {
		$res->content_type('application/x-ns-proxy-autoconfig');
		$res->content(qq|

function FindProxyForURL(url, host) {
//	if (shExpMatch(url, "*.example.com:*/*"))               {return "DIRECT";}

	if (shExpMatch(url, "*.js")) return "DIRECT";
	if (shExpMatch(url, "*.css")) return "DIRECT";
	if (shExpMatch(url, "*.gif")) return "DIRECT";
	if (shExpMatch(url, "*.png")) return "DIRECT";
	if (shExpMatch(url, "*.ico")) return "DIRECT";
 
//	 if (isInNet(host, "10.0.0.0",  "255.255.248.0"))    {
//		return "PROXY fastproxy.example.com:8080";
//	}
 
	return "PROXY $host_port; DIRECT";
}

		|);
		$self->proxy->response( $res );
		return;
	}

	$res->content_type('text/html');
	$res->content(qq|

<h1>HTTP Proxy Archive</h1>

<a href="http://$host_port/proxy.pac">proxy.pac</a>

	| . '<pre>' . dump($info) . '</pre>' );

	$self->proxy->response( $res );
} );
$proxy->push_filter( request => $admin_filter );


#
# start
#

warn "listen on host ", $proxy->host, " port ", $proxy->port, "\n";

$proxy->start;

