blib/lib/HTTP/Promise/Response.pm untested

filter: Statementstmt 0.0% 0 / 197 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 96 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 434 CC 42 SCAR 39.2
CC
42
Cov
0%
freshness_lifetime
132.0
current_age
110.0
filename
90.0
CRAP 50.4
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Response.pm
3 ## Version v0.1.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/03/21
7 ## Modified 2022/03/21
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package HTTP::Promise::Response;
14 BEGIN
15 {
16 use strict;
17 use warnings;
18 warnings::register_categories( 'HTTP::Promise' );
19 use parent qw( HTTP::Promise::Message );
20 use vars qw( $DEFAULT_PROTOCOL $VERSION );
21 use HTTP::Promise::Status;
22 our $DEFAULT_PROTOCOL = 'HTTP/1.0';
23 our $VERSION = 'v0.1.0';
24 };
25  
26 use strict;
27 use warnings;
28  
29 sub init
30 {
31 my $self = shift( @_ );
32 my( $code, $status );
33 if( @_ )
34 {
35 if( @_ == 1 && ref( $_[0] ) eq 'HASH' )
36 {
37 my $opts = $_[0];
38 ( $code, $status ) = CORE::delete( @$opts{qw( code status )} );
39 }
40 elsif( @_ >= 2 &&
41 defined( $_[0] ) &&
42 $_[0] =~ /^\d{3}$/ &&
43 ( ( defined( $_[1] ) && $_[1] =~ /\S/ ) || !defined( $_[1] ) ) )
44 {
45 ( $code, $status ) = splice( @_, 0, 2 );
46 }
47 else
48 {
49 return( $self->error( "Invalid parameters received. I was expecting either an hash reference or at least a code and status, but instead got: ", join( ", ", map( defined( $_ ) ? "'$_'" : 'undef', @_ ) ) ) );
50 }
51 }
52 # properties headers and content are set by our parent class HTTP::Promise::Message
53 $self->{code} = $code;
54 $self->{cookies} = [];
55 $self->{default_protocol} = $DEFAULT_PROTOCOL;
56 $self->{previous} = undef;
57 $self->{request} = undef;
58 $self->{status} = $status;
59 $self->{uri} = undef;
60 $self->{version} = '';
61 $self->{_init_strict_use_sub} = 1;
62 $self->{_init_params_order} = [qw( content headers )];
63 # $self->SUPER::init( ( defined( $headers ) ? $headers : () ), @_ ) || return( $self->pass_error );
64 $self->SUPER::init( @_ ) || return( $self->pass_error );
65 return( $self );
66 }
67  
68 sub base
69 {
70 my $self = shift( @_ );
71 my $base = (
72 $self->header( 'Content-Base' ), # used to be HTTP/1.1
73 $self->header( 'Content-Location' ), # HTTP/1.1
74 $self->header( 'Base' ), # HTTP/1.0
75 )[0];
76 $self->_load_class( 'URI', { version => 5.10 } ) || return( $self->pass_error );
77 if( $base && $base =~ /^[a-zA-Z][a-zA-Z0-9.+\-]*:/ )
78 {
79 # already absolute
80 return( URI->new( $base ) );
81 }
82  
83 my $req = $self->request;
84 if( $req )
85 {
86 # if $base is undef here, the return value is effectively
87 # just a copy of $self->request->uri.
88 return( URI->new_abs( $base, $req->uri ) );
89 }
90 # Cannot find an absolute base
91 return;
92 }
93  
94 sub clone
95 {
96 my $self = shift( @_ );
97 my $new = $self->SUPER::clone;
98 my $code = $self->code;
99 my $status = $self->status;
100 my $prev = $self->previous;
101 my $req = $self->request;
102 $new->code( $code );
103 $new->status( $status );
104 $new->previous( undef );
105 # $new->previous( $prev ) if( defined( $prev ) );
106 $new->request( $req->clone ) if( defined( $req ) );
107 return( $new );
108 }
109  
110 sub code { return( shift->_set_get_number( 'code', @_ ) ); }
111  
112 sub current_age
113 {
114 my $self = shift( @_ );
115 my $time = shift( @_ );
116  
117 my $h_client_date = $self->client_date;
118 my $h_date = $self->date;
119 # Implementation of RFC 2616 section 13.2.3
120 # (age calculations)
121 my $response_time = $h_client_date->epoch if( $h_client_date );
122 my $date = $h_date->epoch if( $h_date );
123  
124 my $age = 0;
125 if( $response_time && $date )
126 {
127 # apparent_age
128 $age = $response_time - $date;
129 $age = 0 if( $age < 0 );
130 }
131  
132 my $age_v = $self->header( 'Age' );
133 if( $age_v && $age_v > $age )
134 {
135 # corrected_received_age
136 $age = $age_v;
137 }
138  
139 if( $response_time )
140 {
141 my $request = $self->request;
142 if( $request )
143 {
144 my $req_date = $request->date;
145 my $request_time = $req_date->epoch if( $req_date );
146 if( $request_time && $request_time < $response_time )
147 {
148 # Add response_delay to age to get 'corrected_initial_age'
149 $age += $response_time - $request_time;
150 }
151 }
152 $age += ( $time || time ) - $response_time;
153 }
154 return( $age );
155 }
156  
157 sub default_protocol { return( shift->_set_get_scalar_as_object( 'default_protocol', @_ ) ); }
158  
159 sub dump
160 {
161 my $self = shift( @_ );
162 my $status_line = $self->status_line;
163 my $proto = $self->protocol;
164 $status_line = "$proto $status_line" if( $proto );
165 return( $self->SUPER::dump( preheader => $status_line, @_ ) );
166 }
167  
168 sub filename
169 {
170 my $self = shift( @_ );
171 my $file;
172 my $dispo = $self->header( 'Content-Disposition' );
173 # e.g.: attachment; filename="filename.jpg"
174 # form-data; name="fieldName"; filename="filename.jpg"
175 # Ref: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition>
176 if( $dispo )
177 {
178 my $cd = $self->headers->new_field( 'Content-Disposition' => "$dispo" );
179 return( $self->pass_error( $self->headers->error ) ) if( !defined( $cd ) );
180 $file = $cd->filename;
181 }
182  
183 unless( defined( $file ) && length( $file ) )
184 {
185 $self->_load_class( 'URI', { version => 5.10 } ) || return( $self->pass_error );
186 my $uri;
187 if( my $cl = $self->header( 'Content-Location' ) )
188 {
189 $uri = URI->new( $cl );
190 }
191 elsif( my $request = $self->request )
192 {
193 $uri = $request->uri;
194 }
195  
196 if( $uri )
197 {
198 my $f = $self->new_file( $uri->path );
199 $file = $f->basename;
200 }
201 }
202  
203 if( $file )
204 {
205 # basename
206 $file =~ s,.*[\\/],,;
207 }
208  
209 if( $file && !length( $file ) )
210 {
211 $file = undef;
212 }
213  
214 return( $file );
215 }
216  
217 sub fresh_until
218 {
219 my $self = shift( @_ );
220 my $opts = $self->_get_args_as_hash( @_ );
221 $opts->{time} ||= time;
222 my $f = $self->freshness_lifetime( %$opts );
223 return unless( defined( $f ) );
224 return( $f - $self->current_age( $opts->{time} ) + $opts->{time} );
225 }
226  
227 sub freshness_lifetime
228 {
229 my $self = shift( @_ );
230 my $opts = $self->_get_args_as_hash( @_ );
231  
232 # First look for the Cache-Control: max-age=n header
233 for my $cc ( $self->header( 'Cache-Control' ) )
234 {
235 next if( !defined( $cc ) || !length( "$cc" ) );
236 for my $cc_dir ( split( /[[:blank:]\h]*,[[:blank:]\h]*/, $cc ) )
237 {
238 return( $1 ) if( $cc_dir =~ /^max-age[[:blank:]\h]*=[[:blank:]\h]*(\d+)/i );
239 }
240 }
241  
242 my $h_date = $self->date;
243 my $h_client_date = $self->client_date;
244 # Next possibility is to look at the "Expires" header
245 my $date = ( $h_date ? $h_date->epoch : '' ) ||
246 ( $h_client_date ? $h_client_date->epoch : '' ) ||
247 $opts->{time} || time;
248 if( my $expires = $self->expires )
249 {
250 return( $expires->epoch - $date );
251 }
252  
253 # Must apply heuristic expiration
254 return if( exists( $opts->{heuristic_expiry} ) && !$opts->{heuristic_expiry} );
255  
256 # Default heuristic expiration parameters
257 $opts->{h_min} ||= 60;
258 $opts->{h_max} ||= 24 * 3600;
259 # 10% since last-mod suggested by RFC2616
260 $opts->{h_lastmod_fraction} ||= 0.10;
261 $opts->{h_default} ||= 3600;
262  
263 # Should give a warning if more than 24 hours according to
264 # RFC 2616 section 13.2.4. Here we just make this the default
265 # maximum value.
266 if( my $last_modified = $self->last_modified )
267 {
268 my $h_exp = ( $date - $last_modified ) * $opts->{h_lastmod_fraction};
269 return( $opts->{h_min} ) if( $h_exp < $opts->{h_min} );
270 return( $opts->{h_max} ) if( $h_exp > $opts->{h_max} );
271 return( $h_exp );
272 }
273  
274 # default when all else fails
275 return( $opts->{h_min} ) if( $opts->{h_min} > $opts->{h_default} );
276 return( $opts->{h_default} );
277 }
278  
279 sub is_client_error { return( HTTP::Promise::Status->is_client_error( shift->code ) ); }
280  
281 sub is_error { return( HTTP::Promise::Status->is_error( shift->code ) ); }
282  
283 sub is_fresh
284 {
285 my $self = shift( @_ );
286 my $opts = $self->_get_args_as_hash( @_ );
287 $opts->{time} ||= time;
288 my $f = $self->freshness_lifetime( %$opts );
289 return unless( defined( $f ) );
290 return( $f > $self->current_age( $opts->{time} ) );
291 }
292  
293 sub is_info { return( HTTP::Promise::Status->is_info( shift->code ) ); }
294  
295 sub is_redirect { return( HTTP::Promise::Status->is_redirect( shift->code ) ); }
296  
297 sub is_server_error { return( HTTP::Promise::Status->is_server_error( shift->code ) ); }
298  
299 sub is_success { return( HTTP::Promise::Status->is_success( shift->code ) ); }
300  
301 sub parse
302 {
303 my $self = shift( @_ );
304 my $str = shift( @_ );
305 warn( 'Undefined argument to ' . ( ref( $self ) || $self ) . '->parse()' ) if( !defined( $str ) && warnings::enabled( 'HTTP::Promise' ) );
306 $self->clear_error;
307 if( !defined( $str ) || !length( $str ) )
308 {
309 return( ref( $self ) ? $self : $self->new );
310 }
311 $str = $self->new_scalar( $str );
312 $self->_load_class( 'HTTP::Promise::Parser' ) || return( $self->pass_error );
313 my $p = HTTP::Promise::Parser->new;
314 my $ent = $p->parse( $str ) || return( $self->pass_error( $p->error ) );
315 return( $ent->http_message );
316 }
317  
318 sub previous { return( shift->_set_get_object_without_init( 'previous', 'HTTP::Promise::Message', @_ ) ); }
319  
320 sub redirects
321 {
322 my $self = shift( @_ );
323 my $reds = $self->new_array;
324 my $r = $self;
325 while( my $p = $r->previous )
326 {
327 $reds->unshift( $p );
328 $r = $p;
329 }
330 return( $reds );
331 }
332  
333 sub request { return( shift->_set_get_object_without_init( 'request', 'HTTP::Promise::Request', @_ ) ); }
334  
335 # See rfc7230, section 3.1 <https://tools.ietf.org/html/rfc7230#section-3.1>
336 sub start_line
337 {
338 my $self = shift( @_ );
339 my $eol = shift( @_ );
340 $eol = "\n" if( !defined( $eol ) );
341 my $status_line = $self->status_line;
342 my $proto = $self->protocol || 'HTTP/1.1';
343 my $resp_line = "$proto $status_line";
344 return( $resp_line );
345 }
346  
347 sub status { return( shift->_set_get_scalar_as_object( 'status', @_ ) ); }
348  
349 sub status_line
350 {
351 my $self = shift( @_ );
352 my $code = $self->code || "000";
353 my $status = $self->status || HTTP::Promise::Status->status_message( $code ) || 'Unknown code';
354 return( "$code $status" );
355 }
356  
357 sub uri { return( shift->_set_get_uri( { field => 'uri', class => 'URI' }, @_ ) ); }
358  
359 # NOTE: sub FREEZE is inherited
360  
361 # NOTE: sub STORABLE_freeze is inherited
362  
363 # NOTE: sub STORABLE_thaw is inherited
364  
365 # NOTE: sub THAW is inherited
366  
367 1;
368 # NOTE: POD
369 __END__
370  
371 =encoding utf-8
372  
373 =head1 NAME
374  
375 HTTP::Promise::Response - HTTP Response Class
376  
377 =head1 SYNOPSIS
378  
379 use HTTP::Promise::Response;
380 my $resp = HTTP::Promise::Response->new ||
381 die( HTTP::Promise::Response->error, "\n" );
382  
383 =head1 VERSION
384  
385 v0.1.0
386  
387 =head1 DESCRIPTION
388  
389 L<HTTP::Promise::Response> implements a similar interface to L<HTTP::Response>, but does not inherit from it. It uses a different API internally and relies on XS modules for speed while offering more features.
390  
391 L<HTTP::Promise::Response> inherits from L<HTTP::Promise::Message>
392  
393 One major difference with C<HTTP::Response> is that the HTTP response content is not necessarily stored in memory, but it relies on L<HTTP::Promise::Body> as you can see below, and this class has 2 subclasses: 1 storing data in memory when the size is reasonable (threshold set by you) and 1 storing data in a file on the filesystem for larger content.
394  
395 Here is how it fits in overall relation with other classes.
396
397 +-------------------------+ +--------------------------+
398 | | | |
399 | HTTP::Promise::Request | | HTTP::Promise::Response |
400 | | | |
401 +------------|------------+ +-------------|------------+
402 | |
403 | |
404 | |
405 | +------------------------+ |
406 | | | |
407 +--- HTTP::Promise::Message |---+
408 | |
409 +------------|-----------+
410 |
411 |
412 +------------|-----------+
413 | |
414 | HTTP::Promise::Entity |
415 | |
416 +------------|-----------+
417 |
418 |
419 +------------|-----------+
420 | |
421 | HTTP::Promise::Body |
422 | |
423 +------------------------+
424  
425 =head1 CONSTRUCTOR
426  
427 =head2 new
428  
429 my $resp = HTTP::Promise::Response->new( $code, $status, $headers, $content,
430 host => 'example.com',
431 uri => 'https://example.com/somewhere',
432 );
433 my $resp = HTTP::Promise::Response->new( $code, $status, $headers, $content, {
434 host => 'example.com',
435 uri => 'https://example.com/somewhere',
436 });
437 my $resp = HTTP::Promise::Response->new( $code, $status, $headers,
438 host => 'example.com',
439 uri => 'https://example.com/somewhere',
440 );
441 my $resp = HTTP::Promise::Response->new( $code, $status, $headers, {
442 host => 'example.com',
443 uri => 'https://example.com/somewhere',
444 });
445  
446 Provided with an HTTP code, HTTP status, an optional set of headers, as either an array reference or a L<HTTP::Promise::Headers> or a L<HTTP::Headers> object, some optional content and an optional hash reference of options (as the last or only parameter), and this instantiates a new L<HTTP::Promise::Response> object. The supported arguments are as follow. Each arguments can be set or changed later using the method with the same name.
447  
448 It returns the newly created object upon success, and upon error, such as bad argument provided, this sets an L<error|Module::Generic/error> and returns C<undef>
449  
450 =over 4
451  
452 =item 1. C<$code>
453  
454 An integer representing the status code, such as C<101> (switching protocol).
455  
456 =item 2. C<$status>
457  
458 The status string, such as C<Switching Protocol>.
459  
460 =item 3. C<$headers>
461  
462 Either an array reference of header-value pairs, or an L<HTTP::Promise::Headers> object or an L<HTTP::Headers> object.
463  
464 If an array reference is provided, an L<HTTP::Promise::Headers> object will be instantiated with it.
465  
466 For example::
467  
468 my $r = HTTP::Promise::Response->new( $code, $status, [
469 'Content-Type' => 'text/html; charset=utf-8',
470 Content_Encoding => 'gzip',
471 ]);
472  
473 =item 4. C<$content>
474  
475 C<$content> can either be a string, a scalar reference, or an L<HTTP::Promise::Body> object (L<HTTP::Promise::Body::File> and L<HTTP::Promise::Body::Scalar>)
476  
477 =back
478  
479 Each supported option below can also be set using its corresponding method.
480  
481 Supported options are:
482  
483 =over 4
484  
485 =item * C<code>
486  
487 Same as C<$code> above.
488  
489 =item * C<content>
490  
491 Same as C<$content> above.
492  
493 =item * C<headers>
494  
495 Same as C<$headers> above.
496  
497 =item * C<protocol>
498  
499 The HTTP protocol, such as C<HTTP/1.1> or C<HTTP/2>
500  
501 =item * C<status>
502  
503 Same as C<$status> above.
504  
505 =item * C<version>
506  
507 The HTTP protocol version. Defaults to C<1.17>
508  
509 =back
510  
511 =head1 METHODS
512  
513 =head2 add_content
514  
515 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/add_content>
516  
517 =head2 add_content_utf8
518  
519 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/add_content_utf8>
520  
521 =head2 add_part
522  
523 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/add_part>
524  
525 =head2 as_string
526  
527 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/as_string>
528  
529 =head2 base
530  
531 Returns the base URI as an L<URI> object if it can find one, or C<undef> otherwise.
532  
533 =head2 boundary
534  
535 This is inherited from L<HTTP::Promise::Message> and returns the multipart boundary currently set in the C<Content-Type> header.
536  
537 =head2 can
538  
539 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/can>
540  
541 =head2 clear
542  
543 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/clear>
544  
545 =head2 clone
546  
547 This clones the current object and returns the clone version.
548  
549 =head2 code
550  
551 Sets or gets the HTTP response C<code>. This returns a L<number object|Module::Generic::Number>
552  
553 =head2 content
554  
555 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/content>
556  
557 Use this method with care, because it will stringify the request body, thus loading it into memory, which could potentially be important if the body size is large. Maybe you can check the body size first? Something like:
558  
559 my $content;
560 $content = $r->content if( $r->body->length < 102400 );
561  
562 =head2 content_charset
563  
564 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/content_charset>
565  
566 =head2 content_ref
567  
568 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/content_ref>
569  
570 =head2 current_age
571  
572 Calculates the "current age" of the response as specified by L<rfc2616, section 13.2.3|https://tools.ietf.org/html/rfc2616#section-13.2.3>.
573  
574 The age of a response is the time since it was sent by the origin server.
575 The returned value is a number representing the age in seconds.
576  
577 =head2 decodable
578  
579 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/decodable>
580  
581 =head2 decode
582  
583 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/decode>
584  
585 =head2 decode_content
586  
587 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/decode_content>
588  
589 =head2 decoded_content
590  
591 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/decoded_content>
592  
593 =head2 decoded_content_utf8
594  
595 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/decoded_content_utf8>
596  
597 =head2 default_protocol
598  
599 Sets or gets the default HTTP protocol to use. This defaults to C<HTTP/1.0>
600  
601 =head2 dump
602  
603 This dumps the HTTP response and prints it on the C<STDOUT> in void context, or returns a string of it.
604  
605 =head2 encode
606  
607 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/encode>
608  
609 =head2 entity
610  
611 Sets or gets an L<HTTP::Promise::Entity> object.
612  
613 This object is automatically created upon instantiation of the HTTP request, and if you also provide some content when creating a new object, an L<HTTP::Promise::Body> object will also be created.
614  
615 =head2 filename
616  
617 Returns a possible filename, if any, for this response.
618  
619 To achieve this, it tries different approaches:
620  
621 =over 4
622  
623 =item 1. C<Content-Disposition> header
624  
625 It will check in the C<Content-Disposition> header of the response to see if there ia a C<filename> attribute, or a C<filename*> attribute (as defined in L<rfc2231|https://tools.ietf.org/html/rfc2231>)
626  
627 For example:
628  
629 Content-Disposition: form-data; name="myfile"; filename*=UTF-8''%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.txt
630  
631 or
632  
633 Content-Disposition: form-data; name="myfile"; filename*=UTF-8'ja-JP'%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.txt
634  
635 In the above example, note the C<*> after the attribute name C<filename>. It is not a typo and part of the L<rfc2231 standard|https://tools.ietf.org/html/rfc2231>
636  
637 or encoded with quoted-printable
638  
639 Content-Disposition: attachment; filename="=?UTF-8?Q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB.txt?="
640  
641 or encoded with base64
642  
643 Content-Disposition: attachment; filename="=?UTF-8?B?44OV44Kh44Kk44OrLnR4dAo?="
644  
645 Here the filename would be C<ファイル.txt> (i.e. "file.txt" in Japanese)
646  
647 =item 2. C<Content-Location> header
648  
649 It will use the base filename of the URI.
650  
651 =item 3. C<request URI>
652  
653 If there was an initial request URI, it will use the URI base filename.
654  
655 This might not be the original request URI, because there might have been some redirect responses first.
656  
657 =back
658  
659 Whatever filename found is returned as-is. You need to be careful there are no dangerous characters in it before relying on it as part of a filepath.
660  
661 If nothing is found, C<undef> is returned.
662  
663 =head2 fresh_until
664  
665 Returns the time (in seconds since epoch) when this entity is no longer fresh.
666  
667 Options might be passed to control expiry heuristics. See the description of L</freshness_lifetime>.
668  
669 =head2 freshness_lifetime
670  
671 Calculates the "freshness lifetime" of the response as specified by L<rfc2616, section 13.2.4|https://tools.ietf.org/html/rfc2616#section-13.2.4> and updated by L<rfc7234, section 4.2|https://tools.ietf.org/html/rfc7234#section-4.2>.
672  
673 The "freshness lifetime" is the length of time between the generation of a response and its expiration time.
674 The returned value is the number of seconds until expiry.
675  
676 If the response does not contain an C<Expires> or a C<Cache-Control> header, then this function will apply some simple heuristic based on the C<Last-Modified> header to determine a suitable lifetime. The following options might be passed to control the heuristics:
677  
678 =over 4
679  
680 =item * C<heuristic_expiry>
681  
682 Boolean. If set to a false value, do not apply heuristics and just return C<undef> when C<Expires> or C<Cache-Control> field is lacking.
683  
684 =item * C<h_lastmod_fraction>
685  
686 Integer. This number represent the fraction of the difference since the C<Last-Modified> timestamp to make the expiry time.
687  
688 The default is C<0.10>, the suggested typical setting of 10% in L<rfc2616|https://tools.ietf.org/html/rfc2616>.
689  
690 =item * C<h_min>
691  
692 Integer representing seconds. This is the lower limit of the heuristic expiry age to use.
693 The default is C<60> (1 minute).
694  
695 =item * C<h_max>
696  
697 Integer representing seconds. This is the upper limit of the heuristic expiry age to use.
698 The default is C<86400> (24 hours).
699  
700 =item * C<h_default>
701  
702 Integer representing seconds. This is the expiry age to use when nothing else applies.
703  
704 The default is C<3600> (1 hour) or C<h_min> if greater.
705  
706 =back
707  
708 =head2 header
709  
710 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/header>
711  
712 =head2 headers
713  
714 Sets or gets a L<HTTP::Promise::Headers> object.
715  
716 A header object is always created upon instantiation, whether you provided headers fields or not.
717  
718 =head2 headers_as_string
719  
720 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/headers_as_string>
721  
722 =head2 is_client_error
723  
724 Returns true if the L</code> corresponds to a client error, which typically is a code from C<400> to C<499>, or false otherwise.
725  
726 See also L<HTTP::Promise::Status/is_client_error>
727  
728 =head2 is_encoding_supported
729  
730 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/is_encoding_supported>
731  
732 =head2 is_error
733  
734 Returns true if the L</code> corresponds to an error (client error or server error), which typically is a code from C<400> to C<599>, or false otherwise.
735  
736 See also L<HTTP::Promise::Status/is_error>
737  
738 =head2 is_fresh
739  
740 Returns true if the response is fresh, based on the values of L</freshness_lifetime> and L</current_age>.
741 If the response is no longer fresh, then it has to be re-fetched or re-validated by the origin server.
742  
743 Options might be passed to control expiry heuristics, see the description of L</freshness_lifetime>.
744  
745 =head2 is_info
746  
747 Returns true if the L</code> corresponds to an informational code, which typically is a code from C<100> to C<199>, or false otherwise.
748  
749 See also L<HTTP::Promise::Status/is_info>
750  
751 =head2 is_redirect
752  
753 Returns true if the L</code> corresponds to a redirection, which typically is a code from C<300> to C<399>, or false otherwise.
754  
755 See also L<HTTP::Promise::Status/is_redirect>
756  
757 =head2 is_server_error
758  
759 Returns true if the L</code> corresponds to a server error, which typically is a code from C<500> to C<599>, or false otherwise.
760  
761 See also L<HTTP::Promise::Status/is_server_error>
762  
763 =head2 is_success
764  
765 Returns true if the L</code> corresponds to a successful response, which typically is a code from C<200> to C<299>, or false otherwise.
766  
767 See also L<HTTP::Promise::Status/is_success>
768  
769 =head2 make_boundary
770  
771 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/make_boundary>
772  
773 =head2 parse
774  
775 Provided with a scalar reference of data, a glob or a file path, and an hash or hash reference of options and this will parse the data provided using L<HTTP::Promise::Parser/parse>, passing it whatever options has been provided. See L<HTTP::Promise::Parser/parse_fh> for the supported options.
776  
777 This returns the resulting L<HTTP::Promise::Message> object from the parsing, or, upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
778  
779 Note that the resulting L<HTTP::Promise::Message> object can be a L<HTTP::Promise::Request> or L<HTTP::Promise::Response> object (both of which inherits from L<HTTP::Promise::Message>) if a start-line was found, or else just an L<HTTP::Promise::Message> object.
780  
781 =head2 parts
782  
783 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/parts>
784  
785 =head2 previous
786  
787 Sets or gets an L<HTTP::Promise::Message> object corresponding to the previous HTTP query. This is used to keep track of redirection.
788  
789 =head2 protocol
790  
791 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/protocol>
792  
793 =head2 redirects
794  
795 Returns an L<array object|Module::Generic::Array> of redirect responses that lead up to this response by following the C<$r->previous> chain. The list order is oldest first.
796  
797 For example:
798  
799 my $reds = $r->redirects;
800 say "Number of redirects: ", $reds->length;
801  
802 =head2 request
803  
804 Sets or gets the L<HTTP::Promise::Request> related to this response.
805  
806 It is not necessarily the same request passed to the L<HTTP::Promise/request>, because there might have been redirects and authorisation retries in between.
807  
808 =head2 start_line
809  
810 Returns a string representing the start-line containing the L<protocol|/protocol>, the L<code|/code> and the L<status|/status> of the response.
811  
812 For example:
813  
814 GET / HTTP/1.1
815  
816 See L<rfc7230, section 3.1|https://tools.ietf.org/html/rfc7230#section-3.1>
817  
818 =head2 status
819  
820 Sets or gets the response status string, such as C<OK> for code C<200>. This returns a L<scalar object|Module::Generic::Scalar>
821  
822 =head2 status_line
823  
824 Returns a regular string made of the L</code> and the L</status>. If no status is set, this will guess it from L<HTTP::Promise::Status/status_message>
825  
826 =head2 version
827  
828 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/version>
829  
830 =head1 THREAD-SAFETY
831  
832 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
833  
834 =head1 AUTHOR
835  
836 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
837  
838 =head1 SEE ALSO
839  
840 L<HTTP::Promise>, L<HTTP::Promise::Request>, L<HTTP::Promise::Response>, L<HTTP::Promise::Message>, L<HTTP::Promise::Entity>, L<HTTP::Promise::Headers>, L<HTTP::Promise::Body>, L<HTTP::Promise::Body::Form>, L<HTTP::Promise::Body::Form::Data>, L<HTTP::Promise::Body::Form::Field>, L<HTTP::Promise::Status>, L<HTTP::Promise::MIME>, L<HTTP::Promise::Parser>, L<HTTP::Promise::IO>, L<HTTP::Promise::Stream>, L<HTTP::Promise::Exception>
841  
842 =head1 COPYRIGHT & LICENSE
843  
844 Copyright(c) 2022 DEGUEST Pte. Ltd.
845  
846 All rights reserved
847 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
848  
849 =cut