| 1 | #!/usr/bin/perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use utf8; |
| 5 | use Benchmark ':all'; |
| 6 | use HTTP::Parser::XS ':all'; |
| 7 | use LWP::UserAgent; |
| 8 | |
| 9 | |
| 10 | my $buf = <<'...'; |
| 11 | HTTP/1.0 200 OK |
| 12 | Date: Fri, 29 Oct 2010 05:19:06 GMT |
| 13 | Server: hi |
| 14 | Status: 200 OK |
| 15 | X-Transaction: 1414179016-1573-34157 |
| 16 | ETag: "0694329108904124516f45127c9543d9"-gzip |
| 17 | Last-Modified: Fri, 29 Oct 2010 05:19:06 GMT |
| 18 | X-Runtime: 0.01082 |
| 19 | Content-Type: text/html; charset=utf-8 |
| 20 | Pragma: no-cache |
| 21 | X-Revision: DEV |
| 22 | Expires: Tue, 31 Mar 1981 05:00:00 GMT |
| 23 | Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0 |
| 24 | Set-Cookie: auth_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT |
| 25 | Set-Cookie: _twitter_sess=BJKL5252d3Hjkmode=block |
| 26 | X-Frame-Options: SAMEORIGIN |
| 27 | Vary: Accept-Encoding |
| 28 | Content-Encoding: gzip |
| 29 | Content-Length: 20 |
| 30 | Connection: close |
| 31 | |
| 32 | ... |
| 33 | $buf =~ s/\015?\012/\015\012/; |
| 34 | |
| 35 | if (my $url = shift @ARGV) { |
| 36 | my $ua = LWP::UserAgent->new(parse_head => 0); |
| 37 | my $res = $ua->head($url); |
| 38 | die $res->status_line unless $res->is_success; |
| 39 | $buf = $res->as_string; |
| 40 | } |
| 41 | |
| 42 | # $buf is valid? |
| 43 | my ($ret, $minor_version, $status, $msg) = parse_http_response($buf, HEADER_NONE); |
| 44 | $ret > 0 or die "*** Cannot parse header ***\n$buf"; |
| 45 | |
| 46 | cmpthese( |
| 47 | -1, { |
| 48 | 'none' => sub { |
| 49 | parse_http_response($buf, HEADER_NONE); |
| 50 | }, |
| 51 | 'arrayref' => sub { |
| 52 | parse_http_response($buf, HEADERS_AS_ARRAYREF); |
| 53 | }, |
| 54 | 'arrayref+special' => sub { |
| 55 | my %special = ( |
| 56 | 'connection' => '', |
| 57 | 'content-length' => undef, |
| 58 | 'location' => '', |
| 59 | 'content-encoding' => '', |
| 60 | 'transfer-encoding' => '', |
| 61 | ); |
| 62 | parse_http_response( |
| 63 | $buf, |
| 64 | HEADERS_AS_ARRAYREF, |
| 65 | \%special, |
| 66 | ); |
| 67 | }, |
| 68 | 'special' => sub { |
| 69 | my %special = ( |
| 70 | 'connection' => '', |
| 71 | 'content-length' => undef, |
| 72 | 'location' => '', |
| 73 | 'content-encoding' => '', |
| 74 | 'transfer-encoding' => '', |
| 75 | ); |
| 76 | parse_http_response( |
| 77 | $buf, |
| 78 | HEADER_NONE, |
| 79 | \%special, |
| 80 | ); |
| 81 | }, |
| 82 | 'hashref' => sub { |
| 83 | parse_http_response($buf, HEADERS_AS_HASHREF); |
| 84 | }, |
| 85 | }, |
| 86 | ); |
| 87 |