StorageClient.pm untested

filter: Statementstmt 0.0% 0 / 143 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 33 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 236 CC 16 SCAR 31.6
CC
16
Cov
0%
request
72.0
sign
20.0
_adjust_path
20.0
CRAP 23.6
1 package Net::Azure::StorageClient;
2 use strict;
3 use warnings;
4 {
5 $Net::Azure::StorageClient::VERSION = '0.5';
6 }
7 use LWP::UserAgent;
8 use HTTP::Date;
9 use URI::QueryParam;
10 use MIME::Base64;
11 use Digest::SHA qw( hmac_sha256_base64 );
12  
13 sub new {
14 my $class = shift;
15 my %args = @_;
16 my $type = $args{ type };
17 if ( $type && $type =~ /^Blob$/i ) { # |Table|Queue
18 $type = ucfirst( $type );
19 $class .= "::" . $type;
20 eval "use $class;";
21 # die "Unsupported StorageClient $class: $@" if $@;
22 }
23 my $obj = bless {}, $class;
24 $obj->{ type } = lc( $type ) if $type;
25 $obj->init( @_ );
26 }
27  
28 sub init {
29 my $storageClient = shift;
30 my %args = @_;
31 $storageClient->{ account_name } = $args{ account_name };
32 $storageClient->{ primary_access_key } = $args{ primary_access_key };
33 $storageClient->{ api_version } = $args{ api_version } || '2012-02-12';
34 $storageClient->{ protocol } = $args{ protocol } || 'https';
35 return $storageClient;
36 }
37  
38 sub sign {
39 my $storageClient = shift;
40 my ( $req, $params ) = @_;
41 my $key = $storageClient->{ primary_access_key };
42 my $api_version = $storageClient->{ api_version };
43 $req->header( 'x-ms-version', $api_version );
44 $req->header( 'x-ms-date', HTTP::Date::time2str() );
45 if ( my $data = $params->{ body } ) {
46 require Digest::MD5;
47 $req->header( 'Content-MD5', Digest::MD5::md5_base64( $data ) . '==' );
48 # $req->header( 'If-None-Match', '*' );
49 }
50 if ( $params && ( my $headers = $params->{ headers } ) ) {
51 for my $key ( keys %$headers ) {
52 $req->header( $key, $headers->{ $key } );
53 }
54 }
55 my $canonicalized_headers = join '', map { lc( $_ ) . ':' .
56 $req->header( $_ ) . "\n" } sort grep { /^x-ms/ } keys %{ $req->headers };
57 my $account = $req->uri->authority;
58 $account =~ s/^([^.]*).*$/$1/;
59 my $path = $req->uri->path;
60 my $canonicalized_resource = "/${account}${path}";
61 $canonicalized_resource .= join '', map { "\n" . lc( $_ ) . ':' .
62 join( ',', sort $req->uri->query_param( $_ ) ) }
63 sort $req->uri->query_param;
64 my $method = $req->method;
65 my $encoding = $req->header( 'Content-Encoding' ) || '';
66 my $language = $req->header( 'Content-Language' ) || '';
67 my $length = $req->header( 'Content-Length' );
68 if (! defined $length ) {
69 $length = '';
70 }
71 my $md5 = $req->header( 'Content-MD5' ) || '';
72 my $content_type = $req->header( 'Content-Type' ) || '';
73 my $date = $req->header( 'Date' ) || '';
74 my $if_mod_since = $req->header( 'If-Modified-Since' ) || '';
75 my $if_match = $req->header( 'If-Match' ) || '';
76 my $if_none_match = $req->header( 'If-None-Match' ) || '';
77 my $if_unmod_since = $req->header( 'If-Unmodified-Since' ) || '';
78 my $range = $req->header( 'Range' ) || '';
79 my @headers = ( $method, $encoding, $language, $length, $md5, $content_type, $date,
80 $if_mod_since, $if_match, $if_none_match, $if_unmod_since, $range );
81 push ( @headers, "${canonicalized_headers}${canonicalized_resource}" );
82 my $string_to_sign = join( "\n", @headers );
83 # print $string_to_sign;
84 my $signature = hmac_sha256_base64( $string_to_sign, decode_base64( $key ) );
85 $signature .= '=' x ( 4 - ( length( $signature ) % 4 ) );
86 $req->authorization( "SharedKey ${account}:${signature}" );
87 return $req;
88 }
89  
90 sub request {
91 my $storageClient = shift;
92 my ( $method, $url, $params ) = @_;
93 $url = '' unless ( $url );
94 if ( $url !~ m!^https{0,1}://! ) {
95 if ( $url !~ m !^/! ) {
96 $url = '/' . $url;
97 }
98 my $type = $storageClient->{ type };
99 my $account = $storageClient->{ account_name };
100 my $protocol = $storageClient->{ protocol };
101 $url = "${protocol}://${account}.${type}.core.windows.net${url}";
102 }
103 my $body;
104 if ( defined( $params->{ body } ) ) {
105 $body = $params->{ body };
106 }
107 $method = 'GET' unless $method;
108 my $req = new HTTP::Request( $method => $url );
109 $req->content_length( length( $body ) ) if defined $body;
110 $req = $storageClient->sign( $req, $params );
111 $req->content( $body ) if defined $body;
112 my $ua = LWP::UserAgent->new;
113 return $ua->request( $req );
114 }
115  
116 sub get {
117 my $storageClient = shift;
118 $storageClient->request( 'GET', @_ );
119 }
120  
121 sub head {
122 my $storageClient = shift;
123 $storageClient->request( 'HEAD', @_ );
124 }
125  
126 sub put {
127 my $storageClient = shift;
128 $storageClient->request( 'PUT', @_ );
129 }
130  
131 sub delete {
132 my $storageClient = shift;
133 $storageClient->request( 'DELETE', @_ );
134 }
135  
136 sub post {
137 my $storageClient = shift;
138 $storageClient->request( 'POST', @_ );
139 }
140  
141 sub _signed_identifier {
142 my $storageClient = shift;
143 my $length = shift;
144 my @char = () ;
145 push @char, ( 'a' .. 'z' );
146 push @char, ( 'A' .. 'Z' );
147 push @char, ( 0 .. 9 );
148 my $res = '';
149 for ( my $i=1; $i <= $length; $i++ ) {
150 $res .= $char[ int( rand( $#char + 1 ) ) ];
151 }
152 return $res;
153 }
154  
155 sub _adjust_path {
156 my $storageClient = shift;
157 my $path = shift;
158 $path =~ s!^/!!;
159 if ( my $type = $storageClient->{ type } ) {
160 my $arg;
161 if ( $type eq 'blob' ) {
162 $arg = 'container_name'
163 }
164 # table, queue...
165 if ( $arg && ( my $root = $storageClient->{ $arg } ) ) {
166 $path = $root . '/' . $path;
167 }
168 }
169 return $path;
170 }
171  
172 1;
173  
174 __END__