#!/usr/local/bin/perl5

$agent="TangoAgent/1.0";
push(@INC,"/servers/npac_http/cgi-tools/gchenglib");

require 'socket.ph';

$index="index.html";
$rn="\r\n";

$input=$ENV{'QUERY_STRING'};
    ($qname,$url)=split(/=/,$input);

$fullurl = $url = &unescape($url);

unless (($host,$port,$path,$file,$extra)=&split_url($url)) {
    &error("$0: Malformed http URL: $url\n") unless $path;
}

if (!$file) {$file=$index;}
if (!$path) {$path="/";}
if (($file !~ /\./)) {
    $path .=$file . "/";
    $file=$index;
}

&init_sock($host,$port) || &error("$0: Cannot initialize socket\n");
     
     $path=$path . $file . $extra;
     $path=~s![^/]*$!!;
     $in="http://$host:$port$path";
     local($url) = $path;
     local($status,$code,$info,$line,$length,$html,$redirect,$update,$data,
	   $bytes,$step,$slice);
     socket(S,&PF_INET,&SOCK_STREAM,$local_prot) || &error("$0: socket: 
$!\n"); #P4
     bind(S,$local_sock) || &error("$0: bind: $!\n");
     connect(S,$remote_sock) || &error("901: TCP/IP error\n");
     $_=select(S); $|=1; select($_);
     
     $request = "$url HTTP/1.0${rn}" .
	 "User-Agent: $agent${rn}Accept: */*${rn}$update${rn}";

     print S "GET $request";

     chop($status=<S>);

     $status=~s/\r$//;
     undef $code;
     ($code,$info)=($status=~m!^HTTP\S+\s+(\d\d\d)\s*(.*)$!i);

     if ($code==404) {
           &error("<HEAD><TITLE>File Not found</TITLE></HEAD>" . 
		   "<BODY><H1>File Not found</H1>" .
	           "The requested URL $url was not found on this server.<P> </BO
DY>");
     }

     if ($code==200 || $code==302) {
	 undef $length;
	 undef $redirect;
	 chop($line=<S>);
	 $line=~s/\r$//;
	 while (defined $line) {
	     last if $line=~/^$/;
	     if ($line=~ m!^Content-Type:\s+[^/\s]+/html.*!i) {
		 $html=1;
		 print "$line\n\n";
	     }
	     $redirect=$1 if $line=~/^Location:\s+(\S+)/i;
	     chop($line=<S>);
	     $line=~s/\r$//;
	 }
	 if ($code==302 && defined($redirect)) {
               &error("The URL: $url is redirected to $redirect.");
	 }
	 else {  
	     while($_ = <S>) { print;}
	     close(S);
	 }
     }	
     else {
             &error("Error in fetching $url.");
     }

exit;

sub split_url {
    local($url)=@_;
    local($host,$port,$path,$file,$extra,$v);
    ($url,$extra)=$url=~m!^([^\?\#]*)(.*)$!;
    $v=$url=~m!^http:(//([^:/]*)(:(\d*))?)?((/([^/]+/)*)?([^/]*))?$!;
    return () unless $v;
    ($host,$port,$path,$file)=(($2?$2:"localhost"),($4?$4:80),$6,$8);
    ($host,$port,$path,$file,$extra);
}

sub init_sock {
    local($host,$port)=@_;
    $sockaddr='S n a4 x8';
    chop($local_host=`hostname`);
    $local_prot=(getprotobyname('tcp'))[2];
    $local_sock=pack($sockaddr,&AF_INET,0,(gethostbyname($local_host))[4]); #P4
    $remote_sock=pack($sockaddr,&AF_INET,$port,(gethostbyname($host))[4]); #P4
}

sub unescape {
    ($_)=@_;
    #tr/+/ /;
    s/%(..)/pack("c",hex($1))/ge;
    $_;
}

sub error {
    local($msg) = @_;
    print "Content-type: text/html\n\n";
    print "<pre>" . $msg . "</pre>\n";
    exit;
}
