blib/lib/Catalyst/Plugin/Compress.pm

filter: Statementstmt 24.2% 16 / 66 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 36.3% 4 / 11 Podpod 33.3% 1 / 3 totaltotal 15.5% 21 / 135 CC 21 SCAR 38.9
CC
21
Cov
14%
finalize
110.0
setup
42.0
_deflate_compress
20.0
CRAP 49.1
↑ Summary
1 package Catalyst::Plugin::Compress;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.005';
8  
9 1 my $_method;
10 1 my %_compression_lib = (
11 gzip => 'Compress::Zlib',
12 deflate => 'Compress::Zlib',
13 bzip2 => 'Compress::Bzip2',
14 );
15  
16 sub _gzip_compress {
17 0 ▶ Compress::Zlib::memGzip(shift);
Subroutine _gzip_compress: not called CC=1 SCAR=6.9
18 }
19  
20 sub _bzip2_compress {
21 0 ▶ Compress::Bzip2::memBzip(shift);
Subroutine _bzip2_compress: not called CC=1 SCAR=6.9
22 }
23  
24 sub _deflate_compress {
25 0 ▶ my $content = shift;
Subroutine _deflate_compress: not called CC=4 SCAR=30.0
26 0 my $result;
27  
28 0 my ($d, $out, $status);
29 0 ($d, $status) = Compress::Zlib::deflateInit(
30 -WindowBits => -Compress::Zlib::MAX_WBITS(),
31 );
32 0 ▶ unless ($status == Compress::Zlib::Z_OK()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status == Compress::Zlib::Z_OK()) 0 0 0
33 0 die("Cannot create a deflation stream. Error: $status");
34 }
35  
36 0 ($out, $status) = $d->deflate($content);
37 0 ▶ unless ($status == Compress::Zlib::Z_OK()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status == Compress::Zlib::Z_OK()) 0 0 0
38 0 die("Deflation failed. Error: $status");
39 }
40 0 $result .= $out;
41  
42 0 ($out, $status) = $d->flush;
43 0 ▶ unless ($status == Compress::Zlib::Z_OK()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status == Compress::Zlib::Z_OK()) 0 0 0
44 0 die("Deflation failed. Error: $status");
45 }
46  
47 0 return $result . $out;
48 }
49  
50 sub setup {
51 0 ▶ my $c = shift;
Subroutine setup: not called CC=6 SCAR=37.4
Pod undocumented
52 0 ▶ if ($_method = $c->config->{compression_format}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_method = $c->config->{"compression_format"}) 0 0 0
53 0 ▶ $_method = 'gzip'
Branch: 1 branch
BranchTrueFalseTotal
if $_method eq "zlib" 0 0 0
54 if $_method eq 'zlib';
55  
56 0 my $lib_name = $_compression_lib{$_method};
57 0 ▶ die qq{No compression_format named "$_method"}
Branch: 1 branch
BranchTrueFalseTotal
unless $lib_name 0 0 0
58 unless $lib_name;
59 0 Catalyst::Utils::ensure_class_loaded($lib_name);
60  
61 0 *_do_compress = \&{"_${_method}_compress"};
62 }
63 0 ▶ if ($c->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->debug) 0 0 0
64 0 ▶ $_method
Branch: 1 branch
BranchTrueFalseTotal
$_method ? : 0 0 0
65 ? $c->log->debug(qq{Catalyst::Plugin::Compress sets compression_format to '$_method'})
66 : $c->log->debug(qq{Catalyst::Plugin::Compress has no compression_format config - disabled.});
67 }
68 0 $c->maybe::next::method(@_);
69 }
70  
71 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
72 sub should_compress_response {
73 0 ▶ my ($self) = @_;
Subroutine should_compress_response: not called CC=4 SCAR=30.0
Pod documented
74 0 my ($ct) = split /;/, $self->res->content_type;
75 0 my @compress_types = qw(
76 application/javascript
77 application/json
78 application/x-javascript
79 application/xml
80 );
81 return 1
82 if ($ct =~ m{^text/})
83 or ($ct =~ m{\+xml$}
84 0 ▶ or (first { lc($ct) eq $_ } @compress_types));
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $ct =~ m[^text/] or ($ct =~ /\+xml$/ or &first(sub { lc $ct eq $_; } , @compress_types)) 0 0 0
Condition0%0/6
$ct =~ m[^text/] or ($ct =~ /\+xml$/ or &first(sub {
l!l&&r!l&&!r
000
$ct =~ /\+xml$/ or &first(sub {
l!l&&r!l&&!r
000
MC/DC0%0/3
$ct =~ m[^text/] or ($ct =~ /\+xml$/ or &first(sub { lc $ct eq $_; } , @compress_types))
$ct =~ m[^text/] $ct =~ /\+xml$/ &first(sub { lc $ct eq $_; } , @compress_types)
Truth table0%0/4
A or B or C
A= $ct =~ m[^text/]B= $ct =~ /\+xml$/C= &first(sub { lc $ct eq $_; } , @compress_types)
ABCresult
1XX1
01X1
0011
0000
85 }
86  
87 sub finalize {
88 0 ▶ my $c = shift;
Subroutine finalize: not called CC=10 SCAR=47.0
Pod undocumented
89  
90 0 ▶ if ((not defined $_method)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_method or $c->res->content_encoding or not $c->res->body or $c->res->status != 200 or not $c->should_compress_response) 0 0 0
Condition0%0/12
not defined $_method or $c->res->content_encoding or not $c->res->body or $c->res->status != 200 or not $c->should_compress_response
l!l&&r!l&&!r
000
not defined $_method or $c->res->content_encoding or not $c->res->body or $c->res->status != 200
l!l&&r!l&&!r
000
not defined $_method or $c->res->content_encoding or not $c->res->body
l!l&&r!l&&!r
000
not defined $_method or $c->res->content_encoding
l!l&&r!l&&!r
000
MC/DC0%0/5
not defined $_method or $c->res->content_encoding or not $c->res->body or $c->res->status != 200 or not $c->should_compress_response
not defined $_method $c->res->content_encoding not $c->res->body $c->res->status != 200 not $c->should_compress_response
Truth table0%0/6
not(A) or B or not(C) or D or not(E)
A= not defined $_methodB= $c->res->content_encodingC= not $c->res->bodyD= $c->res->status != 200E= not $c->should_compress_response
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
91 or $c->res->content_encoding
92 or (not $c->res->body)
93 or ($c->res->status != 200)
94 or (not $c->should_compress_response)
95 ) {
96 0 return $c->maybe::next::method(@_);
97 }
98  
99 0 ▶ my $accept = $c->request->header('Accept-Encoding') || '';
Condition0%0/2
$c->request->header('Accept-Encoding') || ''
l!l
00
MC/DC0%0/1
$c->request->header('Accept-Encoding') || ''
$c->request->header('Accept-Encoding')
Truth table0%0/2
A
A= $c->request->header('Accept-Encoding')
Aresult
11
00
100  
101 0 ▶ unless (index($accept, $_method) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (index($accept, $_method) != -1) 0 0 0
102 0 return $c->maybe::next::method(@_);
103 }
104  
105 0 my $body = $c->res->body;
106 0 ▶ if (ref $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body) 0 0 0
107 0 eval { local $/; $body = <$body> };
108 0 ▶ die "Unknown type of ref in body."
Branch: 1 branch
BranchTrueFalseTotal
if ref $body 0 0 0
109 if ref $body;
110 }
111  
112 0 my $compressed = _do_compress($body);
113 0 $c->response->body($compressed);
114 0 $c->response->content_length(length($compressed));
115 0 $c->response->content_encoding($_method);
116 0 $c->response->headers->push_header('Vary', 'Accept-Encoding');
117  
118 0 $c->maybe::next::method(@_);
119 }
120  
121 1 1;
122  
123 __END__
124  
125 =head1 NAME
126  
127 Catalyst::Plugin::Compress - Compress response
128  
129 =head1 SYNOPSIS
130  
131 use Catalyst qw/Compress/;
132  
133 or
134  
135 use Catalyst qw/
136 Unicode
137 Compress
138 /;
139  
140 If you want to use this plugin with L<Catalyst::Plugin::Unicode>.
141  
142 Remember to specify compression_format with:
143  
144 __PACKAGE__->config(
145 compression_format => $format,
146 );
147  
148 $format can be either gzip bzip2 zlib or deflate. bzip2 is B<*only*> supported
149 by lynx and some other console text-browsers.
150  
151 =head1 DESCRIPTION
152  
153 This module combines L<Catalyst::Plugin::Deflate> L<Catalyst::Plugin::Gzip>
154 L<Catalyst::Plugin::Zlib> into one.
155  
156 It compress response to [gzip bzip2 zlib deflate] if client supports it.
157  
158 B<NOTE>: If you want to use this module with L<Catalyst::Plugin::Unicode>, You
159 B<MUST> load this plugin B<AFTER> L<Catalyst::Plugin::Unicode>.
160  
161 use Catalyst qw/
162 Unicode
163 Compress
164 /;
165  
166 If you don't, You'll get error which is like:
167  
168 [error] Caught exception in engine "Wide character in subroutine entry at
169 /usr/lib/perl5/site_perl/5.8.8/Compress/Zlib.pm line xxx."
170  
171 =head1 INTERNAL METHODS
172  
173 =head2 should_compress_response
174  
175 This method determine wether compressing the reponse using this plugin.
176  
177 =head1 SEE ALSO
178  
179 L<Catalyst>.
180  
181 =head1 AUTHOR
182  
183 Yiyi Hu C<yiyihu@gmail.com>
184  
185 =head1 LICENSE
186  
187 This library is free software. You can redistribute it and/or modify it under
188 the same terms as perl itself.
189  
190 =cut
191  
↑ Summary