Net/Amazon/S3/Response.pm

filter: Statementstmt 94.1% 80 / 85 Branchbran 73.3% 22 / 30 Conditioncond 90.0% 9 / 10 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 89.6% 26 / 29 Podpod 80.0% 12 / 15 totaltotal 88.0% 154 / 175 SCAR 30.2
CC
20
Cov
89%
findnodes
6.0
is_error
3.0
_build_error_code
3.0
CRAP 20.6
« Net/Amazon/S3.pm ↑ Summary Net/Amazon/S3/Signature/V2.pm »
1 package Net::Amazon::S3::Response;
2 # ABSTRACT: Behaviour common to most S3 responses.
3 99 $Net::Amazon::S3::Response::VERSION = '0.99';
4 99 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 99 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 99 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 99 ▶use XML::LibXML::XPathContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 99 ▶use Net::Amazon::S3::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 99 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 99 has http_response => (
15 is => 'ro',
16 required => 1,
17 handles => [
18 qw[ code ],
19 qw[ message ],
20 qw[ is_success ],
21 qw[ is_redirect ],
22 qw[ status_line ],
23 qw[ content ],
24 qw[ decoded_content ],
25 qw[ header ],
26 qw[ headers ],
27 qw[ header_field_names ],
28 ],
29 );
30  
31 99 has xml_document => (
32 is => 'ro',
33 init_arg => undef,
34 lazy => 1,
35 builder => '_build_xml_document',
36 );
37  
38 99 has xpath_context => (
39 is => 'ro',
40 init_arg => undef,
41 lazy => 1,
42 builder => '_build_xpath_context',
43 );
44  
45 99 has error_code => (
46 is => 'ro',
47 init_arg => undef,
48 lazy => 1,
49 builder => '_build_error_code',
50 );
51  
52 99 has error_message => (
53 is => 'ro',
54 init_arg => undef,
55 lazy => 1,
56 builder => '_build_error_message',
57 );
58  
59 99 has error_resource => (
60 is => 'ro',
61 init_arg => undef,
62 lazy => 1,
63 builder => '_build_error_resource',
64 );
65  
66 99 has error_request_id => (
67 is => 'ro',
68 init_arg => undef,
69 lazy => 1,
70 builder => '_build_error_request_id',
71 );
72  
73 99 has _data => (
74 is => 'ro',
75 init_arg => undef,
76 lazy => 1,
77 builder => '_build_data',
78 );
79  
80 sub _parse_data;
81  
82 sub connection {
83 0 ▶ return $_[0]->header ('Connection');
Subroutine connection: not called CC=1 SCAR=6.9
Pod documented
84 }
85  
86 sub content_length {
87 3 ▶ return $_[0]->http_response->content_length || 0;
Subroutine content_length: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]->http_response->content_length || 0
l!l
21
MC/DC100%1/1
$_[0]->http_response->content_length || 0
$_[0]->http_response->content_length
Truth table100%2/2
A
A= $_[0]->http_response->content_length
Aresult
11
00
88 }
89  
90 sub content_type {
91 209 ▶ return $_[0]->http_response->content_type;
Subroutine content_type: called CC=1 SCAR=0.0
Pod documented
92 }
93  
94 sub date {
95 0 ▶ return $_[0]->header ('Date');
Subroutine date: not called CC=1 SCAR=6.9
Pod undocumented
96 }
97  
98 sub etag {
99 4 ▶ return $_[0]->_decode_etag ($_[0]->header ('ETag'));
Subroutine etag: called CC=1 SCAR=0.0
Pod documented
100 }
101  
102 sub server {
103 1 ▶ return $_[0]->header ('Server');
Subroutine server: called CC=1 SCAR=0.0
Pod documented
104 }
105  
106 sub delete_marker {
107 1 ▶ return $_[0]->header (Net::Amazon::S3::Constants::HEADER_DELETE_MARKER);
Subroutine delete_marker: called CC=1 SCAR=0.0
Pod documented
108 }
109  
110 sub id_2 {
111 1 ▶ return $_[0]->header (Net::Amazon::S3::Constants::HEADER_ID_2);
Subroutine id_2: called CC=1 SCAR=0.0
Pod documented
112 }
113  
114 sub request_id {
115 2 ▶ return $_[0]->header (Net::Amazon::S3::Constants::HEADER_REQUEST_ID);
Subroutine request_id: called CC=1 SCAR=0.0
Pod documented
116 }
117  
118 sub version_id {
119 1 ▶ return $_[0]->header (Net::Amazon::S3::Constants::HEADER_VERSION_ID);
Subroutine version_id: called CC=1 SCAR=0.0
Pod documented
120 }
121  
122 sub is_xml_content {
123 206 ▶ my ($self) = @_;
Subroutine is_xml_content: called CC=2 SCAR=6.9
Pod documented
124  
125 206 ▶ return $self->content_type =~ m:[/+]xml\b: && $self->decoded_content;
Condition100%3/3
$self->content_type =~ m[[/+]xml\b] && $self->decoded_content
!ll&&!rl&&r
1573118
MC/DC100%2/2
$self->content_type =~ m[[/+]xml\b] && $self->decoded_content
$self->content_type =~ m[[/+]xml\b] $self->decoded_content
Truth table100%3/3
A && B
A= $self->content_type =~ m[[/+]xml\b]B= $self->decoded_content
ABresult
0X0
100
111
126 }
127  
128 sub is_error {
129 434 ▶ my ($self) = @_;
Subroutine is_error: called CC=3 SCAR=11.0
Pod documented
130  
131 434 ▶ return 1 if $self->http_response->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->http_response->is_error 343 91 434
132 91 ▶ return 1 if $self->findvalue ('/Error');
Branch: 1 branch
BranchTrueFalseTotal
if $self->findvalue("/Error") 0 91 91
133 91 return;
134 }
135  
136 sub is_internal_response {
137 3 ▶ my ($self) = @_;
Subroutine is_internal_response: called CC=2 SCAR=7.0
Pod documented
138  
139 3 my $header = $self->header ('Client-Warning');
140 3 ▶ return !! ($header && $header eq 'Internal response');
Condition66%2/3
$header && $header eq 'Internal response'
!ll&&!rl&&r
201
MC/DC50%1/2
$header && $header eq 'Internal response'
$header $header eq 'Internal response'
Truth table66%2/3
A && B
A= $headerB= $header eq 'Internal response'
ABresult
0X0
100
111
141 }
142  
143 sub findvalue {
144 239 ▶ my ($self, @path) = @_;
Subroutine findvalue: called CC=2 SCAR=6.9
Pod undocumented
145  
146 239 ▶ return '' unless $self->xpath_context;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->xpath_context 52 187 239
147 187 $self->xpath_context->findvalue (@path);
148 }
149  
150 sub findnodes {
151 0 ▶ my ($self, @path) = @_;
Subroutine findnodes: not called CC=2 SCAR=17.9
Pod undocumented
152  
153 0 ▶ return unless $self->xpath_context;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->xpath_context 0 0 0
154 0 $self->xpath_context->findnodes (@path);
155 }
156  
157 sub _build_data {
158 14 ▶ my ($self) = @_;
Subroutine _build_data: called CC=2 SCAR=7.2
159  
160 14 ▶ return $self->is_success
Branch: 1 branch
BranchTrueFalseTotal
$self->is_success ? : 14 0 14
161 ? $self->_parse_data
162 : undef
163 ;
164 }
165 sub _build_error_code {
166 111 ▶ my ($self) = @_;
Subroutine _build_error_code: called CC=3 SCAR=11.0
167  
168 return
169 111 ▶ unless $self->is_error;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_error 0 111 111
170  
171 111 ▶ return $self->http_response->code
Branch: 1 branch
BranchTrueFalseTotal
unless $self->xpath_context 34 77 111
172 unless $self->xpath_context;
173  
174 77 return $self->findvalue ('/Error/Code');
175 }
176  
177 sub _build_error_message {
178 101 ▶ my ($self) = @_;
Subroutine _build_error_message: called CC=3 SCAR=11.0
179  
180 return
181 101 ▶ unless $self->is_error;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_error 0 101 101
182  
183 101 ▶ return $self->http_response->message
Branch: 1 branch
BranchTrueFalseTotal
unless $self->xpath_context 32 69 101
184 unless $self->xpath_context;
185  
186 69 return $self->findvalue ('/Error/Message');
187 }
188  
189 sub _build_error_resource {
190 2 ▶ my ($self) = @_;
Subroutine _build_error_resource: called CC=3 SCAR=11.0
191  
192 return
193 2 ▶ unless $self->is_error;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_error 0 2 2
194  
195 2 ▶ return "${\ $self->http_response->request->uri }"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->xpath_context 1 1 2
196 unless $self->xpath_context;
197  
198 1 return $self->findvalue ('/Error/Resource');
199 }
200  
201 sub _build_error_request_id {
202 2 ▶ my ($self) = @_;
Subroutine _build_error_request_id: called CC=3 SCAR=11.0
203  
204 return
205 2 ▶ unless $self->is_error;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_error 0 2 2
206  
207 2 ▶ return $self->request_id
Branch: 1 branch
BranchTrueFalseTotal
unless $self->xpath_context 1 1 2
208 unless $self->xpath_context;
209  
210 1 return $self->findvalue ('/Error/RequestId');
211 }
212  
213 sub _build_xml_document {
214 201 ▶ my ($self) = @_;
Subroutine _build_xml_document: called CC=2 SCAR=6.9
215  
216 201 ▶ return unless $self->is_xml_content;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_xml_content 86 115 201
217  
218 # TODO: A 200 OK response can contain valid or invalid XML
219 115 return XML::LibXML->new->parse_string ($self->http_response->decoded_content);
220 }
221  
222 sub _build_xpath_context {
223 201 ▶ my ($self) = @_;
Subroutine _build_xpath_context: called CC=3 SCAR=11.0
224  
225 201 my $doc = $self->xml_document;
226 201 ▶ return unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 86 115 201
227  
228 115 my $xpc = XML::LibXML::XPathContext->new ($doc);
229  
230 115 ▶ my $s3_ns = $doc->documentElement->lookupNamespaceURI
Condition100%2/2
$doc->documentElement->lookupNamespaceURI || 'http://s3.amazonaws.com/doc/2006-03-01/'
l!l
1897
MC/DC100%1/1
$doc->documentElement->lookupNamespaceURI || 'http://s3.amazonaws.com/doc/2006-03-01/'
$doc->documentElement->lookupNamespaceURI
Truth table100%2/2
A
A= $doc->documentElement->lookupNamespaceURI
Aresult
11
00
231 || 'http://s3.amazonaws.com/doc/2006-03-01/';
232 115 $xpc->registerNs (s3 => $s3_ns);
233  
234 115 return $xpc;
235 }
236  
237 sub _decode_etag {
238 4 ▶ my ($self, $etag) = @_;
Subroutine _decode_etag: called CC=1 SCAR=0.0
239  
240 4 $etag =~ s/ (?:^") | (?:"$) //gx;
241 4 return $etag;
242 }
243  
244 99 1;
245  
246 __END__
247  
248 =pod
249  
250 =encoding utf-8
251  
252 =head1 NAME
253  
254 Net::Amazon::S3::Response - Behaviour common to most S3 responses.
255  
256 =head1 VERSION
257  
258 version 0.99
259  
260 =head1 SYNOPSIS
261  
262 package Command::Response;
263 extends 'Net::Amazon::S3::Response';
264  
265 ...
266 my $response = Command::Response->new (
267 http_response => $http_response,
268 );
269  
270 =head1 DESCRIPTION
271  
272 Response handler base class providing functionality common to most S3 responses.
273  
274 =head1 EXTENDING
275  
276 L<Net::Amazon::S3::Response> provides methods to cache response data.
277  
278 =over
279  
280 =item _data
281  
282 Read-only accessor initialized by C<_build_data>
283  
284 =item _build_data
285  
286 Data builder, by default calls C<_parse_data> if response is success and provides
287 valid XML document.
288  
289 =item _parse_data
290  
291 Abstract (undefined in parent) method to be implemented by children.
292  
293 =back
294  
295 =head1 METHODS
296  
297 =head2 Constructor
298  
299 Constructor accepts only one (required) parameter - C<http_response>.
300 It should act like L<HTTP::Response>.
301  
302 =head2 Response classification methods
303  
304 =over
305  
306 =item is_success
307  
308 True if response is a success response, false otherwise.
309  
310 Successful response may contain invalid XML.
311  
312 =item is_redirect
313  
314 True if response is a redirect.
315  
316 =item is_error
317  
318 True if response is an error response, false otherwise.
319  
320 Response is considered to be an error either when response code is an HTTP
321 error (4xx or 5xx) or response content is an error XML document.
322  
323 See also L<"S3 Error Response"|https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html>
324 for more details.
325  
326 =item is_internal_response
327  
328 True if response is generated by user agent itself (eg: Cannot connect)
329  
330 =item is_xml_content
331  
332 True if response data is a valid XML document
333  
334 =back
335  
336 =head2 Error handling
337  
338 Apart error classifition L<Net::Amazon::S3::Response> provides also common
339 error data accessors.
340  
341 Error data are available only in case of error response.
342  
343 =over
344  
345 =item error_code
346  
347 Either content of C<Error/Code> XML element or HTTP response code.
348  
349 =item error_message
350  
351 Either content of C<Error/Message> XML element or HTTP response message.
352  
353 =item error_request_id
354  
355 Content of C<Error/RequestId> XML element if available, C<x-amz-request-id> header
356 if available, empty list otherwise.
357  
358 =item error_resource
359  
360 Content of c<Error/Resource> if available, request uri otherwise.
361  
362 =back
363  
364 =head2 Common Response Headers
365  
366 See L<"S3 Common Response Headers"|https://docs.aws.amazon.com/AmazonS3/latest/API/RESTCommonResponseHeaders.html>
367 for more details.
368  
369 =over
370  
371 =item content_length
372  
373 =item content_type
374  
375 =item connection
376  
377 =item etag
378  
379 ETag with trimmed leading/trailing quotes.
380  
381 =item server
382  
383 =item delete_marker
384  
385 =item request_id
386  
387 =item id_2
388  
389 =item version_id
390  
391 =back
392  
393 =head2 XML Document parsing
394  
395 =over
396  
397 =item xml_document
398  
399 Lazy built instance of L<XML::LibXML>.
400  
401 Available only if response is XML response and contains valid XML document.
402  
403 =item xpath_context
404  
405 Lazy built instance of L<XML::LibXML::XPathContext>.
406  
407 Available only if response is XML response and contains valid XML document
408  
409 =back
410  
411 =head2 HTTP Response methods
412  
413 Further methods delegated to C<http_response>.
414 Refer L<HTTP::Response> for description.
415  
416 =over
417  
418 =item code
419  
420 =item message
421  
422 =item status_line
423  
424 =item content
425  
426 =item decoded_content
427  
428 =item header
429  
430 =item headers
431  
432 =item header_field_names
433  
434 =back
435  
436 =head1 AUTHOR
437  
438 Branislav Zahradník <barney@cpan.org>
439  
440 =head1 COPYRIGHT AND LICENSE
441  
442 This module is part of L<Net::Amazon::S3>.
443  
444 =head1 AUTHOR
445  
446 Branislav Zahradník <barney@cpan.org>
447  
448 =head1 COPYRIGHT AND LICENSE
449  
450 This software is copyright (c) 2021 by Amazon Digital Services, Leon Brocard, Brad Fitzpatrick, Pedro Figueiredo, Rusty Conover, Branislav Zahradník.
451  
452 This is free software; you can redistribute it and/or modify it under
453 the same terms as the Perl 5 programming language system itself.
454  
455 =cut
« Net/Amazon/S3.pm ↑ Summary Net/Amazon/S3/Signature/V2.pm »