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.10
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 bandwith 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             There is a small webform, you can instantly test, whether a webserver supports
37             gzip-compression on a particular URL:
38             L
39              
40             =head2 METHODS
41              
42             =over 2
43              
44             =item prepare_request
45              
46             Adds 'Accept-Encoding' => 'gzip' to outgoing HTTP-headers before sending.
47              
48             =item send_request
49              
50             Unzips response-body if 'content-encoding' is 'gzip' and
51             corrects 'content-length' to unzipped content-length.
52              
53             =back
54              
55             =head1 SEE ALSO
56              
57             L
58              
59             L
60              
61             =head1 AUTHOR
62              
63             Peter Giessner C
64              
65             =head1 LICENCE AND COPYRIGHT
66              
67             Copyright (c) 2007, Peter Giessner C.
68             All rights reserved.
69              
70             This module is free software; you can redistribute it and/or
71             modify it under the same terms as Perl itself.
72              
73             =cut
74              
75             package WWW::Mechanize::GZip;
76              
77             our $VERSION = '0.12';
78              
79 1     1   27863 use strict;
  1         2  
  1         38  
80 1     1   6 use warnings;
  1         2  
  1         29  
81 1     1   1065 use Compress::Zlib ();
  1         132163  
  1         60  
82 1     1   10 use base qw(WWW::Mechanize);
  1         1  
  1         1796  
83              
84             ################################################################################
85             sub prepare_request {
86 0     0 1   my ($self, $request) = @_;
87              
88             # call baseclass-method to prepare request...
89 0           $request = $self->SUPER::prepare_request($request);
90              
91             # set HTTP-header to request gzip-transfer-encoding at the webserver
92 0           $request->header('Accept-Encoding' => 'gzip');
93              
94 0           return ($request);
95             }
96              
97             ################################################################################
98             sub send_request {
99 0     0 1   my ($self, $request, $arg, $size) = @_;
100              
101             # call baseclass-method to make the actual request
102 0           my $response = $self->SUPER::send_request($request, $arg, $size);
103              
104             # check if response is declared as gzipped and decode it
105 0 0 0       if ($response && defined($response->headers->header('content-encoding')) && $response->headers->header('content-encoding') eq 'gzip') {
      0        
106             # store original content-length in separate response-header
107 0           $response->headers->header('x-content-length', length($response->{_content}));
108             # decompress ...
109 0           $response->{_content} = Compress::Zlib::memGunzip(\($response->{_content}));
110             # store new content-length in response-header
111 0           $response->{_headers}->{'content-length'} = length($response->{_content});
112             }
113 0           return $response;
114             }
115              
116             1;
117              
118             __END__