Lambda/PSGI.pm

filter: Statementstmt 87.1% 210 / 241 Branchbran 71.7% 56 / 78 Conditioncond 63.6% 28 / 44 MC/DCmcdc 33.3% 8 / 24 Subroutinesub 82.8% 29 / 35 Podpod 0.0% 0 / 8 totaltotal 76.9% 331 / 430 CC 60 SCAR 21.8
CC
60
Cov
81%
_format_input_v1
20.1
app
12.0
call
12.0
CRAP 8.9
↑ Summary Lambda/Bootstrap.pm »
1 package AWS::Lambda::PSGI;
2 3 ▶use 5.026000;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use JSON::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Plack::Middleware::ReverseProxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use AWS::Lambda;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use JSON::XS qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶sub new {
Subroutine new: called CC=7 SCAR=23.7
Pod undocumented
19 2 my $proto = shift;
20 2 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
21
22 2 my $self;
23 2 ▶ if (@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 2 2
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
200
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
24 0 $self = bless {%{$_[0]}}, $class;
25 } else {
26 2 $self = bless {@_}, $class;
27 }
28  
29 2 ▶ if (!defined $self->{invoke_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"invoke_mode"}) 2 0 2
30 my $mode = $ENV{PERL5_LAMBDA_PSGI_INVOKE_MODE}
31 || $ENV{AWS_LWA_INVOKE_MODE} # for compatibility with https://github.com/awslabs/aws-lambda-web-adapter
32 2 ▶ || "BUFFERED";
Condition40%2/5
$ENV{'PERL5_LAMBDA_PSGI_INVOKE_MODE'} || $ENV{'AWS_LWA_INVOKE_MODE'} || 'BUFFERED'
l!l
02
$ENV{"PERL5_LAMBDA_PSGI_INVOKE_MODE"} or $ENV{"AWS_LWA_INVOKE_MODE"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$ENV{'PERL5_LAMBDA_PSGI_INVOKE_MODE'} || $ENV{'AWS_LWA_INVOKE_MODE'} || 'BUFFERED'
$ENV{"PERL5_LAMBDA_PSGI_INVOKE_MODE"} $ENV{"AWS_LWA_INVOKE_MODE"}
Truth table33%1/3
A or B
A= $ENV{"PERL5_LAMBDA_PSGI_INVOKE_MODE"}B= $ENV{"AWS_LWA_INVOKE_MODE"}
ABresult
1X1
011
000
33 2 $self->{invoke_mode} = uc $mode;
34 }
35
36 2 return $self;
37 }
38  
39 0 ▶sub prepare_app { return }
Subroutine prepare_app: not called CC=1 SCAR=6.9
Pod undocumented
40  
41 0 ▶sub app {
Subroutine app: not called CC=3 SCAR=24.8
Pod undocumented
42 0 ▶ return $_[0]->{app} if scalar(@_) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 0 0 0
43 0 ▶ return $_[0]->{app} = scalar(@_) == 2 ? $_[1] : [ @_[1..$#_ ]];
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 2 ? : 0 0 0
44 }
45  
46 0 ▶sub to_app {
Subroutine to_app: not called CC=1 SCAR=6.9
Pod undocumented
47 0 my $self = shift;
48 0 $self->prepare_app;
49 0 ▶ return sub { $self->call(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 }
51  
52 0 ▶sub wrap {
Subroutine wrap: not called CC=2 SCAR=17.9
Pod undocumented
53 0 my($self, $app, @args) = @_;
54  
55 # Lambda function runs as reverse proxy backend.
56 # So, we always enable ReverseProxy middleware.
57 0 $app = Plack::Middleware::ReverseProxy->wrap($app);
58  
59 0 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
60 0 $self->{app} = $app;
61 } else {
62 0 $self = $self->new({ app => $app, @args });
63 }
64 0 return $self->to_app;
65 }
66  
67 0 ▶sub call {
Subroutine call: not called CC=3 SCAR=24.8
Pod undocumented
68 0 my($self, $env, $ctx) = @_;
69  
70 # $ctx is added by #26
71 # fall back to $AWS::Lambda::context because of backward compatibility.
72 0 ▶ $ctx ||= $AWS::Lambda::context;
Condition0%0/3
$ctx ||= $AWS::Lambda::context
l!l&&r!l&&!r
000
MC/DC0%0/2
$ctx ||= $AWS::Lambda::context
$ctx $AWS::Lambda::context
Truth table0%0/3
A ||= B
A= $ctxB= $AWS::Lambda::context
ABresult
1X1
011
000
73  
74 0 ▶ if ($self->{invoke_mode} eq "RESPONSE_STREAM") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'invoke_mode'} eq 'RESPONSE_STREAM') { } 0 0 0
75 0 my $input = $self->_format_input_v2($env, $ctx);
76 0 $input->{'psgi.streaming'} = Plack::Util::TRUE;
77 0 my $res = $self->app->($input);
78 0 return $self->_handle_response_stream($res);
79 } else {
80 0 my $input = $self->format_input($env, $ctx);
81 0 my $res = $self->app->($input);
82 0 return $self->format_output($res);
83 }
84 }
85  
86 14 ▶sub format_input {
Subroutine format_input: called CC=6 SCAR=18.4
Pod undocumented
87 14 my ($self, $payload, $ctx) = @_;
88 14 ▶ if (my $context = $payload->{requestContext}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $context = $payload->{"requestContext"}) 14 0 14
89 14 ▶ if ($context->{elb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"elb"}) 3 11 14
90 # Application Load Balancer https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html
91 3 return $self->_format_input_v1($payload, $ctx);
92 }
93 }
94 11 ▶ if (my $version = $payload->{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $version = $payload->{"version"}) 6 5 11
95 6 ▶ if ($version =~ /^1[.]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /^1[.]/u) 0 6 6
96 # API Gateway for REST https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
97 0 return $self->_format_input_v1($payload, $ctx);
98 }
99 6 ▶ if ($version =~ /^2[.]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /^2[.]/u) 6 0 6
100 # API Gateway for HTTP https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
101 6 return $self->_format_input_v2($payload, $ctx);
102 }
103 }
104 5 return $self->_format_input_v1($payload, $ctx);
105 }
106  
107 8 ▶sub _format_input_v1 {
Subroutine _format_input_v1: called CC=20 SCAR=30.0
108 8 my ($self, $payload, $ctx) = @_;
109 8 my $env = {};
110  
111 # merge queryStringParameters and multiValueQueryStringParameters
112 my $query = {
113 8 ▶ %{$payload->{queryStringParameters} // {}},
Condition100%2/2
$payload->{"queryStringParameters"} // {}
l!l
53
MC/DC100%1/1
$payload->{"queryStringParameters"} // {}
$payload->{"queryStringParameters"}
Truth table100%2/2
A
A= $payload->{"queryStringParameters"}
Aresult
11
00
114 8 ▶ %{$payload->{multiValueQueryStringParameters} // {}},
Condition100%2/2
$payload->{"multiValueQueryStringParameters"} // {}
l!l
44
MC/DC100%1/1
$payload->{"multiValueQueryStringParameters"} // {}
$payload->{"multiValueQueryStringParameters"}
Truth table100%2/2
A
A= $payload->{"multiValueQueryStringParameters"}
Aresult
11
00
115 };
116 8 my @params;
117 8 ▶ while (my ($key, $value) = each %$query) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$query) 4 8 12
118 4 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 4 0 4
119 4 for my $v (@$value) {
120 7 push @params, "$key=$v";
121 }
122 } else {
123 0 push @params, "$key=$value";
124 }
125 }
126 8 $env->{QUERY_STRING} = join '&', @params;
127  
128 # merge headers and multiValueHeaders
129 my $headers = {
130 8 ▶ %{$payload->{headers} // {}},
Condition100%2/2
$payload->{"headers"} // {}
l!l
71
MC/DC100%1/1
$payload->{"headers"} // {}
$payload->{"headers"}
Truth table100%2/2
A
A= $payload->{"headers"}
Aresult
11
00
131 8 ▶ %{$payload->{multiValueHeaders} // {}},
Condition100%2/2
$payload->{"multiValueHeaders"} // {}
l!l
62
MC/DC100%1/1
$payload->{"multiValueHeaders"} // {}
$payload->{"multiValueHeaders"}
Truth table100%2/2
A
A= $payload->{"multiValueHeaders"}
Aresult
11
00
132 };
133 8 ▶ while (my ($key, $value) = each %$headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 66 8 74
134 66 $key =~ s/-/_/g;
135 66 $key = uc $key;
136 66 ▶ if ($key !~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/u) 60 6 66
137 60 $key = "HTTP_$key";
138 }
139 66 ▶ if (ref $value eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 48 18 66
140 48 $value = join ", ", @$value;
141 }
142 66 $env->{$key} = $value;
143 }
144  
145 8 $env->{'psgi.version'} = [1, 1];
146 8 $env->{'psgi.errors'} = *STDERR;
147 8 $env->{'psgi.run_once'} = Plack::Util::FALSE;
148 8 $env->{'psgi.multithread'} = Plack::Util::FALSE;
149 8 $env->{'psgi.multiprocess'} = Plack::Util::FALSE;
150 8 $env->{'psgi.streaming'} = Plack::Util::FALSE;
151 8 $env->{'psgi.nonblocking'} = Plack::Util::FALSE;
152 8 $env->{'psgix.harakiri'} = Plack::Util::TRUE;
153 8 $env->{'psgix.input.buffered'} = Plack::Util::TRUE;
154  
155 # inject the request id that compatible with Plack::Middleware::RequestId
156 8 ▶ if ($ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx) 1 7 8
157 1 $env->{'psgix.request_id'} = $ctx->aws_request_id;
158 1 $env->{'HTTP_X_REQUEST_ID'} = $ctx->aws_request_id;
159 }
160  
161 8 ▶ my $body = encode_utf8($payload->{body} // '');
Condition100%2/2
$payload->{"body"} // ""
l!l
53
MC/DC100%1/1
$payload->{"body"} // ""
$payload->{"body"}
Truth table100%2/2
A
A= $payload->{"body"}
Aresult
11
00
162 8 ▶ if ($payload->{isBase64Encoded}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($payload->{"isBase64Encoded"}) 3 5 8
163 3 $body = decode_base64 $body;
164 }
165 8 open my $input, "<", \$body;
166 8 $env->{REQUEST_METHOD} = $payload->{httpMethod};
167 8 $env->{'psgi.input'} = $input;
168 8 ▶ $env->{CONTENT_LENGTH} //= bytes::length($body);
Condition66%2/3
$env->{"CONTENT_LENGTH"} //= &bytes::length($body)
l!l&&r!l&&!r
260
MC/DC0%0/2
$env->{"CONTENT_LENGTH"} //= &bytes::length($body)
$env->{"CONTENT_LENGTH"} &bytes::length($body)
Truth table66%2/3
A //= B
A= $env->{"CONTENT_LENGTH"}B= &bytes::length($body)
ABresult
1X1
011
000
169 8 $env->{REQUEST_URI} = $payload->{path};
170 8 ▶ if ($env->{QUERY_STRING}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"QUERY_STRING"}) 4 4 8
171 4 $env->{REQUEST_URI} .= '?' . $env->{QUERY_STRING};
172 }
173 8 $env->{PATH_INFO} = URI::Escape::uri_unescape($payload->{path});
174  
175 8 $env->{SCRIPT_NAME} = '';
176 8 my $requestContext = $payload->{requestContext};
177 8 ▶ if ($requestContext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requestContext) 8 0 8
178 8 my $path = $requestContext->{path};
179 8 my $stage = $requestContext->{stage};
180 8 ▶ if ($stage && $path && $path ne $payload->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stage and $path and $path ne $payload->{"path"}) 5 3 8
Condition66%4/6
$stage and $path and $path ne $payload->{"path"}
!ll&&!rl&&r
305
$stage and $path
!ll&&!rl&&r
305
MC/DC33%1/3
$stage and $path and $path ne $payload->{"path"}
$stage $path $path ne $payload->{"path"}
Truth table50%2/4
A and B and C
A= $stageB= $pathC= $path ne $payload->{"path"}
ABCresult
0XX0
10X0
1100
1111
181 5 $env->{SCRIPT_NAME} = "/$stage";
182 }
183 }
184  
185 8 return $env;
186 }
187  
188 6 ▶sub _format_input_v2 {
Subroutine _format_input_v2: called CC=9 SCAR=22.0
189 6 my ($self, $payload, $ctx) = @_;
190 6 my $env = {};
191  
192 6 $env->{QUERY_STRING} = $payload->{rawQueryString};
193  
194 6 ▶ my $headers = $payload->{headers} // {};
Condition50%1/2
$payload->{"headers"} // {}
l!l
60
MC/DC0%0/1
$payload->{"headers"} // {}
$payload->{"headers"}
Truth table50%1/2
A
A= $payload->{"headers"}
Aresult
11
00
195 6 ▶ while (my ($key, $value) = each %$headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 53 6 59
196 53 $key =~ s/-/_/g;
197 53 $key = uc $key;
198 53 ▶ if ($key !~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/u) 46 7 53
199 46 $key = "HTTP_$key";
200 }
201 53 $env->{$key} = $value;
202 }
203  
204 6 $env->{'psgi.version'} = [1, 1];
205 6 $env->{'psgi.errors'} = *STDERR;
206 6 $env->{'psgi.run_once'} = Plack::Util::FALSE;
207 6 $env->{'psgi.multithread'} = Plack::Util::FALSE;
208 6 $env->{'psgi.multiprocess'} = Plack::Util::FALSE;
209 6 $env->{'psgi.streaming'} = Plack::Util::FALSE;
210 6 $env->{'psgi.nonblocking'} = Plack::Util::FALSE;
211 6 $env->{'psgix.harakiri'} = Plack::Util::TRUE;
212 6 $env->{'psgix.input.buffered'} = Plack::Util::TRUE;
213  
214 # inject the request id that compatible with Plack::Middleware::RequestId
215 6 ▶ if ($ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx) 0 6 6
216 0 $env->{'psgix.request_id'} = $ctx->aws_request_id;
217 0 $env->{'HTTP_X_REQUEST_ID'} = $ctx->aws_request_id;
218 }
219  
220 6 ▶ my $body = encode_utf8($payload->{body} // '');
Condition100%2/2
$payload->{"body"} // ""
l!l
42
MC/DC100%1/1
$payload->{"body"} // ""
$payload->{"body"}
Truth table100%2/2
A
A= $payload->{"body"}
Aresult
11
00
221 6 ▶ if ($payload->{isBase64Encoded}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($payload->{"isBase64Encoded"}) 2 4 6
222 2 $body = decode_base64 $body;
223 }
224 6 open my $input, "<", \$body;
225 6 $env->{'psgi.input'} = $input;
226 6 ▶ $env->{CONTENT_LENGTH} //= bytes::length($body);
Condition66%2/3
$env->{"CONTENT_LENGTH"} //= &bytes::length($body)
l!l&&r!l&&!r
330
MC/DC0%0/2
$env->{"CONTENT_LENGTH"} //= &bytes::length($body)
$env->{"CONTENT_LENGTH"} &bytes::length($body)
Truth table66%2/3
A //= B
A= $env->{"CONTENT_LENGTH"}B= &bytes::length($body)
ABresult
1X1
011
000
227 6 my $requestContext = $payload->{requestContext};
228 6 $env->{REQUEST_METHOD} = $requestContext->{http}{method};
229 6 $env->{REQUEST_URI} = $payload->{rawPath};
230 6 ▶ if ($env->{QUERY_STRING}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"QUERY_STRING"}) 2 4 6
231 2 $env->{REQUEST_URI} .= '?' . $env->{QUERY_STRING};
232 }
233 6 $env->{PATH_INFO} = $requestContext->{http}{path};
234 6 $env->{SCRIPT_NAME} = '';
235 6 return $env;
236 }
237  
238 4 ▶sub format_output {
Subroutine format_output: called CC=5 SCAR=16.1
Pod undocumented
239 4 my ($self, $response) = @_;
240 4 my ($status, $headers, $body) = @$response;
241  
242 4 my $singleValueHeaders = {};
243 4 my $multiValueHeaders = {};
244 6 ▶ Plack::Util::header_iter($headers, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
245 6 my ($k, $v) = @_;
246 6 $singleValueHeaders->{lc $k} = string $v;
247 6 ▶ push @{$multiValueHeaders->{lc $k} //= []}, string $v;
Condition100%2/2
$$multiValueHeaders{lc $k} //= []
l!l
15
MC/DC100%1/1
$$multiValueHeaders{lc $k} //= []
$$multiValueHeaders{lc $k}
Truth table100%2/2
A
A= $$multiValueHeaders{lc $k}
Aresult
11
00
248 4 });
249  
250 4 my $content = '';
251 4 ▶ if (reftype($body) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($body) eq 'ARRAY') { } 3 1 4
252 3 $content = join '', grep defined, @$body;
253 } else {
254 1 local $/ = \4096;
255 1 ▶ while (defined(my $buf = $body->getline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $buf = $body->getline)) 1 1 2
256 1 $content .= $buf;
257 }
258 1 $body->close;
259 }
260  
261 4 ▶ my $type = $singleValueHeaders->{'content-type'} // 'application/octet-stream';
Condition50%1/2
$singleValueHeaders->{"content-type"} // "application/octet-stream"
l!l
40
MC/DC0%0/1
$singleValueHeaders->{"content-type"} // "application/octet-stream"
$singleValueHeaders->{"content-type"}
Truth table50%1/2
A
A= $singleValueHeaders->{"content-type"}
Aresult
11
00
262 4 my $isBase64Encoded = $type !~ m(^text/.*|application/(:?json|javascript|xml))i;
263 4 ▶ if ($isBase64Encoded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isBase64Encoded) { } 1 3 4
264 1 $content = encode_base64 $content, '';
265 } else {
266 3 ▶ $content = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
267 # is valid utf-8 string? try to decode as utf-8.
268 3 decode_utf8($content, Encode::FB_CROAK | Encode::LEAVE_SRC);
269 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
270 # it looks not utf-8 encoding. fallback to base64 encoding.
271 1 $isBase64Encoded = 1;
272 1 encode_base64 $content, '';
273 3 };
274 }
275  
276 return +{
277 4 isBase64Encoded => bool $isBase64Encoded,
278 headers => $singleValueHeaders,
279 multiValueHeaders => $multiValueHeaders,
280 statusCode => number $status,
281 body => string $content,
282 }
283 }
284  
285 3 ▶sub _handle_response_stream {
Subroutine _handle_response_stream: called CC=2 SCAR=6.9
286 3 my ($self, $response) = @_;
287 3 ▶ if (reftype($response) ne "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($response) ne "CODE") 2 1 3
288 2 my $orig = $response;
289 2 ▶ $response = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
290 2 my $responder = shift;
291 2 $responder->($orig);
292 2 };
293 }
294  
295 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
296 3 my $lambda_responder = shift;
297 3 ▶ my $psgi_responder = sub {
Subroutine __ANON__: called CC=9 SCAR=22.2
298 3 my $response = shift;
299 3 my ($status, $headers, $body) = @$response;
300  
301 # write the prelude.
302 3 my $writer = $lambda_responder->("application/vnd.awslambda.http-integration-response");
303 3 my $prelude = encode_json($self->_format_response_stream($status, $headers));
304 3 $prelude .= "\x00\x00\x00\x00\x00\x00\x00\x00";
305 3 ▶ $writer->write($prelude) or die "failed to write prelude: $!";
Branch: 1 branch
BranchTrueFalseTotal
$writer->write($prelude) or die "failed to write prelude: $!" 3 0 3
306  
307 # write the body.
308 3 ▶ if (!defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 1 2 3
309 # the caller will write the body.
310 1 return $writer;
311 }
312 2 ▶ if (reftype($body) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($body) eq 'ARRAY') { } 1 1 2
313 # array-ref
314 1 for my $chunk (@$body) {
315 1 ▶ $writer->write($chunk) or die "failed to write chunk: $!";
Branch: 1 branch
BranchTrueFalseTotal
$writer->write($chunk) or die "failed to write chunk: $!" 1 0 1
316 }
317 } else {
318 # IO::Handle-like object
319 1 local $/ = \4096;
320 1 ▶ while (defined(my $chunk = $body->getline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $chunk = $body->getline)) 1 1 2
321 1 ▶ $writer->write($chunk) or die "failed to write chunk: $!";
Branch: 1 branch
BranchTrueFalseTotal
$writer->write($chunk) or die "failed to write chunk: $!" 1 0 1
322 }
323 }
324 2 ▶ $writer->close or die "failed to close writer: $!";
Branch: 1 branch
BranchTrueFalseTotal
$writer->close or die "failed to close writer: $!" 2 0 2
325 2 return;
326 3 };
327 3 $response->($psgi_responder);
328 3 };
329 }
330  
331 3 ▶sub _format_response_stream {
Subroutine _format_response_stream: called CC=1 SCAR=0.2
332 3 my ($self, $status, $headers) = @_;
333 3 my $headers_hash = {};
334 3 my $cookies = [];
335  
336 3 ▶ Plack::Util::header_iter($headers, sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
337 3 my ($k, $v) = @_;
338 3 $k = lc $k;
339 3 ▶ if ($k eq 'set-cookie') {
Branch: 2 branches
BranchTrueFalseTotal
if ($k eq 'set-cookie') { } 0 3 3
elsif (exists $headers_hash->{$k}) { } 0 3 3
340 0 push @$cookies, string $v;
341 } elsif (exists $headers_hash->{$k}) {
342 0 $headers_hash->{$k} = ", $v";
343 } else {
344 3 $headers_hash->{$k} = string $v;
345 }
346 3 });
347  
348 return +{
349 3 statusCode => number $status,
350 headers => $headers_hash,
351 cookies => $cookies,
352 };
353 }
354  
355 3 1;
356 __END__
↑ Summary Lambda/Bootstrap.pm »