StorageClient.pm

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