#!/usr/bin/perl -w use IO::Socket; autoflush STDOUT 1; autoflush STDERR 2; sub handlerequest() { getrequest(); } sub getrequest() { $port = 80; #warn("DEBUG: new client connection $client\n"); while($reqline = <$client>) { chop $reqline; chop $reqline; push(@request, $reqline); if ($reqline =~ /^Host: (.*)$/) { $prehost = $1; if ($prehost =~ /^(.*):(.*)$/) { $host = $1; $port = $2; } else { $host = $prehost; } } if ( $reqline eq "" ) { foreach $line (@request) { print("--> $host: $line\n"); } print("\n"); getresponse(); #print("NOW OUT OF GETRESPONSE, BACK IN GETREQUEST\n"); close($client) and exit; @request = (); } } } sub getresponse() { $webserver = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port, Proto => "tcp", Type => SOCK_STREAM) or warn("Could not contact $host\n") and close($client) and return; print("\tnew webserver connection $webserver\n"); foreach $line (@request) { print($webserver "$line\n"); #print(" -->$line\n"); } $inheader = 1; while($respline = <$webserver>) { chop($respline); if ($respline eq "\r" and $inheader) { print("\t($host: data follows)\n\n"); $inheader = 0; } $inheader && print("\t<-- $host: $respline\n"); print($client "$respline\n"); } print("\t$host closed connection $webserver.\n\n"); } $proxy_port = 8000; $proxy = IO::Socket::INET->new(LocalPort => $proxy_port, Type => SOCK_STREAM, Reuse => 1, Listen => 10 ) # or SOMAXCONN or die "Couldn't bind to port $proxy_port : $@\n"; while ($client = $proxy->accept()) { # $client is the new connection $pid = fork; if ($pid == 0) { close($proxy); handlerequest(); } else { close($client); } } close($proxy);