if (defined $byte_read) {
debug "Client->Proxy ($byte_read bytes)",$sck;
if ($byte_read == 0) { # remote client/server just closed the connection?
debug "Client closed the connection",$sck;
clientclose $sck;
next READ_FHS;
}
else {
$st{$sck}{req} .= $buffer;
}
}
elsif ($! == EAGAIN()) { # socket buffer empty
debug "Client too slow, skipping... (non-blocking)",$sck;
last;
}
else { # anything elses...
debug "Client connection error",$sck;
clientclose $sck;
next READ_FHS;
}
}
debug "Parsing header...", $sck;
if ($st{$sck}{mark} == 1) { # get request line
while (1) { # skip crlf before start-line
unless ($st{$sck}{req} =~ s/^([^\r]*\r\n)//) {
debug "Client send partial request line header...getting some more", $sck;
next READ_FHS;
}
$buffer = $1;
$st{$sck}{req_orig} .= $buffer;
last if $buffer ne "\r\n";
}
# parse request start-line
if ($buffer =~ /^(\w+)\s+([^\s]+)\s+([^\s]+)/) {
$st{$sck}{metode} = $1;
$st{$sck}{uri} = $2;
$st{$sck}{versi} = $3; $st{$sck}{persist} = 0 if $3 ne "HTTP/1.1";
$st{$sck}{uri} =~ m|^((\w*)://)?(.*?)(:(\d*))?(/.*)?$|;
$st{$sck}{uri_scheme} = $2;
$st{$sck}{uri_hostname} = $3;
$st{$sck}{uri_port} = $5 ? $5 : "80";
$st{$sck}{uri_abspath} = $6 ? $6 : "/";
}
else {
quickresp 400, "Bad Request Start-line: $buffer", $sck;
$st{$sck}{persist} = 0;
next READ_FHS;
}
if ($st{$sck}{uri_hostname} eq "!config") { # pure GNU, anyone may see the source code
my $haha = `cat $0`;
$haha =~ s/</>/g;
$haha =~ s/>/</g;
quickresp 200,"Here's the source code of this program:\n</h3><pre>$haha</pre>",$sck;
$st{$sck}{persist} = 0;
next READ_FHS;
}
if ($st{$sck}{uri_hostname} eq "") {
quickresp 400,"You need to give absolute URI",$sck;
$st{$sck}{persist} = 0;
next READ_FHS;
}
for my $word (@sensors) { # access rule by URI
if ( $st{$sck}{uri} =~ /$word/i) {
quickresp 400,"Access blocked by URI ($st{$sck}{uri})",$sck;
next READ_FHS;
}
}
# build request line using URI rule
$st{$sck}{req_line} = join "", $st{$sck}{metode}," ", $proxy ?
$st{$sck}{uri} :
$st{$sck}{uri_abspath},
" ", "HTTP/1.1\r\n";
$st{$sck}{mark} = 2;
}
if ($st{$sck}{mark} == 2) { # get request field header
while (1) {
unless ($st{$sck}{req} =~ s/^([^\r]*\r\n)//) {
debug "Client send partial field header...getting some more", $sck;
next READ_FHS;
}
$buffer = $1;
last if $buffer eq "\r\n";
$st{$sck}{req_orig} .= $buffer;
# removing request field-line
next if $buffer =~ /^Connection:/i;
next if $buffer =~ /^Proxy-Connection:/i;
$st{$sck}{req_fields} .= $buffer;
}
$st{$sck}{req_fields}||="";
# Virtual host rule
if ($st{$sck}{req_fields} !~ /^Host:/im) {
$st{$sck}{req_fields} .= "Host: $st{$sck}{uri_hostname}:$st{$sck}{uri_port}\r\n";
}
# Persistent connection rule
if ($st{$sck}{versi} ne "HTTP/1.1" or
=2= |