Amazon/S3/Util.pm

filter: Statementstmt 88.7% 87 / 98 Branchbran 45.0% 9 / 20 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 90.0% 18 / 20 Podpod 0.0% 0 / 8 totaltotal 72.2% 120 / 166 CC 15 SCAR 12.2
CC
15
Cov
78%
choose
12.0
create_grant_header
6.0
create_api_uri
4.3
CRAP 3.4
« Amazon/S3/Signature/V4.pm ↑ Summary Amazon/S3/Logger.pm »
1 package Amazon::S3::Util;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use Amazon::S3::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Digest::MD5 qw(md5 md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Digest::MD5::File qw(file_md5 file_md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use URI::Escape qw(uri_escape_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 13 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 13 our @EXPORT_OK = qw(
19 choose
20 create_query_string
21 create_grant_header
22 create_xml_request
23 create_api_uri
24 set_md5_header
25 urlencode
26 get_parameters
27 );
28  
29 13 our $VERSION = '2.1.4';
30  
31 13 our %EXPORT_TAGS;
32  
33 13 $EXPORT_TAGS{all} = [@EXPORT_OK];
34  
35 ########################################################################
36 0 ▶sub choose(&) { ## no critic
Subroutine choose: not called CC=3 SCAR=24.8
Pod undocumented
37 ########################################################################
38 0 my $code = shift;
39  
40 # Pass the correct context down to the block
41 0 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
elsif (defined wantarray) { } 0 0 0
42 0 return $code->();
43 }
44 elsif ( defined wantarray ) {
45 0 return scalar $code->();
46 }
47 else {
48 0 $code->();
49 0 return;
50 }
51 }
52  
53 ########################################################################
54 21 ▶sub urlencode {
Subroutine urlencode: called CC=2 SCAR=7.1
Pod undocumented
55 ########################################################################
56 21 my (@args) = @_;
57  
58 21 ▶ my $unencoded = ref $args[0] ? $args[1] : $args[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 21 21
59  
60 ## no critic (RequireInterpolation)
61 21 return uri_escape_utf8( $unencoded, '^A-Za-z0-9\-\._~\x2f' );
62 }
63  
64 # hashref or list of key/value pairs
65 ########################################################################
66 6 ▶sub create_query_string {
Subroutine create_query_string: called CC=3 SCAR=11.4
Pod undocumented
67 ########################################################################
68 6 my (@args) = @_;
69  
70 6 my $parameters = get_parameters(@args);
71  
72 return $EMPTY
73 6 ▶ if !$parameters || !keys %{$parameters};
Branch: 1 branch
BranchTrueFalseTotal
unless $parameters and keys %{$parameters;} 0 6 6
Condition33%1/3
$parameters and keys %{$parameters;}
!ll&&!rl&&r
006
MC/DC0%0/2
$parameters and keys %{$parameters;}
$parameters keys %{$parameters;}
Truth table33%1/3
A and B
A= $parametersB= keys %{$parameters;}
ABresult
0X0
100
111
74  
75 11 return join $AMPERSAND, map { sprintf '%s=%s', $_, urlencode( $parameters->{$_} ) }
76 6 keys %{$parameters};
77 }
78  
79 ########################################################################
80 2 ▶sub create_api_uri {
Subroutine create_api_uri: called CC=4 SCAR=14.7
Pod undocumented
81 ########################################################################
82 2 my (@args) = @_;
83  
84 2 my $parameters = get_parameters(@args);
85  
86 2 my $path = delete $parameters->{path};
87 2 ▶ $path //= $EMPTY;
Condition33%1/3
$path //= $Amazon::S3::Util::EMPTY
l!l&&r!l&&!r
200
MC/DC0%0/2
$path //= $Amazon::S3::Util::EMPTY
$path $Amazon::S3::Util::EMPTY
Truth table33%1/3
A //= B
A= $pathB= $Amazon::S3::Util::EMPTY
ABresult
1X1
011
000
88  
89 2 ▶ if ( $path !~ /\/$/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ m[/$]msx) 2 0 2
90 2 $path = "$path/";
91 }
92  
93 2 my $api = delete $parameters->{api};
94 2 ▶ $api //= $EMPTY;
Condition33%1/3
$api //= $Amazon::S3::Util::EMPTY
l!l&&r!l&&!r
020
MC/DC0%0/2
$api //= $Amazon::S3::Util::EMPTY
$api $Amazon::S3::Util::EMPTY
Truth table33%1/3
A //= B
A= $apiB= $Amazon::S3::Util::EMPTY
ABresult
1X1
011
000
95  
96 2 my $query_string = create_query_string($parameters);
97  
98 2 return sprintf '%s?%s%s', $path, $api, $query_string;
99 }
100  
101 ########################################################################
102 4 ▶sub create_xml_request {
Subroutine create_xml_request: called CC=2 SCAR=7.0
Pod undocumented
103 ########################################################################
104 4 my ( $request, $content_key ) = @_;
105  
106 4 ▶ if ( !$content_key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content_key) 4 0 4
107 4 ($content_key) = keys %{$request};
108 }
109  
110 4 $request->{$content_key}->{xmlns} = $S3_XMLNS;
111  
112 4 return XMLout(
113 $request,
114 NSExpand => $TRUE,
115 KeyAttr => [],
116 KeepRoot => $TRUE,
117 ContentKey => $content_key,
118 NoAttr => $TRUE,
119 XMLDecl => $XMLDECL,
120 );
121 }
122 ########################################################################
123 6 ▶sub set_md5_header {
Subroutine set_md5_header: called CC=4 SCAR=13.9
Pod undocumented
124 ########################################################################
125 6 my (@args) = @_;
126  
127 6 my $parameters = get_parameters(@args);
128  
129 6 my ( $content, $headers ) = @{$parameters}{qw(data headers)};
130  
131 6 my $md5 = eval {
132 6 ▶ if ( ref($content) && reftype($content) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content and builtin::reftype($content) eq 'SCALAR') { } 1 5 6
Condition66%2/3
ref $content and builtin::reftype($content) eq "SCALAR"
!ll&&!rl&&r
501
MC/DC50%1/2
ref $content and builtin::reftype($content) eq "SCALAR"
ref $content builtin::reftype($content) eq "SCALAR"
Truth table66%2/3
A and B
A= ref $contentB= builtin::reftype($content) eq "SCALAR"
ABresult
0X0
100
111
133  
134 1 $headers->{'Content-Length'} = -s ${$content};
135 1 my $md5_hex = file_md5_hex( ${$content} );
136  
137 1 return encode_base64( pack 'H*', $md5_hex );
138 }
139 else {
140 5 $headers->{'Content-Length'} = length $content;
141  
142 5 my $md5 = md5($content);
143  
144 5 my $md5_hex = unpack 'H*', $md5;
145  
146 5 return encode_base64($md5);
147 }
148 };
149  
150 6 ▶ die "$EVAL_ERROR"
Branch: 1 branch
BranchTrueFalseTotal
if $Amazon::S3::Util::EVAL_ERROR 0 6 6
151 if $EVAL_ERROR;
152  
153 6 chomp $md5;
154  
155 6 $headers->{'Content-MD5'} = $md5;
156  
157 6 return;
158 }
159  
160 # grant:
161 # full-control
162 # read
163 # read-acp
164 # write
165 # write-acp
166 #
167 # type:
168 # id
169 # uri
170 # emailAddress
171  
172 ########################################################################
173 0 ▶sub create_grant_header {
Subroutine create_grant_header: not called CC=2 SCAR=17.9
Pod undocumented
174 ########################################################################
175 0 my ( $grant, $type, @args ) = @_;
176  
177 0 ▶ my $values = ref $args[0] ? $args[0] : \@args;
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 0 0
178  
179 return {
180 "x-amz-grant-$grant" => join ', ',
181 0 map { sprintf qq{$type="%s"}, $_ } @{$values}
182 };
183 }
184  
185 #########################################################################
186 43 ▶sub get_parameters { return ref $_[0] ? $_[0] : {@_}; }
Subroutine get_parameters: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 15 28 43
187 ########################################################################
188  
189 13 1;
« Amazon/S3/Signature/V4.pm ↑ Summary Amazon/S3/Logger.pm »