| 1 | package Net::Amazon::S3::HTTPRequest; | ||||||||||||||||||||||||||||
| 2 | 99 | $Net::Amazon::S3::HTTPRequest::VERSION = '0.992'; | |||||||||||||||||||||||||||
| 3 | 99 | ▶ | use Moose 0.85; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 99 | ▶ | use MooseX::StrictConstructor 0.16; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 99 | ▶ | use HTTP::Date; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 99 | ▶ | use MIME::Base64 qw( encode_base64 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 99 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 99 | ▶ | use URI::Escape qw( uri_escape_utf8 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 99 | ▶ | use URI::QueryParam; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 99 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 99 | ▶ | use Net::Amazon::S3::Signature::V2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # ABSTRACT: Create a signed HTTP::Request | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 99 | my $METADATA_PREFIX = 'x-amz-meta-'; | |||||||||||||||||||||||||||
| 17 | 99 | my $AMAZON_HEADER_PREFIX = 'x-amz-'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 99 | enum 'HTTPMethod' => [ qw(DELETE GET HEAD PUT POST) ]; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 99 | with 'Net::Amazon::S3::Role::Bucket'; | |||||||||||||||||||||||||||
| 22 | 99 | has '+bucket' => (required => 0); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 99 | has 's3' => ( is => 'ro', isa => 'Net::Amazon::S3', required => 1 ); | |||||||||||||||||||||||||||
| 25 | 99 | has 'method' => ( is => 'ro', isa => 'HTTPMethod', required => 1 ); | |||||||||||||||||||||||||||
| 26 | 99 | has 'path' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 27 | has 'headers' => | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | ( is => 'ro', isa => 'HashRef', required => 0, default => sub { {} } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 29 | 99 | has 'content' => | |||||||||||||||||||||||||||
| 30 | ( is => 'ro', isa => 'Str|CodeRef|ScalarRef', required => 0, default => '' ); | ||||||||||||||||||||||||||||
| 31 | has 'metadata' => | ||||||||||||||||||||||||||||
| 32 | 430 | ▶ | ( is => 'ro', isa => 'HashRef', required => 0, default => sub { {} } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | has use_virtual_host => ( | ||||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||
| 35 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 36 | lazy => 1, | ||||||||||||||||||||||||||||
| 37 | 263 | ▶ | default => sub { $_[0]->s3->use_virtual_host }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 99 | ); | |||||||||||||||||||||||||||
| 39 | has authorization_method => ( | ||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||
| 41 | isa => 'Str', | ||||||||||||||||||||||||||||
| 42 | lazy => 1, | ||||||||||||||||||||||||||||
| 43 | 218 | ▶ | default => sub { $_[0]->s3->authorization_method }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 99 | ); | |||||||||||||||||||||||||||
| 45 | has region => ( | ||||||||||||||||||||||||||||
| 46 | is => 'ro', | ||||||||||||||||||||||||||||
| 47 | isa => 'Str', | ||||||||||||||||||||||||||||
| 48 | lazy => 1, | ||||||||||||||||||||||||||||
| 49 | 7 | ▶ | default => sub { $_[0]->bucket->region }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 99 | ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 99 | has request_uri => ( | |||||||||||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||||||||||
| 54 | init_arg => undef, | ||||||||||||||||||||||||||||
| 55 | lazy => 1, | ||||||||||||||||||||||||||||
| 56 | builder => '_build_uri', | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 99 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 272 | ▶ | sub _build_uri { | ||||||||||||||||||||||||||
|
Subroutine
_build_uri: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 62 | 272 | my ($self) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 272 | my $path = $self->path; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 272 | ▶ | my $protocol = $self->s3->secure ? 'https' : 'http'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 272 | my $host = $self->s3->host; | |||||||||||||||||||||||||||
| 68 | 272 | my $uri = "$protocol://$host/$path"; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 272 | ▶ | if ($self->use_virtual_host) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | # use https://bucketname.s3.amazonaws.com instead of https://s3.amazonaws.com/bucketname | ||||||||||||||||||||||||||||
| 72 | # see http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html | ||||||||||||||||||||||||||||
| 73 | 263 | $uri =~ s{$host/(.*?)/}{$1.$host/}; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 272 | return $uri; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # make the HTTP::Request object | ||||||||||||||||||||||||||||
| 80 | 218 | ▶ | sub _build_request { | ||||||||||||||||||||||||||
|
Subroutine
_build_request: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 81 | 218 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 218 | my $method = $self->method; | |||||||||||||||||||||||||||
| 84 | 218 | my $headers = $self->headers; | |||||||||||||||||||||||||||
| 85 | 218 | my $content = $self->content; | |||||||||||||||||||||||||||
| 86 | 218 | my $metadata = $self->metadata; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 218 | my $http_headers = $self->_merge_meta( $headers, $metadata ); | |||||||||||||||||||||||||||
| 89 | 218 | my $uri = $self->request_uri; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 218 | my $http_request = HTTP::Request->new( $method, $uri, $http_headers, $content ); | |||||||||||||||||||||||||||
| 92 | 218 | ▶ | $http_request->content_length (0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->s3->vendor->enforce_empty_content_length and not $http_request->content_length
MC/DC50%1/2
$self->s3->vendor->enforce_empty_content_length and not $http_request->content_length
$self->s3->vendor->enforce_empty_content_length
not $http_request->content_length
Truth table66%2/3
A and not(B)
A= $self->s3->vendor->enforce_empty_content_lengthB= not $http_request->content_length
| |||||||||||||||||||||||||||||
| 93 | if $self->s3->vendor->enforce_empty_content_length | ||||||||||||||||||||||||||||
| 94 | && ! $http_request->content_length | ||||||||||||||||||||||||||||
| 95 | ; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 218 | return $http_request; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 210 | ▶ | sub http_request { | ||||||||||||||||||||||||||
|
Subroutine
http_request: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 210 | my $self = shift; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 210 | my $request = $self->_build_request; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 210 | ▶ | $self->authorization_method->new( http_request => $self )->sign_request( $request ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | unless $request->header( 'Authorization' ); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 210 | return $request; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 8 | ▶ | sub query_string_authentication_uri { | ||||||||||||||||||||||||||
|
Subroutine
query_string_authentication_uri: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 8 | my ( $self, $expires ) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 8 | my $request = $self->_build_request; | |||||||||||||||||||||||||||
| 115 | 8 | my $sign = $self->authorization_method->new( http_request => $self ); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 8 | return $sign->sign_uri( $request, $expires ); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 218 | ▶ | sub _merge_meta { | ||||||||||||||||||||||||||
|
Subroutine
_merge_meta: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||
| 121 | 218 | my ( $self, $headers, $metadata ) = @_; | |||||||||||||||||||||||||||
| 122 | 218 | ▶ | $headers ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$headers ||= {}
MC/DC0%0/1
$headers ||= {}
$headers
Truth table50%1/2
A
A= $headers
| |||||||||||||||||||||||||||||
| 123 | 218 | ▶ | $metadata ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$metadata ||= {}
MC/DC0%0/1
$metadata ||= {}
$metadata
Truth table50%1/2
A
A= $metadata
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 218 | my $http_header = HTTP::Headers->new; | |||||||||||||||||||||||||||
| 126 | 218 | ▶ | while ( my ( $k, $v ) = each %$headers ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 228 | $http_header->header( $k => $v ); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 218 | ▶ | while ( my ( $k, $v ) = each %$metadata ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | $http_header->header( "$METADATA_PREFIX$k" => $v ); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 218 | return $http_header; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 99 | 1; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||