blib/lib/MasonX/Plugin/Compress.pm

filter: Statementstmt 100% 59 / 59 Branchbran 59.0% 13 / 22 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 83.6% 87 / 104 CC 14 SCAR 15.6
CC
14
Cov
85%
_deflate
7.7
end_request_hook
7.4
_set_headers
2.1
CRAP 4.7
↑ Summary
1 package MasonX::Plugin::Compress;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Compress::Zlib();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Compress::Bzip2 2.0 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use List::Util();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #use Apache::Constants();
11  
12 7 ▶use base 'HTML::Mason::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 our $VERSION = 0.1;
15  
16 =head1 NAME
17  
18 MasonX::Plugin::Compress - send compressed output if the client supports it
19  
20 =head1 SYNOPSIS
21  
22 PerlAddVar MasonPlugins MasonX::Plugin::Compress
23
24 # or in a handler.pl script
25 my $ah = HTML::Mason::ApacheHandler->new( plugins => [ MasonX::Plugin::Compress-> new ],
26 # ...
27 );
28  
29 =head1 DESCRIPTION
30  
31 Negotiates a preferred compression method (currently, gzip, bzip2 or deflate) with the client,
32 compresses the response and sets appropriate headers.
33  
34 =head2 Methods
35  
36 =over 4
37  
38 =item end_request_hook
39  
40 =back
41  
42 =cut
43  
44 7 my %AcceptMap = ( gzip => '_gzip',
45 'x-gzip' => '_gzip',
46 deflate => '_deflate',
47 'x-deflate' => '_deflate', # does this exist?
48 bzip2 => '_bzip2',
49 'x-bzip2' => '_bzip2', # does this exist?
50 );
51
52 sub end_request_hook
53 5 ▶{
Subroutine end_request_hook: called CC=7 SCAR=20.0
Pod documented
54 5 my ( $proto, $context ) = @_;
55
56 5 my $o = $context->output;
57 5 my $m = $context->request;
58
59 5 ▶ my $r = $m->apache_req || $m->cgi_request;
Condition33%1/3
$m->apache_req || $m->cgi_request
l!l&&r!l&&!r
500
MC/DC0%0/2
$m->apache_req || $m->cgi_request
$m->apache_req $m->cgi_request
Truth table33%1/3
A || B
A= $m->apache_reqB= $m->cgi_request
ABresult
1X1
011
000
60
61 5 ▶ return unless length $$o;
Branch: 1 branch
BranchTrueFalseTotal
unless length $$o 0 5 5
62
63 5 ▶ return if $r->content_encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $r->content_encoding 0 5 5
64
65 #return if $context->error; # there always seems to be one
66
67 # not sure from the docs if results->[0] contains the request return code, but
68 # it seems to - does this break CGI? Anyway, seems to be undef.
69 #return unless $context->result->[0] == Apache::Constants::OK();
70
71 # maybe worth accepting a few others, see e.g. http://www.pipeboost.com/contenttypes.asp
72 5 ▶ return unless $r->content_type =~ /^text/;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->content_type =~ /^text/ 1 4 5
73
74 # FireFox gives gzip, deflate
75 4 ▶ return unless my @accept = split /[\s,]/, $r->header_in( 'Accept-Encoding' );
Branch: 1 branch
BranchTrueFalseTotal
unless my @accept = split(/[\s,]/, $r->header_in("Accept-Encoding"), 0) 0 4 4
76
77 4 ▶ return unless my $encoding = List::Util::first { $AcceptMap{ $_ } } @accept;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless my $encoding = &List::Util::first(sub { $AcceptMap{$_}; } , @accept) 1 3 4
78
79 # Phew, we're really going to do this!
80
81 3 my $compress = $AcceptMap{ $encoding };
82
83 3 $proto->$compress( $context );
84 }
85  
86 sub _set_headers
87 3 ▶{
Subroutine _set_headers: called CC=2 SCAR=7.2
88 3 my ( $class, $context, $enc ) = @_;
89
90 3 my $m = $context->request;
91 3 ▶ my $r = $m->apache_req || $m->cgi_request;
Condition33%1/3
$m->apache_req || $m->cgi_request
l!l&&r!l&&!r
300
MC/DC0%0/2
$m->apache_req || $m->cgi_request
$m->apache_req $m->cgi_request
Truth table33%1/3
A || B
A= $m->apache_reqB= $m->cgi_request
ABresult
1X1
011
000
92
93 3 $r->content_encoding( $enc );
94
95 3 $r->header_out( Vary => 'Accept-Encoding' );
96
97 # I guess Mason sets this
98 #$r->content_length( length ${ $context->output } );
99 }
100  
101 sub _gzip
102 1 ▶{
Subroutine _gzip: called CC=1 SCAR=0.0
103 1 my ( $class, $context ) = @_;
104
105 1 my $o = $context->output;
106
107 1 $$o = Compress::Zlib::memGzip( $$o );
108
109 1 $class->_set_headers( $context, 'gzip' );
110 }
111  
112 sub _bzip2
113 1 ▶{
Subroutine _bzip2: called CC=1 SCAR=0.0
114 1 my ( $class, $context ) = @_;
115  
116 1 my $o = $context->output;
117
118 1 $$o = Compress::Bzip2::memBzip( $$o );
119
120 1 $class->_set_headers( $context, 'bzip2' );
121 }
122  
123 sub _deflate
124 1 ▶{
Subroutine _deflate: called CC=7 SCAR=20.4
125 1 my ( $class, $context ) = @_;
126
127 1 my $d = Compress::Zlib::deflateInit;
128
129 1 ▶ warn "Cannot create a deflation stream" unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 1 1
130
131 1 ▶ return unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 1 1
132
133 1 my $o = $context->output;
134
135 1 my ( $out1, $status ) = $d->deflate( $o );
136
137 1 ▶ warn "Deflation failed: $status" unless $status == Compress::Zlib::Z_OK();
Branch: 1 branch
BranchTrueFalseTotal
unless $status == Compress::Zlib::Z_OK() 0 1 1
138
139 1 ▶ return unless $status == Compress::Zlib::Z_OK();
Branch: 1 branch
BranchTrueFalseTotal
unless $status == Compress::Zlib::Z_OK() 0 1 1
140
141 1 ( my $out2, $status ) = $d->flush;
142  
143 1 ▶ warn "Deflation failed during flush: $status" unless $status == Compress::Zlib::Z_OK();
Branch: 1 branch
BranchTrueFalseTotal
unless $status == Compress::Zlib::Z_OK() 0 1 1
144
145 1 ▶ return unless $status == Compress::Zlib::Z_OK();
Branch: 1 branch
BranchTrueFalseTotal
unless $status == Compress::Zlib::Z_OK() 0 1 1
146
147 1 $$o = $out1 . $out2;
148
149 1 $class->_set_headers( $context, 'deflate' );
150 }
151  
152 =head1 TODO
153  
154 Investigate what other types to compress (currently, only compresses text/*).
155  
156 =head1 SEE ALSO
157  
158 C<Catalyst::Plugin::Compress::*>.
159  
160 L<Apache::Compress|Apache::Compress>.
161  
162 =head1 AUTHOR
163  
164 David Baird, C<< <cpan@riverside-cms.co.uk> >>
165  
166 =head1 BUGS
167  
168 Please report any bugs or feature requests to
169 C<bug-masonx-plugin-compress@rt.cpan.org>, or through the web interface at
170 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MasonX-Plugin-Compress>.
171 I will be notified, and then you'll automatically be notified of progress on
172 your bug as I make changes.
173  
174 =head1 COPYRIGHT & LICENSE
175  
176 Copyright 2005 David Baird, All Rights Reserved.
177  
178 This program is free software; you can redistribute it and/or modify it
179 under the same terms as Perl itself.
180  
181 =cut
182  
183 7 1; # End of MasonX::Plugin::Compress
↑ Summary