#!/usr/bin/perl

#
# getw: get a WEB page
#
# a simple script to get a page from an HTTP server
# 
# Another Aromatic Productions
#   http://www.aromatic.com/
# 
# Copyright (C) 1998 Raffaele Sena (raff@aromatic.com)
# All rights reserved
#

#
# Don't buffer output (needed for exec)
#
$| = 1;

$cmd = "GET";		# default method
$post = "";		# no post data
$cookie = "";		# no cookies
$referer = "";		# no referer
$verbose = "";		# verbose off

#
# Some valid User-Agent(s)
#
$UA{'ns3'    } = "Mozilla/3.0 (X11; I; Linux 2.0.0 i586)";
$UA{'ns4'    } = "Mozilla/4.04 [en] (X11; I; Linux 2.0.0 i586)";
$UA{'msie'   } = "Mozilla/3.0 (compatible; MSIE 4.0; Windows 95)";
$UA{'erider' } = "Mozilla/4.0 (compatible; MSIE 3.0; ARM; 32bit; EasyRider-ZT; IA) libwww/2.17 modified";

#
# select User-Agent
#
$UA = $UA{'erider'};

#
# Parse parameters
#
while ($ARGV[0] =~ /^-/) {
    if ($ARGV[0] eq "-h") {
	$cmd = "HEAD";
    } elsif ($ARGV[0] eq "-p") {
	$cmd = "POST";
	shift(@ARGV);
	$post = $ARGV[0];
    } elsif ($ARGV[0] eq "-g") {
	$cmd = "GET";
    } elsif ($ARGV[0] eq "-k") {
	shift(@ARGV);
	$cookie = $ARGV[0];
    } elsif ($ARGV[0] eq "-r") {
	shift(@ARGV);
	$referer = $ARGV[0];
    } elsif ($ARGV[0] eq "-ns3") {
	$UA = $UA{'ns3'};
    } elsif ($ARGV[0] eq "-ns4") {
	$UA = $UA{'ns4'};
    } elsif ($ARGV[0] eq "-msie") {
	$UA = $UA{'msie'};
    } elsif ($ARGV[0] eq "-erider") {
	$UA = $UA{'erider'};
    } elsif ($ARGV[0] eq "-ua") {
	shift(@ARGV);
	$UA = $ARGV[0];
    } elsif ($ARGV[0] eq "-v") {
	$verbose = "-v";
    }
    shift(@ARGV);
}

if (! $ARGV[0]) { 	# No parameters
	print "
usage: getw [-v] [-g|-h|-p post] [-k cookie] [-r referer] [-ns3|-ns4|-msie|-ua user-agent] [httpurl|host [page]]
        -v         : verbose (dump request)
        -g         : GET method (default)
        -h         : HEAD method
        -p post    : POST method
        -k cookie  : send cookie
        -r referer : send referer
	-ns3       : user-agent: Netscape 3.0
	-ns4       : user-agent: Netscape 4.0
	-msie      : user-agent: Microsoft Internet Explorer 4.0
	-ua ua     : user-agent: ua
";
	exit 1;
}

#
# Parse URL (or host/page)
#
$host = $ARGV[0];
$page = "";
shift(@ARGV);

#
# HTTP URL is in the form "http://" + host[:port] [ + "/" + pagepath  ]
#
if ($host =~ /^http:\/\/([^\/]*)(.*)/) {
	$host = $1;
	$page = $2;
} elsif ($ARGV[0]) {
	$page = $ARGV[0];
}

#
# Default page is /
#
if (!$page) {
	$page = "/";
}

#
# Prepare for connecting to the server
#
$pat = 'S n a4 x8';
$inet = 2;
$port = 80;
chop($me = `hostname`);

($h, $p) = split(':', $host);

if ($p) {
	$host = $h;
	$port = $p;
}

sub send_to_server {
	local ($socket, $data) = @_;
	print $data if ($verbose);
	print $socket $data;
}

# $SIG{'INT'} = 'dokill';

($name, $aliases, $adrtype, $length, $addr) = gethostbyname($me);
$this = pack($pat,$inet,0, $addr);

($name, $aliases, $adrtype, $length, $addr) = gethostbyname($host);
$that = pack($pat,$inet,$port,$addr);

print "socket...\n" if ($verbose);
socket(S,2,1,6) || die "socket: $!";
print "bind...\n" if ($verbose);
bind(S,$this) || die "bind: $!";
print "connect...\n" if ($verbose);
connect(S,$that) || die "connect: $!";
select(S); $| = 1; select(stdout);

#
# Request page
#
# (pass a valid User-Agent for "picky" servers, and the Host for
#  virtual-domain server)
#
send_to_server(S, "$cmd $page HTTP/1.0\r\n");
send_to_server(S, "User-Agent: $UA\r\n");
send_to_server(S, "Host: $host:$port\r\n");
send_to_server(S, "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*\r\n");

#
# if there is a referer, send it
#
send_to_server(S, "Referer: $referer\r\n") if ($referer);

#
# if there are cookies, send them
#
send_to_server(S, "Cookie: $cookie\r\n") if ($cookie);

#
# if POST, also add the data to post
#
if ($cmd eq "POST") {
	$l = length($post);
	send_to_server(S, "Content-type: application/x-www-form-urlencoded\r\n");
	send_to_server(S, "Content-length: $l\r\n\r\n");
	send_to_server(S, "$post\r\n");
}

#
# An empty line to terminate and send the request
#
send_to_server(S, "\r\n");

$in_header = 1;
$redir = "";

#
# Read reply, and parse header for redirection
#
while ( <S> ) {
	$line = $_;

	if ($in_header) {
		if ($line eq "\r\n") {
			$in_header = 0;
		} else {
			if ($line =~ /^Location: / || $line =~ /^LOCATION: /) {
				$redir = $line;
				chop($redir);
				chop($redir);
			}
		}
	}

	print $line;
}

#
# Redirection: send request to the new server
#
if ($redir) {
	$redir =~ s|^........: http://(.*)|\1|;
	($host, $path) = split('/',$redir, 2);
	print "---------------------------\n";
	print "Redirect to: $host $path\n";

	exec("getw $verbose $host '/$path'");
}

exit 0;

