N> Hello all
N> How to make simple http proxy with Perl ?
N> I just want to dump requests in 2 files - in_file and out_file.
N> 10x
You can take mine. It is very simple but works well for debug
purposes. You will need to hack it a little if you need to dump
requests.
#!/usr/bin/perl -w
use strict;
use LWP;
use HTTP::Daemon;
use HTTP::Status;
use constant MAX_REQ => 5;
use constant PORT => 3128;
use constant FORK => 0;
$SIG{CHLD} = 'IGNORE';
my $ua = new LWP::UserAgent;
my $daemon = create_daemon();
while (1) {
while(my $client = $daemon->accept) {
create_child($client);
}
}
sub create_child {
my $client = shift;
if(FORK) {
defined(my $pid = fork) or die "Can't fork: $!";
if($pid > 0) {
print "Started child PID $pid\n";
} else {
process_connection($client);
exit 0;
}
} else {
process_connection($client);
}
}
sub process_connection {
my $client = shift;
my $request;
for(my $i = 0; $i < MAX_REQ and $request = $client->get_request; $i ++) {
print "Got request\n";
print $request->as_string, "\n";
my $response = $ua->simple_request($request);
print "Got response\n";
print $response->headers_as_string, "\n";
$client->send_response($response);
print "Sent response\n";
$client->close;
}
}
sub create_daemon {
my $daemon;
my $port = PORT;
until($daemon = new HTTP::Daemon LocalPort => $port) {
$port ++;
}
if(defined $daemon) {
print "Please contact me at: <URL:", $daemon->url, ">\n";
} else {
die "Can't start daemon: $!";
}
return $daemon;
}
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Ilya Martynov (http://martynov.org/) |
| GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80 E4AE BE1A 53EB 323B DEE6 |
| AGAVA Software Company (http://www.agava.com/) |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-