File Coverage

blib/lib/WWW/Mechanize/GZip.pm
Criterion Covered Total %
statement 12 23 52.1
branch 0 2 0.0
condition 0 6 0.0
subroutine 4 6 66.6
pod 2 2 100.0
total 18 39 46.1


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             WWW::Mechanize::GZip - tries to fetch webpages with gzip-compression
4              
5             =head1 VERSION
6              
7             Version 0.13
8              
9             =head1 SYNOPSIS
10              
11             use WWW::Mechanize::GZip;
12              
13             my $mech = WWW::Mechanize::GZip->new();
14             my $response = $mech->get( $url );
15              
16             print "x-content-length (before unzip) = ", $response->header('x-content-length');
17             print "content-length (after unzip) = ", $response->header('content-length');
18              
19             =head1 DESCRIPTION
20              
21             The L module tries to fetch a URL by requesting
22             gzip-compression from the webserver.
23              
24             If the response contains a header with 'Content-Encoding: gzip', it
25             decompresses the response in order to get the original (uncompressed) content.
26            
27             This module will help to reduce bandwidth fetching webpages, if supported by the
28             webeserver. If the webserver does not support gzip-compression, no decompression
29             will be made.
30              
31             This modules is a direct subclass of L and will therefore support
32             any methods provided by L.
33              
34             The decompression is handled by L::memGunzip.
35              
36             =head2 METHODS
37              
38             =over 2
39              
40             =item prepare_request
41              
42             Adds 'Accept-Encoding' => 'gzip' to outgoing HTTP-headers before sending.
43              
44             =item send_request
45              
46             Unzips response-body if 'content-encoding' is 'gzip' and
47             corrects 'content-length' to unzipped content-length.
48              
49             =back
50              
51             =head1 SEE ALSO
52              
53             L
54              
55             L
56              
57             =head1 AUTHOR
58              
59             Peter Giessner C
60              
61             =head1 LICENCE AND COPYRIGHT
62              
63             Copyright (c) 2007, Peter Giessner C.
64             All rights reserved.
65              
66             This module is free software; you can redistribute it and/or
67             modify it under the same terms as Perl itself.
68              
69             =cut
70              
71             package WWW::Mechanize::GZip;
72              
73             our $VERSION = '0.13';
74              
75 1     1   46873 use strict;
  1         1  
  1         21  
76 1     1   3 use warnings;
  1         1  
  1         17  
77 1     1   468 use Compress::Zlib ();
  1         46969  
  1         26  
78 1     1   6 use base qw(WWW::Mechanize);
  1         1  
  1         641  
79              
80             ################################################################################
81             sub prepare_request {
82 0     0 1   my ($self, $request) = @_;
83              
84             # call baseclass-method to prepare request...
85 0           $request = $self->SUPER::prepare_request($request);
86              
87             # set HTTP-header to request gzip-transfer-encoding at the webserver
88 0           $request->header('Accept-Encoding' => 'gzip');
89              
90 0           return ($request);
91             }
92              
93             ################################################################################
94             sub send_request {
95 0     0 1   my ($self, $request, $arg, $size) = @_;
96              
97             # call baseclass-method to make the actual request
98 0           my $response = $self->SUPER::send_request($request, $arg, $size);
99              
100             # check if response is declared as gzipped and decode it
101 0 0 0       if ($response && defined($response->headers->header('content-encoding')) && $response->headers->header('content-encoding') eq 'gzip') {
      0        
102             # store original content-length in separate response-header
103 0           $response->headers->header('x-content-length', length($response->{_content}));
104             # decompress ...
105 0           $response->{_content} = Compress::Zlib::memGunzip(\($response->{_content}));
106             # store new content-length in response-header
107 0           $response->{_headers}->{'content-length'} = length($response->{_content});
108             }
109 0           return $response;
110             }
111              
112             1;
113              
114             __END__