blib/lib/CGI/WebGzip.pm

filter: Statementstmt 18.1% 20 / 110 Branchbran 8.9% 5 / 56 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 36.8% 7 / 19 Podpod 80.0% 8 / 10 totaltotal 18.2% 40 / 219 CC 36 SCAR 49.4
CC
36
Cov
14%
ob_gzhandler
380.0
flush
21.1
getAbility
19.0
CRAP 139.8
↑ Summary
1 package CGI::WebGzip;
2 1 our $VERSION = '0.13';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 # Compression level.
6 1 my $level = 9;
7 # Compression status (undef if compression is used).
8 1 my $status = undef;
9 # Callback.
10 1 my $callback = undef;
11
12
13 ##
14 ## Interface.
15 ##
16
17 # Import code.
18 # Usage:
19 # use CGI::WebGzip(level);
20 1 ▶sub import {
Subroutine import: called CC=2 SCAR=8.1
21 1 setLevel($_[1]);
22 1 ▶ if (!defined getAbility()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined getAbility()) 0 1 1
23 0 startCapture();
24 }
25 }
26
27 # Finish code.
28 1 ▶END {
Subroutine END: called CC=1 SCAR=0.0
29 1 flush();
30 }
31
32
33 # void flush()
34 # Flushes the compressed buffer immediately and releases STDOUT capture.
35 1 ▶sub flush {
Subroutine flush: called CC=5 SCAR=30.5
Pod documented
36 #CGI::WebOut::_Debug("Finished %s, %s, %s", __PACKAGE__, $capture->line_pointer, tied(*STDOUT));
37 1 ▶ my $data = stopCapture(); return if !defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 0 1
38 0 my ($headers, $body) = split /\r?\n\r?\n/, $data, 2;
39
40 # Run compression.
41 0 my ($newBody, $newHeaders, $stat);
42 0 ▶ if (length($body) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body == 0) { } 0 0 0
43 0 ($newBody, $newHeaders) = ($body, $headers);
44 } else {
45 0 ($newBody, $newHeaders, $stat) = ob_gzhandler($body, $headers);
46 0 $status = $stat;
47 }
48
49 # Run callback if defined. Callback may set additional cookies
50 # printing Set-Cookie header. If callback returns 0, no data
51 # is output by this function (presume callback did it itself).
52 0 ▶ if ($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) 0 0 0
53 0 ▶ $callback->($newBody, $newHeaders, $body) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$callback($newBody, $newHeaders, $body) or return 0 0 0
54 }
55 0 binmode(STDOUT);
56 0 print $newHeaders;
57 0 print "\r\n\r\n";
58 0 print $newBody;
59 }
60
61
62 # bool getAbility()
63 # Returns non-false is we are in CGI mode and browser understands compression
64 # Also loads Compress::Zlib and silently returns false if not found.
65 # Returns undef if compression ca be used.
66 1 ▶sub getAbility {
Subroutine getAbility: called CC=5 SCAR=29.4
Pod documented
67 1 ▶ if (!$ENV{SCRIPT_NAME}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"SCRIPT_NAME"}) 1 0 1
68 1 return "no: not a CGI script";
69 }
70 0 ▶ my $acc = $ENV{HTTP_ACCEPT_ENCODING}||"";
Condition0%0/2
$ENV{'HTTP_ACCEPT_ENCODING'} || ''
l!l
00
MC/DC0%0/1
$ENV{'HTTP_ACCEPT_ENCODING'} || ''
$ENV{'HTTP_ACCEPT_ENCODING'}
Truth table0%0/2
A
A= $ENV{'HTTP_ACCEPT_ENCODING'}
Aresult
11
00
71 0 ▶ if ($acc !~ /\bgzip\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($acc =~ /\bgzip\b/i) 0 0 0
72 0 return "no: incompatible browser";
73 }
74 0 ▶ if (!eval { require Compress::Zlib }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Compress::Zlib } }) 0 0 0
75 0 return "no: Compress::Zlib not found";
76 }
77 0 return undef;
78 }
79
80
81 # bool isCompressibleType($type)
82 # Returns true if MIME type $type is compressible.
83 0 ▶sub isCompressibleType {
Subroutine isCompressibleType: not called CC=1 SCAR=6.9
Pod documented
84 0 my ($type) = @_;
85 0 return $type =~ m{^text/}i;
86 }
87
88
89 # CODE setCallback(CODE $func)
90 # Sets the new callback function. Returns previous.
91 0 ▶sub setCallback {
Subroutine setCallback: not called CC=1 SCAR=6.9
Pod documented
92 0 my $prev = $callback;
93 0 $callback = $_[0];
94 0 return $prev;
95 }
96
97
98 # int setLevel($level)
99 # Sets compression level. Returns previous.
100 1 ▶sub setLevel {
Subroutine setLevel: called CC=2 SCAR=7.1
Pod documented
101 1 my $prev = $level;
102 1 ▶ $level = defined $_[0]? $_[0] : 9;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 1 1
103 1 return $prev;
104 }
105
106
107 # string getStatus()
108 # Returns status string. If compression is failed, status string is
109 # non-empty and contains diagnostic message. Otherwise it is undef.
110 0 ▶sub getStatus {
Subroutine getStatus: not called CC=1 SCAR=6.9
Pod documented
111 0 return $status;
112 }
113
114
115
116 ##
117 ## Compression abstraction level.
118 ##
119
120 # ($compressedBody, $modifiedHeaders, $status) ob_gzhandler(string $body, [string $headers])
121 # Returns compressed data (additionally analysing headers, if present).
122 # In scalar context returns $compressedBody only.
123 # Input headers can be modified, thus this function returns $modifiedHeaders.
124 # Compression error message is returned in $status (or undef if everything is OK).
125 # This function can be used exactly as PHP's ob_gzhandler().
126 0 ▶sub ob_gzhandler {
Subroutine ob_gzhandler: not called CC=19 SCAR=59.4
Pod documented
127 0 my ($body, $h) = @_;
128 0 ▶ $h ||= "";
Condition0%0/2
$h ||= ""
l!l
00
MC/DC0%0/1
$h ||= ""
$h
Truth table0%0/2
A
A= $h
Aresult
11
00
129 0 my $status = undef;
130
131 # Process all the headers.
132 0 my $ContentEncoding = undef;
133 0 my $ContentType = undef;
134 0 my $Status = undef;
135 0 my @headers = ();
136 0 foreach (split /\r?\n/, $h) {
137 0 ▶ if (/^Content[-_]Encoding:\s*(.*)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^Content[-_]Encoding:\s*(.*)/i) 0 0 0
138 0 $ContentEncoding = $1;
139 0 next;
140 }
141 0 ▶ if (/^Content[-_]Type:\s*(.*)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^Content[-_]Type:\s*(.*)/i) 0 0 0
142 0 $ContentType = $1;
143 }
144 0 ▶ if (/^Status:\s*(\d+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^Status:\s*(\d+)/i) 0 0 0
145 0 $Status = $1;
146 }
147 0 ▶ push @headers, $_ if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 0 0
148 }
149
150 # Determine if we need to compress.
151 0 my $needCompress = 1;
152 0 ▶ if (defined $ContentType && !isCompressibleType($ContentType)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ContentType and not isCompressibleType($ContentType)) 0 0 0
Condition0%0/3
defined $ContentType and not isCompressibleType($ContentType)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ContentType and not isCompressibleType($ContentType)
defined $ContentType not isCompressibleType($ContentType)
Truth table0%0/3
A and not(B)
A= defined $ContentTypeB= not isCompressibleType($ContentType)
ABresult
0X0
100
111
153 0 ▶ $ContentType ||= "undef";
Condition0%0/2
$ContentType ||= "undef"
l!l
00
MC/DC0%0/1
$ContentType ||= "undef"
$ContentType
Truth table0%0/2
A
A= $ContentType
Aresult
11
00
154 0 $status = "no: incompatible Content-type ($ContentType)";
155 0 $needCompress = undef;
156 }
157 0 ▶ if ($Status && $Status ne 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Status and $Status ne 200) 0 0 0
Condition0%0/3
$Status and $Status ne 200
!ll&&!rl&&r
000
MC/DC0%0/2
$Status and $Status ne 200
$Status $Status ne 200
Truth table0%0/3
A and B
A= $StatusB= $Status ne 200
ABresult
0X0
100
111
158 0 $status = "no: Status must be 200 (given $Status)";
159 0 $needCompress = undef;
160 }
161 0 ▶ if (defined($status=getAbility())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($status = getAbility())) 0 0 0
162 0 $needCompress = undef;
163 }
164
165 # Echo compression header.
166 0 ▶ if ($needCompress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needCompress) 0 0 0
167 0 ▶ $ContentEncoding = "gzip" . ($ContentEncoding? ", $ContentEncoding" : "")
Branch: 2 branches
BranchTrueFalseTotal
unless $ContentEncoding and $ContentEncoding =~ /\bgzip\b/i 0 0 0
$ContentEncoding ? : 0 0 0
Condition0%0/3
$ContentEncoding and $ContentEncoding =~ /\bgzip\b/i
!ll&&!rl&&r
000
MC/DC0%0/2
$ContentEncoding and $ContentEncoding =~ /\bgzip\b/i
$ContentEncoding $ContentEncoding =~ /\bgzip\b/i
Truth table0%0/3
A and B
A= $ContentEncodingB= $ContentEncoding =~ /\bgzip\b/i
ABresult
0X0
100
111
168 if !$ContentEncoding || $ContentEncoding !~ /\bgzip\b/i;
169 0 push @headers, "Content-Encoding: $ContentEncoding";
170 0 push @headers, "Vary: Accept-Encoding";
171 }
172
173 # Compress output.
174 0 my $headers = join "\r\n", @headers;
175 0 ▶ my $out = $needCompress? deflate_gzip($body, $level) : $body;
Branch: 1 branch
BranchTrueFalseTotal
$needCompress ? : 0 0 0
176
177 0 ▶ return wantarray? ($out, $headers, $status) : $out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
178 }
179
180
181 # string deflate_gzip($text, $level);
182 # Compresses the input string and returns result.
183 0 ▶sub deflate_gzip {
Subroutine deflate_gzip: not called CC=2 SCAR=17.9
Pod documented
184 0 ▶ my ($d, $st) = Compress::Zlib::deflateInit(-Level => defined $_[1]? $_[1] : 9);
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
185 0 my ($out, $Status) = $d->deflate($_[0]);
186 0 my ($outF, $StatusF) = $d->flush();
187 0 $out = $out.$outF;
188
189 # Shamanian code - without them nothing works! Hmmm...
190 0 my $pre = pack('CCCCCCCC', 0x1f,0x8b,0x08,0x00,0x00,0x00,0x00,0x00);
191 0 $out = $pre . substr($out, 0, -4) . pack('V', Compress::Zlib::crc32($_[0])) . pack('V', length($_[0]));
192
193 0 return $out;
194 }
195
196
197
198 ##
199 ## STDOUT capture abstraction level.
200 ##
201
202 # Capture object.
203 1 my $capture = undef;
204
205 # Starts STDOUT capturing.
206 0 ▶sub startCapture {
Subroutine startCapture: not called CC=2 SCAR=17.9
Pod undocumented
207 # Tie STDOUT only once.
208 0 ▶ return if $capture;
Branch: 1 branch
BranchTrueFalseTotal
if $capture 0 0 0
209 0 $capture = tie *STDOUT, "CGI::WebGzip::Tie";
210 }
211
212 # Finishes STDOUT capturing.
213 1 ▶sub stopCapture {
Subroutine stopCapture: called CC=2 SCAR=13.0
Pod undocumented
214 1 ▶ return undef if !$capture;
Branch: 1 branch
BranchTrueFalseTotal
unless $capture 1 0 1
215 0 my $obj = tied *STDOUT;
216 0 my $data = join "", @$obj;
217 0 $obj = $capture = undef;
218 0 untie(*STDOUT);
219 0 return $data;
220 }
221
222 # Package to tie STOUT. Captures all the output.
223 package CGI::WebGzip::Tie;
224 0 ▶sub TIEHANDLE { return bless [], $_[0] }
Subroutine TIEHANDLE: not called CC=1 SCAR=6.9
225 0 ▶sub PRINT {
Subroutine PRINT: not called CC=3 SCAR=24.8
226 0 my $th = shift;
227 push @$th, map {
228 0 ▶ if (!defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_) { } 0 0 0
229 0 ▶ eval { require Carp } and Carp::carp("Use of uninitialized value in print");
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Carp } } and Carp::carp("Use of uninitialized value in print") 0 0 0
230 0 ""
231 } else {
232 0 $_
233 }
234 } @_;
235 }
236 0 ▶sub PRINTF {
Subroutine PRINTF: not called CC=3 SCAR=24.8
237 0 my $th = shift;
238 push @$th, sprintf map {
239 0 ▶ if (!defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_) { } 0 0 0
240 0 ▶ eval { require Carp } and Carp::carp("Use of uninitialized value in printf");
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Carp } } and Carp::carp("Use of uninitialized value in printf") 0 0 0
241 0 ""
242 } else {
243 0 $_
244 }
245 } @_;
246 }
247 0 ▶sub WRITE { goto &PRINT; }
Subroutine WRITE: not called CC=1 SCAR=6.9
248 0 ▶sub CLOSE { CGI::WebGzip::flush() }
Subroutine CLOSE: not called CC=1 SCAR=6.9
249 0 ▶sub BINMODE { }
Subroutine BINMODE: not called CC=1 SCAR=0.0
250
251 1 return 1;
252 __END__
↑ Summary