Lambda/ResponseWriter.pm

filter: Statementstmt 81.3% 74 / 91 Branchbran 40.0% 8 / 20 Conditioncond 33.3% 7 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 3 totaltotal 62.9% 102 / 162 CC 19 SCAR 17.2
CC
19
Cov
67%
_close_with_error
12.0
new
10.3
_request
6.2
CRAP 5.6
« Lambda/Bootstrap.pm ↑ Summary Lambda/Context.pm »
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
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
19 1 my %args;
20 1 ▶ if (@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 1 1
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
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
l!l&&r!l&&!r
100
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
ABresult
1X1
011
000
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")
l!l
10
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"}
Aresult
11
00
28 1 ▶ my $content_type = $args{content_type} // 'application/json';
Condition50%1/2
$args{"content_type"} // "application/json"
l!l
01
MC/DC0%0/1
$args{"content_type"} // "application/json"
$args{"content_type"}
Truth table50%1/2
A
A= $args{"content_type"}
Aresult
11
00
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
BranchTrueFalseTotal
$port == $DefaultPort{$scheme} ? : 0 1 1
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
BranchTrueFalseTotal
if ($handle) 0 1 1
72 0 ▶ unless ( $handle->can_reuse( $scheme, $host, $port, $peer ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle->can_reuse($scheme, $host, $port, $peer)) 0 0 0
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)
l!l&&r!l&&!r
010
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)
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless ($self->{"closed"}) 0 1 1
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
BranchTrueFalseTotal
substr($response->{"status"}, 0, 1) ne 1 or do { do { $response = $handle->read_response_header } } 1 0 1
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
BranchTrueFalseTotal
if ($self->{"closed"}) 0 2 2
108 # already closed
109 0 croak "write failed: already closed";
110 }
111  
112 2 ▶ if (!defined($data) || length($data) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $data or length $data == 0) 0 2 2
Condition33%1/3
not defined $data or length $data == 0
l!l&&r!l&&!r
002
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($self->{"closed"}) 0 1 1
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
BranchTrueFalseTotal
if ($self->{"closed"}) 0 0 0
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"
l!l
00
MC/DC0%0/1
builtin::blessed($error) // "Error"
builtin::blessed($error)
Truth table0%0/2
A
A= builtin::blessed($error)
Aresult
11
00
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;
« Lambda/Bootstrap.pm ↑ Summary Lambda/Context.pm »