blib/lib/Catalyst/Seal/Middleware.pm

filter: Statementstmt 60.6% 111 / 183 Branchbran 34.2% 26 / 76 Conditioncond 21.2% 7 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 74.0% 20 / 27 Podpod n/a totaltotal 48.5% 164 / 338 CC 56 SCAR 32.8
CC
56
Cov
49%
__ANON__
110.0
_fix_filter
67.2
__ANON__
20.0
CRAP 26.6
« blib/lib/Catalyst/Seal/Execute.pm ↑ Summary blib/lib/Catalyst/Seal/Exceptions.pm »
1 package Catalyst::Seal::Middleware;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use Plack::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use Plack::Middleware ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 ▶use Catalyst::Seal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use Catalyst::Seal::Guard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 19 our $VERSION = '0.04';
15  
16 0 ▶sub _content_length_call {
Subroutine _content_length_call: not called CC=1 SCAR=6.9
17 0 my $self = shift;
18 0 my $res = $self->app->(@_);
19  
20 0 ▶ return $self->response_cb($res, sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
21 0 my $r = shift;
22  
23 0 my $status = $r->[0];
24 0 ▶ return if $status < 200 || $status == 204 || $status == 304;
Branch: 1 branch
BranchTrueFalseTotal
if $status < 200 or $status == 204 or $status == 304 0 0 0
Condition0%0/6
$status < 200 or $status == 204 or $status == 304
l!l&&r!l&&!r
000
$status < 200 or $status == 204
l!l&&r!l&&!r
000
MC/DC0%0/3
$status < 200 or $status == 204 or $status == 304
$status < 200 $status == 204 $status == 304
Truth table0%0/4
A or B or C
A= $status < 200B= $status == 204C= $status == 304
ABCresult
1XX1
01X1
0011
0000
25  
26 0 my $h = $r->[1];
27 0 ▶ for (my $i = 0; $i < @$h; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$h) 0 0 0
28 0 my $k = lc $h->[$i];
29 0 ▶ return if $k eq 'content-length' || $k eq 'transfer-encoding';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "content-length" or $k eq "transfer-encoding" 0 0 0
Condition0%0/3
$k eq "content-length" or $k eq "transfer-encoding"
l!l&&r!l&&!r
000
MC/DC0%0/2
$k eq "content-length" or $k eq "transfer-encoding"
$k eq "content-length" $k eq "transfer-encoding"
Truth table0%0/3
A or B
A= $k eq "content-length"B= $k eq "transfer-encoding"
ABresult
1X1
011
000
30 }
31  
32 0 my $body = $r->[2];
33 0 my $len;
34 0 ▶ if (ref $body eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body eq 'ARRAY') { } 0 0 0
35 0 $len = 0;
36 0 $len += length $_ for @$body;
37 }
38 else {
39 0 $len = Plack::Util::content_length($body);
40 }
41  
42 0 ▶ push @$h, 'Content-Length' => $len if defined $len;
Branch: 1 branch
BranchTrueFalseTotal
if defined $len 0 0 0
43 0 return;
44 0 });
45 }
46  
47 367 ▶sub _http_exceptions_call {
Subroutine _http_exceptions_call: called CC=3 SCAR=12.5
48 367 my ($self, $env) = @_;
49  
50 367 my $res;
51 367 ▶ unless (eval { $res = $self->app->($env); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $res = $self->app->($env); 1 } }) 0 367 367
52 0 $res = $self->transform_error($@, $env);
53 }
54  
55 367 ▶ return $res if ref $res eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $res eq "ARRAY" 0 367 367
56  
57 367 my $delayed = $res;
58 367 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
59 367 my $respond = shift;
60  
61 367 my $writer;
62 367 ▶ unless (eval { $delayed->(sub { return $writer = $respond->(@_) }); 1 }) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { &$delayed(sub { return $writer = &$respond(@_); } ); 1 } }) 0 367 367
63 0 my $err = $@;
64 0 ▶ if ($writer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writer) { } 0 0 0
65 0 Carp::cluck $err;
66 0 $writer->close;
67 }
68 else {
69 0 $respond->($self->transform_error($err, $env));
70 }
71 }
72 367 return;
73 367 };
74 }
75  
76 367 ▶sub _rrb_filter {
Subroutine _rrb_filter: called CC=5 SCAR=21.6
77 367 my ($r) = @_;
78 367 ▶ return unless @$r == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @$r == 3 0 367 367
79 367 my $status = $r->[0];
80 367 ▶ return if !($status < 200 || $status == 204 || $status == 304);
Branch: 1 branch
BranchTrueFalseTotal
unless $status < 200 or $status == 204 or $status == 304 367 0 367
Condition33%2/6
$status < 200 or $status == 204 or $status == 304
l!l&&r!l&&!r
00367
$status < 200 or $status == 204
l!l&&r!l&&!r
00367
MC/DC0%0/3
$status < 200 or $status == 204 or $status == 304
$status < 200 $status == 204 $status == 304
Truth table25%1/4
A or B or C
A= $status < 200B= $status == 204C= $status == 304
ABCresult
1XX1
01X1
0011
0000
81 0 $r->[2] = [];
82 0 Plack::Util::header_remove($r->[1], 'Content-Length');
83 0 return;
84 }
85  
86 367 ▶sub _fix_filter {
Subroutine _fix_filter: called CC=9 SCAR=42.1
87 367 my ($r) = @_;
88 367 ▶ return unless $r->[0] >= 300 && $r->[0] < 400;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->[0] >= 300 and $r->[0] < 400 367 0 367
Condition66%2/3
$r->[0] >= 300 and $r->[0] < 400
!ll&&!rl&&r
1532140
MC/DC0%0/2
$r->[0] >= 300 and $r->[0] < 400
$r->[0] >= 300 $r->[0] < 400
Truth table66%2/3
A and B
A= $r->[0] >= 300B= $r->[0] < 400
ABresult
0X0
100
111
89  
90 0 my $headers = Plack::Util::headers($r->[1]);
91 0 ▶ return unless $headers->exists('Location');
Branch: 1 branch
BranchTrueFalseTotal
unless $headers->exists("Location") 0 0 0
92 0 my $location = $headers->get('Location');
93  
94 0 my $class = 'Plack::Middleware::FixMissingBodyInRedirect';
95 0 ▶ if (@$r == 3 && !$class->can('_is_body_set')->($r->[2])) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$r == 3 and not $class->can('_is_body_set')->($r->[2])) { } 0 0 0
elsif (@$r == 2 or builtin::blessed($r->[2])) { } 0 0 0
Condition0%0/3
@$r == 3 and not $class->can("_is_body_set")->($r->[2])
!ll&&!rl&&r
000
MC/DC0%0/2
@$r == 3 and not $class->can("_is_body_set")->($r->[2])
@$r == 3 not $class->can("_is_body_set")->($r->[2])
Truth table0%0/3
A and not(B)
A= @$r == 3B= not $class->can("_is_body_set")->($r->[2])
ABresult
0X0
100
111
96 0 my $body = $class->_default_html_body($location);
97 0 $r->[2] = [$body];
98 0 $headers->set('Content-Length' => Plack::Util::content_length([$body]));
99 0 $headers->set('Content-Type' => 'text/html; charset=utf-8');
100 0 return;
101 }
102 ▶ elsif (@$r == 2 || Scalar::Util::blessed($r->[2])) {
Condition0%0/3
@$r == 2 or builtin::blessed($r->[2])
l!l&&r!l&&!r
000
MC/DC0%0/2
@$r == 2 or builtin::blessed($r->[2])
@$r == 2 builtin::blessed($r->[2])
Truth table0%0/3
A or B
A= @$r == 2B= builtin::blessed($r->[2])
ABresult
1X1
011
000
103 0 ▶ $headers->set('Content-Type' => 'text/html; charset=utf-8')
Branch: 1 branch
BranchTrueFalseTotal
unless $headers->exists("Content-Type") 0 0 0
104 unless $headers->exists('Content-Type');
105 0 my $done;
106 0 ▶ return sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
107 0 my $chunk = shift;
108 0 ▶ return $chunk if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
109 0 ▶ if (!defined $chunk) { $done = 1; return $class->_default_html_body($location) }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $chunk) { } 0 0 0
elsif (length $chunk) { } 0 0 0
110 0 elsif (length $chunk) { $done = 1 }
111 0 return $chunk;
112 0 };
113 }
114 0 return;
115 }
116  
117 367 ▶sub _cl_filter {
Subroutine _cl_filter: called CC=10 SCAR=24.3
118 367 my ($r) = @_;
119 367 my $status = $r->[0];
120 367 ▶ return if $status < 200 || $status == 204 || $status == 304;
Branch: 1 branch
BranchTrueFalseTotal
if $status < 200 or $status == 204 or $status == 304 0 367 367
Condition33%2/6
$status < 200 or $status == 204 or $status == 304
l!l&&r!l&&!r
00367
$status < 200 or $status == 204
l!l&&r!l&&!r
00367
MC/DC0%0/3
$status < 200 or $status == 204 or $status == 304
$status < 200 $status == 204 $status == 304
Truth table25%1/4
A or B or C
A= $status < 200B= $status == 204C= $status == 304
ABCresult
1XX1
01X1
0011
0000
121  
122 367 my $h = $r->[1];
123 367 ▶ for (my $i = 0; $i < @$h; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$h) 368 367 735
124 368 my $k = lc $h->[$i];
125 368 ▶ return if $k eq 'content-length' || $k eq 'transfer-encoding';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "content-length" or $k eq "transfer-encoding" 0 368 368
Condition33%1/3
$k eq "content-length" or $k eq "transfer-encoding"
l!l&&r!l&&!r
00368
MC/DC0%0/2
$k eq "content-length" or $k eq "transfer-encoding"
$k eq "content-length" $k eq "transfer-encoding"
Truth table33%1/3
A or B
A= $k eq "content-length"B= $k eq "transfer-encoding"
ABresult
1X1
011
000
126 }
127  
128 367 my $body = $r->[2];
129 367 my $len;
130 367 ▶ if (ref $body eq 'ARRAY') { $len = 0; $len += length $_ for @$body }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body eq 'ARRAY') { } 366 1 367
131 1 else { $len = Plack::Util::content_length($body) }
132  
133 367 ▶ push @$h, 'Content-Length' => $len if defined $len;
Branch: 1 branch
BranchTrueFalseTotal
if defined $len 366 1 367
134 367 return;
135 }
136  
137 0 ▶sub _apply_filter {
Subroutine _apply_filter: not called CC=4 SCAR=30.0
138 0 my ($r, $filter) = @_;
139  
140 0 ▶ if (ref $r->[2] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r->[2] eq 'ARRAY') { } 0 0 0
141 0 for my $line (@{ $r->[2] }) { $line = $filter->($line) }
142 0 my $eof = $filter->(undef);
143 0 ▶ push @{ $r->[2] }, $eof if defined $eof;
Branch: 1 branch
BranchTrueFalseTotal
if defined $eof 0 0 0
144 }
145 else {
146 0 my $body = $r->[2];
147 0 ▶ my $getline = sub { $body->getline };
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 $r->[2] = Plack::Util::inline_object(
149 0 ▶ getline => sub { $filter->($getline->()) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 ▶ close => sub { $body->close },
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 0 );
152 }
153 0 return;
154 }
155  
156 367 ▶sub _fused_call {
Subroutine _fused_call: called CC=1 SCAR=0.7
157 367 my $self = shift;
158 367 my $res = $self->app->(@_);
159  
160 367 ▶ return $self->response_cb($res, sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
161 367 my $r = shift;
162  
163 367 _rrb_filter($r);
164  
165 367 my $filter = _fix_filter($r);
166 367 ▶ if (ref $filter eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filter eq "CODE") 0 367 367
167 0 Plack::Util::header_remove($r->[1], 'Content-Length');
168 0 ▶ if (defined $r->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->[2]) 0 0 0
169 0 _apply_filter($r, $filter);
170 0 $filter = undef;
171 }
172 }
173  
174 367 _cl_filter($r);
175  
176 367 return $filter;
177 367 });
178 }
179  
180 {
181 package Catalyst::Seal::Middleware::Response;
182 19 our @ISA = ('Plack::Middleware');
183 367 ▶ sub call { goto &Catalyst::Seal::Middleware::_fused_call }
Subroutine call: called CC=1 SCAR=0.0
184 }
185  
186 19 ▶sub _default_middleware {
Subroutine _default_middleware: called CC=4 SCAR=13.9
187 12 my $class = shift;
188  
189 12 my $stock = $Catalyst::Seal::Guard::ORIGINAL{'Catalyst::default_middleware'};
190 12 ▶ my @mw = $stock ? $stock->($class, @_) : ();
Branch: 1 branch
BranchTrueFalseTotal
$stock ? : 12 0 12
191  
192 12 my @want = qw(
193 Plack::Middleware::RemoveRedundantBody
194 Plack::Middleware::FixMissingBodyInRedirect
195 Plack::Middleware::ContentLength
196 );
197  
198 12 for my $i (0 .. $#mw - 2) {
199 36 ▶ next unless join('|', map { ref $mw[$i + $_] } 0 .. 2) eq join('|', @want);
Branch: 1 branch
BranchTrueFalseTotal
unless join("|", map({ref $mw[$i + $_];} 0..2)) eq join("|", @want) 24 12 36
200 12 splice @mw, $i, 3, Catalyst::Seal::Middleware::Response->new;
201 12 last;
202 }
203  
204 12 return @mw;
205 }
206  
207 36 ▶sub _looks_like {
Subroutine _looks_like: called CC=5 SCAR=16.5
208 36 my ($class, $method, @want) = @_;
209  
210 36 ▶ my $cv = $class->can($method) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $cv = $class->can($method) or return 0 36 0 36
211 36 my $text = eval {
212 36 require B::Deparse;
213 36 B::Deparse->new->coderef2text($cv);
214 };
215 36 ▶ return 0 unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 36 36
216  
217 36 for my $want (@want) {
218 132 ▶ return 0 unless index($text, $want) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($text, $want) != -1 0 132 132
219 }
220 36 return 1;
221 }
222  
223 19 my %PATCH = (
224 'Plack::Middleware::ContentLength' => {
225 code => \&_content_length_call,
226 want => ['response_cb', 'content_length', 'Content-Length'],
227 },
228 'Plack::Middleware::HTTPExceptions' => {
229 code => \&_http_exceptions_call,
230 want => ['transform_error', '&try(', '&catch(', 'respond'],
231 },
232 );
233  
234 19 my $PATCHED = 0;
235  
236 12 ▶Catalyst::Seal::register_step('middleware' => sub {
Subroutine __ANON__: called CC=7 SCAR=21.8
237 12 ▶ return if $PATCHED++;
Branch: 1 branch
BranchTrueFalseTotal
if $PATCHED++ 0 12 12
238  
239 12 my $done = 0;
240 12 for my $class (sort keys %PATCH) {
241 24 my $p = $PATCH{$class};
242  
243 24 ▶ unless (eval { Plack::Util::load_class($class); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { Plack::Util::load_class($class); 1 } }) 0 24 24
244 0 Catalyst::Seal::note("$class is not installed, not patched");
245 0 next;
246 }
247 24 ▶ unless (_looks_like($class, 'call', @{ $p->{want} })) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_looks_like($class, "call", @{$$p{"want"};})) 0 24 24
248 0 Catalyst::Seal::note("$class\::call is not the shape we replace, left alone");
249 0 next;
250 }
251  
252 24 $done += Catalyst::Seal::Guard::replace("${class}::call" => $p->{code});
253 }
254  
255 12 my $fused = 0;
256 12 ▶ if (_looks_like('Catalyst', 'default_middleware',
Branch: 1 branch
BranchTrueFalseTotal
if (_looks_like('Catalyst', 'default_middleware', 'RemoveRedundantBody', 'FixMissingBodyInRedirect', 'ContentLength', 'HTTPExceptions')) { } 12 0 12
257 'RemoveRedundantBody', 'FixMissingBodyInRedirect',
258 'ContentLength', 'HTTPExceptions')) {
259 12 $fused = Catalyst::Seal::Guard::replace(
260 'Catalyst::default_middleware' => \&_default_middleware);
261 }
262 else {
263 0 Catalyst::Seal::note(
264 'Catalyst::default_middleware is not the shape we replace, left alone');
265 }
266  
267 12 ▶ Catalyst::Seal::note("middleware: patched $done of " . scalar(keys %PATCH)
Branch: 1 branch
BranchTrueFalseTotal
if $Catalyst::Seal::DEBUG 0 12 12
268 . ", fused=$fused") if $Catalyst::Seal::DEBUG;
269 12 return;
270 19 });
271  
272 19 1;
273  
274 __END__
« blib/lib/Catalyst/Seal/Execute.pm ↑ Summary blib/lib/Catalyst/Seal/Exceptions.pm »