| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::CheckGzip; | 
| 2 | 1 |  |  | 1 |  | 528 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 3 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 30 |  | 
| 4 |  |  |  |  |  |  | our $VERSION = '0.02'; | 
| 5 | 1 |  |  | 1 |  | 4 | use Carp; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 47 |  | 
| 6 | 1 |  |  | 1 |  | 162 | use Gzip::Faster; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | use LWP::UserAgent; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | sub builtin_test | 
| 10 |  |  |  |  |  |  | { | 
| 11 |  |  |  |  |  |  | my ($ok, $message) = @_; | 
| 12 |  |  |  |  |  |  | if ($ok) { | 
| 13 |  |  |  |  |  |  | print "OK - $message.\n"; | 
| 14 |  |  |  |  |  |  | } | 
| 15 |  |  |  |  |  |  | else { | 
| 16 |  |  |  |  |  |  | print "Not OK - $message.\n"; | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub new | 
| 21 |  |  |  |  |  |  | { | 
| 22 |  |  |  |  |  |  | my ($class, $test) = @_; | 
| 23 |  |  |  |  |  |  | my $o = bless {}; | 
| 24 |  |  |  |  |  |  | $o->{ua} = LWP::UserAgent->new (agent => __PACKAGE__); | 
| 25 |  |  |  |  |  |  | if (! $test) { | 
| 26 |  |  |  |  |  |  | $test = \& builtin_test; | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  | $o->{test} = $test; | 
| 29 |  |  |  |  |  |  | return $o; | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub get_compressed | 
| 33 |  |  |  |  |  |  | { | 
| 34 |  |  |  |  |  |  | my ($o, $url) = @_; | 
| 35 |  |  |  |  |  |  | my $ua = $o->{ua}; | 
| 36 |  |  |  |  |  |  | $ua->default_header ('Accept-Encoding' => 'gzip'); | 
| 37 |  |  |  |  |  |  | my $r = $ua->get ($url); | 
| 38 |  |  |  |  |  |  | return $r; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub get_uncompressed | 
| 42 |  |  |  |  |  |  | { | 
| 43 |  |  |  |  |  |  | my ($o, $url) = @_; | 
| 44 |  |  |  |  |  |  | my $ua = $o->{ua}; | 
| 45 |  |  |  |  |  |  | $ua->default_header ('Accept-Encoding' => ''); | 
| 46 |  |  |  |  |  |  | my $r = $ua->get ($url); | 
| 47 |  |  |  |  |  |  | return $r; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | sub check | 
| 51 |  |  |  |  |  |  | { | 
| 52 |  |  |  |  |  |  | my ($o, $url) = @_; | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | if (! $url) { | 
| 55 |  |  |  |  |  |  | carp "No URL supplied"; | 
| 56 |  |  |  |  |  |  | return; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | # Test with compressed. | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | my $r = $o->get_compressed ($url); | 
| 62 |  |  |  |  |  |  | my $get_ok = $r->is_success (); | 
| 63 |  |  |  |  |  |  | & {$o->{test}} (!! $get_ok, "successfully got compressed $url"); | 
| 64 |  |  |  |  |  |  | if (! $get_ok) { | 
| 65 |  |  |  |  |  |  | return; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  | my $content_encoding = $r->header ('Content-Encoding'); | 
| 68 |  |  |  |  |  |  | & {$o->{test}} (!! $content_encoding, "got content encoding"); | 
| 69 |  |  |  |  |  |  | & {$o->{test}} ($content_encoding eq 'gzip', "content encoding is gzip"); | 
| 70 |  |  |  |  |  |  | my $text = $r->content (); | 
| 71 |  |  |  |  |  |  | my $unc; | 
| 72 |  |  |  |  |  |  | eval { | 
| 73 |  |  |  |  |  |  | $unc = gunzip ($text); | 
| 74 |  |  |  |  |  |  | }; | 
| 75 |  |  |  |  |  |  | & {$o->{test}} (! $@, "$url correctly gzipped"); | 
| 76 |  |  |  |  |  |  | & {$o->{test}} (length ($unc) > length ($text), | 
| 77 |  |  |  |  |  |  | "compression made it smaller"); | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | # Test with uncompressed. | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | my $runc = $o->get_uncompressed ($url); | 
| 82 |  |  |  |  |  |  | my $get_ok_unc = $runc->is_success (); | 
| 83 |  |  |  |  |  |  | & {$o->{test}} (!! $get_ok, "successfully got uncompressed $url"); | 
| 84 |  |  |  |  |  |  | my $content_encoding_unc = $runc->header ('Content-Encoding'); | 
| 85 |  |  |  |  |  |  | & {$o->{test}} (! $content_encoding_unc, "Did not get content encoding"); | 
| 86 |  |  |  |  |  |  | my $unctext = $runc->content (); | 
| 87 |  |  |  |  |  |  | eval { | 
| 88 |  |  |  |  |  |  | gunzip ($unctext); | 
| 89 |  |  |  |  |  |  | }; | 
| 90 |  |  |  |  |  |  | & {$o->{test}} ($@, "$url not gzipped when requesting ungzipped"); | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | 1; |