| 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
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 13 | my $self; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 13 | ▶ | if (@_ == 1 && ref $_[0] eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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'
$ENV{"PERL5_LAMBDA_PSGI_INVOKE_MODE"} or $ENV{"AWS_LWA_INVOKE_MODE"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | $self->{invoke_mode} = uc $mode; | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 13 | ▶ | if (!defined $self->{allowed_hosts}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 6 | ▶ | my @hosts = ref $hosts eq 'ARRAY' ? @$hosts : split /,/, $hosts; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 6 | ▶ | @hosts = grep { $_ ne '' } map { my $h = lc($_ // ''); $h =~ s/^\s+|\s+$//g; $h =~ s/\.$//; $h } @hosts; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_ // ""
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 6 | ▶ | return @hosts ? \@hosts : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 31 | ▶ | my $host = $env->{HTTP_HOST} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$env->{"HTTP_HOST"} // ""
MC/DC100%1/1
$env->{"HTTP_HOST"} // ""
$env->{"HTTP_HOST"}
Truth table100%2/2
A
A= $env->{"HTTP_HOST"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$host eq $domain or $host =~ /\Q$pattern\E$/u
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
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 24 | ▶ | return 1 if $host eq $pattern; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | return $_[0]->{app} = scalar(@_) == 2 ? $_[1] : [ @_[1..$#_ ]]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$ctx ||= $AWS::Lambda::context
$ctx
$AWS::Lambda::context
Truth table33%1/3
A ||= B
A= $ctxB= $AWS::Lambda::context
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 45 | ▶ | if ($self->{invoke_mode} eq "RESPONSE_STREAM") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 145 | ▶ | if ($context->{elb}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 54 | ▶ | if ($version =~ /^1[.]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // {}
MC/DC100%1/1
$payload->{"queryStringParameters"} // {}
$payload->{"queryStringParameters"}
Truth table100%2/2
A
A= $payload->{"queryStringParameters"}
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 91 | ▶ | %{$payload->{multiValueQueryStringParameters} // {}}, | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$payload->{"multiValueQueryStringParameters"} // {}
MC/DC100%1/1
$payload->{"multiValueQueryStringParameters"} // {}
$payload->{"multiValueQueryStringParameters"}
Truth table100%2/2
A
A= $payload->{"multiValueQueryStringParameters"}
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 91 | my @params; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 91 | ▶ | while (my ($key, $value) = each %$query) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 86 | ▶ | if (ref($value) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // {}
MC/DC100%1/1
$payload->{"headers"} // {}
$payload->{"headers"}
Truth table100%2/2
A
A= $payload->{"headers"}
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 91 | ▶ | %{$payload->{multiValueHeaders} // {}}, | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$payload->{"multiValueHeaders"} // {}
MC/DC100%1/1
$payload->{"multiValueHeaders"} // {}
$payload->{"multiValueHeaders"}
Truth table100%2/2
A
A= $payload->{"multiValueHeaders"}
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 91 | ▶ | while (my ($key, $value) = each %$headers) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 903 | ▶ | next if _has_underscore($key); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 770 | $key =~ s/-/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 770 | $key = uc $key; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 770 | ▶ | if ($key !~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 763 | $key = "HTTP_$key"; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 770 | ▶ | if (ref $value eq "ARRAY") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // ""
MC/DC100%1/1
$payload->{"body"} // ""
$payload->{"body"}
Truth table100%2/2
A
A= $payload->{"body"}
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 91 | ▶ | if ($payload->{isBase64Encoded}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 91 | $env->{REQUEST_URI} = $payload->{path}; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 91 | ▶ | if ($env->{QUERY_STRING}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 91 | my $path = $requestContext->{path}; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 91 | my $stage = $requestContext->{stage}; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 91 | ▶ | if ($stage && $path && $path ne $payload->{path}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$stage and $path and $path ne $payload->{"path"}
$stage and $path
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 31 | $env->{SCRIPT_NAME} = "/$stage"; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 91 | ▶ | if ($requestContext && $requestContext->{elb}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$requestContext and $requestContext->{"elb"}
MC/DC50%1/2
$requestContext and $requestContext->{"elb"}
$requestContext
$requestContext->{"elb"}
Truth table66%2/3
A and B
A= $requestContextB= $requestContext->{"elb"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $xff and $xff =~ /([^,\s]+)\s*$/u
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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") // ""
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 60 | ▶ | $env->{'psgi.url_scheme'} = lc $proto eq 'https' ? 'https' : 'http'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 60 | ▶ | $env->{HTTPS} = 'ON' if $env->{'psgi.url_scheme'} eq 'https'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 60 | ▶ | my $port = _last_header_value($payload, 'x-forwarded-port') // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
_last_header_value($payload, "x-forwarded-port") // ""
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | $env->{SERVER_PORT} = $port =~ /^\s*([0-9]+)\s*$/ ? $1 | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 60 | ▶ | : $env->{'psgi.url_scheme'} eq 'https' ? 443 : 80; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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'}
$requestContext and $requestContext->{"identity"}
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 31 | ▶ | if (defined $source_ip) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 180 | for my $key (sort keys %$headers) { | |||||||||||||||||||||||||||||||||||||||||
| 280 | 1584 | ▶ | next if lc $key ne $name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 178 | my $v = $headers->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 282 | 178 | ▶ | $v = $v->[-1] if ref $v eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 178 | ▶ | $value = $v if defined $v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // {}
MC/DC0%0/1
$payload->{"headers"} // {}
$payload->{"headers"}
Truth table50%1/2
A
A= $payload->{"headers"}
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 56 | ▶ | while (my ($key, $value) = each %$headers) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 650 | ▶ | next if _has_underscore($key); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 567 | $key =~ s/-/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 567 | $key = uc $key; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 567 | ▶ | if ($key !~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // ""
MC/DC100%1/1
$payload->{"body"} // ""
$payload->{"body"}
Truth table100%2/2
A
A= $payload->{"body"}
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 56 | ▶ | if ($payload->{isBase64Encoded}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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} //= []
MC/DC100%1/1
$$multiValueHeaders{lc $k} //= []
$$multiValueHeaders{lc $k}
Truth table100%2/2
A
A= $$multiValueHeaders{lc $k}
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | 47 | }); | |||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | 47 | my $content = ''; | |||||||||||||||||||||||||||||||||||||||||
| 362 | 47 | ▶ | if (reftype($body) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 46 | $content = join '', grep defined, @$body; | |||||||||||||||||||||||||||||||||||||||||
| 364 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 365 | 1 | local $/ = \4096; | |||||||||||||||||||||||||||||||||||||||||
| 366 | 1 | ▶ | while (defined(my $buf = $body->getline)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$singleValueHeaders->{"content-type"} // "application/octet-stream"
$singleValueHeaders->{"content-type"}
Truth table50%1/2
A
A= $singleValueHeaders->{"content-type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 47 | my $isBase64Encoded = $type !~ m(^text/.*|application/(:?json|javascript|xml))i; | |||||||||||||||||||||||||||||||||||||||||
| 374 | 47 | ▶ | if ($isBase64Encoded) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | # write the body. | ||||||||||||||||||||||||||||||||||||||||||
| 419 | 5 | ▶ | if (!defined $body) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 420 | # the caller will write the body. | ||||||||||||||||||||||||||||||||||||||||||
| 421 | 1 | return $writer; | |||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||
| 423 | 4 | ▶ | if (reftype($body) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | # array-ref | ||||||||||||||||||||||||||||||||||||||||||
| 425 | 3 | for my $chunk (@$body) { | |||||||||||||||||||||||||||||||||||||||||
| 426 | 3 | ▶ | $writer->write($chunk) or die "failed to write chunk: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 429 | # IO::Handle-like object | ||||||||||||||||||||||||||||||||||||||||||
| 430 | 1 | local $/ = \4096; | |||||||||||||||||||||||||||||||||||||||||
| 431 | 1 | ▶ | while (defined(my $chunk = $body->getline)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 432 | 1 | ▶ | $writer->write($chunk) or die "failed to write chunk: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 4 | ▶ | $writer->close or die "failed to close writer: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||