http://qs321.pair.com?node_id=126739
Description: Inspired by LWP head mystery, where a server replies incorrectly to a HEAD request, which is apparently a common bug, this snippet makes a GET request, but closes the socket after reading only 1 character of the content, thus doing basically the same thing HEAD does, while not having to deal with the common problem of a broken HEAD reply.
#!/usr/bin/perl -w

$^W = 552 >> 3;

use strict;          # for sanity (ALWAYS!!!)
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;

HEAD('http://123box.co.uk/');
HEAD('http://japhy.perlmonk.org/book/');

real_HEAD('http://123box.co.uk/');
real_HEAD('http://japhy.perlmonk.org/book/');

sub HEAD {
    my $req = HTTP::Request->new(GET => shift);
    my $UA = new LWP::UserAgent;

    my $res = $UA->request($req, sub { die }, 1);
    
    if($res->is_success) {
        print $res->as_string();
    } else {
        print "Error: " . $res->status_line . "\n";
    }
}


sub real_HEAD {
    my $req = HTTP::Request->new(HEAD => shift);
    my $UA = new LWP::UserAgent;

    my $res = $UA->request($req);
    
    if($res->is_success) {
        print $res->as_string();
    } else {
        print "Error: " . $res->status_line . "\n";
    }
}
__END__
F:\dev\snippets>perl fake.HEAD.pl
HTTP/1.1 200 OK
Connection: close
Date: Wed, 21 Nov 2001 07:49:38 GMT
Server: Apache/1.3.19 (Unix) mod_gzip/1.3.19.1a Resin/1.2.2
Content-Type: text/html
Client-Date: Wed, 21 Nov 2001 07:39:46 GMT
Client-Peer: 212.67.197.196:80
X-Died: Died at fake.HEAD.pl line 18.


HTTP/1.1 200 OK
Connection: close
Date: Wed, 21 Nov 2001 07:40:25 GMT
Accept-Ranges: bytes
Server: Apache/1.3.22 (Unix) mod_perl/1.26 PHP/4.0.6
Content-Length: 1766
Content-Type: text/html
ETag: "57532-6e6-3bf6bcac"
Last-Modified: Sat, 17 Nov 2001 19:38:20 GMT
Client-Date: Wed, 21 Nov 2001 07:39:55 GMT
Client-Peer: 66.92.212.9:80
X-Died: Died at fake.HEAD.pl line 18.


Error: 500 unexpected EOF before status line seen

HTTP/1.1 200 OK
Connection: close
Date: Wed, 21 Nov 2001 07:40:35 GMT
Accept-Ranges: bytes
Server: Apache/1.3.22 (Unix) mod_perl/1.26 PHP/4.0.6
Content-Length: 1766
Content-Type: text/html
ETag: "57532-6e6-3bf6bcac"
Last-Modified: Sat, 17 Nov 2001 19:38:20 GMT
Client-Date: Wed, 21 Nov 2001 07:40:03 GMT
Client-Peer: 66.92.212.9:80
If you wanna try to benchmark (which wouldn't be accurate cause it's network stuff) just add
print "\n>>>>>>>>>>>>>>>>>>>>and now for the benchmark\n";
use Benchmark;
timethese(50, { 'real_HEAD' => sub { real_HEAD('http://japhy.perlmonk.
+org/book/')},
                'HEAD' => sub { HEAD('http://japhy.perlmonk.org/book/'
+)},
              });
I got something resembling
 ........ output from the HEAD .....
      HEAD: 140 wallclock secs (139.73 usr +  0.00 sys = 139.73 CPU) @
+  0.36/s (n=50)
 ........ output from the HEAD .....
 real_HEAD: 208 wallclock secs (208.00 usr +  0.00 sys = 208.00 CPU) @
+  0.24/s (n=50)