1#!/usr/bin/perl -w
2use HTTP::Proxy;
3use HTTP::Proxy::HeaderFilter::simple;
4use Fcntl ':flock';
5use strict;
6
7# this is a tracker proxy that stores Referer, URL, CODE
8# and output them to STDOUT or the given file
9#
10# Example output:
11#
12# NULL http://www.perl.org/ 200
13# http://www.perl.org/ http://learn.perl.org/ 200
14#
15my $file = shift || '-';
16open OUT, ">> $file" or die "Can't open $file: $!";
17
18my $proxy = HTTP::Proxy->new( @ARGV ); # pass the args you want
19$proxy->push_filter(
20    response => HTTP::Proxy::HeaderFilter::simple->new(
21        sub {
22            my ( $self, $headers, $message ) = @_;
23
24            flock( OUT, LOCK_EX );
25            print OUT join( " ",
26                  $message->request->headers->header( 'Referer' ) || 'NULL',
27                  $message->request->uri,
28                  $message->code ), $/;
29            flock( OUT, LOCK_UN );
30        }
31    )
32);
33$proxy->start;
34
35END { close OUT; }
36
37