Net/Amazon/S3/HTTPRequest.pm

filter: Statementstmt 97.6% 83 / 85 Branchbran 75.0% 9 / 12 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 94.7% 18 / 19 Podpod 100% 2 / 2 totaltotal 90.7% 117 / 129 SCAR 23.1
CC
10
Cov
92%
_merge_meta
5.3
_build_uri
3.0
_build_request
3.0
CRAP 10.0
« Net/Amazon/S3/Signature.pm ↑ Summary Net/Amazon/S3/Request.pm »
1 package Net::Amazon::S3::HTTPRequest;
2 99 $Net::Amazon::S3::HTTPRequest::VERSION = '0.99';
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 sub _build_uri {
62 272 ▶ my ($self) = @_;
Subroutine _build_uri: called CC=3 SCAR=11.0
63  
64 272 my $path = $self->path;
65  
66 272 ▶ my $protocol = $self->s3->secure ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
$self->s3->secure ? : 272 0 272
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
BranchTrueFalseTotal
if ($self->use_virtual_host) 263 9 272
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 sub _build_request {
81 218 ▶ my $self = shift;
Subroutine _build_request: called CC=3 SCAR=11.0
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
BranchTrueFalseTotal
if $self->s3->vendor->enforce_empty_content_length and not $http_request->content_length 172 46 218
Condition66%2/3
$self->s3->vendor->enforce_empty_content_length and not $http_request->content_length
!ll&&!rl&&r
046172
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
ABresult
0X0
100
111
93 if $self->s3->vendor->enforce_empty_content_length
94 && ! $http_request->content_length
95 ;
96  
97 218 return $http_request;
98 }
99  
100 sub http_request {
101 210 ▶ my $self = shift;
Subroutine http_request: called CC=2 SCAR=7.0
Pod documented
102  
103 210 my $request = $self->_build_request;
104  
105 210 ▶ $self->authorization_method->new( http_request => $self )->sign_request( $request )
Branch: 1 branch
BranchTrueFalseTotal
unless $request->header("Authorization") 210 0 210
106 unless $request->header( 'Authorization' );
107  
108 210 return $request;
109 }
110  
111 sub query_string_authentication_uri {
112 8 ▶ my ( $self, $expires ) = @_;
Subroutine query_string_authentication_uri: called CC=1 SCAR=0.0
Pod documented
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 sub _merge_meta {
121 218 ▶ my ( $self, $headers, $metadata ) = @_;
Subroutine _merge_meta: called CC=5 SCAR=16.7
122 218 ▶ $headers ||= {};
Condition50%1/2
$headers ||= {}
l!l
2180
MC/DC0%0/1
$headers ||= {}
$headers
Truth table50%1/2
A
A= $headers
Aresult
11
00
123 218 ▶ $metadata ||= {};
Condition50%1/2
$metadata ||= {}
l!l
2180
MC/DC0%0/1
$metadata ||= {}
$metadata
Truth table50%1/2
A
A= $metadata
Aresult
11
00
124  
125 218 my $http_header = HTTP::Headers->new;
126 218 ▶ while ( my ( $k, $v ) = each %$headers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 228 218 446
127 228 $http_header->header( $k => $v );
128 }
129 218 ▶ while ( my ( $k, $v ) = each %$metadata ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$metadata) 0 218 218
130 0 $http_header->header( "$METADATA_PREFIX$k" => $v );
131 }
132  
133 218 return $http_header;
134 }
135  
136 99 1;
137  
138 __END__
139  
140 =pod
141  
142 =encoding UTF-8
143  
144 =head1 NAME
145  
146 Net::Amazon::S3::HTTPRequest - Create a signed HTTP::Request
147  
148 =head1 VERSION
149  
150 version 0.99
151  
152 =head1 SYNOPSIS
153  
154 my $http_request = Net::Amazon::S3::HTTPRequest->new(
155 s3 => $self->s3,
156 method => 'PUT',
157 path => $self->bucket . '/',
158 headers => $headers,
159 content => $content,
160 )->http_request;
161  
162 =head1 DESCRIPTION
163  
164 This module creates an HTTP::Request object that is signed
165 appropriately for Amazon S3.
166  
167 =for test_synopsis no strict 'vars'
168  
169 =head1 METHODS
170  
171 =head2 http_request
172  
173 This method creates, signs and returns a HTTP::Request object.
174  
175 =head2 query_string_authentication_uri
176  
177 This method creates, signs and returns a query string authentication
178 URI.
179  
180 =head1 AUTHOR
181  
182 Branislav Zahradník <barney@cpan.org>
183  
184 =head1 COPYRIGHT AND LICENSE
185  
186 This software is copyright (c) 2021 by Amazon Digital Services, Leon Brocard, Brad Fitzpatrick, Pedro Figueiredo, Rusty Conover, Branislav Zahradník.
187  
188 This is free software; you can redistribute it and/or modify it under
189 the same terms as the Perl 5 programming language system itself.
190  
191 =cut
« Net/Amazon/S3/Signature.pm ↑ Summary Net/Amazon/S3/Request.pm »