blib/lib/HTTP/Caching.pm

filter: Statementstmt 16.5% 57 / 344 Branchbran 5.2% 9 / 170 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 34.8% 15 / 43 Podpod n/a totaltotal 13.5% 81 / 600 CC 102 SCAR 66.4
CC
102
Cov
12%
_may_reuse_from_cache
1482.0
_may_store_in_cache
1406.0
_retrieve
110.0
CRAP 762.9
↑ Summary
1 package HTTP::Caching;
2  
3 =head1 NAME
4  
5 HTTP::Caching - The RFC 7234 compliant brains to do caching right
6  
7 =head1 VERSION
8  
9 Version 0.05
10  
11 =cut
12  
13 our $VERSION = '0.09';
14  
15 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use HTTP::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use HTTP::Status ':constants';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use List::MoreUtils qw{ any };
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use Monkey::Patch::Action;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 8 ▶use MooX::Types::MooseLike::Base ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 our $DEBUG = 0;
31  
32 8 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 Readonly my $REUSE_NO_MATCH => 0; # mismatch of headers etc
35 Readonly my $REUSE_IS_OK => 1;
36 Readonly my $REUSE_IS_STALE => 2;
37 Readonly my $REUSE_REVALIDATE => 4;
38 Readonly my $REUSE_IS_STALE_OK => $REUSE_IS_STALE | $REUSE_IS_OK;
39 Readonly my $REUSE_IS_STALE_REVALIDATE => $REUSE_IS_STALE | $REUSE_REVALIDATE;
40  
41 =head1 SYNOPSIS
42  
43 my $chi_cache = CHI->new(
44 driver => 'File',
45 root_dir => '/tmp/HTTP_Caching',
46 file_extension => '.cache',
47 l1_cache => {
48 driver => 'Memory',
49 global => 1,
50 max_size => 1024*1024
51 }
52 );
53
54 my $ua = LWP::UserAgent->new();
55
56 my $http_caching = HTTP::Caching->new(
57 cache => $chi_cache,
58 cache_type => 'private',
59 forwarder => sub { return $ua->request(shift) }
60 );
61
62 my $rqst = HTTP::Request->new( GET => 'http://example.com' );
63
64 my $resp = $http_caching->make_request( $rqst );
65
66 =cut
67  
68 has cache => (
69 is => 'ro',
70 required => 0,
71 isa => Maybe[ HasMethods['set', 'get'] ],
72 builder => sub {
73 0 ▶ warn __PACKAGE__ . " without cache, forwards requests and responses\n";
Subroutine _build_cache: not called CC=1 SCAR=6.9
74 return undef
75 0 },
76 );
77  
78 has cache_meta => (
79 is => 'ro',
80 required => 0,
81 isa => Maybe[ HasMethods['set', 'get'] ],
82 lazy => 1,
83 builder => sub {
84 return shift->cache
85 0 ▶ },
Subroutine _build_cache_meta: not called CC=1 SCAR=6.9
86 );
87  
88 has cache_type => (
89 is => 'ro',
90 required => 1,
91 isa => Maybe[ Enum['private', 'public'] ],
92 );
93  
94 has cache_control_request => (
95 is => 'ro',
96 required => 0,
97 isa => Maybe[ Str ],
98 );
99  
100 has cache_control_response => (
101 is => 'ro',
102 required => 0,
103 isa => Maybe[ Str ],
104 );
105  
106 has forwarder => (
107 is => 'ro',
108 required => 1,
109 isa => CodeRef,
110 );
111  
112 sub is_shared {
113 0 ▶ my $self = shift;
Subroutine is_shared: not called CC=2 SCAR=17.9
114
115 0 ▶ return unless $self->cache_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cache_type 0 0 0
116 0 return $self->cache_type eq 'public'
117 }
118 =head1 DESCRIPTION
119  
120 This module tries to provide caching for HTTP responses based on
121 L<RFC 7234 Hypertext Transfer Protocol (HTTPE<sol>1.1): Caching|
122 http://tools.ietf.org/html/rfc7234>.
123  
124 Basicly it looks like the following steps below:
125  
126 =over
127  
128 =item
129  
130 For a presented request, it will check with the cache if there is a suitable
131 response available AND if it can be served or that it needs to be revalidated
132 with an upstream server.
133  
134 =item
135  
136 If there was no response available at all, or non were suitable, the (modified)
137 request will simply be forwarded.
138  
139 =item
140  
141 Depending on the response it gets back, it will do one of the following
142 dependingon the response status code:
143  
144 =over
145  
146 =item 200 OK
147  
148 it will update the cache and serve the response as is
149  
150 =item 304 Not Modified
151  
152 the cached version is valid, update the cache with new header info and serve the
153 cached response
154  
155 =item 500 Server Error
156  
157 in general, this is an error, and pass that onto the caller, however, in some
158 cases it could be fine to serve a (stale) cached response
159  
160 =back
161  
162 =back
163  
164 The above is a over-simplified version of the RFC
165  
166 =cut
167  
168 =head1 CONSTRUCTORS
169  
170 =head2 new
171  
172 my $http_caching = HTTP::Caching->new(
173 cache => $chi_cache,
174 cache_type => 'private',
175 cache_request => 'max-age=86400, min-fresh=60',
176 forwarder => sub { return $ua->request(shift) }
177 );
178  
179 Constructs a new C<HTTP::Caching> object that knows how to find cached responses
180 and will forward if needed.
181  
182 =head1 ATRRIBUTES
183  
184 =head2 cache
185  
186 Cache must be an object that MUST implement two methods
187  
188 =over
189  
190 =item sub set ($key, $data)
191  
192 to store data in the cache
193  
194 =item sub get ($key)
195  
196 to retrieve the data stored under the key
197  
198 =back
199  
200 This can be as simple as a hash, like we use in the tests:
201  
202 use Test::MockObject;
203
204 my %cache;
205 my $mocked_cache = Test::MockObject->new;
206 $mocked_cache->mock( set => sub { $cache{$_[1]} = $_[2] } );
207 $mocked_cache->mock( get => sub { return $cache{$_[1]} } );
208  
209 But very convenient is to use L<CHI>, which implements both required methods and
210 also has the option to use a L1 cache to speed things up even more. See the
211 SYNOPSIS for an example
212  
213 =head2 cache_type
214  
215 This must either be C<'private'> or C<'public'>. For most L<LWP::UserAgents>, it
216 can be C<'private'> as it will probably not be shared with other processes on
217 the same macine. If this module is being used at the serverside in a
218 L<Plack::Middleware> then the cache will be used by all other clients connecting
219 to the server, and thus should be set to C<'public'>.
220  
221 Responses to Authenticated request should not be held in public caches and also
222 those responses that specifacally have their cache-control headerfield set to
223 C<'private'>.
224  
225 =head2 cache_control_request
226  
227 A string that contains the Cache-control header-field settings that will be sent
228 as default with the request. So you do not have to set those each time. See
229 RFC 7234 Section 5.2.1 for the list of available cache-control directives.
230  
231 =head2 cache_control_response
232  
233 Like the above, but those will be set for each response. This is useful for
234 server side caching. See RFC 7234 Section 5.2.2.
235  
236 =head2 forwarder
237  
238 This CodeRef must be a callback function that accepts a L<HTTP::Request> and
239 returns a L<HTTP::Response>. Since this module does not know how to do a request
240 it will use the C<forwarder>. It will be used to sent of validation requests
241 with C<If-None-Match> and/or C<If-Modified-Since> header-fields. Or if it does
242 not have a stored response it will send the original full request (with the
243 extra directives from C<cache_request>).
244  
245 Failing to return a C<HTTP::Response> might cause the module to die or generate
246 a response itself with status code C<502 Bad Gateway>.
247  
248 =head1 METHODS
249  
250 =head2 make_request
251  
252 This is the only public provided method and will take a L<HTTP::Request>. Like
253 described above, it might have to forward the (modified) request throug the
254 CodeRef in the C<forwarder> attribute.
255  
256 It will return a L<HTTP::Response> from cache or a new retrieved one. This might
257 be a HTTP respons with a C<500 Error> message.
258  
259 In other cases it might die and let the caller know what was wrong, or send
260 another 5XX Error.
261  
262 =cut
263  
264 sub make_request {
265 5 ▶ my $self = shift;
Subroutine make_request: called CC=10 SCAR=31.6
266
267 5 ▶ croak __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 4 5
268 . " missing request"
269 unless defined $_[0];
270 4 ▶ croak __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($_[0], "HTTP::Request") 1 3 4
271 . " request is not a HTTP::Request [$_[0]]"
272 unless UNIVERSAL::isa($_[0],'HTTP::Request');
273
274 3 my $presented_request = shift->clone;
275
276 3 my @params = @_;
277  
278 # add the default Cache-Control request header-field
279 3 ▶ $presented_request->headers->push_header(
Branch: 1 branch
BranchTrueFalseTotal
if $self->cache_control_request 0 3 3
280 cache_control => $self->cache_control_request,
281 ) if $self->cache_control_request();
282
283 3 my $response;
284
285 3 ▶ unless ($self->cache) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->cache) { } 0 3 3
if (do { $self->_non_safe($presented_request) }) { } 0 0 0
286 3 $response = $self->_forward($presented_request, @params);
287 } elsif ( $self->_non_safe($presented_request) ) {
288
289 # always forwad requests with unsafe methods
290 0 $response = $self->_forward($presented_request, @params);
291
292 # when returned with a non-err, invalidate the cache
293 0 ▶ if ( $response->is_success or $response->is_redirect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success or $response->is_redirect) 0 0 0
Condition0%0/3
$response->is_success or $response->is_redirect
l!l&&r!l&&!r
000
MC/DC0%0/2
$response->is_success or $response->is_redirect
$response->is_success $response->is_redirect
Truth table0%0/3
A or B
A= $response->is_successB= $response->is_redirect
ABresult
1X1
011
000
294 0 $self->_invalidate($presented_request)
295 }
296 } else {
297 0 ▶ if (my $cache_resp =
Branch: 1 branch
BranchTrueFalseTotal
if (my $cache_resp = $self->_retrieve($presented_request)) { } 0 0 0
298 $self->_retrieve($presented_request)
299 ) {
300 0 $response = $cache_resp;
301 } else {
302 0 $response = $self->_forward($presented_request, @params);
303 0 $self->_store($presented_request, $response);
304 }
305 }
306
307 # add the default Cache-Control response header-field
308 3 ▶ $response->headers->push_header(
Branch: 1 branch
BranchTrueFalseTotal
if $self->cache_control_response 0 3 3
309 cache_control => $self->cache_control_response,
310 ) if $self->cache_control_response;
311
312 3 return $response;
313
314 }
315  
316 sub _forward {
317 3 ▶ my $self = shift;
Subroutine _forward: called CC=2 SCAR=6.9
318
319 3 my $forwarded_rqst = shift;
320
321 3 my $forwarded_resp = $self->forwarder->($forwarded_rqst, @_);
322
323 3 ▶ unless ( UNIVERSAL::isa($forwarded_resp,'HTTP::Response') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($forwarded_resp, "HTTP::Response")) 1 2 3
324 1 carp __PACKAGE__
325 . " response is not a HTTP::Response [$forwarded_resp]";
326 # rescue from a failed forwarding, HTTP::Caching should not break
327 1 $forwarded_resp = HTTP::Response->new(502); # Bad Gateway
328 }
329
330 3 return $forwarded_resp;
331 }
332  
333  
334 =head1 ABOUT CACHING
335  
336 If one would read the RFC7234 Section 2. Overview of Cache Operation, it becomes
337 clear that a cache can hold multiple responses for the same URI. Caches that
338 conform to CHI and many others, typically use a key / value storage. But this
339 will become a problem as that it can not use the URI as a key to the various
340 responses.
341  
342 The way it is solved is to create an intermediate meta-dictionary. This can be
343 stored by URI as key. Each response will simply be stored with a unique key and
344 these keys will be used as the entries in the dictionary.
345  
346 The meta-dictionary entries will hold (relevant) request and response headers so
347 that it willbe more quick to figure wich entrie can be used. Otherwise we would
348 had to read the entire responses to analyze them.
349  
350 =cut
351  
352 # _store may or may not store the response into the cache
353 #
354 # depending on the response it _may_store_in_cache()
355 #
356 sub _store {
357 0 ▶ my $self = shift;
Subroutine _store: not called CC=3 SCAR=24.8
358 0 my $rqst = shift;
359 0 my $resp = shift;
360
361 0 ▶ return unless $self->_may_store_in_cache($rqst, $resp);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_may_store_in_cache($rqst, $resp) 0 0 0
362
363 0 ▶ if ( my $resp_key = $self->_store_response($resp) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $resp_key = $self->_store_response($resp)) 0 0 0
364 0 my $rqst_key = Digest::MD5::md5_hex($rqst->uri()->as_string);
365 0 my $rsqt_stripped = $rqst->clone; $rsqt_stripped->content(undef);
366 0 my $resp_stripped = $resp->clone; $resp_stripped->content(undef);
367 0 $self->_insert_meta_dict(
368 $rqst_key,
369 $resp_key,
370 {
371 resp_stripped => $resp_stripped,
372 rqst_stripped => $rsqt_stripped,
373 },
374 );
375 0 return $resp_key;
376 }
377
378 return
379 0 }
380  
381 sub _store_response {
382 0 ▶ my $self = shift;
Subroutine _store_response: not called CC=2 SCAR=17.9
383 0 my $resp = shift;
384
385 0 my $resp_key = Digest::MD5::md5_hex(Time::HiRes::time());
386
387 0 eval { $self->cache->set( $resp_key => $resp ) };
388 0 ▶ return $resp_key unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
389
390 0 croak __PACKAGE__
391 . " could not store response in cache with key [$resp_key], $@";
392
393 return
394 0 }
395  
396 sub _insert_meta_dict {
397 0 ▶ my $self = shift;
Subroutine _insert_meta_dict: not called CC=2 SCAR=17.9
398 0 my $rqst_key = shift;
399 0 my $resp_key = shift;
400 0 my $meta_data = shift;
401
402 0 ▶ my $meta_dict = $self->cache_meta->get($rqst_key) || {};
Condition0%0/2
$self->cache_meta->get($rqst_key) || {}
l!l
00
MC/DC0%0/1
$self->cache_meta->get($rqst_key) || {}
$self->cache_meta->get($rqst_key)
Truth table0%0/2
A
A= $self->cache_meta->get($rqst_key)
Aresult
11
00
403 0 $meta_dict->{$resp_key} = $meta_data;
404 0 $self->cache_meta->set( $rqst_key => $meta_dict );
405
406 0 return $meta_dict;
407 }
408  
409 sub _retrieve {
410 0 ▶ my $self = shift;
Subroutine _retrieve: not called CC=10 SCAR=47.0
411 0 my $rqst_presented = shift;
412
413 0 my $rqst_key = Digest::MD5::md5_hex($rqst_presented->uri()->as_string);
414 0 my $meta_dict = $self->_retrieve_meta_dict($rqst_key);
415
416 0 ▶ return unless $meta_dict;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta_dict 0 0 0
417
418 0 my @meta_keys = keys %$meta_dict;
419
420 0 foreach my $meta_key (@meta_keys) {
421 my $reuse_status = $self->_may_reuse_from_cache(
422 $rqst_presented,
423 $meta_dict->{$meta_key}{resp_stripped},
424 $meta_dict->{$meta_key}{rqst_stripped}
425 0 );
426 0 $meta_dict->{$meta_key}{reuse_status} = $reuse_status
427 }
428
429 my @okay_keys =
430 0 grep { $meta_dict->{$_}{reuse_status} & $REUSE_IS_OK} @meta_keys;
431
432 0 ▶ if (scalar @okay_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @okay_keys) 0 0 0
433 #
434 # TODO: do content negotiation if possible
435 #
436 # TODO: Sort to select lates response
437 #
438 0 my ($resp_key) = @okay_keys;
439 0 my $resp = $self->_retrieve_response($resp_key);
440 0 return $resp
441 }
442
443 my @vldt_keys =
444 0 grep { $meta_dict->{$_}{reuse_status} & $REUSE_REVALIDATE} @meta_keys;
445
446 0 ▶ if (scalar @vldt_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @vldt_keys) 0 0 0
447 #
448 # RFC 7234 Section 4.3.1
449 #
450 # Sending a Validation Request
451 #
452 0 my ($resp_key) = @vldt_keys;
453 0 my $resp_stripped = $meta_dict->{$resp_key}{resp_stripped};
454
455 # Assume we have validation headers, otherwise we'll need a HEAD request
456 #
457 0 my $etag = $resp_stripped->header('ETag');
458 0 my $last = $resp_stripped->header('Last-Modified');
459
460 0 my $rqst_forwarded = $rqst_presented->clone;
461 0 ▶ $rqst_forwarded->header('If-None-Match' => $etag) if $etag;
Branch: 1 branch
BranchTrueFalseTotal
if $etag 0 0 0
462 0 ▶ $rqst_forwarded->header('If-Modified-Since' => $last) if $last;
Branch: 1 branch
BranchTrueFalseTotal
if $last 0 0 0
463
464 0 my $resp_forwarded = $self->_forward($rqst_forwarded);
465
466 # RFC 7234 Section 4.3.3.
467 #
468 # Handling a Validation Response
469 #
470 # Cache handling of a response to a conditional request is dependent
471 # upon its status code:
472
473
474 # A 304 (Not Modified) response status code indicates that the
475 # stored response can be updated and reused; see Section 4.3.4.
476 #
477 0 ▶ if ($resp_forwarded->code == HTTP_NOT_MODIFIED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp_forwarded->code == 304) 0 0 0
478 0 my $resp = $self->_retrieve_response($resp_key);
479 0 return $resp
480 #
481 # TODO: make it all compliant with Section 4.3.4 on how to select
482 # which stored responses need update
483 # TODO: ade 'Age' header
484 }
485
486
487 # A full response (i.e., one with a payload body) indicates that
488 # none of the stored responses nominated in the conditional request
489 # is suitable. Instead, the cache MUST use the full response to
490 # satisfy the request and MAY replace the stored response(s).
491 #
492 0 ▶ if ( not HTTP::Status::is_server_error($resp_forwarded->code) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&HTTP::Status::is_server_error(scalar $resp_forwarded->code)) 0 0 0
493 0 $self->_store($rqst_presented, $resp_forwarded);
494 0 return $resp_forwarded;
495 }
496
497
498 # However, if a cache receives a 5xx (Server Error) response while
499 # attempting to validate a response, it can either forward this
500 # response to the requesting client, or act as if the server failed
501 # to respond. In the latter case, the cache MAY send a previously
502 # stored response (see Section 4.2.4).
503 #
504 0 ▶ if ( HTTP::Status::is_server_error($resp_forwarded->code) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&HTTP::Status::is_server_error(scalar $resp_forwarded->code)) 0 0 0
505 0 return $resp_forwarded;
506 }
507 #
508 # TODO: check if we can use a cached stale version
509
510
511 0 return undef;
512 }
513
514 0 return undef;
515 }
516  
517 sub _retrieve_meta_dict {
518 0 ▶ my $self = shift;
Subroutine _retrieve_meta_dict: not called CC=1 SCAR=6.9
519 0 my $rqst_key = shift;
520
521 0 my $meta_dict = $self->cache_meta->get($rqst_key);
522
523 0 return $meta_dict;
524 }
525  
526 sub _retrieve_response {
527 0 ▶ my $self = shift;
Subroutine _retrieve_response: not called CC=2 SCAR=17.9
528 0 my $resp_key = shift;
529
530 0 ▶ if (my $resp = eval { $self->cache->get( $resp_key ) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $resp = eval { do { $self->cache->get($resp_key) } }) 0 0 0
531 0 return $resp
532 }
533
534 0 carp __PACKAGE__
535 . " could not retrieve response from cache with key [$resp_key], $@";
536
537 return
538 0 }
539  
540 sub _invalidate {
541 0 ▶ my $self = shift;
Subroutine _invalidate: not called CC=3 SCAR=24.8
542 0 my $rqst_presented = shift;
543
544 0 my $rqst_key = Digest::MD5::md5_hex($rqst_presented->uri()->as_string);
545 0 my $meta_dict = $self->_retrieve_meta_dict($rqst_key);
546
547 0 ▶ return unless $meta_dict;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta_dict 0 0 0
548
549 0 my @meta_keys = keys %$meta_dict;
550
551 0 foreach my $meta_key (@meta_keys) {
552 0 $self->_invalidate_response($meta_key);
553 }
554
555 0 $self->_invalidate_meta_dict($rqst_key);
556
557 0 return;
558 }
559  
560 sub _invalidate_meta_dict {
561 0 ▶ my $self = shift;
Subroutine _invalidate_meta_dict: not called CC=1 SCAR=6.9
562 0 my $rqst_key = shift;
563
564 0 $self->cache_meta->remove($rqst_key);
565
566 return
567 0 }
568  
569 sub _invalidate_response {
570 0 ▶ my $self = shift;
Subroutine _invalidate_response: not called CC=1 SCAR=6.9
571 0 my $resp_key = shift;
572
573 0 $self->cache->remove($resp_key);
574
575 return
576 0 }
577  
578 sub _non_safe {
579 0 ▶ my $self = shift;
Subroutine _non_safe: not called CC=2 SCAR=17.9
580 0 my $rqst = shift;
581
582 0 my $method = eval { HTTP::Method->new( uc $rqst->method ) };
583 0 ▶ return 1 unless $method; #safety can not be guaranteed
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 0 0
584
585 0 return not $method->is_method_safe
586 }
587  
588 # _may_store_in_cache()
589 #
590 # based on some headers in the request, but mostly on those in the new response
591 # the cache can hold a copy of it or not.
592 #
593 # see RFC 7234 Section 3: Storing Responses in Caches
594 #
595 sub _may_store_in_cache {
596 0 ▶ my $self = shift;
Subroutine _may_store_in_cache: not called CC=37 SCAR=72.5
597 0 my $rqst = shift;
598 0 my $resp = shift;
599
600 # $msg->header('cache-control) is supposed to return a list, but only works
601 # if it has been generated as a list, not as string with 'comma'
602 # $msg->header in scalar context gives a ', ' joined string
603 # which we now split and trim whitespace
604 my @rqst_directives =
605 0 ▶ map { my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str }
Condition0%0/2
scalar $rqst->header('cache-control') || ''
l!l
00
MC/DC0%0/1
scalar $rqst->header('cache-control') || ''
scalar $rqst->header('cache-control')
Truth table0%0/2
A
A= scalar $rqst->header('cache-control')
Aresult
11
00
606 split ',', scalar $rqst->header('cache-control') || '';
607 my @resp_directives =
608 0 ▶ map { my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str }
Condition0%0/2
scalar $resp->header('cache-control') || ''
l!l
00
MC/DC0%0/1
scalar $resp->header('cache-control') || ''
scalar $resp->header('cache-control')
Truth table0%0/2
A
A= scalar $resp->header('cache-control')
Aresult
11
00
609 split ',', scalar $resp->header('cache-control') || '';
610
611
612 # RFC 7234 Section 3
613 #
614 # A cache MUST NOT store a response to any request, unless:
615
616 # RFC 7234 Section 3 #1
617 #
618 # the request method is understood by the cache and defined as being
619 # cacheable
620 #
621 0 do {
622 0 my $string = $rqst->method;
623 0 my $method = eval { HTTP::Method->new($string) };
624
625 0 ▶ unless ($method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 0 0 0
626 0 ▶ carp "NO CACHE: method is not understood: '$string'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
627 if $DEBUG;
628 0 return 0
629 }
630 0 ▶ unless ($method->is_method_cachable) { # XXX Fix cacheable
Branch: 1 branch
BranchTrueFalseTotal
unless ($method->is_method_cachable) 0 0 0
631 0 ▶ carp "NO CACHE: method is not cacheable: '$string'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
632 if $DEBUG;
633 0 return 0
634 }
635 };
636
637 # RFC 7234 Section 3 #2
638 #
639 # the response status code is understood by the cache
640 #
641 0 do {
642 0 my $code = $resp->code;
643 0 my $message = eval { HTTP::Status::status_message($code) };
644
645 0 ▶ unless ($message) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($message) 0 0 0
646 0 ▶ carp "NO CACHE: response status code is not understood: '$code'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
647 if $DEBUG;
648 0 return 0
649 }
650 };
651
652
653 # RFC 7234 Section 3 #3
654 #
655 # the "no-store" cache directive (see Section 5.2) does not appear
656 # in request or response header fields
657 #
658 0 do {
659 0 ▶ if (any { lc $_ eq 'no-store' } @rqst_directives) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ eq "no-store"; } , @rqst_directives)) 0 0 0
660 0 ▶ carp "NO CACHE: 'no-store' appears in request cache directives\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
661 if $DEBUG;
662 0 return 0
663 }
664 0 ▶ if (any { lc $_ eq 'no-store' } @resp_directives) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ eq "no-store"; } , @resp_directives)) 0 0 0
665 0 ▶ carp "NO CACHE: 'no-store' appears in response cache directives\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
666 if $DEBUG;
667 0 return 0
668 }
669 };
670
671 # RFC 7234 Section 3 #4
672 #
673 # the "private" response directive (see Section 5.2.2.6) does not
674 # appear in the response, if the cache is shared
675 #
676 0 ▶ if ($self->is_shared) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_shared) 0 0 0
677 0 ▶ if (any { lc $_ eq 'private' } @resp_directives) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ eq "private"; } , @resp_directives)) 0 0 0
678 0 ▶ carp "NO CACHE: 'private' appears in cache directives when shared\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
679 if $DEBUG;
680 0 return 0
681 }
682 };
683
684 # RFC 7234 Section 3 #5
685 #
686 # the Authorization header field (see Section 4.2 of [RFC7235]) does
687 # not appear in the request, if the cache is shared, unless the
688 # response explicitly allows it (see Section 3.2)
689 #
690 0 ▶ if ($self->is_shared) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_shared) 0 0 0
691 0 ▶ if ($rqst->header('Authorization')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rqst->header("Authorization")) 0 0 0
692 0 ▶ if (any { lc $_ eq 'must-revalidate' } @resp_directives) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ eq "must-revalidate"; } , @resp_directives)) 0 0 0
693 0 ▶ carp "DO CACHE: 'Authorization' appears: must-revalidate\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
694 if $DEBUG;
695 0 return 1
696 }
697 0 ▶ if (any { lc $_ eq 'public' } @resp_directives) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ eq "public"; } , @resp_directives)) 0 0 0
698 0 ▶ carp "DO CACHE: 'Authorization' appears: public\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
699 if $DEBUG;
700 0 return 1
701 }
702 0 ▶ if (any { lc $_ =~ m/^s-maxage=\d+$/ } @resp_directives) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ =~ /^s-maxage=\d+$/; } , @resp_directives)) 0 0 0
703 0 ▶ carp "DO CACHE: 'Authorization' appears: s-maxage\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
704 if $DEBUG;
705 0 return 1
706 }
707 0 ▶ carp "NO CACHE: 'Authorization' appears in request when shared\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
708 if $DEBUG;
709 0 return 0
710 }
711 };
712
713
714 # RFC 7234 Section 3 #6
715 #
716 # the response either:
717 #
718 # - contains an Expires header field (see Section 5.3)
719 #
720 0 do {
721 0 my $expires_at = $resp->header('Expires');
722
723 0 ▶ if ($expires_at) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires_at) 0 0 0
724 0 ▶ carp "OK CACHE: 'Expires' at: $expires_at\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
725 if $DEBUG;
726 0 return 1
727 }
728 };
729
730 # - contains a max-age response directive (see Section 5.2.2.8)
731 #
732 0 do {
733 0 ▶ if (any { lc $_ =~ m/^max-age=\d+$/ } @resp_directives) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ =~ /^max-age=\d+$/; } , @resp_directives)) 0 0 0
734 0 ▶ carp "DO CACHE: 'max-age' appears in response cache directives\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
735 if $DEBUG;
736 0 return 1
737 }
738 };
739
740 # - contains a s-maxage response directive (see Section 5.2.2.9)
741 # and the cache is shared
742 #
743 0 ▶ if ($self->is_shared) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_shared) 0 0 0
744 0 ▶ if (any { lc $_ =~ m/^s-maxage=\d+$/ } @resp_directives) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ =~ /^s-maxage=\d+$/; } , @resp_directives)) 0 0 0
745 0 ▶ carp "DO CACHE: 's-maxage' appears in response cache directives\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
746 if $DEBUG;
747 0 return 1
748 }
749 };
750
751
752 # - contains a Cache Control Extension (see Section 5.2.3) that
753 # allows it to be cache
754 #
755 # TODO it looks like this is only used for special defined cache-control
756 # directives. As such, those need special treatment.
757 # It does not seem a good idea to hardcode those here, a config would
758 # be a better solution.
759
760
761 # - has a status code that is defined as cacheable by default (see
762 # Section 4.2.2)
763 #
764 0 do {
765 0 my $code = $resp->code;
766
767 0 ▶ if (HTTP::Status::is_cacheable_by_default($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&HTTP::Status::is_cacheable_by_default($code)) 0 0 0
768 0 ▶ carp "DO CACHE: status code is cacheable by default: '$code'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
769 if $DEBUG;
770 0 return 1
771 }
772 };
773
774 # - contains a public response directive (see Section 5.2.2.5)
775 #
776 0 do {
777 0 ▶ if (any { lc $_ eq 'public' } @resp_directives) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ eq "public"; } , @resp_directives)) 0 0 0
778 0 ▶ carp "DO CACHE: 'public' appears in response cache directives\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
779 if $DEBUG;
780 0 return 1
781 }
782 };
783
784 # Falls trough ... SHOULD NOT store in cache
785 #
786 0 ▶ carp "NO CACHE: Does not match the six criteria above\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
787 if $DEBUG;
788
789 0 return undef;
790 }
791  
792  
793 # _may_reuse_from_cache
794 #
795 # my $status = _may_reuse_from_cache (
796 # $presented_request,
797 # $stored_response,
798 # $associated_request,
799 # )
800 #
801 # will return false if the stored response can not be used for this request at
802 # all. In all other cases, it either
803 # - can be used, because it matches all the criteria and os fresh
804 # - is stale and can be used if needed
805 # - needs revalidation
806 #
807 # see RFC 7234 Section 4: Constructing Responses from Caches
808 #
809 sub _may_reuse_from_cache {
810 0 ▶ my $self = shift;
Subroutine _may_reuse_from_cache: not called CC=38 SCAR=73.0
811 0 my $rqst_presented = shift;
812 0 my $resp_stored = shift;
813 0 my $rqst_associated = shift;
814
815 # RFC 7234 Section 4
816 #
817 # When presented with a request, a cache MUST NOT reuse a stored
818 # response, unless:
819
820
821 # RFC 7234 Section 4 #1
822 #
823 # The presented effective request URI (Section 5.5 of [RFC7230]) and
824 # that of the stored response match
825 #
826 0 do {
827 0 ▶ unless ( URI::eq($rqst_presented->uri, $rqst_associated->uri) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (URI::eq($rqst_presented->uri, $rqst_associated->uri)) 0 0 0
828 0 ▶ carp "NO REUSE: URI's do not match\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
829 if $DEBUG;
830 0 return $REUSE_NO_MATCH
831 }
832 };
833
834
835 # RFC 7234 Section 4 #2
836 #
837 # the request method associated with the stored response allows it
838 # to be used for the presented request
839 #
840 0 do {
841 0 ▶ unless ( $rqst_presented->method eq $rqst_associated->method ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rqst_presented->method eq $rqst_associated->method) 0 0 0
842 0 ▶ carp "NO REUSE: Methods do not match\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
843 if $DEBUG;
844 0 return $REUSE_NO_MATCH
845 }
846 };
847 #
848 # NOTE: We did not make the test case insensitive, according to RFC 7231.
849 #
850 # NOTE: We might want to extend it so that we can serve a chopped response
851 # where the presented request is a HEAD request
852
853
854 # RFC 7234 Section 4 #3
855 #
856 # selecting header fields nominated by the stored response (if any)
857 # match those presented (see Section 4.1)
858 0 ▶ if ( $resp_stored->header('Vary') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp_stored->header("Vary")) 0 0 0
859
860 0 ▶ if ( scalar $resp_stored->header('Vary') eq '*' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $resp_stored->header("Vary") eq "*") 0 0 0
861 0 ▶ carp "NO REUSE: 'Vary' equals '*'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
862 if $DEBUG;
863 0 return $REUSE_NO_MATCH
864 }
865
866 #create an array with nominated headers
867 my @vary_headers =
868 0 ▶ map { my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str }
Condition0%0/2
scalar $resp_stored->header('Vary') || ''
l!l
00
MC/DC0%0/1
scalar $resp_stored->header('Vary') || ''
scalar $resp_stored->header('Vary')
Truth table0%0/2
A
A= scalar $resp_stored->header('Vary')
Aresult
11
00
869 split ',', scalar $resp_stored->header('Vary') || '';
870
871 0 foreach my $header ( @vary_headers ) {
872 0 ▶ my $header_presented = $rqst_presented->header($header) || '';
Condition0%0/2
$rqst_presented->header($header) || ''
l!l
00
MC/DC0%0/1
$rqst_presented->header($header) || ''
$rqst_presented->header($header)
Truth table0%0/2
A
A= $rqst_presented->header($header)
Aresult
11
00
873 0 ▶ my $header_associated = $rqst_associated->header($header) || '';
Condition0%0/2
$rqst_associated->header($header) || ''
l!l
00
MC/DC0%0/1
$rqst_associated->header($header) || ''
$rqst_associated->header($header)
Truth table0%0/2
A
A= $rqst_associated->header($header)
Aresult
11
00
874 0 ▶ unless ( $header_presented eq $header_associated ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header_presented eq $header_associated) 0 0 0
875 0 ▶ carp "NO REUSE: Nominated headers in 'Vary' do not match\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
876 if $DEBUG;
877 0 return $REUSE_NO_MATCH
878 }
879 }
880 };
881 #
882 # TODO: According to Section 4.1, we could do normalization and reordering
883 # This requires further investigation and is worth doing to increase
884 # the hit chance
885
886
887 # RFC 7234 Section 4 #4
888 #
889 # the presented request does not contain the no-cache pragma
890 # (Section 5.4), nor the no-cache cache directive (Section 5.2.1),
891 # unless the stored response is successfully validated
892 # (Section 4.3)
893 #
894 0 do {
895 # generate an array with cache-control directives
896 my @rqst_directives =
897 0 ▶ map { my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str }
Condition0%0/2
scalar $rqst_presented->header('cache-control') || ''
l!l
00
MC/DC0%0/1
scalar $rqst_presented->header('cache-control') || ''
scalar $rqst_presented->header('cache-control')
Truth table0%0/2
A
A= scalar $rqst_presented->header('cache-control')
Aresult
11
00
898 split ',', scalar $rqst_presented->header('cache-control') || '';
899
900 0 ▶ if (any { lc $_ eq 'no-cache' } @rqst_directives) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ eq "no-cache"; } , @rqst_directives)) 0 0 0
901 0 ▶ carp "NO REUSE: 'no-cache' appears in request cache directives\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
902 if $DEBUG;
903 0 return $REUSE_REVALIDATE
904 }
905
906 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($rqst_presented->header("Pragma") and scalar $rqst_presented->header("Pragma") =~ /no-cache/) 0 0 0
Condition0%0/2
$rqst_presented->header("Pragma") and scalar $rqst_presented->header("Pragma") =~ /no-cache/
!ll
00
MC/DC0%0/1
$rqst_presented->header("Pragma") and scalar $rqst_presented->header("Pragma") =~ /no-cache/
$rqst_presented->header("Pragma")
Truth table0%0/2
A
A= $rqst_presented->header("Pragma")
Aresult
00
11
907 $rqst_presented->header('Pragma')
908 and
909 scalar $rqst_presented->header('Pragma') =~ /no-cache/
910 ) {
911 0 ▶ carp "NO REUSE: Pragma: 'no-cache' appears in request\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
912 if $DEBUG;
913 0 return $REUSE_REVALIDATE
914 }
915 };
916
917
918 # RFC 7234 Section 4 #5
919 #
920 # the stored response does not contain the no-cache cache directive
921 # (Section 5.2.2.2), unless it is successfully validated
922 # (Section 4.3)
923 #
924 0 do {
925 # generate an array with cache-control directives
926 my @resp_directives =
927 0 ▶ map { my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str }
Condition0%0/2
scalar $resp_stored->header('cache-control') || ''
l!l
00
MC/DC0%0/1
scalar $resp_stored->header('cache-control') || ''
scalar $resp_stored->header('cache-control')
Truth table0%0/2
A
A= scalar $resp_stored->header('cache-control')
Aresult
11
00
928 split ',', scalar $resp_stored->header('cache-control') || '';
929
930 0 ▶ if (any { lc $_ eq 'no-cache' } @resp_directives) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ eq "no-cache"; } , @resp_directives)) 0 0 0
931 0 ▶ carp "NO REUSE: 'no-cache' appears in response cache directives\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
932 if $DEBUG;
933 0 return $REUSE_REVALIDATE
934 }
935 };
936
937 # RFC 7234 Section 4 #6
938 #
939 # the stored response is either:
940 #
941 # - fresh (see Section 4.2), or
942 #
943 0 do {
944 0 ▶ if ($resp_stored->is_fresh(heuristic_expiry => undef)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp_stored->is_fresh("heuristic_expiry", undef)) 0 0 0
945 0 ▶ carp "DO REUSE: Response is fresh\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
946 if $DEBUG;
947 0 return $REUSE_IS_OK
948 }
949 };
950 #
951 # TODO: heuristic_expiry => undef should be a option, not hardcoded
952
953 # - allowed to be served stale (see Section 4.2.4), or
954 #
955 0 do {
956 # generate an array with cache-control directives
957 my @resp_directives =
958 0 ▶ map { my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str }
Condition0%0/2
scalar $resp_stored->header('cache-control') || ''
l!l
00
MC/DC0%0/1
scalar $resp_stored->header('cache-control') || ''
scalar $resp_stored->header('cache-control')
Truth table0%0/2
A
A= scalar $resp_stored->header('cache-control')
Aresult
11
00
959 split ',', scalar $resp_stored->header('cache-control') || '';
960
961 # RFC 7234 Section 5.2.2.1
962 #
963 # must-revalidate
964 #
965 0 ▶ if (any { lc $_ eq 'must-revalidate' } @resp_directives) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ eq "must-revalidate"; } , @resp_directives)) 0 0 0
966 0 ▶ carp "NO REUSE: Stale but 'must-revalidate'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
967 if $DEBUG;
968 0 return $REUSE_IS_STALE_REVALIDATE
969 }
970
971 # RFC 7234 Section 5.2.2.7
972 #
973 # proxy-revalidate
974 #
975 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { lc $_ eq "proxy-revalidate"; } , @resp_directives) and $self->is_shared) 0 0 0
Condition0%0/3
&any(sub { lc $_ eq "proxy-revalidate"; } , @resp_directives) and $self->is_shared
!ll&&!rl&&r
000
MC/DC0%0/2
&any(sub { lc $_ eq "proxy-revalidate"; } , @resp_directives) and $self->is_shared
&any(sub { lc $_ eq "proxy-revalidate"; } , @resp_directives) $self->is_shared
Truth table0%0/3
A and B
A= &any(sub { lc $_ eq "proxy-revalidate"; } , @resp_directives)B= $self->is_shared
ABresult
0X0
100
111
976 0 ▶ any { lc $_ eq 'proxy-revalidate' } @resp_directives
Subroutine __ANON__: not called CC=1 SCAR=6.9
977 and
978 $self->is_shared
979 ) {
980 0 ▶ carp "NO REUSE: Stale but 'proxy-revalidate'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
981 if $DEBUG;
982 0 return $REUSE_IS_STALE_REVALIDATE
983 }
984
985
986 # RFC 7234 Section 5.2.1.2
987 #
988 # max-stale = ...
989 #
990 my @rqst_directives =
991 0 ▶ map { my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str }
Condition0%0/2
scalar $rqst_presented->header('cache-control') || ''
l!l
00
MC/DC0%0/1
scalar $rqst_presented->header('cache-control') || ''
scalar $rqst_presented->header('cache-control')
Truth table0%0/2
A
A= scalar $rqst_presented->header('cache-control')
Aresult
11
00
992 split ',', scalar $rqst_presented->header('cache-control') || '';
993
994 my ($directive) =
995 0 grep { $_ =~ /^max-stale\s*=?\s*\d*$/ } @rqst_directives;
996
997 0 ▶ if ($directive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($directive) 0 0 0
998 0 my ($max_stale) = $directive =~ /:(\d+)$/;
999 0 ▶ unless ($max_stale) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($max_stale) 0 0 0
1000 0 ▶ carp "DO REUSE: 'max-stale' for unlimited time\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
1001 if $DEBUG;
1002 0 return $REUSE_IS_STALE_OK
1003 }
1004 0 my $freshness = # not fresh!!! so, this is a negative number
1005 $resp_stored->freshness_lifetime(heuristic_expiry => undef);
1006 0 ▶ if ( abs($freshness) < $max_stale ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $freshness < $max_stale) 0 0 0
1007 0 ▶ carp "DO REUSE: 'max-stale' not exceeded\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
1008 if $DEBUG;
1009 0 return $REUSE_IS_STALE_OK
1010 }
1011 }
1012
1013 };
1014
1015 # - successfully validated (see Section 4.3).
1016 #
1017 0 do {
1018 0 ▶ carp "NO REUSE: must successfully validated"
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Caching::DEBUG 0 0 0
1019 if $DEBUG;
1020 0 return $REUSE_IS_STALE_REVALIDATE
1021 };
1022
1023 }
1024  
1025 # HTTP::Status::is_cacheable_by_default
1026 #
1027 # that subroutine is missing. Until it's added there, it's been monkey-patched
1028 # here.
1029 #
1030 # RFC 7231 - HTTP/1.1 Semantics and Content
1031 # Section 6.1. Overview of Status Codes
1032 #
1033 # Responses with status codes that are defined as cacheable by default
1034 # (e.g., 200, 203, 204, 206, 300, 301, 404, 405, 410, 414, and 501 in
1035 # this specification) can be reused by a cache with heuristic
1036 # expiration unless otherwise indicated by the method definition or
1037 # explicit cache controls [RFC7234]; all other status codes are not
1038 # cacheable by default.
1039 #
1040 my $handle = Monkey::Patch::Action::patch_package (
1041 'HTTP::Status', 'is_cacheable_by_default', 'add', sub {
1042 my $code = shift;
1043 $code = $code +0;
1044
1045 return any {$_ == $code} (200,203,204,206,300,301,404,405,410,414,501)
1046 }
1047 );
1048  
1049  
1050 1;
↑ Summary