#!/usr/local/bin/perl
#  Obtain a document via HTTP protocol.
# Jim Davis, Nov 4 1992


($host,$d) = @ARGV;        # get command line args

if ($host eq "") {die "No host supplied.  Stopped";}

if ($d eq "") 
 {$i = index ($host, "/");
  if ($i == -1) {die "No document id supplied.  Stopped";}
  $d = substr ($host,$i);
  $host = substr($host,0,$i);}

($host,$port) = split (':',$host);        # separate optional port

#print ("Host is ", $host, "\nDoc is ", $d ,"\nPort is ", $port, "\n");


if ( $port eq "") {$port = 80;}
if ( $port !~ /\d+$/)            # translate symbolic port 
 {($name, $aliases, $nport) = getservbyname($port, 'tcp');
 if ( $nport =~ /\d+$/)
    { $port = $nport;}
   else
    {die "Could not translate port $port to number.  Stopped";}}


# Can't find this library here, so have to fake it.
#require 'sys/socket.ph';

$sockaddr = 'S n a4 x8';         # packing format

$hostname = `hostname`;
chop($hostname);                 # get rid of trailing CR
($name, $aliases, $type, $len, $myaddr) = gethostbyname($hostname);
$here = pack($sockaddr, 2, 0, $myaddr);

($name, $aliases, $type, $len, $destaddr) = gethostbyname($host);

#print ("Name = ", $name ," " addr = ", $destaddr, "\n");
if ($destaddr eq "") {die "No such host: $host. Stopped";}

$there= pack($sockaddr, 2, $port, $destaddr);

($name, $aliases, $protocol) = getprotobyname('tcp');
socket($S, 2, 1, $protocol) || die "socket: $!";
bind ($S, $here) || die "bind: $!";
connect ($S, $there) || die "connect: $!";


select($S);
$| = 1;           # force output
print ("GET ", $d ,"\n");
select(STDOUT);
while (<$S>) { print;}

