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