S3/Roles/Request.pm

filter: Statementstmt 98.5% 66 / 67 Branchbran 87.5% 14 / 16 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 1 totaltotal 96.3% 104 / 108 CC 11 SCAR 10.8
CC
11
Cov
97%
__ANON__
6.0
_send_request
4.0
__ANON__
2.1
CRAP 2.9
« S3/Signer.pm ↑ Summary S3/ResponseParser.pm »
1 package AWS::S3::Roles::Request;
2 4 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use AWS::S3::ResponseParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use MooseX::Types::URI qw(Uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use URI::Escape qw/ uri_escape /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use AWS::S3::Signer::V4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Log::Any qw( $LOG );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 has 's3' => (
11 is => 'ro',
12 isa => 'AWS::S3',
13 required => 1,
14 );
15  
16 4 has 'type' => (
17 is => 'ro',
18 isa => 'Str',
19 required => 1,
20 );
21  
22 has 'protocol' => (
23 is => 'ro',
24 isa => 'Str',
25 lazy => 1,
26 24 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
27 24 ▶ shift->s3->secure ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
(shift())->s3->secure ? : 0 24 24
28 }
29 4 );
30  
31 has 'endpoint' => (
32 is => 'ro',
33 isa => 'Str',
34 lazy => 1,
35 24 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 24 shift->s3->endpoint;
37 }
38 4 );
39  
40 # XXX should be required=>1; https://rt.cpan.org/Ticket/Display.html?id=77863
41 4 has "_action" => (
42 isa => 'Str',
43 is => 'ro',
44 init_arg => undef,
45 #required => 1
46 );
47  
48 4 has '_expect_nothing' => ( isa => 'Bool', is => 'ro', init_arg => undef );
49  
50 has '_uri' => (
51 isa => Uri,
52 is => 'ro',
53 lazy => 1,
54 19 ▶ default => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
55 19 my $self = shift;
56 19 my $m = $self->meta;
57  
58 19 ▶ my $uri = URI->new(
Branch: 1 branch
BranchTrueFalseTotal
$m->has_attribute('bucket') ? : 10 9 19
59 $self->protocol . '://'
60 . ( $m->has_attribute('bucket') ? $self->bucket . '.' : '' )
61 . $self->endpoint
62 . '/'
63 );
64  
65 # note we add some extra exceptions to uri_escape to prevent
66 # encoding of things like "/", ":", "="
67 19 ▶ if ( $m->has_attribute('key') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->has_attribute("key")) 8 11 19
68 8 my $escaped_path = uri_escape( $self->key,"^A-Za-z0-9\-\._~\/:=" );
69  
70 # if we have a leading slash in the key name we need to add *another*
71 # slash in the call to ->path to ensure it is retained
72 8 ▶ if ( $escaped_path =~ m!^/! && $self->s3->honor_leading_slashes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escaped_path =~ m[^/] and $self->s3->honor_leading_slashes) { } 3 5 8
Condition100%3/3
$escaped_path =~ m[^/] and $self->s3->honor_leading_slashes
!ll&&!rl&&r
323
MC/DC100%2/2
$escaped_path =~ m[^/] and $self->s3->honor_leading_slashes
$escaped_path =~ m[^/] $self->s3->honor_leading_slashes
Truth table100%3/3
A and B
A= $escaped_path =~ m[^/]B= $self->s3->honor_leading_slashes
ABresult
0X0
100
111
73 3 $uri->path( '/'.$escaped_path )
74 } else {
75 5 $uri->path( $escaped_path )
76 }
77 }
78  
79 19 ▶ $uri->query_keywords( $self->_subresource )
Branch: 1 branch
BranchTrueFalseTotal
if $m->has_attribute("_subresource") 1 18 19
80 if $m->has_attribute('_subresource');
81  
82 19 $uri;
83 }
84 4 );
85  
86 has 'signerv4' => (
87 is => 'ro',
88 isa => 'AWS::S3::Signer::V4',
89 lazy => 1,
90 29 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
91 29 my $s = shift;
92 29 AWS::S3::Signer::V4->new(
93 -access_key => $s->s3->access_key_id,
94 -secret_key => $s->s3->secret_access_key,
95 );
96 }
97 4 );
98  
99 27 ▶sub _send_request {
Subroutine _send_request: called CC=4 SCAR=13.9
100 27 my ( $s, $method, $uri, $headers, $content ) = @_;
101 27 $LOG->debug('Making AWS request', {method => $method, uri => "$uri"});
102  
103 27 my $req = HTTP::Request->new( $method => $uri );
104 27 ▶ $req->content( $content ) if $content;
Branch: 1 branch
BranchTrueFalseTotal
if $content 5 22 27
105  
106 27 delete($headers->{Authorization}); # we will use a v4 signature
107 27 map { $req->header( $_ => $headers->{$_} ) } keys %$headers;
108  
109 27 $s->_sign($req);
110 27 my $res = $s->s3->ua->request( $req );
111  
112 # After creating a bucket and setting its location constraint, we get this
113 # strange 'TemporaryRedirect' response. Deal with it.
114 27 ▶ if ( $res->header( 'location' ) && $res->content =~ m{>TemporaryRedirect<}s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->header("location") and $res->content =~ />TemporaryRedirect</s) 1 26 27
Condition100%3/3
$res->header("location") and $res->content =~ />TemporaryRedirect</s
!ll&&!rl&&r
11151
MC/DC100%2/2
$res->header("location") and $res->content =~ />TemporaryRedirect</s
$res->header("location") $res->content =~ />TemporaryRedirect</s
Truth table100%3/3
A and B
A= $res->header("location")B= $res->content =~ />TemporaryRedirect</s
ABresult
0X0
100
111
115 1 $req->uri( $res->header( 'location' ) );
116 1 $res = $s->s3->ua->request( $req );
117 }
118 27 return $s->parse_response( $res );
119 }
120  
121 27 ▶sub _sign {
Subroutine _sign: called CC=2 SCAR=7.1
122 27 my ($s, $request) = @_;
123 27 my $signer = $s->signerv4;
124 27 ▶ if (defined $s->s3->session_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->s3->session_token) 0 27 27
125 0 $request->header('X-Amz-Security-Token', $s->s3->session_token);
126 }
127 27 my $digest = Digest::SHA::sha256_hex($request->content);
128 27 $request->header('X-Amz-Content-SHA256', $digest);
129 27 $signer->sign($request, $s->s3->region, $digest);
130 27 $request;
131 }
132  
133 27 ▶sub parse_response {
Subroutine parse_response: called CC=1 SCAR=0.0
Pod undocumented
134 27 my ( $self, $res ) = @_;
135  
136 27 AWS::S3::ResponseParser->new(
137 response => $res,
138 expect_nothing => $self->_expect_nothing,
139 type => $self->type,
140 );
141 }
142  
143 4 1;
« S3/Signer.pm ↑ Summary S3/ResponseParser.pm »