#!/usr/bin/perl
die "Usage $0 url \n" if $#ARGV <0;
$url = &parse_url($ARGV[0]);
print &GetHTTP($$url{'host'},$$url{'port'},$$url{'path'});
exit 0;


sub GetHTTP {
  use Socket;
  my ($remote,$port,$doc) = @_;
  my ($iaddr, $paddr, $proto, $line,@output);

  $sockaddr = 'S n a4 x8';
  $EOL = "\015\012";
  $BLANK = $EOL x 2;

  if ($port =~ /\D/) { $port = getservbyname($port, 'tcp'); }
  die "No port" unless $port;
  $thataddr = gethostbyname($remote);
  $that = pack($sockaddr, AF_INET, $port, $thataddr);
  $proto = (getprotobyname('tcp'))[2] || 6;
  socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || die "socket: $!";
  connect(SOCK, $that)    || die "connect: $!";
  binmode(SOCK);
  $ofh = select(SOCK); $| = 1; select($ofh); # flush buffer on every write

	print SOCK "GET $doc HTTP/1.1".$EOL.
	           "Host: $thisaddr".$EOL.
	           "User-Agent: $USER_AGENT".$BLANK;

	vec($rin='', fileno(SOCK), 1) = 1;
	select($rin, undef, undef, 20) || die "no respones from server.";

	while( <SOCK> ) {
		s/\r\n/\n/g;
		s/\r/\n/g;
		if ( /HTTP([\/\.\d]+)\s+(\d+)\s+(.*)\n/i ) { $status         = $2; }
		if ( /Content-Type: (\s*)([^;]+)(.*)\n/i ) { $content_type   = $2; }
		if ( /Content-Length: (\s*)(\d+)\n/i )     { $content_length = $2; }
		last if $_ =~ /^$/;
		$header .= $_;
	}

#	die "$status\t$content_type\t$content_length";

	$content='';
	if ($content_length) {
		read(SOCK, $content, $content_length);
	}
	else {
		while ( <SOCK> ) { $content .= $_; }
	}
	close(SOCK);
	select($ofh);
	
	$content;
}

sub parse_url {
    local($url) = @_;

	$ftp_port = 21;
	$http_port = 80;
	$gopher_port = 70;
	$telnet_port = 23;
	$wais_port = 210;
	$news_port = 119;

    if ($url =~ m#^(\w+):#) {
	$protocol = $1;
	$protocol =~ tr/A-Z/a-z/;
    } else {
	return undef;
    }

# URL of type: http://host[:port]/path[?search-string]

    if ($protocol eq "http") {
	if ($url =~ m#^\s*\w+://([\w-\.]+):?(\d*)([^ \t]*)$#) {
	    $server = $1;
	    $server =~ tr/A-Z/a-z/;
	    $port = ($2 ne "" ? $2 : $http_port);
	    $path = ( $3 ? $3 : '/');
	    return { protocol => $protocol, host => $server, port => $port, path =>$path };
	}
	return undef;
    }

}
