| 1 | |||||||||||||||||||||||||||||
| 2 | package AWS::S3::ResponseParser; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use constant DEBUG => $ENV{AWS_S3_DEBUG}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use XML::LibXML::XPathContext; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Log::Any qw( $LOG ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | has 'expect_nothing' => ( | ||||||||||||||||||||||||||||
| 11 | is => 'ro', | ||||||||||||||||||||||||||||
| 12 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 13 | required => 1, | ||||||||||||||||||||||||||||
| 14 | default => 0, | ||||||||||||||||||||||||||||
| 15 | 94 | ▶ | trigger => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 16 | 94 | my ( $self, $expect_nothing) = @_; | |||||||||||||||||||||||||||
| 17 | 94 | ▶ | if ( $expect_nothing ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 34 | my $code = $self->response->code; | |||||||||||||||||||||||||||
| 19 | 34 | ▶ | if ( $code =~ m{^2\d\d} && !$self->response->content ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$code =~ /^2\d\d/ and not $self->response->content
MC/DC50%1/2
$code =~ /^2\d\d/ and not $self->response->content
$code =~ /^2\d\d/
not $self->response->content
Truth table66%2/3
A and not(B)
A= $code =~ /^2\d\d/B= not $self->response->content
| |||||||||||||||||||||||||||||
| 20 | 26 | return; # not sure what jdrago wanted this to do originally | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | else { | ||||||||||||||||||||||||||||
| 23 | 8 | ▶ | if ( $self->_parse_errors() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | # die $self->friendly_error(); | ||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | else { | ||||||||||||||||||||||||||||
| 27 | 0 | return; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 11 | ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 11 | has 'response' => ( | |||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||
| 36 | isa => 'HTTP::Response', | ||||||||||||||||||||||||||||
| 37 | required => 1, | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 11 | has 'type' => ( | |||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||
| 42 | isa => 'Str', | ||||||||||||||||||||||||||||
| 43 | required => 1, | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Responses are not trusted (they may have been tampered with on the network, | ||||||||||||||||||||||||||||
| 47 | # particularly if not using https), so make sure the parser can't be made to | ||||||||||||||||||||||||||||
| 48 | # read local files, make network requests or expand entities. Predefined | ||||||||||||||||||||||||||||
| 49 | # entities (& and friends) are still decoded. | ||||||||||||||||||||||||||||
| 50 | has 'libxml' => ( | ||||||||||||||||||||||||||||
| 51 | is => 'ro', | ||||||||||||||||||||||||||||
| 52 | isa => 'XML::LibXML', | ||||||||||||||||||||||||||||
| 53 | required => 1, | ||||||||||||||||||||||||||||
| 54 | 106 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 106 | return XML::LibXML->new( | |||||||||||||||||||||||||||
| 56 | no_network => 1, | ||||||||||||||||||||||||||||
| 57 | load_ext_dtd => 0, | ||||||||||||||||||||||||||||
| 58 | expand_entities => 0, | ||||||||||||||||||||||||||||
| 59 | expand_xinclude => 0, | ||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||
| 62 | 11 | ); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 11 | has 'error_code' => ( | |||||||||||||||||||||||||||
| 65 | is => 'rw', | ||||||||||||||||||||||||||||
| 66 | isa => 'Str', | ||||||||||||||||||||||||||||
| 67 | required => 0, | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 11 | has 'error_message' => ( | |||||||||||||||||||||||||||
| 71 | is => 'rw', | ||||||||||||||||||||||||||||
| 72 | isa => 'Str', | ||||||||||||||||||||||||||||
| 73 | required => 0, | ||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | has 'xml' => ( | ||||||||||||||||||||||||||||
| 77 | is => 'ro', | ||||||||||||||||||||||||||||
| 78 | isa => 'XML::LibXML::Document', | ||||||||||||||||||||||||||||
| 79 | required => 0, | ||||||||||||||||||||||||||||
| 80 | lazy => 1, | ||||||||||||||||||||||||||||
| 81 | clearer => '_clear_xml', | ||||||||||||||||||||||||||||
| 82 | 28 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 83 | 28 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 28 | my $src = $self->response->content; | |||||||||||||||||||||||||||
| 86 | 28 | print STDERR ">>> AWS Response:\n", $src, "\n" if DEBUG; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 28 | ▶ | return unless $src =~ m/^[[:space:]]*</s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # S3 never sends a DOCTYPE, which is required to declare entities or an | ||||||||||||||||||||||||||||
| 91 | # external DTD, so a response with one is not something we should parse | ||||||||||||||||||||||||||||
| 92 | # whatever the options of the parser are (XXE, entity expansion bombs) | ||||||||||||||||||||||||||||
| 93 | 28 | ▶ | die "Refusing to parse AWS response containing a DOCTYPE declaration\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | if $src =~ m/<!DOCTYPE/i; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 17 | return $self->libxml->parse_string( $src ); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 11 | ); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | has 'xpc' => ( | ||||||||||||||||||||||||||||
| 101 | is => 'ro', | ||||||||||||||||||||||||||||
| 102 | isa => 'XML::LibXML::XPathContext', | ||||||||||||||||||||||||||||
| 103 | required => 0, | ||||||||||||||||||||||||||||
| 104 | lazy => 1, | ||||||||||||||||||||||||||||
| 105 | clearer => '_clear_xpc', | ||||||||||||||||||||||||||||
| 106 | 13 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 107 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | 13 | my $doc = $self->xml; | |||||||||||||||||||||||||||
| 109 | 7 | ▶ | return unless $doc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 7 | my $xpc = XML::LibXML::XPathContext->new( $doc ); | |||||||||||||||||||||||||||
| 112 | 7 | $xpc->registerNs( 's3', 'http://s3.amazonaws.com/doc/2006-03-01/' ); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 7 | return $xpc; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 11 | ); | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | has 'friendly_error' => ( | ||||||||||||||||||||||||||||
| 119 | is => 'ro', | ||||||||||||||||||||||||||||
| 120 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||
| 121 | lazy => 1, | ||||||||||||||||||||||||||||
| 122 | required => 0, | ||||||||||||||||||||||||||||
| 123 | 32 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 124 | 32 | my $s = shift; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 32 | ▶ | return unless $s->error_code || $s->error_message; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$s->error_code or $s->error_message
MC/DC50%1/2
$s->error_code or $s->error_message
$s->error_code
$s->error_message
Truth table66%2/3
A or B
A= $s->error_codeB= $s->error_message
| |||||||||||||||||||||||||||||
| 127 | 5 | $s->type . " call had errors: [" . $s->error_code . "] " . $s->error_message; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 11 | ); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 8 | ▶ | sub _parse_errors { | ||||||||||||||||||||||||||
|
Subroutine
_parse_errors: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 132 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 8 | my $src = $self->response->content; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Do not try to parse non-xml: | ||||||||||||||||||||||||||||
| 137 | 8 | ▶ | unless ( $src =~ m/^[[:space:]]*</s ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # the code is a number the body starts with, otherwise it is the status | ||||||||||||||||||||||||||||
| 140 | # of the response (an empty or text body from, say, a proxy) | ||||||||||||||||||||||||||||
| 141 | 5 | my ( $code ) = $src =~ m/^[[:space:]]*([0-9]+)/; | |||||||||||||||||||||||||||
| 142 | 5 | ▶ | $code = $self->response->code unless defined $code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 5 | $LOG->error('Error response from AWS', {code => $code, msg => $src}); | |||||||||||||||||||||||||||
| 145 | 5 | $self->error_code( $code ); | |||||||||||||||||||||||||||
| 146 | 5 | $self->error_message( $src ); | |||||||||||||||||||||||||||
| 147 | 5 | return 1; | |||||||||||||||||||||||||||
| 148 | } # end unless() | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | ## Originally at this point the re-setting of xpc would happen | ||||||||||||||||||||||||||||
| 151 | ## Does not seem to be needed but it may be a problem area | ||||||||||||||||||||||||||||
| 152 | ## Feel free to delete - Evan Carroll 2012/06/14 | ||||||||||||||||||||||||||||
| 153 | #### $s->_clear_xpc; | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 3 | ▶ | if ( $self->xpc->findnodes( "//Error" ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 2 | my $code = $self->xpc->findvalue( "//Error/Code" ); | |||||||||||||||||||||||||||
| 157 | 2 | my $msg = $self->xpc->findvalue( "//Error/Message" ); | |||||||||||||||||||||||||||
| 158 | 2 | $LOG->error('Error response from AWS', {code => $code, msg => $msg}); | |||||||||||||||||||||||||||
| 159 | 2 | $self->error_code( $code ); | |||||||||||||||||||||||||||
| 160 | 2 | $self->error_message( $msg ); | |||||||||||||||||||||||||||
| 161 | 2 | return 1; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | return 0; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 11 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||