> How to make simple http proxy with Perl ?
> I just want to dump requests in 2 files - in_file and out_file.

This is what I use. It is from a older issue of the german "Linux
Magazin", http://www.linux-magazin.de/ausgabe/2000/04/Proxy/proxy.html
I have used it on both, linux and windows NT/2000.

Regards,
Reiner.

----- BEGIN PERL SCRIPT -----
#!/usr/bin/perl -w

my $PORT = 3128;

use HTTP::Daemon;
use LWP::UserAgent;
use URI::Escape;

# If Browser disconnects suddenly
$SIG{PIPE} = 'IGNORE';

my $SRV = HTTP::Daemon->new( LocalPort => $PORT );
die "Can�t start server ($@)" unless defined $SRV;
print "Server listening at port $PORT\n";

my $UA = LWP::UserAgent->new;
$UA->agent("perlproxy/1.0");

while (my $conn = $SRV->accept) {
        while (my $request = $conn->get_request) {
                my $resp = $UA->simple_request($request);
                details($request) if $resp->is_success;
                $conn->send_response($resp);
        }
        $conn->close;
}

sub pkv {
        my ($key, $value, $indent) = @_;
        $indent ||= 0;
        my $COLS = 20;

        $dots = $COLS - length($key) - $indent - 2;
        print " " x $indent,
        "$key ", "." x $dots, " ", "$value\n";
}

sub details {
        my $req = shift;
        my $cookies = $req->header("Cookie");

        pkv "URL", "http://"; . $req->uri->netloc . $req->uri->path;
        pkv "Method", $req->method;

        if($cookies) {
                print "Cookies:\n";
                dump_form(map { uri_unescape $_ }
                split /;\s*|=/, $cookies);
        }

        if($req->header("Referer")) {
                pkv "Referer", $req->header("Referer");
        }

        if($req->method eq "GET") {
                print "Parameters\n" if $req->uri =~ /\?/;
                dump_form($req->uri->query_form);
        } elsif ($req->method eq "POST") {
                print "Parameters\n";
                dump_form(map { uri_unescape $_ }
                split /&|=/, $req->content);
        }

        print "=" x 50, "\n";
}

sub dump_form {
        my @form = @_;

        while(my ($key, $val) = splice(@form, 0, 2)) {
                pkv($key, $val, 4);
        }
}
----- END PERL SCRIPT -----

Reply via email to