Lambda/PSGI.pm

filter: Statementstmt 97.3% 293 / 301 Branchbran 83.0% 108 / 130 Conditioncond 67.1% 45 / 67 MC/DCmcdc 40.5% 15 / 37 Subroutinesub 100% 39 / 39 Podpod 0.0% 0 / 8 totaltotal 85.9% 500 / 582 CC 98 SCAR 26.0
CC
98
Cov
90%
_format_input_v1
36.0
_format_input_v2
11.1
_is_allowed_host
10.0
CRAP 13.4
↑ 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 AWS::Lambda;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use JSON::XS qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 13 ▶sub new {
Subroutine new: called CC=8 SCAR=21.4
Pod undocumented
18 13 my $proto = shift;
19 13 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0130
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
20
21 13 my $self;
22 13 ▶ if (@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 11 2 13
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
2011
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
23 11 $self = bless {%{$_[0]}}, $class;
24 } else {
25 2 $self = bless {@_}, $class;
26 }
27  
28 13 ▶ if (!defined $self->{invoke_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"invoke_mode"}) 2 11 13
29 my $mode = $ENV{PERL5_LAMBDA_PSGI_INVOKE_MODE}
30 || $ENV{AWS_LWA_INVOKE_MODE} # for compatibility with https://github.com/awslabs/aws-lambda-web-adapter
31 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
32 2 $self->{invoke_mode} = uc $mode;
33 }
34  
35 13 ▶ if (!defined $self->{allowed_hosts}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"allowed_hosts"}) 9 4 13
36 9 $self->{allowed_hosts} = $ENV{PERL5_LAMBDA_PSGI_ALLOWED_HOSTS};
37 }
38 13 $self->{allowed_hosts} = _parse_allowed_hosts($self->{allowed_hosts});
39
40 13 return $self;
41 }
42  
43 # returns an array ref of lower-cased host names, or undef if any host is allowed.
44 # $hosts is an array ref or a comma-separated string.
45 13 ▶sub _parse_allowed_hosts {
Subroutine _parse_allowed_hosts: called CC=5 SCAR=16.1
46 13 my $hosts = shift;
47 13 ▶ return undef unless defined $hosts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hosts 7 6 13
48 6 ▶ my @hosts = ref $hosts eq 'ARRAY' ? @$hosts : split /,/, $hosts;
Branch: 1 branch
BranchTrueFalseTotal
ref $hosts eq 'ARRAY' ? : 3 3 6
49 6 ▶ @hosts = grep { $_ ne '' } map { my $h = lc($_ // ''); $h =~ s/^\s+|\s+$//g; $h =~ s/\.$//; $h } @hosts;
Condition50%1/2
$_ // ""
l!l
70
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
50 6 ▶ return @hosts ? \@hosts : undef;
Branch: 1 branch
BranchTrueFalseTotal
@hosts ? : 5 1 6
51 }
52  
53 # returns true if the Host header of $env is one of the allowed hosts.
54 45 ▶sub _is_allowed_host {
Subroutine _is_allowed_host: called CC=10 SCAR=23.0
55 45 my ($self, $env) = @_;
56 45 ▶ my $allowed = $self->{allowed_hosts} or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $allowed = $self->{"allowed_hosts"} or return 1 31 14 45
57 31 ▶ my $host = $env->{HTTP_HOST} // '';
Condition100%2/2
$env->{"HTTP_HOST"} // ""
l!l
301
MC/DC100%1/1
$env->{"HTTP_HOST"} // ""
$env->{"HTTP_HOST"}
Truth table100%2/2
A
A= $env->{"HTTP_HOST"}
Aresult
11
00
58  
59 # strip the port. an IPv6 address is in brackets, e.g. "[2001:db8::1]:8080".
60 31 ▶ if ($host =~ /^(\[[0-9A-Fa-f:.]+\])(?::[0-9]+)?$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($host =~ /^(\[[0-9A-Fa-f:.]+\])(?::[0-9]+)?$/u) { } 2 29 31
elsif ($host =~ /^([A-Za-z0-9.-]+)(?::[0-9]+)?$/u) { } 22 7 29
61 2 $host = $1;
62 } elsif ($host =~ /^([A-Za-z0-9.-]+)(?::[0-9]+)?$/) {
63 22 $host = $1;
64 } else {
65 7 return 0;
66 }
67 24 $host = lc $host;
68 24 $host =~ s/\.$//; # "example.com." is "example.com"
69  
70 24 for my $pattern (@$allowed) {
71 37 ▶ if ($pattern =~ /^\.(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /^\.(.+)$/u) { } 13 24 37
72 # ".example.com" matches example.com and its subdomains.
73 13 my $domain = $1;
74 13 ▶ return 1 if $host eq $domain || $host =~ /\Q$pattern\E$/;
Branch: 1 branch
BranchTrueFalseTotal
if $host eq $domain or $host =~ /\Q$pattern\E$/u 5 8 13
Condition100%3/3
$host eq $domain or $host =~ /\Q$pattern\E$/u
l!l&&r!l&&!r
148
MC/DC100%2/2
$host eq $domain or $host =~ /\Q$pattern\E$/u
$host eq $domain $host =~ /\Q$pattern\E$/u
Truth table100%3/3
A or B
A= $host eq $domainB= $host =~ /\Q$pattern\E$/u
ABresult
1X1
011
000
75 } else {
76 24 ▶ return 1 if $host eq $pattern;
Branch: 1 branch
BranchTrueFalseTotal
if $host eq $pattern 9 15 24
77 }
78 }
79 10 return 0;
80 }
81  
82 17 ▶sub _bad_request {
Subroutine _bad_request: called CC=1 SCAR=0.0
83 17 return [400, ['Content-Type' => 'text/plain'], ['Bad Request']];
84 }
85  
86 11 ▶sub prepare_app { return }
Subroutine prepare_app: called CC=1 SCAR=0.0
Pod undocumented
87  
88 28 ▶sub app {
Subroutine app: called CC=3 SCAR=17.3
Pod undocumented
89 28 ▶ return $_[0]->{app} if scalar(@_) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 28 0 28
90 0 ▶ return $_[0]->{app} = scalar(@_) == 2 ? $_[1] : [ @_[1..$#_ ]];
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 2 ? : 0 0 0
91 }
92  
93 11 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=0.0
Pod undocumented
94 11 my $self = shift;
95 11 $self->prepare_app;
96 11 ▶ return sub { $self->call(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
97 }
98  
99 11 ▶sub wrap {
Subroutine wrap: called CC=2 SCAR=7.4
Pod undocumented
100 11 my($self, $app, @args) = @_;
101  
102 # Lambda function runs as reverse proxy backend,
103 # but Plack::Middleware::ReverseProxy is not enabled here.
104 # format_input sets REMOTE_ADDR, psgi.url_scheme, HTTPS and SERVER_PORT from the Lambda event.
105 # The X-Forwarded-* headers that ReverseProxy reads are not trustworthy:
106 # the AWS front ends do not set X-Forwarded-Host, X-Forwarded-Server and X-Forwarded-HTTPS at all.
107  
108 11 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 11 11
109 0 $self->{app} = $app;
110 } else {
111 11 $self = $self->new({ app => $app, @args });
112 }
113 11 return $self->to_app;
114 }
115  
116 45 ▶sub call {
Subroutine call: called CC=5 SCAR=16.2
Pod undocumented
117 45 my($self, $env, $ctx) = @_;
118  
119 # $ctx is added by #26
120 # fall back to $AWS::Lambda::context because of backward compatibility.
121 45 ▶ $ctx ||= $AWS::Lambda::context;
Condition33%1/3
$ctx ||= $AWS::Lambda::context
l!l&&r!l&&!r
0045
MC/DC0%0/2
$ctx ||= $AWS::Lambda::context
$ctx $AWS::Lambda::context
Truth table33%1/3
A ||= B
A= $ctxB= $AWS::Lambda::context
ABresult
1X1
011
000
122  
123 45 ▶ if ($self->{invoke_mode} eq "RESPONSE_STREAM") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'invoke_mode'} eq 'RESPONSE_STREAM') { } 2 43 45
124 2 my $input = $self->_format_input_v2($env, $ctx);
125 2 $input->{'psgi.streaming'} = Plack::Util::TRUE;
126 2 ▶ my $res = $self->_is_allowed_host($input) ? $self->app->($input) : _bad_request();
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_allowed_host($input) ? : 1 1 2
127 2 return $self->_handle_response_stream($res);
128 } else {
129 43 my $input = $self->format_input($env, $ctx);
130 43 ▶ my $res = $self->_is_allowed_host($input) ? $self->app->($input) : _bad_request();
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_allowed_host($input) ? : 27 16 43
131 43 return $self->format_output($res);
132 }
133 }
134  
135 145 ▶sub format_input {
Subroutine format_input: called CC=6 SCAR=18.4
Pod undocumented
136 145 my ($self, $payload, $ctx) = @_;
137 145 ▶ if (my $context = $payload->{requestContext}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $context = $payload->{"requestContext"}) 145 0 145
138 145 ▶ if ($context->{elb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"elb"}) 60 85 145
139 # Application Load Balancer https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html
140 60 return $self->_format_input_v1($payload, $ctx);
141 }
142 }
143 85 ▶ if (my $version = $payload->{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $version = $payload->{"version"}) 54 31 85
144 54 ▶ if ($version =~ /^1[.]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /^1[.]/u) 0 54 54
145 # API Gateway for REST https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
146 0 return $self->_format_input_v1($payload, $ctx);
147 }
148 54 ▶ if ($version =~ /^2[.]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /^2[.]/u) 54 0 54
149 # API Gateway for HTTP https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
150 54 return $self->_format_input_v2($payload, $ctx);
151 }
152 }
153 31 return $self->_format_input_v1($payload, $ctx);
154 }
155  
156 91 ▶sub _format_input_v1 {
Subroutine _format_input_v1: called CC=34 SCAR=35.8
157 91 my ($self, $payload, $ctx) = @_;
158 91 my $env = {};
159  
160 # merge queryStringParameters and multiValueQueryStringParameters
161 my $query = {
162 91 ▶ %{$payload->{queryStringParameters} // {}},
Condition100%2/2
$payload->{"queryStringParameters"} // {}
l!l
3061
MC/DC100%1/1
$payload->{"queryStringParameters"} // {}
$payload->{"queryStringParameters"}
Truth table100%2/2
A
A= $payload->{"queryStringParameters"}
Aresult
11
00
163 91 ▶ %{$payload->{multiValueQueryStringParameters} // {}},
Condition100%2/2
$payload->{"multiValueQueryStringParameters"} // {}
l!l
865
MC/DC100%1/1
$payload->{"multiValueQueryStringParameters"} // {}
$payload->{"multiValueQueryStringParameters"}
Truth table100%2/2
A
A= $payload->{"multiValueQueryStringParameters"}
Aresult
11
00
164 };
165 91 my @params;
166 91 ▶ while (my ($key, $value) = each %$query) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$query) 86 91 177
167 86 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 86 0 86
168 86 for my $v (@$value) {
169 171 push @params, "$key=$v";
170 }
171 } else {
172 0 push @params, "$key=$value";
173 }
174 }
175 91 $env->{QUERY_STRING} = join '&', @params;
176  
177 # merge headers and multiValueHeaders
178 my $headers = {
179 91 ▶ %{$payload->{headers} // {}},
Condition100%2/2
$payload->{"headers"} // {}
l!l
3358
MC/DC100%1/1
$payload->{"headers"} // {}
$payload->{"headers"}
Truth table100%2/2
A
A= $payload->{"headers"}
Aresult
11
00
180 91 ▶ %{$payload->{multiValueHeaders} // {}},
Condition100%2/2
$payload->{"multiValueHeaders"} // {}
l!l
892
MC/DC100%1/1
$payload->{"multiValueHeaders"} // {}
$payload->{"multiValueHeaders"}
Truth table100%2/2
A
A= $payload->{"multiValueHeaders"}
Aresult
11
00
181 };
182 91 ▶ while (my ($key, $value) = each %$headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 903 91 994
183 903 ▶ next if _has_underscore($key);
Branch: 1 branch
BranchTrueFalseTotal
if _has_underscore($key) 133 770 903
184 770 $key =~ s/-/_/g;
185 770 $key = uc $key;
186 770 ▶ if ($key !~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/u) 763 7 770
187 763 $key = "HTTP_$key";
188 }
189 770 ▶ if (ref $value eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 752 18 770
190 752 $value = join ", ", @$value;
191 }
192 770 $env->{$key} = $value;
193 }
194  
195 91 $env->{'psgi.version'} = [1, 1];
196 91 $env->{'psgi.errors'} = *STDERR;
197 91 $env->{'psgi.run_once'} = Plack::Util::FALSE;
198 91 $env->{'psgi.multithread'} = Plack::Util::FALSE;
199 91 $env->{'psgi.multiprocess'} = Plack::Util::FALSE;
200 91 $env->{'psgi.streaming'} = Plack::Util::FALSE;
201 91 $env->{'psgi.nonblocking'} = Plack::Util::FALSE;
202 91 $env->{'psgix.harakiri'} = Plack::Util::TRUE;
203 91 $env->{'psgix.input.buffered'} = Plack::Util::TRUE;
204  
205 # inject the request id that compatible with Plack::Middleware::RequestId
206 91 ▶ if ($ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx) 1 90 91
207 1 $env->{'psgix.request_id'} = $ctx->aws_request_id;
208 1 $env->{'HTTP_X_REQUEST_ID'} = $ctx->aws_request_id;
209 }
210  
211 91 ▶ my $body = encode_utf8($payload->{body} // '');
Condition100%2/2
$payload->{"body"} // ""
l!l
6328
MC/DC100%1/1
$payload->{"body"} // ""
$payload->{"body"}
Truth table100%2/2
A
A= $payload->{"body"}
Aresult
11
00
212 91 ▶ if ($payload->{isBase64Encoded}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($payload->{"isBase64Encoded"}) 60 31 91
213 60 $body = decode_base64 $body;
214 }
215 91 open my $input, "<", \$body;
216 91 $env->{REQUEST_METHOD} = $payload->{httpMethod};
217 91 $env->{'psgi.input'} = $input;
218 91 ▶ $env->{CONTENT_LENGTH} //= bytes::length($body);
Condition66%2/3
$env->{"CONTENT_LENGTH"} //= &bytes::length($body)
l!l&&r!l&&!r
2890
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
219 91 $env->{REQUEST_URI} = $payload->{path};
220 91 ▶ if ($env->{QUERY_STRING}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"QUERY_STRING"}) 86 5 91
221 86 $env->{REQUEST_URI} .= '?' . $env->{QUERY_STRING};
222 }
223 91 $env->{PATH_INFO} = URI::Escape::uri_unescape($payload->{path});
224  
225 91 $env->{SCRIPT_NAME} = '';
226 91 my $requestContext = $payload->{requestContext};
227 91 ▶ if ($requestContext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requestContext) 91 0 91
228 91 my $path = $requestContext->{path};
229 91 my $stage = $requestContext->{stage};
230 91 ▶ if ($stage && $path && $path ne $payload->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stage and $path and $path ne $payload->{"path"}) 31 60 91
Condition66%4/6
$stage and $path and $path ne $payload->{"path"}
!ll&&!rl&&r
60031
$stage and $path
!ll&&!rl&&r
60031
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
231 31 $env->{SCRIPT_NAME} = "/$stage";
232 }
233 }
234  
235 91 ▶ if ($requestContext && $requestContext->{elb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requestContext and $requestContext->{'elb'}) { } 60 31 91
Condition66%2/3
$requestContext and $requestContext->{"elb"}
!ll&&!rl&&r
03160
MC/DC50%1/2
$requestContext and $requestContext->{"elb"}
$requestContext $requestContext->{"elb"}
Truth table66%2/3
A and B
A= $requestContextB= $requestContext->{"elb"}
ABresult
0X0
100
111
236 # Application Load Balancer has no sourceIp in the event.
237 # It sets X-Forwarded-For, X-Forwarded-Proto and X-Forwarded-Port instead.
238 # Read them by their exact names, so that a header such as "X_Forwarded_For" cannot be used.
239 60 my $xff = _last_header_value($payload, 'x-forwarded-for');
240 60 ▶ if (defined $xff && $xff =~ /([^,\s]+)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xff and $xff =~ /([^,\s]+)\s*$/u) 59 1 60
Condition66%2/3
defined $xff and $xff =~ /([^,\s]+)\s*$/u
!ll&&!rl&&r
1059
MC/DC50%1/2
defined $xff and $xff =~ /([^,\s]+)\s*$/u
defined $xff $xff =~ /([^,\s]+)\s*$/u
Truth table66%2/3
A and B
A= defined $xffB= $xff =~ /([^,\s]+)\s*$/u
ABresult
0X0
100
111
241 59 $env->{REMOTE_ADDR} = $1;
242 }
243 60 ▶ my $proto = _last_header_value($payload, 'x-forwarded-proto') // '';
Condition100%2/2
_last_header_value($payload, "x-forwarded-proto") // ""
l!l
591
MC/DC100%1/1
_last_header_value($payload, "x-forwarded-proto") // ""
_last_header_value($payload, "x-forwarded-proto")
Truth table100%2/2
A
A= _last_header_value($payload, "x-forwarded-proto")
Aresult
11
00
244 60 ▶ $env->{'psgi.url_scheme'} = lc $proto eq 'https' ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
lc $proto eq 'https' ? : 55 5 60
245 60 ▶ $env->{HTTPS} = 'ON' if $env->{'psgi.url_scheme'} eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"psgi.url_scheme"} eq "https" 55 5 60
246 60 ▶ my $port = _last_header_value($payload, 'x-forwarded-port') // '';
Condition50%1/2
_last_header_value($payload, "x-forwarded-port") // ""
l!l
600
MC/DC0%0/1
_last_header_value($payload, "x-forwarded-port") // ""
_last_header_value($payload, "x-forwarded-port")
Truth table50%1/2
A
A= _last_header_value($payload, "x-forwarded-port")
Aresult
11
00
247 $env->{SERVER_PORT} = $port =~ /^\s*([0-9]+)\s*$/ ? $1
248 60 ▶ : $env->{'psgi.url_scheme'} eq 'https' ? 443 : 80;
Branch: 2 branches
BranchTrueFalseTotal
$port =~ /^\s*([0-9]+)\s*$/u ? : 60 0 60
$env->{'psgi.url_scheme'} eq 'https' ? : 0 0 0
249 } else {
250 # API Gateway REST API accepts only HTTPS.
251 31 ▶ my $source_ip = $requestContext && $requestContext->{identity} && $requestContext->{identity}{sourceIp};
Condition33%2/6
$requestContext && $requestContext->{'identity'} && $requestContext->{'identity'}{'sourceIp'}
!ll&&!rl&&r
0031
$requestContext and $requestContext->{"identity"}
!ll&&!rl&&r
0031
MC/DC0%0/3
$requestContext && $requestContext->{'identity'} && $requestContext->{'identity'}{'sourceIp'}
$requestContext $requestContext->{"identity"} $requestContext->{'identity'}{'sourceIp'}
Truth table25%1/4
A and B && C
A= $requestContextB= $requestContext->{"identity"}C= $requestContext->{'identity'}{'sourceIp'}
ABCresult
0XX0
10X0
1100
1111
252 31 ▶ if (defined $source_ip) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source_ip) 31 0 31
253 31 $env->{REMOTE_ADDR} = $source_ip;
254 }
255 31 $env->{'psgi.url_scheme'} = 'https';
256 31 $env->{HTTPS} = 'ON';
257 31 $env->{SERVER_PORT} = 443;
258 }
259  
260 91 return $env;
261 }
262  
263 # Header names that contain "_" are dropped, as Apache httpd and nginx do by default.
264 # "-" becomes "_" in the PSGI env, so a client could send "X_Forwarded_For" or "x_amzn_oidc_identity"
265 # and overwrite "X-Forwarded-For" or "x-amzn-oidc-identity" that the AWS front end sets.
266 1553 ▶sub _has_underscore {
Subroutine _has_underscore: called CC=1 SCAR=0.0
267 1553 my $key = shift;
268 1553 return index($key, '_') >= 0;
269 }
270  
271 # returns the last value of the header $name in the payload.
272 # $name must be lower case, and it is compared with the header names as is,
273 # i.e. "x_forwarded_for" does not match "x-forwarded-for".
274 180 ▶sub _last_header_value {
Subroutine _last_header_value: called CC=7 SCAR=19.5
275 180 my ($payload, $name) = @_;
276 180 my $value;
277 180 for my $headers ($payload->{headers}, $payload->{multiValueHeaders}) {
278 360 ▶ next if ref $headers ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers ne "HASH" 180 180 360
279 180 for my $key (sort keys %$headers) {
280 1584 ▶ next if lc $key ne $name;
Branch: 1 branch
BranchTrueFalseTotal
if lc $key ne $name 1406 178 1584
281 178 my $v = $headers->{$key};
282 178 ▶ $v = $v->[-1] if ref $v eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "ARRAY" 172 6 178
283 178 ▶ $value = $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 178 0 178
284 }
285 }
286 180 return $value;
287 }
288  
289 56 ▶sub _format_input_v2 {
Subroutine _format_input_v2: called CC=11 SCAR=24.1
290 56 my ($self, $payload, $ctx) = @_;
291 56 my $env = {};
292  
293 56 $env->{QUERY_STRING} = $payload->{rawQueryString};
294  
295 56 ▶ my $headers = $payload->{headers} // {};
Condition50%1/2
$payload->{"headers"} // {}
l!l
560
MC/DC0%0/1
$payload->{"headers"} // {}
$payload->{"headers"}
Truth table50%1/2
A
A= $payload->{"headers"}
Aresult
11
00
296 56 ▶ while (my ($key, $value) = each %$headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 650 56 706
297 650 ▶ next if _has_underscore($key);
Branch: 1 branch
BranchTrueFalseTotal
if _has_underscore($key) 83 567 650
298 567 $key =~ s/-/_/g;
299 567 $key = uc $key;
300 567 ▶ if ($key !~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/u) 538 29 567
301 538 $key = "HTTP_$key";
302 }
303 567 $env->{$key} = $value;
304 }
305  
306 56 $env->{'psgi.version'} = [1, 1];
307 56 $env->{'psgi.errors'} = *STDERR;
308 56 $env->{'psgi.run_once'} = Plack::Util::FALSE;
309 56 $env->{'psgi.multithread'} = Plack::Util::FALSE;
310 56 $env->{'psgi.multiprocess'} = Plack::Util::FALSE;
311 56 $env->{'psgi.streaming'} = Plack::Util::FALSE;
312 56 $env->{'psgi.nonblocking'} = Plack::Util::FALSE;
313 56 $env->{'psgix.harakiri'} = Plack::Util::TRUE;
314 56 $env->{'psgix.input.buffered'} = Plack::Util::TRUE;
315  
316 # inject the request id that compatible with Plack::Middleware::RequestId
317 56 ▶ if ($ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx) 0 56 56
318 0 $env->{'psgix.request_id'} = $ctx->aws_request_id;
319 0 $env->{'HTTP_X_REQUEST_ID'} = $ctx->aws_request_id;
320 }
321  
322 56 ▶ my $body = encode_utf8($payload->{body} // '');
Condition100%2/2
$payload->{"body"} // ""
l!l
452
MC/DC100%1/1
$payload->{"body"} // ""
$payload->{"body"}
Truth table100%2/2
A
A= $payload->{"body"}
Aresult
11
00
323 56 ▶ if ($payload->{isBase64Encoded}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($payload->{"isBase64Encoded"}) 2 54 56
324 2 $body = decode_base64 $body;
325 }
326 56 open my $input, "<", \$body;
327 56 $env->{'psgi.input'} = $input;
328 56 ▶ $env->{CONTENT_LENGTH} //= bytes::length($body);
Condition66%2/3
$env->{"CONTENT_LENGTH"} //= &bytes::length($body)
l!l&&r!l&&!r
25310
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
329 56 my $requestContext = $payload->{requestContext};
330 56 $env->{REQUEST_METHOD} = $requestContext->{http}{method};
331 56 $env->{REQUEST_URI} = $payload->{rawPath};
332 56 ▶ if ($env->{QUERY_STRING}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"QUERY_STRING"}) 52 4 56
333 52 $env->{REQUEST_URI} .= '?' . $env->{QUERY_STRING};
334 }
335 56 $env->{PATH_INFO} = $requestContext->{http}{path};
336 56 $env->{SCRIPT_NAME} = '';
337  
338 # API Gateway HTTP API and Lambda Function URLs accept only HTTPS.
339 56 my $source_ip = $requestContext->{http}{sourceIp};
340 56 ▶ if (defined $source_ip) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source_ip) 56 0 56
341 56 $env->{REMOTE_ADDR} = $source_ip;
342 }
343 56 $env->{'psgi.url_scheme'} = 'https';
344 56 $env->{HTTPS} = 'ON';
345 56 $env->{SERVER_PORT} = 443;
346 56 return $env;
347 }
348  
349 47 ▶sub format_output {
Subroutine format_output: called CC=5 SCAR=16.1
Pod undocumented
350 47 my ($self, $response) = @_;
351 47 my ($status, $headers, $body) = @$response;
352  
353 47 my $singleValueHeaders = {};
354 47 my $multiValueHeaders = {};
355 49 ▶ Plack::Util::header_iter($headers, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
356 49 my ($k, $v) = @_;
357 49 $singleValueHeaders->{lc $k} = string $v;
358 49 ▶ push @{$multiValueHeaders->{lc $k} //= []}, string $v;
Condition100%2/2
$$multiValueHeaders{lc $k} //= []
l!l
148
MC/DC100%1/1
$$multiValueHeaders{lc $k} //= []
$$multiValueHeaders{lc $k}
Truth table100%2/2
A
A= $$multiValueHeaders{lc $k}
Aresult
11
00
359 47 });
360  
361 47 my $content = '';
362 47 ▶ if (reftype($body) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($body) eq 'ARRAY') { } 46 1 47
363 46 $content = join '', grep defined, @$body;
364 } else {
365 1 local $/ = \4096;
366 1 ▶ while (defined(my $buf = $body->getline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $buf = $body->getline)) 1 1 2
367 1 $content .= $buf;
368 }
369 1 $body->close;
370 }
371  
372 47 ▶ my $type = $singleValueHeaders->{'content-type'} // 'application/octet-stream';
Condition50%1/2
$singleValueHeaders->{"content-type"} // "application/octet-stream"
l!l
470
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
373 47 my $isBase64Encoded = $type !~ m(^text/.*|application/(:?json|javascript|xml))i;
374 47 ▶ if ($isBase64Encoded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isBase64Encoded) { } 1 46 47
375 1 $content = encode_base64 $content, '';
376 } else {
377 46 ▶ $content = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
378 # is valid utf-8 string? try to decode as utf-8.
379 46 decode_utf8($content, Encode::FB_CROAK | Encode::LEAVE_SRC);
380 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
381 # it looks not utf-8 encoding. fallback to base64 encoding.
382 1 $isBase64Encoded = 1;
383 1 encode_base64 $content, '';
384 46 };
385 }
386  
387 return +{
388 47 isBase64Encoded => bool $isBase64Encoded,
389 headers => $singleValueHeaders,
390 multiValueHeaders => $multiValueHeaders,
391 statusCode => number $status,
392 body => string $content,
393 }
394 }
395  
396 5 ▶sub _handle_response_stream {
Subroutine _handle_response_stream: called CC=2 SCAR=6.9
397 5 my ($self, $response) = @_;
398 5 ▶ if (reftype($response) ne "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($response) ne "CODE") 4 1 5
399 4 my $orig = $response;
400 4 ▶ $response = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
401 4 my $responder = shift;
402 4 $responder->($orig);
403 4 };
404 }
405  
406 5 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
407 5 my $lambda_responder = shift;
408 5 ▶ my $psgi_responder = sub {
Subroutine __ANON__: called CC=9 SCAR=22.2
409 5 my $response = shift;
410 5 my ($status, $headers, $body) = @$response;
411  
412 # write the prelude.
413 5 my $writer = $lambda_responder->("application/vnd.awslambda.http-integration-response");
414 5 my $prelude = encode_json($self->_format_response_stream($status, $headers));
415 5 $prelude .= "\x00\x00\x00\x00\x00\x00\x00\x00";
416 5 ▶ $writer->write($prelude) or die "failed to write prelude: $!";
Branch: 1 branch
BranchTrueFalseTotal
$writer->write($prelude) or die "failed to write prelude: $!" 5 0 5
417  
418 # write the body.
419 5 ▶ if (!defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 1 4 5
420 # the caller will write the body.
421 1 return $writer;
422 }
423 4 ▶ if (reftype($body) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($body) eq 'ARRAY') { } 3 1 4
424 # array-ref
425 3 for my $chunk (@$body) {
426 3 ▶ $writer->write($chunk) or die "failed to write chunk: $!";
Branch: 1 branch
BranchTrueFalseTotal
$writer->write($chunk) or die "failed to write chunk: $!" 3 0 3
427 }
428 } else {
429 # IO::Handle-like object
430 1 local $/ = \4096;
431 1 ▶ while (defined(my $chunk = $body->getline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $chunk = $body->getline)) 1 1 2
432 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
433 }
434 }
435 4 ▶ $writer->close or die "failed to close writer: $!";
Branch: 1 branch
BranchTrueFalseTotal
$writer->close or die "failed to close writer: $!" 4 0 4
436 4 return;
437 5 };
438 5 $response->($psgi_responder);
439 5 };
440 }
441  
442 5 ▶sub _format_response_stream {
Subroutine _format_response_stream: called CC=1 SCAR=0.2
443 5 my ($self, $status, $headers) = @_;
444 5 my $headers_hash = {};
445 5 my $cookies = [];
446  
447 5 ▶ Plack::Util::header_iter($headers, sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
448 5 my ($k, $v) = @_;
449 5 $k = lc $k;
450 5 ▶ if ($k eq 'set-cookie') {
Branch: 2 branches
BranchTrueFalseTotal
if ($k eq 'set-cookie') { } 0 5 5
elsif (exists $headers_hash->{$k}) { } 0 5 5
451 0 push @$cookies, string $v;
452 } elsif (exists $headers_hash->{$k}) {
453 0 $headers_hash->{$k} = ", $v";
454 } else {
455 5 $headers_hash->{$k} = string $v;
456 }
457 5 });
458  
459 return +{
460 5 statusCode => number $status,
461 headers => $headers_hash,
462 cookies => $cookies,
463 };
464 }
465  
466 3 1;
467 __END__
↑ Summary Lambda/Bootstrap.pm »