#!/usr/local/bin/perl -w
##!c:\Perl\bin\perl -w

use Socket;

require './server_conf.pl';
require './cgilib.pl';

$use_proxy = 0;
$method = "GET";


$query = '';

&init_form();

$query = '?' . $query;

#if($form{'host'}){
#	$host = $form{'host'};
#}

#if($form{'path'}){
#	$path = $form{'path'};
#}

#if($form{'port'}){
#	$port = $form{'port'};
#}

#$query =~ s/\&host.+$//g;

$connect_host = $host;
$connect_port = $port;

$iaddr = inet_aton($connect_host)
	|| die "$connect_host not found.\n";

$sock_addr = pack_sockaddr_in($connect_port, $iaddr);

socket(SOCKET, PF_INET, SOCK_STREAM, 0) || die "error create socket\n";

connect(SOCKET, $sock_addr) || die "$connect_host can not connect $connect_host:$connect_port\n";

#set SOCKET not buffering
select(SOCKET); $|=1; select(STDOUT);

if ( $use_proxy ){
    print SOCKET "$method http://$host:$port$path$query HTTP/1.0\r\n";
} else {
	print SOCKET "$method $path$query HTTP/1.0\r\n";
}
print SOCKET "User-Agent: gbgate\r\n";
print SOCKET "\r\n";

#skip HTTP ...
<SOCKET>;

#header
while (<SOCKET>){
	if(/^Content\-Type\:/){
		$mode = $form{'md'} || '';
		if(($mode eq 'info') or ($mode eq 'info2')){
			print "Content-Type: text/plain\x0D\x0A";
		}
		else{
			print "Content-Type: text/xml\x0D\x0A";
		}
	}
	else{
		print $_;
	}
	m/^\x0D\x0A$/ && last;
}

#body
while (<SOCKET>){
	s/\r\n/\n/g;
	print $_;
}


