lib/Mojolicious/Plugin/PlackMiddleware.pm

filter: Statementstmt 97.5% 162 / 166 Branchbran 82.5% 33 / 40 Conditioncond 58.8% 10 / 17 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 93.3% 28 / 30 Podpod 100% 5 / 5 totaltotal 89.9% 242 / 269 CC 30 SCAR 11.5
CC
30
Cov
92%
psgi_env_to_mojo_req
6.2
_load_class
5.2
mojo_res_to_psgi_res
5.2
CRAP 3.2
↑ Summary
1 package Mojolicious::Plugin::PlackMiddleware;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Mojo::Message::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Mojo::Message::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 our $VERSION = '0.39';
9 6 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 ### ---
12 ### register
13 ### ---
14 17 ▶ sub register {
Subroutine register: called CC=4 SCAR=13.9
Pod documented
15 17 my ($self, $app, $mws) = @_;
16
17 24 ▶ my $plack_app = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
18 24 my $env = shift;
19 24 my $c = $env->{'mojo.c'};
20 24 my $tx = $c->tx;
21
22 24 $tx->req(psgi_env_to_mojo_req($env));
23
24 24 ▶ if ($env->{'mojo.routed'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{'mojo.routed'}) { } 2 22 24
25 2 my $stash = $c->stash;
26 2 for my $key (grep {$_ =~ qr{^mojo\.}} keys %{$stash}) {
27 14 delete $stash->{$key};
28 }
29 2 delete $stash->{'status'};
30 2 my $sever = $tx->res->headers->header('server');
31 2 $tx->res(Mojo::Message::Response->new);
32 2 $tx->res->headers->header('server', $sever);
33 2 $c->match(Mojolicious::Routes::Match->new->root($c->app->routes));
34 2 $env->{'mojo.inside_app'}->();
35 } else {
36 22 $env->{'mojo.inside_app'}->();
37 22 $env->{'mojo.routed'} = 1;
38 }
39
40 24 return mojo_res_to_psgi_res($tx->res);
41 17 };
42
43 17 my @mws = reverse @$mws;
44 17 ▶ while (scalar @mws) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @mws) 20 17 37
45 20 ▶ my $args = (ref $mws[0] eq 'HASH') ? shift @mws : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $mws[0] eq 'HASH' ? : 9 11 20
46 20 ▶ my $cond = (ref $mws[0] eq 'CODE') ? shift @mws : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $mws[0] eq 'CODE' ? : 2 18 20
47 20 my $e = _load_class(shift @mws, 'Plack::Middleware');
48 $plack_app = Mojolicious::Plugin::PlackMiddleware::_Cond->wrap(
49 $plack_app,
50 condition => $cond,
51 20 ▶ builder => sub {$e->wrap($_[0], %$args)},
Subroutine __ANON__: called CC=1 SCAR=0.0
52 20 );
53 }
54
55 23 ▶ $app->hook('around_dispatch' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
56 23 my ($next, $c) = @_;
57
58 23 ▶ return $next->() if ($c->tx->req->error);
Branch: 1 branch
BranchTrueFalseTotal
if $c->tx->req->error 0 23 23
59
60 23 my $plack_env = mojo_req_to_psgi_env($c->req);
61 23 $plack_env->{'mojo.c'} = $c;
62 23 $plack_env->{'mojo.inside_app'} = $next;
63 $plack_env->{'psgi.errors'} =
64 0 ▶ Mojolicious::Plugin::PlackMiddleware::_EH->new(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 $c->app->log->debug(shift);
66 23 });
67
68 23 $c->tx->res(psgi_res_to_mojo_res($plack_app->($plack_env)));
69 23 ▶ $c->rendered if (! $plack_env->{'mojo.routed'});
Branch: 1 branch
BranchTrueFalseTotal
if not $plack_env->{"mojo.routed"} 1 22 23
70 17 });
71 }
72
73 ### ---
74 ### chunk size
75 ### ---
76 6 ▶ use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 131072;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_CHUNK_SIZE'} || 131072
l!l
06
MC/DC0%0/1
$ENV{'MOJO_CHUNK_SIZE'} || 131072
$ENV{'MOJO_CHUNK_SIZE'}
Truth table50%1/2
A
A= $ENV{'MOJO_CHUNK_SIZE'}
Aresult
11
00
77
78 ### ---
79 ### convert psgi env to mojo req
80 ### ---
81 25 ▶ sub psgi_env_to_mojo_req {
Subroutine psgi_env_to_mojo_req: called CC=6 SCAR=18.2
Pod documented
82
83 25 my $env = shift;
84 25 my $req = Mojo::Message::Request->new->parse($env);
85
86 25 $req->reverse_proxy($env->{MOJO_REVERSE_PROXY});
87
88 # Request body
89 25 my $len = $env->{CONTENT_LENGTH};
90 25 ▶ while (!$req->is_finished) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->is_finished) 4 24 28
91 4 ▶ my $chunk = ($len && $len < CHUNK_SIZE) ? $len : CHUNK_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
$len && $len < 131072 ? : 4 0 4
Condition33%1/3
$len and $len < 131072
!ll&&!rl&&r
004
MC/DC0%0/2
$len and $len < 131072
$len $len < 131072
Truth table33%1/3
A and B
A= $lenB= $len < 131072
ABresult
0X0
100
111
92 4 my $read = $env->{'psgi.input'}->read(my $buffer, $chunk, 0);
93 4 ▶ last unless $read;
Branch: 1 branch
BranchTrueFalseTotal
unless $read 1 3 4
94 3 $req->parse($buffer);
95 3 $len -= $read;
96 3 ▶ last if $len <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len <= 0 0 3 3
97 }
98
99 25 return $req;
100 }
101
102 ### ---
103 ### convert mojo tx to psgi env
104 ### ---
105 24 ▶ sub mojo_req_to_psgi_env {
Subroutine mojo_req_to_psgi_env: called CC=3 SCAR=11.0
Pod documented
106
107 24 my $mojo_req = shift;
108 24 my $url = $mojo_req->url;
109 24 my $base = $url->base;
110 24 my $body =
111 Mojolicious::Plugin::PlackMiddleware::_PSGIInput->new($mojo_req->build_body);
112
113 24 my %headers_org = %{$mojo_req->headers->to_hash};
114 24 my %headers;
115 24 for my $key (keys %headers_org) {
116
117 79 my $value = $headers_org{$key};
118 79 $key =~ s{-}{_}g;
119 79 $key = uc $key;
120 79 ▶ $key = "HTTP_$key" if ($key !~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/u 72 7 79
121 79 $headers{$key} = $value;
122 }
123
124 return {
125 24 %ENV,
126 %headers,
127 'SERVER_PROTOCOL' => $base->protocol. '/'. $mojo_req->version,
128 'SERVER_NAME' => $base->host,
129 'SERVER_PORT' => $base->port,
130 'REQUEST_METHOD' => $mojo_req->method,
131 'SCRIPT_NAME' => '',
132 'PATH_INFO' => $url->path->to_string,
133 'REQUEST_URI' => $url->to_string,
134 'QUERY_STRING' => $url->query->to_string,
135 'psgi.url_scheme' => $base->scheme,
136 'psgi.version' => [1,1],
137 'psgi.errors' => *STDERR,
138 'psgi.input' => $body,
139 'psgi.multithread' => Plack::Util::FALSE,
140 'psgi.multiprocess' => Plack::Util::TRUE,
141 'psgi.run_once' => Plack::Util::FALSE,
142 'psgi.streaming' => Plack::Util::TRUE,
143 'psgi.nonblocking' => Plack::Util::FALSE,
144 };
145 }
146
147 ### ---
148 ### convert psgi res to mojo res
149 ### ---
150 24 ▶ sub psgi_res_to_mojo_res {
Subroutine psgi_res_to_mojo_res: called CC=2 SCAR=6.9
Pod documented
151 24 my $psgi_res = shift;
152 24 my $mojo_res = Mojo::Message::Response->new;
153 24 $mojo_res->code($psgi_res->[0]);
154 24 my $headers = $mojo_res->headers;
155 24 ▶ while (scalar @{$psgi_res->[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$psgi_res->[1];}) 58 24 82
156 58 $headers->add(shift @{$psgi_res->[1]} => shift @{$psgi_res->[1]});
157 }
158
159 24 $headers->remove('Content-Length'); # should be set by mojolicious later
160
161 24 my $asset = $mojo_res->content->asset;
162 24 ▶ Plack::Util::foreach($psgi_res->[2], sub {$asset->add_chunk($_[0])});
Subroutine __ANON__: called CC=1 SCAR=0.0
163 24 weaken($psgi_res);
164 24 return $mojo_res;
165 }
166
167 ### ---
168 ### convert mojo res to psgi res
169 ### ---
170 24 ▶ sub mojo_res_to_psgi_res {
Subroutine mojo_res_to_psgi_res: called CC=5 SCAR=16.4
Pod documented
171 24 my $mojo_res = shift;
172 24 my $status = $mojo_res->code;
173 24 my $headers = $mojo_res->content->headers;
174 24 my @headers;
175 24 push @headers, $_ => $headers->header($_) for (@{$headers->names});
176 24 my @body;
177 24 my $offset = 0;
178
179 # don't know why but this block makes long polling tests to pass
180 24 ▶ if ($mojo_res->content->is_dynamic && $mojo_res->content->{delay}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mojo_res->content->is_dynamic and $mojo_res->content->{"delay"}) 0 24 24
Condition33%1/3
$mojo_res->content->is_dynamic and $mojo_res->content->{"delay"}
!ll&&!rl&&r
2400
MC/DC0%0/2
$mojo_res->content->is_dynamic and $mojo_res->content->{"delay"}
$mojo_res->content->is_dynamic $mojo_res->content->{"delay"}
Truth table33%1/3
A and B
A= $mojo_res->content->is_dynamicB= $mojo_res->content->{"delay"}
ABresult
0X0
100
111
181 0 $mojo_res->get_body_chunk(0);
182 }
183
184 24 ▶ while (length(my $chunk = $mojo_res->get_body_chunk($offset))) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $chunk = $mojo_res->get_body_chunk($offset))) 24 24 48
185 24 push(@body, $chunk);
186 24 $offset += length $chunk;
187 }
188 24 return [$status, \@headers, \@body];
189 }
190
191 ### ---
192 ### load mw class
193 ### ---
194 20 ▶ sub _load_class {
Subroutine _load_class: called CC=5 SCAR=16.6
195 20 my($class, $prefix) = @_;
196
197 20 ▶ if ($prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) 20 0 20
198 20 ▶ unless ($class =~ s/^\+// || $class =~ /^$prefix/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class =~ s/^\+//u or $class =~ /^$prefix/u) 20 0 20
Condition33%1/3
$class =~ s/^\+//u or $class =~ /^$prefix/u
l!l&&r!l&&!r
0020
MC/DC0%0/2
$class =~ s/^\+//u or $class =~ /^$prefix/u
$class =~ s/^\+//u $class =~ /^$prefix/u
Truth table33%1/3
A or B
A= $class =~ s/^\+//uB= $class =~ /^$prefix/u
ABresult
1X1
011
000
199 20 $class = "$prefix\::$class";
200 }
201 }
202 20 ▶ if ($class->can('call')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("call")) 16 4 20
203 16 return $class;
204 }
205 4 my $file = $class;
206 4 $file =~ s!::!/!g;
207 4 require "$file.pm"; ## no critic
208
209 4 return $class;
210 }
211  
212  
213 ### ---
214 ### Error Handler
215 ### ---
216 package Mojolicious::Plugin::PlackMiddleware::_EH;
217 6 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
218
219 6 __PACKAGE__->attr('handler');
220
221 23 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
222 23 my ($class, $handler) = @_;
223 23 my $self = $class->SUPER::new;
224 23 $self->handler($handler);
225 }
226
227 0 ▶ sub print {
Subroutine print: not called CC=1 SCAR=6.9
228 0 my ($self, $error) = @_;
229 0 $self->handler->($error);
230 }
231  
232 ### ---
233 ### Port of Plack::Middleware::Conditional with mojolicious controller
234 ### ---
235 package Mojolicious::Plugin::PlackMiddleware::_Cond;
236 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
237 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
238 6 ▶use parent qw(Plack::Middleware::Conditional);
Subroutine BEGIN: called CC=1 SCAR=0.0
239
240 29 ▶ sub call {
Subroutine call: called CC=3 SCAR=11.0
241 29 my($self, $env) = @_;
242 29 my $cond = $self->condition;
243 29 ▶ if (! $cond || $cond->($env->{'mojo.c'}, $env)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cond or &$cond($env->{'mojo.c'}, $env)) { } 28 1 29
Condition100%3/3
not $cond or &$cond($env->{"mojo.c"}, $env)
l!l&&r!l&&!r
2711
MC/DC100%2/2
not $cond or &$cond($env->{"mojo.c"}, $env)
not $cond &$cond($env->{"mojo.c"}, $env)
Truth table100%3/3
not(A) or B
A= not $condB= &$cond($env->{"mojo.c"}, $env)
ABresult
1X1
011
000
244 28 return $self->middleware->($env);
245 } else {
246 1 return $self->app->($env);
247 }
248 }
249
250 ### ---
251 ### PSGI Input handler
252 ### ---
253 package Mojolicious::Plugin::PlackMiddleware::_PSGIInput;
254 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
255 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
256
257 27 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
258 27 my ($class, $content) = @_;
259 27 return bless [$content, 0, length($content)], $class;
260 }
261
262 13 ▶ sub read {
Subroutine read: called CC=4 SCAR=13.9
263 13 my $self = shift;
264 13 ▶ my $offset = ($_[2] || $self->[1]);
Condition100%3/3
$_[2] || $self->[1]
l!l&&r!l&&!r
166
MC/DC100%2/2
$_[2] || $self->[1]
$_[2] $self->[1]
Truth table100%3/3
A || B
A= $_[2]B= $self->[1]
ABresult
1X1
011
000
265 13 ▶ if ($offset <= $self->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset <= $self->[2]) 13 0 13
266 13 ▶ if ($_[0] = substr($self->[0], $offset, $_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] = substr($self->[0], $offset, $_[1])) 10 3 13
267 10 $self->[1] = $offset + length($_[0]);
268 10 return 1;
269 }
270 }
271 }
272  
273 6 1;
274  
275 __END__
↑ Summary