Simple clear-operations HTTP/1.0 GET/HEAD dumper
authorshort <>
Mon, 17 Jun 2002 02:07:16 +0000 (02:07 +0000)
committershort <>
Mon, 17 Jun 2002 02:07:16 +0000 (02:07 +0000)
bin/httpgetdumb [new file with mode: 0755]

diff --git a/bin/httpgetdumb b/bin/httpgetdumb
new file mode 100755 (executable)
index 0000000..9aafa31
--- /dev/null
@@ -0,0 +1,50 @@
+#! /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 "hH",\%opts or die "getopts()";
+@ARGV==1 && $ARGV[0]=~m#^http://([^/]+)(:\d+)?(/.*)$#
+               or die "Syntax: $0 [-hH] http://hostname/pathname";
+my($host,$port,$path)=($1,$2,$3);
+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\nHost: $host\r\n\r\n";
+if ($opts{"H"}) {
+       while (<STDIN>) {
+               chomp;
+               last if /^$/;
+               print SOCK "$_\r\n";
+               }
+       }
+SOCK->flush();
+while (<SOCK>) {
+       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;