#!/usr/bin/perl -w # vim: set sw=4 ts=4 si et: # copyright: freeware, do with it whatever you want. No warranty of any kind. use strict; use Socket; if(!$ARGV[0] || $ARGV[0]!~/http:\/\//i){ print "USAGE: webclient-get.pl http://some.url/and/path\n"; exit(0); } my $host; my $port=80; my $path="/"; if ($ARGV[0]=~/http:\/\/([^\:\/]+):(\d+)/i){ $host=$1; $port=$2; }elsif ($ARGV[0]=~/http:\/\/([^\:\/]+)/i){ $host=$1; } if ($ARGV[0]=~/http:\/\/[^\/]+(\/.+)/i){ $path=$1; } socket(SOCKET,PF_INET,SOCK_STREAM,6)|| die "ERROR: can not create a socket $!\n"; my $sockaddr = pack_sockaddr_in($port, inet_aton($host)) || die ("ERROR: pack_sockaddr_in: $!"); connect( SOCKET, $sockaddr)|| die "ERROR: can not connect to port $port and host $host\n"; send(SOCKET, "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: webclient-get.pl/1.0\r\nAccept: */*\r\n" , 0); send(SOCKET, "\r\n" , 0); alarm 4; # kill me after amount of sec, e.g. in case Content-Length: does not come my $line; my $cl=0; my $bodylen=0; my $start_couting_bodylen=0; while ($line = ) { print "$line"; if ($start_couting_bodylen){ $bodylen+=length($line); if ($bodylen>=$cl){ #print "=== done ==\n"; last; #exit loop } } if (!$cl && $line =~/^Content-Length: (\d+)/){ $cl=$1; alarm 0; # cancel alarm } if($cl && !$start_couting_bodylen && $line eq "\r\n"){ $start_couting_bodylen=1; } } close SOCKET or die "close: $!"; # end of webclient-get.pl __END__