#! /usr/bin/perl # # $Id$ use strict; use warnings; use Socket; use IO::Handle; use Getopt::Std; use constant DEFAULT_PORT=>80; use constant BUFFER_LENGTH=>0x1000; my %opts; getopts "xhH",\%opts or die "getopts()"; @ARGV==1 && $ARGV[0]=~m#^http://(([^/:]+)(?::(\d+))?)(/.*)$# or die "Syntax: $0 [-hH] http://hostname/pathname"; my($hostport,$host,$port,$path)=($1,$2,$3,$4); defined $port or $port=DEFAULT_PORT; my $proto=getprotobyname "tcp" or die "getprotobyname \"tcp\": $!"; socket SOCK,PF_INET,SOCK_STREAM,$proto or die "socket PF_INET,SOCK_STREAM,\"tcp\": $!"; my $hostaddr=gethostbyname $host or die "hostname \"$host\": $!"; my $sockaddr=sockaddr_in $port,$hostaddr or die "sockaddr_in(".inet_ntoa($hostaddr).":$port): $!"; connect SOCK,$sockaddr or die "connect \"$host\"(".inet_ntoa($hostaddr).":$port): $!"; print SOCK ($opts{"h"} ? "HEAD" : "GET")." $path HTTP/1.0\r\n"; my %header=( "Host"=>$hostport, "X-httpgetdumb"=>'$Id$', ); $header{"Accept"}="application/xhtml+xml" if $opts{"x"}; while (my($key,$val)=each(%header)) { print SOCK "$key: $val\r\n"; } print SOCK "\r\n"; if ($opts{"H"}) { while () { chomp; last if /^$/; print SOCK "$_\r\n"; } } SOCK->flush(); while () { print STDERR; chomp; last if /^\r?$/; } for (;;) { my $buf; my $got=read SOCK,$buf,BUFFER_LENGTH; defined $got or die "read: $!"; last if !$got; die "read()=$got (<0)" if $got<0; print $buf; } exit 0;