#!/usr/bin/perl
#
# htget.pl - get a document from a HTTP server
#
#$DEBUG=1;

sub url_split {
        local($URL,$proto,$host,$port,$document);
        $URL = $_[0];
	if ($URL !~ /\//) {
		$host = $URL;
	} else {
		if ($URL =~ /^(\w+):\/\/([^:\/]+)/) {
			$proto = $1;
			$host = $2;
	        	($port) = ($URL =~ /:\/\/[^:\/]+:(\d+)\//);
		        ($document) = ($URL =~ /\w+:\/\/[^\/]+(\/.+)$/);
		} else {
		        ($host) = ($URL =~ /^([^:\/]+)/);
	        	($port) = ($URL =~ /[^:\/]+:(\d+)\//);
		        ($document) = ($URL =~ /[^\/]+(\/.+)$/);
		}
	}
	$proto = 'http' unless $proto;
        $port = 80 unless $port;
	$document = '/' unless $document;
        return($proto,$host,$port,$document);
}

# below are constants and should not require changes
$AF_INET=2;
$TCP=(getprotobyname('tcp'))[2];
$SOCK_STREAM=2 if socket(X,$AF_INET,2,$TCP);
$SOCK_STREAM=1 if socket(X,$AF_INET,1,$TCP);
close X;

$URL = $ARGV[0];
($proto,$host,$port,$document) = &url_split($URL);

print "($proto) $host:$port$document\n" if $DEBUG;

$thataddr = (gethostbyname($host))[4];

$sockaddr = 'S n a4 x8';
$this = pack($sockaddr, $AF_INET, 0, "\0\0\0\0");
$that = pack($sockaddr, $AF_INET, $port, $thataddr);

socket(S, $AF_INET, $SOCK_STREAM, $TCP) || die "socket: $!\n";

bind(S, $this) || die "bind: $!\n";

connect(S, $that) || die "connect: $!\n";

binmode(S); # for Windows, VMS

$WAS=select(S);
$|=1;
select($WAS);

print S "GET ${document} HTTP/1.0\r\n\r\n";
while (<S>) {
	print if $DEBUG;
	last if /^\r?\n?$/;
}
while (<S>) {
	print;
}
close S;
