| 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 | sub BUILD { | ||||||||||||||||||||||||||||
| 38 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head2 request(%options) | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||
| 48 | sub request { | ||||||||||||||||||||||||||||
| 49 | 6 | ▶ | my ($self, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
request: called CC=11 SCAR=24.7
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 6 | ▶ | $options{method} = 'GET' if !defined $options{method}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 6 | foreach my $req (qw/bucket path/) { | |||||||||||||||||||||||||||
| 54 | 10 | ▶ | die "$req parameter required" if !defined $options{$req}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 4 | ▶ | if ($self->auto_region && $response->code == 400) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->auto_region and $response->code == 400
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 75 | 1 | my $region = $1; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | print "# set region to: $region\n" if $ENV{AWS_S3_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 1 | $self->region($region); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | $response = request(@_); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | ▶ | if ($response->is_success()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | sub _uri { | ||||||||||||||||||||||||||||
| 95 | 4 | ▶ | my ($self, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_uri: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||