File Coverage

blib/lib/AWS/S3/ResponseParser.pm
Criterion Covered Total %
statement 30 31 96.7
branch 3 4 75.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 39 41 95.1


line stmt bran cond sub pod time code
1              
2             package AWS::S3::ResponseParser;
3              
4 6     6   54 use constant DEBUG => $ENV{AWS_S3_DEBUG};
  6         11  
  6         537  
5 6     6   40 use Moose;
  6         14  
  6         65  
6 6     6   51346 use XML::LibXML;
  6         204781  
  6         45  
7 6     6   1618 use XML::LibXML::XPathContext;
  6         14  
  6         236  
8 6     6   34 use Log::Any qw( $LOG );
  6         15  
  6         63  
9              
10             has 'expect_nothing' => (
11             is => 'ro',
12             isa => 'Bool',
13             required => 1,
14             default => 0,
15             trigger => sub {
16             my ( $self, $expect_nothing) = @_;
17             if ( $expect_nothing ) {
18             my $code = $self->response->code;
19             if ( $code =~ m{^2\d\d} && !$self->response->content ) {
20             return; # not sure what jdrago wanted this to do originally
21             }
22             else {
23             if ( $self->_parse_errors() ) {
24             # die $self->friendly_error();
25             }
26             else {
27             return;
28             }
29             }
30             }
31             }
32             );
33              
34             has 'response' => (
35             is => 'ro',
36             isa => 'HTTP::Response',
37             required => 1,
38             );
39              
40             has 'type' => (
41             is => 'ro',
42             isa => 'Str',
43             required => 1,
44             );
45              
46             has 'libxml' => (
47             is => 'ro',
48             isa => 'XML::LibXML',
49             required => 1,
50             default => sub { return XML::LibXML->new() },
51             );
52              
53             has 'error_code' => (
54             is => 'rw',
55             isa => 'Str',
56             required => 0,
57             );
58              
59             has 'error_message' => (
60             is => 'rw',
61             isa => 'Str',
62             required => 0,
63             );
64              
65             has 'xml' => (
66             is => 'ro',
67             isa => 'XML::LibXML::Document',
68             required => 0,
69             lazy => 1,
70             clearer => '_clear_xml',
71             default => sub {
72             my $self = shift;
73              
74             my $src = $self->response->content;
75             print STDERR ">>> AWS Response:\n", $src, "\n" if DEBUG;
76              
77             return unless $src =~ m/^[[:space:]]*</s;
78             return $self->libxml->parse_string( $src );
79             }
80             );
81              
82             has 'xpc' => (
83             is => 'ro',
84             isa => 'XML::LibXML::XPathContext',
85             required => 0,
86             lazy => 1,
87             clearer => '_clear_xpc',
88             default => sub {
89             my $self = shift;
90             my $doc = $self->xml;
91             return unless $doc;
92              
93             my $xpc = XML::LibXML::XPathContext->new( $doc );
94             $xpc->registerNs( 's3', 'http://s3.amazonaws.com/doc/2006-03-01/' );
95              
96             return $xpc;
97             }
98             );
99              
100             has 'friendly_error' => (
101             is => 'ro',
102             isa => 'Maybe[Str]',
103             lazy => 1,
104             required => 0,
105             default => sub {
106             my $s = shift;
107              
108             return unless $s->error_code || $s->error_message;
109             $s->type . " call had errors: [" . $s->error_code . "] " . $s->error_message;
110             }
111             );
112              
113             sub _parse_errors {
114 2     2   4 my $self = shift;
115              
116 2         88 my $src = $self->response->content;
117              
118             # Do not try to parse non-xml:
119 2 100       30 unless ( $src =~ m/^[[:space:]]*</s ) {
120 1         6 ( my $code = $src ) =~ s/^[[:space:]]*\([0-9]*\).*$/$1/s;
121 1         53 $LOG->error('Error response from AWS', {code => $code, msg => $src});
122 1         102 $self->error_code( $code );
123 1         64 $self->error_message( $src );
124 1         43 return 1;
125             } # end unless()
126              
127             ## Originally at this point the re-setting of xpc would happen
128             ## Does not seem to be needed but it may be a problem area
129             ## Feel free to delete - Evan Carroll 2012/06/14
130             #### $s->_clear_xpc;
131              
132 1 50       41 if ( $self->xpc->findnodes( "//Error" ) ) {
133 1         250 my $code = $self->xpc->findvalue( "//Error/Code" );
134 1         181 my $msg = $self->xpc->findvalue( "//Error/Message" );
135 1         83 $LOG->error('Error response from AWS', {code => $code, msg => $msg});
136 1         76 $self->error_code( $code );
137 1         46 $self->error_message( $msg );
138 1         43 return 1;
139             }
140              
141 0           return 0;
142             }
143              
144             __PACKAGE__->meta->make_immutable;