| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$escaped_path =~ m[^/] and $self->s3->honor_leading_slashes
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$res->header("location") and $res->content =~ />TemporaryRedirect</s
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||