Simple/HTTP.pm

filter: Statementstmt 97.2% 35 / 36 Branchbran 65.0% 13 / 20 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 84.0% 58 / 69 CC 14 SCAR 20.0
CC
14
Cov
85%
request
11.8
BUILD
3.1
_uri
2.1
CRAP 7.4
↑ Summary Simple/Object/File.pm »
1 package Net::AmazonS3::Simple::HTTP;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Class::Tiny qw(http_client signer auto_region region secure host);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Net::AmazonS3::Simple::HTTP - request formater and caller
12  
13 =head1 SYNOPSIS
14  
15 =head1 DESCRIPTION
16  
17 =head1 METHODS
18  
19 =head2 new(%attributes)
20  
21 =head3 %attributes
22  
23 =head4 http_client
24  
25 =head4 signer
26  
27 =head4 auto_region
28  
29 =head4 region
30  
31 =head4 secure
32  
33 =head4 host
34  
35 =cut
36  
37 2 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.2
Pod undocumented
38 2 my ($self) = @_;
39  
40 2 foreach my $req ( qw/http_client signer auto_region region secure host/ ) {
41 12 ▶ die "$req attribute required" if! defined $self->$req;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->$req 0 12 12
42 }
43 }
44  
45 =head2 request(%options)
46  
47 =cut
48 6 ▶sub request {
Subroutine request: called CC=11 SCAR=24.7
49 6 my ($self, %options) = @_;
50  
51 6 ▶ $options{method} = 'GET' if !defined $options{method};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"method"} 5 1 6
52  
53 6 foreach my $req (qw/bucket path/) {
54 10 ▶ die "$req parameter required" if !defined $options{$req};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{$req} 2 8 10
55 }
56  
57 my $request = HTTP::Request->new(
58 $options{method},
59 4 $self->_uri(%options),
60 ['x-amz-content-sha256' => 'UNSIGNED-PAYLOAD']
61 );
62  
63 4 $self->signer->sign($request, $self->region, 'UNSIGNED-PAYLOAD');
64  
65 4 ▶ print $request->as_string() . "\n" if $ENV{AWS_S3_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AWS_S3_DEBUG"} 4 0 4
66  
67 4 my $response = $self->http_client->request($request, $options{ content_to_file });
68  
69 4 ▶ print $response->as_string() . "\n" if $ENV{AWS_S3_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AWS_S3_DEBUG"} 4 0 4
70  
71 4 ▶ if ($self->auto_region && $response->code == 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_region and $response->code == 400) 1 3 4
Condition66%2/3
$self->auto_region and $response->code == 400
!ll&&!rl&&r
031
MC/DC50%1/2
$self->auto_region and $response->code == 400
$self->auto_region $response->code == 400
Truth table66%2/3
A and B
A= $self->auto_regionB= $response->code == 400
ABresult
0X0
100
111
72 #I know - XML don't be parsed with regex, but for this simple case I do it
73 #maybe with next versions I use XML::LibXML;)
74 1 ▶ if ($response->content() =~ m{<Region>([-\w]+)</Region>}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->content =~ m[<Region>([-\w]+)</Region>]) 1 0 1
75 1 my $region = $1;
76  
77 1 ▶ print "# set region to: $region\n" if $ENV{AWS_S3_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AWS_S3_DEBUG"} 1 0 1
78 1 $self->region($region);
79  
80 1 $response = request(@_);
81 }
82 }
83  
84 4 ▶ if ($response->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 4 0 4
85 4 return $response;
86 }
87  
88 0 die sprintf
89 "Unknown response %s:\n:%s",
90 $response->message,
91 $response->content;
92 }
93  
94 4 ▶sub _uri {
Subroutine _uri: called CC=2 SCAR=7.2
95 4 my ($self, %options) = @_;
96  
97 return sprintf
98 '%s://%s.%s/%s',
99 $self->secure ? 'https' : 'http',
100 $options{bucket},
101 $self->host,
102 4 ▶ $options{path};
Branch: 1 branch
BranchTrueFalseTotal
$self->secure ? : 4 0 4
103 }
104  
105 =head1 LICENSE
106  
107 Copyright (C) Avast Software.
108  
109 This library is free software; you can redistribute it and/or modify
110 it under the same terms as Perl itself.
111  
112 =head1 AUTHOR
113  
114 Jan Seidl E<lt>seidl@avast.comE<gt>
115  
116 =cut
117  
118 2 1;
119  
↑ Summary Simple/Object/File.pm »