| 1 | package AWS::Lambda::ResponseWriter; | ||||||||||||||||||||||||||||
| 2 | 18 | ▶ | use 5.026000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 18 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 18 | my %DefaultPort = ( | |||||||||||||||||||||||||||
| 12 | http => 80, | ||||||||||||||||||||||||||||
| 13 | https => 443, | ||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=23.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 1 | my $proto = shift; | |||||||||||||||||||||||||||
| 18 | 1 | ▶ | 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
| |||||||||||||||||||||||||||||
| 19 | 1 | my %args; | |||||||||||||||||||||||||||
| 20 | 1 | ▶ | if (@_ == 1 && ref $_[0] eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
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"
| |||||||||||||||||||||||||||||
| 21 | 0 | %args = %{$_[0]}; | |||||||||||||||||||||||||||
| 22 | } else { | ||||||||||||||||||||||||||||
| 23 | 1 | %args = @_; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | my $http = $args{http} // HTTP::Tiny->new; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{"http"} // "HTTP::Tiny"->new
MC/DC0%0/2
$args{"http"} // "HTTP::Tiny"->new
$args{"http"}
"HTTP::Tiny"->new
Truth table33%1/3
A // B
A= $args{"http"}B= "HTTP::Tiny"->new
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | my $response_url = $args{response_url} // die '$LAMBDA_TASK_ROOT is not found'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"response_url"} // die("\$LAMBDA_TASK_ROOT is not found")
MC/DC0%0/1
$args{"response_url"} // die("\$LAMBDA_TASK_ROOT is not found")
$args{"response_url"}
Truth table50%1/2
A
A= $args{"response_url"}
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | my $content_type = $args{content_type} // 'application/json'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"content_type"} // "application/json"
MC/DC0%0/1
$args{"content_type"} // "application/json"
$args{"content_type"}
Truth table50%1/2
A
A= $args{"content_type"}
| |||||||||||||||||||||||||||||
| 29 | 1 | my $self = bless +{ | |||||||||||||||||||||||||||
| 30 | response_url => $response_url, | ||||||||||||||||||||||||||||
| 31 | http => $http, | ||||||||||||||||||||||||||||
| 32 | handle => undef, | ||||||||||||||||||||||||||||
| 33 | closed => 0, | ||||||||||||||||||||||||||||
| 34 | }, $class; | ||||||||||||||||||||||||||||
| 35 | 1 | return $self; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | sub _request { | ||||||||||||||||||||||||||
|
Subroutine
_request: called CC=5 SCAR=18.2
| |||||||||||||||||||||||||||||
| 39 | 1 | my ($self, $content_type) = @_; | |||||||||||||||||||||||||||
| 40 | 1 | my $response_url = $self->{response_url}; | |||||||||||||||||||||||||||
| 41 | 1 | my $http = $self->{http}; | |||||||||||||||||||||||||||
| 42 | 1 | my ($scheme, $host, $port, $path_query, $auth) = $http->_split_url($response_url); | |||||||||||||||||||||||||||
| 43 | 1 | ▶ | my $host_port = ($port == $DefaultPort{$scheme} ? $host : "$host:$port"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 1 | my $request = { | |||||||||||||||||||||||||||
| 45 | method => "POST", | ||||||||||||||||||||||||||||
| 46 | scheme => $scheme, | ||||||||||||||||||||||||||||
| 47 | host => $host, | ||||||||||||||||||||||||||||
| 48 | port => $port, | ||||||||||||||||||||||||||||
| 49 | host_port => $host_port, | ||||||||||||||||||||||||||||
| 50 | uri => $path_query, | ||||||||||||||||||||||||||||
| 51 | headers => { | ||||||||||||||||||||||||||||
| 52 | "host" => $host_port, | ||||||||||||||||||||||||||||
| 53 | "content-type" => $content_type, | ||||||||||||||||||||||||||||
| 54 | "transfer-encoding" => "chunked", | ||||||||||||||||||||||||||||
| 55 | "trailer" => "Lambda-Runtime-Function-Error-Type, Lambda-Runtime-Function-Error-Body", | ||||||||||||||||||||||||||||
| 56 | "lambda-runtime-function-response-mode" => "streaming", | ||||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||
| 58 | header_case => { | ||||||||||||||||||||||||||||
| 59 | "host" => "Host", | ||||||||||||||||||||||||||||
| 60 | "content-type" => "Content-Type", | ||||||||||||||||||||||||||||
| 61 | "transfer-encoding" => "Transfer-Encoding", | ||||||||||||||||||||||||||||
| 62 | "trailer" => "Trailer", | ||||||||||||||||||||||||||||
| 63 | "lambda-runtime-function-response-mode" => "Lambda-Runtime-Function-Response-Mode", | ||||||||||||||||||||||||||||
| 64 | }, | ||||||||||||||||||||||||||||
| 65 | }; | ||||||||||||||||||||||||||||
| 66 | 1 | my $peer = $host; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # We remove the cached handle so it is not reused in the case of redirect. | ||||||||||||||||||||||||||||
| 69 | # reuse for the same scheme, host and port | ||||||||||||||||||||||||||||
| 70 | 1 | my $handle = delete $http->{handle}; | |||||||||||||||||||||||||||
| 71 | 1 | ▶ | if ( $handle ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | unless ( $handle->can_reuse( $scheme, $host, $port, $peer ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $handle->close; | |||||||||||||||||||||||||||
| 74 | 0 | undef $handle; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 1 | ▶ | $handle ||= $http->_open_handle( $request, $scheme, $host, $port, $peer ); | ||||||||||||||||||||||||||
|
Condition33%1/3
$handle ||= $http->_open_handle($request, $scheme, $host, $port, $peer)
MC/DC0%0/2
$handle ||= $http->_open_handle($request, $scheme, $host, $port, $peer)
$handle
$http->_open_handle($request, $scheme, $host, $port, $peer)
Truth table33%1/3
A ||= B
A= $handleB= $http->_open_handle($request, $scheme, $host, $port, $peer)
| |||||||||||||||||||||||||||||
| 78 | 1 | $self->{handle} = $handle; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | $handle->write_request_header(@{$request}{qw/method uri headers header_case/}); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | ▶ | sub _handle_response { | ||||||||||||||||||||||||||
|
Subroutine
_handle_response: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 84 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 85 | 1 | ▶ | if (!$self->{closed}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | $self->close; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | my $http = $self->{http}; | |||||||||||||||||||||||||||
| 90 | 1 | my $handle = $self->{handle}; | |||||||||||||||||||||||||||
| 91 | 1 | my $response; | |||||||||||||||||||||||||||
| 92 | 1 | do { $response = $handle->read_response_header } | |||||||||||||||||||||||||||
| 93 | 1 | ▶ | until (substr($response->{status},0,1) ne '1'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | my $data_cb = $http->_prepare_data_cb($response, {}); | |||||||||||||||||||||||||||
| 96 | 1 | my $known_message_length = $handle->read_body($data_cb, $response); | |||||||||||||||||||||||||||
| 97 | 1 | $handle->close; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | $response->{success} = substr( $response->{status}, 0, 1 ) eq '2'; | |||||||||||||||||||||||||||
| 100 | 1 | $response->{url} = $self->{response_url}; | |||||||||||||||||||||||||||
| 101 | 1 | return $response; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=4 SCAR=15.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 2 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 2 | ▶ | if ($self->{closed}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | # already closed | ||||||||||||||||||||||||||||
| 109 | 0 | croak "write failed: already closed"; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | ▶ | if (!defined($data) || length($data) == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $data or length $data == 0
MC/DC0%0/2
not defined $data or length $data == 0
not defined $data
length $data == 0
Truth table33%1/3
not(A) or B
A= not defined $dataB= length $data == 0
| |||||||||||||||||||||||||||||
| 113 | 0 | return "0E0"; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | my $chunk = sprintf '%X', length $data; | |||||||||||||||||||||||||||
| 117 | 2 | $chunk .= "\x0D\x0A"; | |||||||||||||||||||||||||||
| 118 | 2 | $chunk .= $data; | |||||||||||||||||||||||||||
| 119 | 2 | $chunk .= "\x0D\x0A"; | |||||||||||||||||||||||||||
| 120 | 2 | return $self->{handle}->write($chunk); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 125 | 1 | ▶ | if ($self->{closed}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | # already closed | ||||||||||||||||||||||||||||
| 127 | 0 | return; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 1 | my $handle = $self->{handle}; | |||||||||||||||||||||||||||
| 130 | 1 | $self->{closed} = 1; | |||||||||||||||||||||||||||
| 131 | 1 | return $handle->write("0\x0D\x0A\x0D\x0A"); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub _close_with_error { | ||||||||||||||||||||||||||
|
Subroutine
_close_with_error: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 135 | 0 | my ($self, $error) = @_; | |||||||||||||||||||||||||||
| 136 | 0 | ▶ | if ($self->{closed}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | # already closed | ||||||||||||||||||||||||||||
| 138 | 0 | return; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 0 | $self->{closed} = 1; | |||||||||||||||||||||||||||
| 141 | 0 | my $handle = $self->{handle}; | |||||||||||||||||||||||||||
| 142 | 0 | $handle->write("0\x0D\x0A"); | |||||||||||||||||||||||||||
| 143 | 0 | ▶ | my $type = blessed($error) // "Error"; | ||||||||||||||||||||||||||
|
Condition0%0/2
builtin::blessed($error) // "Error"
MC/DC0%0/1
builtin::blessed($error) // "Error"
builtin::blessed($error)
Truth table0%0/2
A
A= builtin::blessed($error)
| |||||||||||||||||||||||||||||
| 144 | 0 | return $handle->write_header_lines({ | |||||||||||||||||||||||||||
| 145 | "lambda-runtime-function-error-type" => "$type", | ||||||||||||||||||||||||||||
| 146 | "lambda-runtime-function-error-body" => encode_base64("$error", ""), | ||||||||||||||||||||||||||||
| 147 | }, { | ||||||||||||||||||||||||||||
| 148 | "lambda-runtime-function-error-type" => "Lambda-Runtime-Function-Error-Type", | ||||||||||||||||||||||||||||
| 149 | "lambda-runtime-function-error-body" => "Lambda-Runtime-Function-Error-Body", | ||||||||||||||||||||||||||||
| 150 | }); | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 18 | 1; | |||||||||||||||||||||||||||