File Coverage

lib/Amazon/S3/Util.pm
Criterion Covered Total %
statement 36 87 41.3
branch 0 16 0.0
condition 0 12 0.0
subroutine 12 19 63.1
pod 0 7 0.0
total 48 141 34.0


line stmt bran cond sub pod time code
1             package Amazon::S3::Util;
2              
3 2     2   13 use strict;
  2         3  
  2         66  
4 2     2   7 use warnings;
  2         2  
  2         85  
5              
6 2     2   7 use Amazon::S3::Constants qw(:all);
  2         1  
  2         482  
7 2     2   1040 use Data::Dumper;
  2         13333  
  2         196  
8 2     2   19 use Digest::MD5 qw(md5 md5_hex);
  2         3  
  2         116  
9 2     2   919 use Digest::MD5::File qw(file_md5 file_md5_hex);
  2         135327  
  2         13  
10 2     2   542 use English qw(-no_match_vars);
  2         3  
  2         21  
11 2     2   2241 use MIME::Base64;
  2         1756  
  2         143  
12 2     2   17 use Scalar::Util qw(reftype);
  2         3  
  2         98  
13 2     2   12 use URI::Escape qw(uri_escape_utf8);
  2         4  
  2         139  
14 2     2   1912 use XML::Simple;
  2         24497  
  2         16  
15              
16 2     2   203 use parent qw(Exporter);
  2         3  
  2         20  
17              
18             our @EXPORT_OK = qw(
19             create_query_string
20             create_grant_header
21             create_xml_request
22             create_api_uri
23             set_md5_header
24             urlencode
25             get_parameters
26             );
27              
28             our %EXPORT_TAGS;
29              
30             $EXPORT_TAGS{all} = [@EXPORT_OK];
31              
32             ########################################################################
33             sub urlencode {
34             ########################################################################
35 0     0 0   my (@args) = @_;
36              
37 0 0         my $unencoded = ref $args[0] ? $args[1] : $args[0];
38              
39             ## no critic (RequireInterpolation)
40 0           return uri_escape_utf8( $unencoded, '^A-Za-z0-9\-\._~\x2f' );
41             }
42              
43             # hashref or list of key/value pairs
44             ########################################################################
45             sub create_query_string {
46             ########################################################################
47 0     0 0   my (@args) = @_;
48              
49 0           my $parameters = get_parameters(@args);
50              
51             return $EMPTY
52 0 0 0       if !$parameters || !keys %{$parameters};
  0            
53              
54             return join $AMPERSAND,
55 0           map { sprintf '%s=%s', $_, urlencode( $parameters->{$_} ) }
56 0           keys %{$parameters};
  0            
57             }
58              
59             ########################################################################
60             sub create_api_uri {
61             ########################################################################
62 0     0 0   my (@args) = @_;
63              
64 0           my $parameters = get_parameters(@args);
65              
66 0           my $path = delete $parameters->{path};
67 0   0       $path //= $EMPTY;
68              
69 0 0         if ( $path !~ /\/$/xsm ) {
70 0           $path = "$path/";
71             }
72              
73 0           my $api = delete $parameters->{api};
74 0   0       $api //= $EMPTY;
75              
76 0           my $query_string = create_query_string($parameters);
77              
78 0           return sprintf '%s?%s%s', $path, $api, $query_string;
79             }
80              
81             ########################################################################
82             sub create_xml_request {
83             ########################################################################
84 0     0 0   my ( $request, $content_key ) = @_;
85              
86 0 0         if ( !$content_key ) {
87 0           ($content_key) = keys %{$request};
  0            
88             }
89              
90 0           $request->{$content_key}->{xmlns} = $S3_XMLNS;
91              
92 0           return XMLout(
93             $request,
94             NSExpand => $TRUE,
95             KeyAttr => [],
96             KeepRoot => $TRUE,
97             ContentKey => $content_key,
98             NoAttr => $TRUE,
99             XMLDecl => $XMLDECL,
100             );
101             }
102             ########################################################################
103             sub set_md5_header {
104             ########################################################################
105 0     0 0   my (@args) = @_;
106              
107 0           my $parameters = get_parameters(@args);
108              
109 0           my ( $content, $headers ) = @{$parameters}{qw(data headers)};
  0            
110              
111 0           my $md5 = eval {
112 0 0 0       if ( ref($content) && reftype($content) eq 'SCALAR' ) {
113              
114 0           $headers->{'Content-Length'} = -s ${$content};
  0            
115 0           my $md5_hex = file_md5_hex( ${$content} );
  0            
116              
117 0           return encode_base64( pack 'H*', $md5_hex );
118             }
119             else {
120 0           $headers->{'Content-Length'} = length $content;
121              
122 0           my $md5 = md5($content);
123              
124 0           my $md5_hex = unpack 'H*', $md5;
125              
126 0           return encode_base64($md5);
127             }
128             };
129              
130 0 0         die "$EVAL_ERROR"
131             if $EVAL_ERROR;
132              
133 0           chomp $md5;
134              
135 0           $headers->{'Content-MD5'} = $md5;
136              
137 0           return;
138             }
139              
140             # grant:
141             # full-control
142             # read
143             # read-acp
144             # write
145             # write-acp
146             #
147             # type:
148             # id
149             # uri
150             # emailAddress
151              
152             ########################################################################
153             sub create_grant_header {
154             ########################################################################
155 0     0 0   my ( $grant, $type, @args ) = @_;
156              
157 0 0         my $values = ref $args[0] ? $args[0] : \@args;
158              
159             return {
160             "x-amz-grant-$grant" => join ', ',
161 0           map { sprintf qq{$type="%s"}, $_ } @{$values}
  0            
  0            
162             };
163             }
164              
165             #########################################################################
166 0 0   0 0   sub get_parameters { return ref $_[0] ? $_[0] : {@_}; }
167             ########################################################################
168              
169             1;