MicroserviceReq.pm

filter: Statementstmt 80.2% 183 / 228 Branchbran 71.0% 54 / 76 Conditioncond 58.3% 21 / 36 MC/DCmcdc 35.0% 7 / 20 Subroutinesub 78.2% 36 / 46 Podpod 80.0% 4 / 5 totaltotal 74.2% 305 / 411 CC 52 SCAR 21.0
CC
52
Cov
76%
_build_http_base_params
23.3
_encode_jsonp_payload
10.3
_build_base_url
6.0
CRAP 8.2
« Microservice.pm ↑ Summary Microservice/HelloWorld.pm »
1 package Async::MicroserviceReq;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.09';
8  
9 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use AnyEvent::IO qw(aio_load);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Plack::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use MooseX::Types::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use HTTP::Negotiate qw(choose);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 our $json = JSON::XS->new->utf8->pretty->canonical;
23 3 our @no_cache_headers =
24 ( 'Cache-Control' => 'private, max-age=0', 'Expires' => '-1' );
25 3 our $pending_req = 0;
26  
27 3 has 'method' => ( is => 'ro', isa => 'Str', required => 1 );
28 3 has 'headers' => ( is => 'ro', isa => 'Object', required => 1 );
29 3 has 'path' => ( is => 'ro', isa => 'Str', required => 1 );
30 3 has 'content' => ( is => 'ro', isa => 'Str', required => 1 );
31 3 has 'json_content' => (
32 is => 'ro',
33 isa => 'Ref',
34 required => 0,
35 lazy => 1,
36 builder => '_build_json_content'
37 );
38 3 has 'params' => ( is => 'ro', isa => 'Object', required => 1 );
39 3 has 'plack_respond' => (
40 is => 'rw',
41 isa => 'CodeRef',
42 required => 0,
43 clearer => 'clear_plack_respond'
44 );
45 3 has 'static_dir' =>
46 ( is => 'ro', isa => 'Path::Class::Dir', required => 1, coerce => 1 );
47  
48 3 has 'base_url' => (
49 is => 'ro',
50 isa => 'URI',
51 required => 1,
52 lazy => 1,
53 builder => '_build_base_url'
54 );
55 3 has '_http_base_params' => (
56 is => 'ro',
57 isa => 'HashRef',
58 required => 1,
59 lazy => 1,
60 builder => '_build_http_base_params'
61 );
62 has 'http_host' => (
63 is => 'ro',
64 isa => 'Maybe[Str]',
65 required => 1,
66 lazy => 1,
67 25 ▶ default => sub { return $_[0]->_http_base_params->{host}; },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 3 );
69 has 'http_port' => (
70 is => 'ro',
71 isa => 'Str',
72 required => 1,
73 lazy => 1,
74 23 ▶ default => sub { return $_[0]->_http_base_params->{port}; },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 3 );
76 has 'http_schema' => (
77 is => 'ro',
78 isa => 'Str',
79 required => 1,
80 lazy => 1,
81 23 ▶ default => sub { return $_[0]->_http_base_params->{schema}; },
Subroutine __ANON__: called CC=1 SCAR=0.0
82 3 );
83 3 has 'want_json' => (
84 is => 'ro',
85 isa => 'Bool',
86 required => 1,
87 lazy => 1,
88 builder => '_build_want_json'
89 );
90 3 has 'jsonp' => (
91 is => 'ro',
92 isa => 'Str',
93 required => 1,
94 );
95 3 has 'using_frontend_proxy' => (
96 is => 'ro',
97 isa => 'Bool',
98 required => 1,
99 );
100 3 has 'server_host' => (
101 is => 'ro',
102 isa => 'Maybe[Str]',
103 required => 0,
104 default => undef,
105 );
106 3 has 'server_port' => (
107 is => 'ro',
108 isa => 'Maybe[Int]',
109 required => 0,
110 default => undef,
111 );
112 3 has 'server_scheme' => (
113 is => 'ro',
114 isa => 'Str',
115 required => 0,
116 default => 'http',
117 );
118 3 has 'pending_ref' => (
119 is => 'ro',
120 isa => 'ScalarRef[Int]',
121 required => 1,
122 );
123 has 'request_start' => (
124 is => 'ro',
125 isa => 'Num',
126 required => 1,
127 36 ▶ default => sub { time() },
Subroutine __ANON__: called CC=1 SCAR=0.0
128 3 );
129 3 has 'request_timeout' => ( is => 'ro', isa => 'Num', required => 1 );
130 3 has '_warn_running_too_long' => (
131 is => 'ro',
132 lazy => 1,
133 builder => '_build_warn_running_too_long'
134 );
135  
136 36 ▶after 'BUILD' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
137 36 my ($self) = @_;
138 36 $self->_warn_running_too_long; # init timer
139 36 return;
140 3 };
141  
142 19 ▶sub _build_base_url {
Subroutine _build_base_url: called CC=6 SCAR=18.0
143 19 my ($self) = @_;
144  
145 19 ▶ unless ( $self->http_host ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->http_host) 2 17 19
146 2 $log->warn(
147 'no host information in headers; unable to build absolute base URL'
148 );
149 2 return URI->new('/');
150 }
151  
152 17 my $redirect_host_port;
153 17 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->http_port eq '80' and $self->http_schema eq 'http' or $self->http_port eq '443' and $self->http_schema eq 'https') { } 10 7 17
Condition77%7/9
$self->http_port eq 80 and $self->http_schema eq "http" or $self->http_port eq 443 and $self->http_schema eq "https"
l!l&&r!l&&!r
647
$self->http_port eq 80 and $self->http_schema eq "http"
!ll&&!rl&&r
1106
$self->http_port eq 443 and $self->http_schema eq "https"
!ll&&!rl&&r
704
MC/DC50%2/4
$self->http_port eq 80 and $self->http_schema eq "http" or $self->http_port eq 443 and $self->http_schema eq "https"
$self->http_port eq 80 $self->http_schema eq "http" $self->http_port eq 443 $self->http_schema eq "https"
Truth table42%3/7
A and B or C and D
A= $self->http_port eq 80B= $self->http_schema eq "http"C= $self->http_port eq 443D= $self->http_schema eq "https"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
154 ( ( $self->http_port eq '80' ) && ( $self->http_schema eq 'http' ) )
155 ||
156 ( ( $self->http_port eq '443' ) && ( $self->http_schema eq 'https' ) )
157 ) {
158 10 $redirect_host_port = $self->http_host;
159 }
160 else {
161 7 $redirect_host_port = $self->http_host . ':' . $self->http_port;
162 }
163  
164 17 return URI->new( $self->http_schema . '://' . $redirect_host_port . '/' );
165 }
166  
167 25 ▶sub _build_http_base_params {
Subroutine _build_http_base_params: called CC=23 SCAR=31.5
168 25 my ($self) = @_;
169  
170 25 ▶ my $url_scheme = $self->server_scheme || 'http';
Condition50%1/2
$self->server_scheme || 'http'
l!l
250
MC/DC0%0/1
$self->server_scheme || 'http'
$self->server_scheme
Truth table50%1/2
A
A= $self->server_scheme
Aresult
11
00
171 25 my $redirect_host;
172 25 my $redirect_port = 80;
173  
174 25 ▶ my $parse_host_port = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
175 25 my ($host_header_value) = @_;
176 25 ▶ return unless defined $host_header_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host_header_value 3 22 25
177 22 my ($host) = $host_header_value =~ /([^,\s]+)$/;
178 22 ▶ return unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 22 22
179 22 ▶ if ( $host =~ /^(.+):(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /^(.+):(\d+)$/) 8 14 22
180 8 return ( $1, $2 );
181 }
182 14 return ( $host, undef );
183 25 };
184  
185 25 ▶ if ( $self->using_frontend_proxy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->using_frontend_proxy) { } 18 7 25
186 18 my $https_on = '';
187 18 ▶ $https_on = $self->headers->header('HTTP_X_FORWARDED_HTTPS')
Branch: 1 branch
BranchTrueFalseTotal
if $self->headers->header("HTTP_X_FORWARDED_HTTPS") 5 13 18
188 if $self->headers->header('HTTP_X_FORWARDED_HTTPS');
189 18 ▶ $https_on = 'ON'
Branch: 1 branch
BranchTrueFalseTotal
if $self->headers->header("HTTP_X_FORWARDED_PROTO") and $self->headers->header("HTTP_X_FORWARDED_PROTO") eq "https" 1 17 18
Condition66%2/3
$self->headers->header("HTTP_X_FORWARDED_PROTO") and $self->headers->header("HTTP_X_FORWARDED_PROTO") eq "https"
!ll&&!rl&&r
1701
MC/DC50%1/2
$self->headers->header("HTTP_X_FORWARDED_PROTO") and $self->headers->header("HTTP_X_FORWARDED_PROTO") eq "https"
$self->headers->header("HTTP_X_FORWARDED_PROTO") $self->headers->header("HTTP_X_FORWARDED_PROTO") eq "https"
Truth table66%2/3
A and B
A= $self->headers->header("HTTP_X_FORWARDED_PROTO")B= $self->headers->header("HTTP_X_FORWARDED_PROTO") eq "https"
ABresult
0X0
100
111
190 if $self->headers->header('HTTP_X_FORWARDED_PROTO')
191 && $self->headers->header('HTTP_X_FORWARDED_PROTO') eq
192 'https'; # Pound
193 18 ▶ $url_scheme =
Branch: 1 branch
BranchTrueFalseTotal
$https_on && uc $https_on eq 'ON' ? : 6 12 18
Condition66%2/3
$https_on and uc $https_on eq "ON"
!ll&&!rl&&r
1206
MC/DC50%1/2
$https_on and uc $https_on eq "ON"
$https_on uc $https_on eq "ON"
Truth table66%2/3
A and B
A= $https_onB= uc $https_on eq "ON"
ABresult
0X0
100
111
194 ( $https_on && uc $https_on eq 'ON' ? 'https' : 'http' );
195 18 ▶ my $default_port = $url_scheme eq 'https' ? 443 : 80;
Branch: 1 branch
BranchTrueFalseTotal
$url_scheme eq 'https' ? : 6 12 18
196 18 $redirect_port = $default_port;
197  
198 18 ▶ if ( $self->headers->header('HTTP_X_FORWARDED_HOST') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->headers->header('HTTP_X_FORWARDED_HOST')) { } 15 3 18
199  
200 # in apache1 ServerName example.com:443
201 15 ▶ if ( $self->headers->header('HTTP_X_FORWARDED_SERVER') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->headers->header("HTTP_X_FORWARDED_SERVER")) 2 13 15
202 2 my ( $host, $port ) =
203 $parse_host_port->(
204 $self->headers->header('HTTP_X_FORWARDED_SERVER') );
205 2 ▶ if ($host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host) 2 0 2
206 2 $redirect_host = $host;
207 2 ▶ $redirect_port = $port if defined $port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port 1 1 2
208 }
209 }
210 15 my ( $host, $port ) =
211 $parse_host_port->(
212 $self->headers->header('HTTP_X_FORWARDED_HOST') );
213 15 ▶ if ($host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host) 15 0 15
214 15 $redirect_host = $host;
215 15 ▶ if ( defined $port ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $port) { } 4 11 15
elsif ($self->headers->header('HTTP_X_FORWARDED_PORT')) { } 4 7 11
216 4 $redirect_port = $port;
217 }
218 elsif ( $self->headers->header('HTTP_X_FORWARDED_PORT') ) {
219  
220 # in apache2 httpd.conf (RequestHeader set X-Forwarded-Port 8443)
221 4 $redirect_port =
222 $self->headers->header('HTTP_X_FORWARDED_PORT');
223 }
224 }
225 }
226 else {
227 3 my ( $host, $port ) = $parse_host_port->( $self->headers->header('Host') );
228 3 ▶ if ($host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host) 2 1 3
229 2 $log->warn(
230 'using front-end proxy but missing forwarded host headers; falling back to Host header'
231 );
232 2 $redirect_host = $host;
233 2 ▶ $redirect_port = $port if defined $port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port 1 1 2
234 }
235 }
236 }
237 else {
238 7 ▶ if ( defined $self->server_host && length $self->server_host ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->server_host and length $self->server_host) { } 2 5 7
Condition66%2/3
defined $self->server_host and length $self->server_host
!ll&&!rl&&r
502
MC/DC50%1/2
defined $self->server_host and length $self->server_host
defined $self->server_host length $self->server_host
Truth table66%2/3
A and B
A= defined $self->server_hostB= length $self->server_host
ABresult
0X0
100
111
239 2 $redirect_host = $self->server_host;
240 2 ▶ $redirect_port = $self->server_port if defined $self->server_port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->server_port 2 0 2
241 }
242 else {
243 5 my ( $host, $port ) =
244 $parse_host_port->( $self->headers->header('Host') );
245 5 ▶ if ($host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host) 3 2 5
246 3 $redirect_host = $host;
247 3 ▶ $redirect_port = $port if defined $port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port 2 1 3
248 }
249 }
250 }
251  
252 return {
253 25 host => $redirect_host,
254 port => '' . $redirect_port,
255 schema => $url_scheme,
256 };
257 }
258  
259 36 ▶sub _build_warn_running_too_long {
Subroutine _build_warn_running_too_long: called CC=1 SCAR=0.2
260 36 my ($o_self) = @_;
261  
262 36 weaken( my $self = $o_self );
263 return AnyEvent->timer(
264 'after' => $self->request_timeout,
265 'interval' => $self->request_timeout,
266 0 ▶ 'cb' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
267 0 $log->errorf(
268 'request %s %s running too long for %d seconds',
269 $self->method, $self->path, ( time - $self->request_start ),
270 );
271 },
272 36 );
273 }
274  
275 11 ▶sub _build_want_json {
Subroutine _build_want_json: called CC=4 SCAR=13.9
276 11 my ($self) = @_;
277  
278 11 my $accept = $self->headers->header('Accept');
279 11 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $accept and length $accept 4 7 11
Condition66%2/3
defined $accept and length $accept
!ll&&!rl&&r
407
MC/DC50%1/2
defined $accept and length $accept
defined $accept length $accept
Truth table66%2/3
A and B
A= defined $acceptB= length $accept
ABresult
0X0
100
111
280 unless defined($accept) && length($accept);
281  
282 7 my $chosen =
283 choose( [ [ 'json', 1.0, 'application/json' ] ], $self->headers, );
284 7 ▶ return defined($chosen) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $chosen ? : 3 4 7
285 }
286  
287 0 ▶sub _build_json_content {
Subroutine _build_json_content: not called CC=1 SCAR=6.9
288 0 my ($self) = @_;
289 0 return $json->decode( $self->content );
290 }
291  
292 36 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
293 36 ${ $_[0]->pending_ref }++;
294 36 return;
295 }
296  
297 36 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=1 SCAR=0.0
Pod undocumented
298 36 ${ $_[0]->pending_ref }--;
299 36 return;
300 }
301  
302 0 ▶sub text_plain {
Subroutine text_plain: not called CC=1 SCAR=6.9
Pod documented
303 0 my ( $self, @text ) = @_;
304 0 return $self->respond( 200, [], join( "\n", ( @text, q{} ) ) );
305 }
306  
307 3 ▶sub _should_wrap_payload_as_json {
Subroutine _should_wrap_payload_as_json: called CC=4 SCAR=17.9
308 3 my ( $self, $headers_as_hash, $payload ) = @_;
309 return ( $self->want_json
310 && !ref($payload)
311 3 ▶ && !$headers_as_hash->{'content-type'} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->want_json && !ref($payload) && !$headers_as_hash->{'content-type'} ? : 0 3 3
Condition33%2/6
$self->want_json and not ref $payload and not $headers_as_hash->{"content-type"}
!ll&&!rl&&r
300
$self->want_json and not ref $payload
!ll&&!rl&&r
300
MC/DC0%0/3
$self->want_json and not ref $payload and not $headers_as_hash->{"content-type"}
$self->want_json not ref $payload not $headers_as_hash->{"content-type"}
Truth table25%1/4
A and not(B) and not(C)
A= $self->want_jsonB= not ref $payloadC= not $headers_as_hash->{"content-type"}
ABCresult
0XX0
10X0
1100
1111
312 }
313  
314 3 ▶sub _wrap_payload {
Subroutine _wrap_payload: called CC=3 SCAR=17.3
315 3 my ( $self, $state ) = @_;
316  
317 return $state->{payload}
318 unless $self->_should_wrap_payload_as_json( $state->{headers_as_hash},
319 3 ▶ $state->{payload} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_should_wrap_payload_as_json($state->{"headers_as_hash"}, $state->{"payload"}) 3 0 3
320  
321 0 ▶ if ( $state->{status} < 400 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"status"} < 400) 0 0 0
322 0 return { 'data' => $state->{payload} };
323 }
324  
325 return {
326 'error' => {
327 err_status => $state->{status},
328 err_msg => $state->{payload},
329 }
330 0 };
331 }
332  
333 2 ▶sub _encode_jsonp_payload {
Subroutine _encode_jsonp_payload: called CC=4 SCAR=23.3
334 2 my ( $self, $state ) = @_;
335  
336 2 ▶ if ( my $jsonp = $self->jsonp ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $jsonp = $self->jsonp) 0 2 2
337 0 ▶ if ( my $js_func = $self->params->{$jsonp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $js_func = $self->params->{$jsonp}) 0 0 0
338 0 ▶ if ( $js_func !~ m/^[a-zA-Z_\$][0-9a-zA-Z_\$\.]*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $js_func =~ /^[a-zA-Z_\$][0-9a-zA-Z_\$\.]*$/) { } 0 0 0
339 0 $state->{status} = 405;
340 $state->{payload} = {
341 'error' => {
342 err_status => $state->{status},
343 0 err_msg => 'unsupported call-back function name',
344 }
345 };
346 }
347 else {
348 $state->{payload} = sprintf( '%s(%s);',
349 0 $js_func, $json->encode( $state->{payload} ) );
350 0 $state->{content_type} = 'application/javascript';
351 }
352 }
353 }
354  
355 2 return $state;
356 }
357  
358 1 ▶sub _set_serialization_failure {
Subroutine _set_serialization_failure: called CC=3 SCAR=12.7
359 1 my ( $self, $state, $err ) = @_;
360  
361 1 $state->{status} = 500;
362 $state->{payload} = eval {
363 $json->encode(
364 { 'error' => {
365 err_status => $state->{status},
366 1 err_msg => 'failed to serialize response: ' . $err,
367 }
368 }
369 );
370 1 ▶ } // eval {
Condition33%1/3
eval { do { $Async::MicroserviceReq::json->encode({"error", {"err_status", $state->{"status"}, "err_msg", 'failed to serialize response: ' . $err}}) } } // eval { do { $Async::MicroserviceReq::json->encode({"error", {"err_status", $state->{"status"}, "err_msg", "failed to serialize response and error message"}}) } }
l!l&&r!l&&!r
100
MC/DC0%0/2
eval { do { $Async::MicroserviceReq::json->encode({"error", {"err_status", $state->{"status"}, "err_msg", 'failed to serialize response: ' . $err}}) } } // eval { do { $Async::MicroserviceReq::json->encode({"error", {"err_status", $state->{"status"}, "err_msg", "failed to serialize response and error message"}}) } }
eval { do { $Async::MicroserviceReq::json->encode({"error", {"err_status", $state->{"status"}, "err_msg", 'failed to serialize response: ' . $err}}) } } eval { do { $Async::MicroserviceReq::json->encode({"error", {"err_status", $state->{"status"}, "err_msg", "failed to serialize response and error message"}}) } }
Truth table33%1/3
A // B
A= eval { do { $Async::MicroserviceReq::json->encode({"error", {"err_status", $state->{"status"}, "err_msg", 'failed to serialize response: ' . $err}}) } }B= eval { do { $Async::MicroserviceReq::json->encode({"error", {"err_status", $state->{"status"}, "err_msg", "failed to serialize response and error message"}}) } }
ABresult
1X1
011
000
371 $json->encode(
372 { 'error' => {
373 err_status => $state->{status},
374 0 err_msg =>
375 'failed to serialize response and error message',
376 }
377 }
378 );
379 };
380  
381 1 ▶ if ( $state->{payload} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{'payload'}) { } 1 0 1
382 1 $state->{content_type} = 'application/json';
383 }
384 else {
385 0 $state->{payload} = 'failed to serialize json';
386 0 delete $state->{content_type};
387 }
388  
389 1 return $state;
390 }
391  
392 3 ▶sub _serialize_payload {
Subroutine _serialize_payload: called CC=2 SCAR=6.9
393 3 my ( $self, $state ) = @_;
394  
395 return $state
396 3 ▶ unless ref( $state->{payload} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $state->{"payload"} 1 2 3
397  
398 2 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=7.0
399 2 $state = $self->_encode_jsonp_payload($state);
400  
401 2 ▶ if ( ref( $state->{payload} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $state->{"payload"}) 2 0 2
402 2 $state->{payload} = $json->encode( $state->{payload} );
403 1 $state->{content_type} = 'application/json';
404 }
405 }
406 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
407 1 $state = $self->_set_serialization_failure( $state, $_ );
408 2 };
409  
410 2 return $state;
411 }
412  
413 3 ▶sub _emit_response {
Subroutine _emit_response: called CC=3 SCAR=11.0
414 3 my ( $self, $state ) = @_;
415  
416 push(
417 3 ▶ @{ $state->{headers} },
Condition100%2/2
$state->{'content_type'} || 'text/plain'
l!l
21
MC/DC100%1/1
$state->{'content_type'} || 'text/plain'
$state->{'content_type'}
Truth table100%2/2
A
A= $state->{'content_type'}
Aresult
11
00
418 ( 'Content-Type' => ( $state->{content_type} || 'text/plain' ) )
419 3 ▶ ) unless ( $state->{headers_as_hash}->{'content-type'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"headers_as_hash"}{"content-type"} 3 0 3
420  
421 return $self->plack_respond->(
422 [ $state->{status},
423 3 [ @no_cache_headers, @{ $state->{headers} } ],
424 3 [ $state->{payload} ]
425 ]
426 );
427 }
428  
429 3 ▶sub respond {
Subroutine respond: called CC=2 SCAR=7.6
Pod documented
430 3 my ( $self, $status, $headers, $payload ) = @_;
431  
432 0 ▶ my %headers_as_hash = map { defined($_) ? lc($_) : $_ } @$headers;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
433 3 my $state = {
434 status => $status,
435 headers => $headers,
436 headers_as_hash => \%headers_as_hash,
437 payload => $payload,
438 };
439  
440 3 $state->{payload} = $self->_wrap_payload($state);
441 3 $state = $self->_serialize_payload($state);
442  
443 3 return $self->_emit_response($state);
444 }
445  
446 0 ▶sub redirect {
Subroutine redirect: not called CC=1 SCAR=6.9
Pod documented
447 0 my ( $self, $location_path ) = @_;
448 0 my $location = $self->base_url->clone;
449 0 $location->path($location_path);
450 0 return $self->respond(
451 302,
452 [ "Location" => $location ],
453 "redirect to " . $location
454 );
455 }
456  
457 0 ▶async sub static_ft {
Subroutine static_ft: not called CC=2 SCAR=17.9
Pod documented
458 0 my ( $self, $file_name, $content_cb ) = @_;
459 0 my $static_file = $self->static_dir->file($file_name)->stringify;
460 0 ▶ my $content_type = Plack::MIME->mime_type($static_file) || 'text/plain';
Condition0%0/2
'Plack::MIME'->mime_type($static_file) || 'text/plain'
l!l
00
MC/DC0%0/1
'Plack::MIME'->mime_type($static_file) || 'text/plain'
'Plack::MIME'->mime_type($static_file)
Truth table0%0/2
A
A= 'Plack::MIME'->mime_type($static_file)
Aresult
11
00
461 return await _fetch_file_ft($static_file)->then(
462 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
463 0 my ($content) = @_;
464 0 ▶ $content = $content_cb->($content) if $content_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $content_cb 0 0 0
465 0 return [ 200, [ 'Content-Type' => $content_type ], $content ];
466 }
467 )->catch(
468 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
469 0 return [ 404, [@no_cache_headers], 'no such static file' ];
470 }
471 0 );
472 3 }
473  
474 0 ▶sub _fetch_file_ft {
Subroutine _fetch_file_ft: not called CC=1 SCAR=6.9
475 0 my ($file) = @_;
476  
477 0 my $aio_load_f = Future->new;
478 0 $aio_load_f->retain;
479 aio_load(
480 $file,
481 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
482 0 my ($content) = @_;
483 0 $aio_load_f->done($content);
484 }
485 0 );
486  
487 0 my $fetch_file_ft = Future->new;
488 $aio_load_f->on_done(
489 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
490 0 my ($content) = @_;
491 0 ▶ unless ( defined($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 0 0
492 0 $fetch_file_ft->fail(
493 'failed to load content of file: ' . $file );
494 0 return;
495 }
496 0 $fetch_file_ft->done($content);
497 }
498 0 );
499  
500 0 return $fetch_file_ft;
501 }
502  
503 3 __PACKAGE__->meta->make_immutable;
504  
505 3 1;
506  
507 __END__
« Microservice.pm ↑ Summary Microservice/HelloWorld.pm »