blib/lib/WWW/Mechanize/GZip.pm

filter: Statementstmt 56.0% 14 / 25 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 66.6% 4 / 6 Podpod 100% 2 / 2 totaltotal 45.4% 20 / 44 CC 4 SCAR 22.6
CC
4
Cov
42%
send_request
20.0
prepare_request
2.0
BEGIN
1.0
CRAP 9.6
↑ Summary
1 =head1 NAME
2  
3 WWW::Mechanize::GZip - tries to fetch webpages with gzip-compression
4  
5 =head1 VERSION
6  
7 Version 0.14
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<WWW::Mechanize::GZip> 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<WWW::Mechanize> and will therefore support
32 any methods provided by L<WWW::Mechanize>.
33  
34 The decompression is handled by L<Compress::Zlib>::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<WWW::Mechanize>
54  
55 L<Compress::Zlib>
56  
57 =head1 AUTHOR
58  
59 Peter Giessner C<cardb@planet-elektronik.de>
60  
61 =head1 LICENCE AND COPYRIGHT
62  
63 Copyright (c) 2007, Peter Giessner C<cardb@planet-elektronik.de>.
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 2 our $VERSION = '0.14';
74  
75 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 ▶use Compress::Zlib ();
Subroutine BEGIN: called CC=1 SCAR=0.0
78 2 ▶use base qw(WWW::Mechanize);
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 ################################################################################
81 0 ▶sub prepare_request {
Subroutine prepare_request: not called CC=1 SCAR=6.9
Pod documented
82 0 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 0 ▶sub send_request {
Subroutine send_request: not called CC=4 SCAR=30.0
Pod documented
95 0 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 ▶ if ($response && defined($response->headers->header('content-encoding')) && $response->headers->header('content-encoding') eq 'gzip') {
Branch: 1 branch
BranchTrueFalseTotal
if ($response and defined $response->headers->header("content-encoding") and $response->headers->header("content-encoding") eq "gzip") 0 0 0
Condition0%0/6
$response and defined $response->headers->header("content-encoding") and $response->headers->header("content-encoding") eq "gzip"
!ll&&!rl&&r
000
$response and defined $response->headers->header("content-encoding")
!ll&&!rl&&r
000
MC/DC0%0/3
$response and defined $response->headers->header("content-encoding") and $response->headers->header("content-encoding") eq "gzip"
$response defined $response->headers->header("content-encoding") $response->headers->header("content-encoding") eq "gzip"
Truth table0%0/4
A and B and C
A= $responseB= defined $response->headers->header("content-encoding")C= $response->headers->header("content-encoding") eq "gzip"
ABCresult
0XX0
10X0
1100
1111
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 2 1;
113  
114 __END__
↑ Summary