#!/usr/bin/perl -w sub help { print STDERR <<'HELPEOF'; Talk To Http Server (tths) Downloads HTTP documents to stdout. Version 1.3 by muppet, mar-apr 2000 usage: tths [options] URL URL may be relative to access localhost. relative URL MUST include a leading slash (/). otherwise, include a standard URL of the form http://host:port/path/to/file if a port number is not supplied, tths will use 80. NOTE: no HTTP brains (such as redirect) are included in this program!!! options: -pPROXY.ADDRESS --proxy=PROXY.ADDRESS use the given HTTP proxy server. if the port number is not given (in the normal fashion) it is assumed to be 80. -h or --help print this message and exit. --head use the HEAD request instead of GET. -v or --verbose print debug info to stderr. HELPEOF } # init $action = "GET"; $verbose = 0; foreach (@ARGV) { SWITCH: { /^(-p|--proxy=)/ and do { s/$1//; $proxyURL = $_; last SWITCH; }; /^(-v|--verbose)/ and do { $verbose = 1; last SWITCH; }; /^(-h|--help)/ and do { help; exit; }; /^--head/ and do { $action = "HEAD"; last SWITCH; }; $URL = $_ if (!defined($URL)); } } if (!defined($URL)) { help; exit; } if (defined($proxyURL)) { ($remote,$port) = split(/:/,$proxyURL); # get host and port $port = 80 if (!defined($port)); # use default if not supplied $page = $URL; } elsif ($URL =~ /^http:\/\//) { # user specified full URL --- parse into pieces... $URL =~ s/http:\/\///; # strip protocol name $URL =~ s/\//\ \//; # separate host and path ($remote,$page) = split(/\ /,$URL); # get host and path $page = "/" if (!defined($page)); # use default if not supplied ($remote,$port) = split(/:/,$remote); # get host and port $port = 80 if (!defined($port)); # use default if not supplied } else { # make some assumptions for relative URL $remote = "localhost"; $port = 80; $page = $URL; } if ($verbose) { print STDERR "------------------------------\n"; print STDERR " host: $remote\n"; print STDERR " port: $port\n"; print STDERR " path: $page\n"; print STDERR "------------------------------\n"; } # we need the Socket library in order to talk over the network... use Socket; # resolve the hostname into an actual internet address. $iaddr = inet_aton($remote) || die "no host: $remote\n"; $paddr = sockaddr_in($port, $iaddr); $proto = getprotobyname('tcp'); # create and open the network connection socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $1\n"; connect(SOCK, $paddr) || die "connect: $!\n"; # set the socket to autoflush. select(SOCK); $| = 1; select(STDOUT); # send the HTTP request. $request = "$action $page HTTP/1.0\015\012Host: $remote\015\012User-Agent: foobar\015\012\015\012"; print STDERR "------------\n$request\n-----------\n" if ($verbose); print SOCK $request; # copy the response to stdout. while (defined($line = )) { print STDOUT $line; } close(SOCK) || die "close: $!\n"; exit;