Hi. I have a code for a web proxy that forces HTTP/1.0. It works fine and all except for the following:
1. It won't do any POST methods... it just waits then timeouts
2. for some reason, mail sites like gmail, AOL and yahoo mail won't work. I'm getting an "Internet Explorer cannot display the webpage" error on my browser
Can anyone help?
1. It won't do any POST methods... it just waits then timeouts
2. for some reason, mail sites like gmail, AOL and yahoo mail won't work. I'm getting an "Internet Explorer cannot display the webpage" error on my browser
Can anyone help?
Code:
#!c:\bin\perl -w
use URI;
use IO::Socket;
use threads('yield', 'stack_size' => 64*4096);
use strict;
require URI::_foreign;
my $showOpenedSockets=1;
$SIG{PIPE} = 'IGNORE';
# command-line arguments
my $proxy_port=shift(@ARGV);
$proxy_port=8080 unless $proxy_port =~ /\d+/;
my $hostname=`hostname`;
chop $hostname;
# proxy socket
my $server = IO::Socket::INET->new (
LocalHost => $hostname,
LocalPort => $proxy_port,
proto => 'tcp',
Listen => 20,
Reuse => 1
);
# die "Listening Socket could not be created: $!" unless $server;
binmode $server;
while (my $browsera = $server->accept()) {
print "\n\n----Accepted Connection--------\n";
binmode $browsera;
my $t = threads->new(\&fetch, $browsera);
$t->detach;
}
sub fetch{
my $browser = $_[0];
my $method ="";
my $content_length = 0;
my $content = 0;
my $accu_content_length = 0;
my $host;
my $hostAddr;
my $httpVer;
while (my $browser_line = <$browser>) {
unless ($method) {
($method, $hostAddr, $httpVer) = $browser_line =~ /^(\w+) +(\S+) +(\S+)/;
print "$hostAddr";
my $uri = URI->new($hostAddr) or print "Could not build URI for $hostAddr";
$host = IO::Socket::INET->new (
PeerAddr=> $uri->host,
PeerPort=> $uri->port );
die "couldn't open $hostAddr" unless $host;
if ($showOpenedSockets) {
print "Opened ".$uri->host." , port ".$uri->port."\n";
}
binmode $host;
$httpVer="HTTP/1.0";
print $host "$method ".$uri->path_query." $httpVer\n";
# print "$method ".$uri->path_query." $httpVer\n";
next;
}
$content_length = $1 if $browser_line=~/Content-length: +(\d+)/i;
$accu_content_length+=length $browser_line;
#print $browser_line;
print $host $browser_line;
last if $browser_line =~ /^\s*$/ and $method ne 'POST';
if ($browser_line =~ /^\s*$/ and $method eq "POST") {
$content = 1;
last unless $content_length;
next;
}
if ($content) {
$accu_content_length+=length $browser_line;
last if $accu_content_length >= $content_length;
}
}
print "\n\nThread id --> ".threads->self->tid()."\n";
print "No of. threads at present --> ".threads->list()."\n";
print "Stack size --> ".threads->get_stack_size()."\n";
$content_length = 0;
$content = 0;
$accu_content_length = 0;
while (my $host_line = <$host>) {
#print $host_line;
print $browser $host_line;
$content_length = $1 if $host_line=~/Content-length: +(\d+)/i;
if ($host_line =~ m/^\s*$/ and not $content) {
$content = 1;
#last unless $content_length;
next;
}
if ($content) {
if ($content_length) {
$accu_content_length+=length $host_line;
#print "\nContent Length: $content_length, accu: $accu_content_length\n";
last if $accu_content_length >= $content_length;
}
}
}
$browser-> close;
$host -> close;
print "---- End thread ----\n";
}