| 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.02 Alpha 03 | ||||||||||||
| 10 | |||||||||||||
| 11 | =cut | ||||||||||||
| 12 | |||||||||||||
| 13 | 5 | our $VERSION = '0.02_03'; | |||||||||||
| 14 | |||||||||||||
| 15 | 6 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 6 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 6 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 6 | ▶ | use Digest::MD5; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 6 | ▶ | use HTTP::Method; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 6 | ▶ | use List::MoreUtils qw{ any }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 6 | ▶ | use Monkey::Patch::Action; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 6 | ▶ | use Time::HiRes; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | |||||||||||||
| 25 | 6 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | 6 | ▶ | use MooX::Types::MooseLike::Base ':all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | |||||||||||||
| 28 | 5 | our $DEBUG = 0; | |||||||||||
| 29 | |||||||||||||
| 30 | =head1 SYNOPSIS | ||||||||||||
| 31 | |||||||||||||
| 32 | my $chi_cache = CHI->new( | ||||||||||||
| 33 | driver => 'File', | ||||||||||||
| 34 | root_dir => '/tmp/HTTP_Caching', | ||||||||||||
| 35 | file_extension => '.cache', | ||||||||||||
| 36 | l1_cache => { | ||||||||||||
| 37 | driver => 'Memory', | ||||||||||||
| 38 | global => 1, | ||||||||||||
| 39 | max_size => 1024*1024 | ||||||||||||
| 40 | } | ||||||||||||
| 41 | ); | ||||||||||||
| 42 | |||||||||||||
| 43 | my $ua = LWP::UserAgent->new(); | ||||||||||||
| 44 | |||||||||||||
| 45 | my $http_caching = HTTP::Caching->new( | ||||||||||||
| 46 | cache => $chi_cache, | ||||||||||||
| 47 | cache_type => 'private', | ||||||||||||
| 48 | forwarder => sub { return $ua->request(shift) } | ||||||||||||
| 49 | ); | ||||||||||||
| 50 | |||||||||||||
| 51 | my $rqst = HTTP::Request->new( GET => 'http://example.com' ); | ||||||||||||
| 52 | |||||||||||||
| 53 | my $resp = $http_caching->make_request( $rqst ); | ||||||||||||
| 54 | |||||||||||||
| 55 | =cut | ||||||||||||
| 56 | |||||||||||||
| 57 | has cache => ( | ||||||||||||
| 58 | is => 'ro', | ||||||||||||
| 59 | required => 0, | ||||||||||||
| 60 | isa => Maybe[ HasMethods['set', 'get'] ], | ||||||||||||
| 61 | builder => sub { | ||||||||||||
| 62 | 0 | ▶ | warn __PACKAGE__ . " without cache, forwards requests and responses\n"; | ||||||||||
|
Subroutine
_build_cache: not called CC=1 SCAR=1.2
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||||
| 63 | return undef | ||||||||||||
| 64 | 0 | }, | |||||||||||
| 65 | 5 | ); | |||||||||||
| 66 | |||||||||||||
| 67 | 5 | has cache_type => ( | |||||||||||
| 68 | is => 'ro', | ||||||||||||
| 69 | required => 1, | ||||||||||||
| 70 | isa => Maybe[ Enum['private', 'public'] ], | ||||||||||||
| 71 | ); | ||||||||||||
| 72 | |||||||||||||
| 73 | 5 | has cache_control_request => ( | |||||||||||
| 74 | is => 'ro', | ||||||||||||
| 75 | required => 0, | ||||||||||||
| 76 | isa => Str, | ||||||||||||
| 77 | ); | ||||||||||||
| 78 | |||||||||||||
| 79 | 5 | has cache_control_response => ( | |||||||||||
| 80 | is => 'ro', | ||||||||||||
| 81 | required => 0, | ||||||||||||
| 82 | isa => Str, | ||||||||||||
| 83 | ); | ||||||||||||
| 84 | |||||||||||||
| 85 | 5 | has forwarder => ( | |||||||||||
| 86 | is => 'ro', | ||||||||||||
| 87 | required => 1, | ||||||||||||
| 88 | isa => CodeRef, | ||||||||||||
| 89 | ); | ||||||||||||
| 90 | |||||||||||||
| 91 | sub is_shared { | ||||||||||||
| 92 | 40 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
is_shared: called CC=2 SCAR=6.9
| |||||||||||||
| 93 | |||||||||||||
| 94 | 40 | ▶ | return unless $self->cache_type; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 15 | return $self->cache_type eq 'public' | |||||||||||
| 96 | } | ||||||||||||
| 97 | =head1 DESCRIPTION | ||||||||||||
| 98 | |||||||||||||
| 99 | This module tries to provide caching for HTTP responses based on | ||||||||||||
| 100 | L<RFC 7234 Hypertext Transfer Protocol (HTTPE<sol>1.1): Caching| | ||||||||||||
| 101 | http://tools.ietf.org/html/rfc7234>. | ||||||||||||
| 102 | |||||||||||||
| 103 | Basicly it looks like the following steps below: | ||||||||||||
| 104 | |||||||||||||
| 105 | =over | ||||||||||||
| 106 | |||||||||||||
| 107 | =item | ||||||||||||
| 108 | |||||||||||||
| 109 | For a presented request, it will check with the cache if there is a suitable | ||||||||||||
| 110 | response available AND if it can be served or that it needs to be revalidated | ||||||||||||
| 111 | with an upstream server. | ||||||||||||
| 112 | |||||||||||||
| 113 | =item | ||||||||||||
| 114 | |||||||||||||
| 115 | If there was no response available at all, or non were suitable, the (modified) | ||||||||||||
| 116 | request will simply be forwarded. | ||||||||||||
| 117 | |||||||||||||
| 118 | =item | ||||||||||||
| 119 | |||||||||||||
| 120 | Depending on the response it gets back, it will do one of the following | ||||||||||||
| 121 | dependingon the response status code: | ||||||||||||
| 122 | |||||||||||||
| 123 | =over | ||||||||||||
| 124 | |||||||||||||
| 125 | =item 200 OK | ||||||||||||
| 126 | |||||||||||||
| 127 | it will update the cache and serve the response as is | ||||||||||||
| 128 | |||||||||||||
| 129 | =item 304 Not Modified | ||||||||||||
| 130 | |||||||||||||
| 131 | the cached version is valid, update the cache with new header info and serve the | ||||||||||||
| 132 | cached response | ||||||||||||
| 133 | |||||||||||||
| 134 | =item 500 Server Error | ||||||||||||
| 135 | |||||||||||||
| 136 | in general, this is an error, and pass that onto the caller, however, in some | ||||||||||||
| 137 | cases it could be fine to serve a (stale) cached response | ||||||||||||
| 138 | |||||||||||||
| 139 | =back | ||||||||||||
| 140 | |||||||||||||
| 141 | =back | ||||||||||||
| 142 | |||||||||||||
| 143 | The above is a over-simplified version of the RFC | ||||||||||||
| 144 | |||||||||||||
| 145 | =cut | ||||||||||||
| 146 | |||||||||||||
| 147 | =head1 CONSTRUCTORS | ||||||||||||
| 148 | |||||||||||||
| 149 | =head2 new | ||||||||||||
| 150 | |||||||||||||
| 151 | my $http_caching = HTTP::Caching->new( | ||||||||||||
| 152 | cache => $chi_cache, | ||||||||||||
| 153 | cache_type => 'private', | ||||||||||||
| 154 | cache_request => 'max-age=86400, min-fresh=60', | ||||||||||||
| 155 | forwarder => sub { return $ua->request(shift) } | ||||||||||||
| 156 | ); | ||||||||||||
| 157 | |||||||||||||
| 158 | Constructs a new C<HTTP::Caching> object that knows how to find cached responses | ||||||||||||
| 159 | and will forward if needed. | ||||||||||||
| 160 | |||||||||||||
| 161 | =head1 ATRRIBUTES | ||||||||||||
| 162 | |||||||||||||
| 163 | =head2 cache | ||||||||||||
| 164 | |||||||||||||
| 165 | Cache must be an object that MUST implement two methods | ||||||||||||
| 166 | |||||||||||||
| 167 | =over | ||||||||||||
| 168 | |||||||||||||
| 169 | =item sub set ($key, $data) | ||||||||||||
| 170 | |||||||||||||
| 171 | to store data in the cache | ||||||||||||
| 172 | |||||||||||||
| 173 | =item sub get ($key) | ||||||||||||
| 174 | |||||||||||||
| 175 | to retrieve the data stored under the key | ||||||||||||
| 176 | |||||||||||||
| 177 | =back | ||||||||||||
| 178 | |||||||||||||
| 179 | This can be as simple as a hash, like we use in the tests: | ||||||||||||
| 180 | |||||||||||||
| 181 | use Test::MockObject; | ||||||||||||
| 182 | |||||||||||||
| 183 | my %cache; | ||||||||||||
| 184 | my $mocked_cache = Test::MockObject->new; | ||||||||||||
| 185 | $mocked_cache->mock( set => sub { $cache{$_[1]} = $_[2] } ); | ||||||||||||
| 186 | $mocked_cache->mock( get => sub { return $cache{$_[1]} } ); | ||||||||||||
| 187 | |||||||||||||
| 188 | But very convenient is to use L<CHI>, which implements both required methods and | ||||||||||||
| 189 | also has the option to use a L1 cache to speed things up even more. See the | ||||||||||||
| 190 | SYNOPSIS for an example | ||||||||||||
| 191 | |||||||||||||
| 192 | =head2 cache_type | ||||||||||||
| 193 | |||||||||||||
| 194 | This must either be C<'private'> or C<'public'>. For most L<LWP::UserAgents>, it | ||||||||||||
| 195 | can be C<'private'> as it will probably not be shared with other processes on | ||||||||||||
| 196 | the same macine. If this module is being used at the serverside in a | ||||||||||||
| 197 | L<Plack::Middleware> then the cache will be used by all other clients connecting | ||||||||||||
| 198 | to the server, and thus should be set to C<'public'>. | ||||||||||||
| 199 | |||||||||||||
| 200 | Responses to Authenticated request should not be held in public caches and also | ||||||||||||
| 201 | those responses that specifacally have their cache-control headerfield set to | ||||||||||||
| 202 | C<'private'>. | ||||||||||||
| 203 | |||||||||||||
| 204 | =head2 cache_control_request | ||||||||||||
| 205 | |||||||||||||
| 206 | A string that contains the Cache-control header-field settings that will be sent | ||||||||||||
| 207 | as default with the request. So you do not have to set those each time. See | ||||||||||||
| 208 | RFC 7234 Section 5.2.1 for the list of available cache-control directives. | ||||||||||||
| 209 | |||||||||||||
| 210 | =head2 cache_control_response | ||||||||||||
| 211 | |||||||||||||
| 212 | Like the above, but those will be set for each response. This is useful for | ||||||||||||
| 213 | server side caching. See RFC 7234 Section 5.2.2. | ||||||||||||
| 214 | |||||||||||||
| 215 | =head2 forwarder | ||||||||||||
| 216 | |||||||||||||
| 217 | This CodeRef must be a callback function that accepts a L<HTTP::Request> and | ||||||||||||
| 218 | returns a L<HTTP::Response>. Since this module does not know how to do a request | ||||||||||||
| 219 | it will use the C<forwarder>. It will be used to sent of validation requests | ||||||||||||
| 220 | with C<If-None-Match> and/or C<If-Modified-Since> header-fields. Or if it does | ||||||||||||
| 221 | not have a stored response it will send the original full request (with the | ||||||||||||
| 222 | extra directives from C<cache_request>). | ||||||||||||
| 223 | |||||||||||||
| 224 | Failing to return a C<HTTP::Response> might cause the module to die or generate | ||||||||||||
| 225 | a response itself with status code C<502 Bad Gateway>. | ||||||||||||
| 226 | |||||||||||||
| 227 | =head1 METHODS | ||||||||||||
| 228 | |||||||||||||
| 229 | =head2 make_request | ||||||||||||
| 230 | |||||||||||||
| 231 | This is the only public provided method and will take a L<HTTP::Request>. Like | ||||||||||||
| 232 | described above, it might have to forward the (modified) request throug the | ||||||||||||
| 233 | CodeRef in the C<forwarder> attribute. | ||||||||||||
| 234 | |||||||||||||
| 235 | It will return a L<HTTP::Response> from cache or a new retrieved one. This might | ||||||||||||
| 236 | be a HTTP respons with a C<500 Error> message. | ||||||||||||
| 237 | |||||||||||||
| 238 | In other cases it might die and let the caller know what was wrong, or send | ||||||||||||
| 239 | another 5XX Error. | ||||||||||||
| 240 | |||||||||||||
| 241 | =cut | ||||||||||||
| 242 | |||||||||||||
| 243 | sub make_request { | ||||||||||||
| 244 | 10 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
make_request: called CC=7 SCAR=19.5
| |||||||||||||
| 245 | |||||||||||||
| 246 | 10 | ▶ | croak __PACKAGE__ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 247 | . " missing request" | ||||||||||||
| 248 | unless defined $_[0]; | ||||||||||||
| 249 | 9 | ▶ | croak __PACKAGE__ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 250 | . " request is not a HTTP::Request [$_[0]]" | ||||||||||||
| 251 | unless UNIVERSAL::isa($_[0],'HTTP::Request'); | ||||||||||||
| 252 | |||||||||||||
| 253 | 8 | my $presented_request = shift->clone; | |||||||||||
| 254 | |||||||||||||
| 255 | 8 | my @params = @_; | |||||||||||
| 256 | |||||||||||||
| 257 | # add the default Cache-Control request header-field | ||||||||||||
| 258 | 8 | ▶ | $presented_request->headers->push_header( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 259 | cache_control => $self->cache_control_request, | ||||||||||||
| 260 | ) if $self->cache_control_request(); | ||||||||||||
| 261 | |||||||||||||
| 262 | 8 | my $response; | |||||||||||
| 263 | |||||||||||||
| 264 | 8 | ▶ | unless ($self->cache) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 265 | 5 | $response = $self->_forward($presented_request, @params); | |||||||||||
| 266 | } else { | ||||||||||||
| 267 | 3 | ▶ | if (my $cache_resp = | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 268 | $self->_retrieve($presented_request) | ||||||||||||
| 269 | ) { | ||||||||||||
| 270 | 2 | $response = $cache_resp; | |||||||||||
| 271 | } else { | ||||||||||||
| 272 | 1 | $response = $self->_forward($presented_request, @params); | |||||||||||
| 273 | 1 | $self->_store($presented_request, $response); | |||||||||||
| 274 | } | ||||||||||||
| 275 | } | ||||||||||||
| 276 | |||||||||||||
| 277 | # add the default Cache-Control response header-field | ||||||||||||
| 278 | 8 | ▶ | $response->headers->push_header( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 279 | cache_control => $self->cache_control_response, | ||||||||||||
| 280 | ) if $self->cache_control_request; | ||||||||||||
| 281 | |||||||||||||
| 282 | 8 | return $response; | |||||||||||
| 283 | |||||||||||||
| 284 | } | ||||||||||||
| 285 | |||||||||||||
| 286 | sub _forward { | ||||||||||||
| 287 | 6 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_forward: called CC=2 SCAR=6.9
| |||||||||||||
| 288 | |||||||||||||
| 289 | 6 | my $forwarded_rqst = shift; | |||||||||||
| 290 | |||||||||||||
| 291 | 6 | my $forwarded_resp = $self->forwarder->($forwarded_rqst, @_); | |||||||||||
| 292 | |||||||||||||
| 293 | 6 | ▶ | unless ( UNIVERSAL::isa($forwarded_resp,'HTTP::Response') ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 294 | 1 | carp __PACKAGE__ | |||||||||||
| 295 | . " response is not a HTTP::Response [$forwarded_resp]"; | ||||||||||||
| 296 | # rescue from a failed forwarding, HTTP::Caching should not break | ||||||||||||
| 297 | 1 | $forwarded_resp = HTTP::Response->new(502); # Bad Gateway | |||||||||||
| 298 | } | ||||||||||||
| 299 | |||||||||||||
| 300 | 6 | return $forwarded_resp; | |||||||||||
| 301 | } | ||||||||||||
| 302 | |||||||||||||
| 303 | # _store may or may not store the response into the cache | ||||||||||||
| 304 | # | ||||||||||||
| 305 | # depending on the response it _may_store_in_cache() | ||||||||||||
| 306 | # | ||||||||||||
| 307 | sub _store { | ||||||||||||
| 308 | 1 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_store: called CC=3 SCAR=11.3
| |||||||||||||
| 309 | 1 | my $rqst = shift; | |||||||||||
| 310 | 1 | my $resp = shift; | |||||||||||
| 311 | |||||||||||||
| 312 | 1 | ▶ | return unless $self->_may_store_in_cache($rqst, $resp); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 313 | |||||||||||||
| 314 | 1 | ▶ | if ( my $resp_key = $self->_store_response($resp) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 315 | 1 | my $rqst_key = Digest::MD5::md5_hex($rqst->uri()->as_string); | |||||||||||
| 316 | 1 | $self->_insert_meta_dict( $rqst_key, $resp_key ); | |||||||||||
| 317 | 1 | return $resp_key; | |||||||||||
| 318 | } | ||||||||||||
| 319 | |||||||||||||
| 320 | return | ||||||||||||
| 321 | 0 | } | |||||||||||
| 322 | |||||||||||||
| 323 | sub _store_response { | ||||||||||||
| 324 | 1 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_store_response: called CC=2 SCAR=7.5
| |||||||||||||
| 325 | 1 | my $resp = shift; | |||||||||||
| 326 | |||||||||||||
| 327 | 1 | my $resp_key = Digest::MD5::md5_hex(Time::HiRes::time()); | |||||||||||
| 328 | |||||||||||||
| 329 | 1 | eval { $self->cache->set( $resp_key => $resp ) }; | |||||||||||
| 330 | 1 | ▶ | return $resp_key unless $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 331 | |||||||||||||
| 332 | 0 | croak __PACKAGE__ | |||||||||||
| 333 | . " could not store response in cache with key [$resp_key], $@"; | ||||||||||||
| 334 | |||||||||||||
| 335 | return | ||||||||||||
| 336 | 0 | } | |||||||||||
| 337 | |||||||||||||
| 338 | sub _insert_meta_dict { | ||||||||||||
| 339 | 1 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_insert_meta_dict: called CC=2 SCAR=7.0
| |||||||||||||
| 340 | 1 | my $rqst_key = shift; | |||||||||||
| 341 | 1 | my $resp_key = shift; | |||||||||||
| 342 | 1 | my $meta_data = shift; | |||||||||||
| 343 | |||||||||||||
| 344 | 1 | ▶ | my $meta_dict = $self->cache->get($rqst_key) || {}; | ||||||||||
|
Condition50%1/2
$self->cache->get($rqst_key) || {}
MC/DC0%0/1
$self->cache->get($rqst_key) || {}
$self->cache->get($rqst_key)
Truth table50%1/2
A
A= $self->cache->get($rqst_key)
| |||||||||||||
| 345 | 1 | $meta_dict->{$resp_key} = $meta_data; | |||||||||||
| 346 | 1 | $self->cache->set( $rqst_key => $meta_dict ); | |||||||||||
| 347 | |||||||||||||
| 348 | 1 | return $meta_dict; | |||||||||||
| 349 | } | ||||||||||||
| 350 | |||||||||||||
| 351 | sub _retrieve { | ||||||||||||
| 352 | 3 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_retrieve: called CC=2 SCAR=6.9
| |||||||||||||
| 353 | 3 | my $rqst = shift; | |||||||||||
| 354 | |||||||||||||
| 355 | 3 | my $rqst_key = Digest::MD5::md5_hex($rqst->uri()->as_string); | |||||||||||
| 356 | 3 | my $meta_dict = $self->_retrieve_meta_dict($rqst_key); | |||||||||||
| 357 | |||||||||||||
| 358 | 3 | ▶ | return unless $meta_dict; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 359 | |||||||||||||
| 360 | # TODO reduce meta_dict | ||||||||||||
| 361 | |||||||||||||
| 362 | # XXX pick one randomly from the list of keys | ||||||||||||
| 363 | 2 | my ($resp_key) = keys %$meta_dict; | |||||||||||
| 364 | |||||||||||||
| 365 | 2 | my $resp = $self->_retrieve_response($resp_key); | |||||||||||
| 366 | 2 | return $resp; | |||||||||||
| 367 | } | ||||||||||||
| 368 | |||||||||||||
| 369 | sub _retrieve_meta_dict { | ||||||||||||
| 370 | 3 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_retrieve_meta_dict: called CC=1 SCAR=0.0
| |||||||||||||
| 371 | 3 | my $rqst_key = shift; | |||||||||||
| 372 | |||||||||||||
| 373 | 3 | my $meta_dict = $self->cache->get($rqst_key); | |||||||||||
| 374 | |||||||||||||
| 375 | 3 | return $meta_dict; | |||||||||||
| 376 | } | ||||||||||||
| 377 | |||||||||||||
| 378 | sub _retrieve_response { | ||||||||||||
| 379 | 2 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_retrieve_response: called CC=2 SCAR=7.6
| |||||||||||||
| 380 | 2 | my $resp_key = shift; | |||||||||||
| 381 | |||||||||||||
| 382 | 2 | ▶ | if (my $resp = eval { $self->cache->get( $resp_key ) } ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 383 | 2 | return $resp | |||||||||||
| 384 | } | ||||||||||||
| 385 | |||||||||||||
| 386 | 0 | carp __PACKAGE__ | |||||||||||
| 387 | . " could not retrieve response from cache with key [$resp_key], $@"; | ||||||||||||
| 388 | |||||||||||||
| 389 | return | ||||||||||||
| 390 | 0 | } | |||||||||||
| 391 | |||||||||||||
| 392 | # _may_store_in_cache() | ||||||||||||
| 393 | # | ||||||||||||
| 394 | # based on some headers in the request, but mostly on those in the new response | ||||||||||||
| 395 | # the cache can hold a copy of it or not. | ||||||||||||
| 396 | # | ||||||||||||
| 397 | # see RFC 7234 Section 3: Storing Responses in Caches | ||||||||||||
| 398 | # | ||||||||||||
| 399 | sub _may_store_in_cache { | ||||||||||||
| 400 | 21 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_may_store_in_cache: called CC=37 SCAR=36.5
| |||||||||||||
| 401 | 21 | my $rqst = shift; | |||||||||||
| 402 | 21 | my $resp = shift; | |||||||||||
| 403 | |||||||||||||
| 404 | # $msg->header('cache-control) is supposed to return a list, but only works | ||||||||||||
| 405 | # if it has been generated as a list, not as string with 'comma' | ||||||||||||
| 406 | # $msg->header in scalar context gives a ', ' joined string | ||||||||||||
| 407 | # which we now split and trim whitespace | ||||||||||||
| 408 | my @rqst_directives = | ||||||||||||
| 409 | 21 | ▶ | map { my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str } | ||||||||||
|
Condition100%2/2
scalar $rqst->header('cache-control') || ''
MC/DC100%1/1
scalar $rqst->header('cache-control') || ''
scalar $rqst->header('cache-control')
Truth table100%2/2
A
A= scalar $rqst->header('cache-control')
| |||||||||||||
| 410 | split ',', scalar $rqst->header('cache-control') || ''; | ||||||||||||
| 411 | my @resp_directives = | ||||||||||||
| 412 | 21 | ▶ | map { my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str } | ||||||||||
|
Condition100%2/2
scalar $resp->header('cache-control') || ''
MC/DC100%1/1
scalar $resp->header('cache-control') || ''
scalar $resp->header('cache-control')
Truth table100%2/2
A
A= scalar $resp->header('cache-control')
| |||||||||||||
| 413 | split ',', scalar $resp->header('cache-control') || ''; | ||||||||||||
| 414 | |||||||||||||
| 415 | |||||||||||||
| 416 | # RFC 7234 Section 3 | ||||||||||||
| 417 | # | ||||||||||||
| 418 | # A cache MUST NOT store a response to any request, unless: | ||||||||||||
| 419 | |||||||||||||
| 420 | # RFC 7234 Section 3 #1 | ||||||||||||
| 421 | # | ||||||||||||
| 422 | # the request method is understood by the cache and defined as being | ||||||||||||
| 423 | # cacheable | ||||||||||||
| 424 | # | ||||||||||||
| 425 | 21 | do { | |||||||||||
| 426 | 21 | my $string = $rqst->method; | |||||||||||
| 427 | 21 | my $method = eval { HTTP::Method->new($string) }; | |||||||||||
| 428 | |||||||||||||
| 429 | 21 | ▶ | unless ($method) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 430 | 1 | ▶ | carp "NO CACHE: method is not understood: '$string'\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 431 | if $DEBUG; | ||||||||||||
| 432 | 1 | return 0 | |||||||||||
| 433 | } | ||||||||||||
| 434 | 20 | ▶ | unless ($method->is_method_cachable) { # XXX Fix cacheable | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 435 | 1 | ▶ | carp "NO CACHE: method is not cacheable: '$string'\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 436 | if $DEBUG; | ||||||||||||
| 437 | 1 | return 0 | |||||||||||
| 438 | } | ||||||||||||
| 439 | }; | ||||||||||||
| 440 | |||||||||||||
| 441 | # RFC 7234 Section 3 #2 | ||||||||||||
| 442 | # | ||||||||||||
| 443 | # the response status code is understood by the cache | ||||||||||||
| 444 | # | ||||||||||||
| 445 | 19 | do { | |||||||||||
| 446 | 19 | my $code = $resp->code; | |||||||||||
| 447 | 19 | my $message = eval { HTTP::Status::status_message($code) }; | |||||||||||
| 448 | |||||||||||||
| 449 | 19 | ▶ | unless ($message) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 450 | 1 | ▶ | carp "NO CACHE: response status code is not understood: '$code'\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 451 | if $DEBUG; | ||||||||||||
| 452 | 1 | return 0 | |||||||||||
| 453 | } | ||||||||||||
| 454 | }; | ||||||||||||
| 455 | |||||||||||||
| 456 | |||||||||||||
| 457 | # RFC 7234 Section 3 #3 | ||||||||||||
| 458 | # | ||||||||||||
| 459 | # the "no-store" cache directive (see Section 5.2) does not appear | ||||||||||||
| 460 | # in request or response header fields | ||||||||||||
| 461 | # | ||||||||||||
| 462 | 18 | do { | |||||||||||
| 463 | 18 | ▶ | if (any { lc $_ eq 'no-store' } @rqst_directives) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 464 | 1 | ▶ | carp "NO CACHE: 'no-store' appears in request cache directives\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 465 | if $DEBUG; | ||||||||||||
| 466 | 1 | return 0 | |||||||||||
| 467 | } | ||||||||||||
| 468 | 17 | ▶ | if (any { lc $_ eq 'no-store' } @resp_directives) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 469 | 1 | ▶ | carp "NO CACHE: 'no-store' appears in response cache directives\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 470 | if $DEBUG; | ||||||||||||
| 471 | 1 | return 0 | |||||||||||
| 472 | } | ||||||||||||
| 473 | }; | ||||||||||||
| 474 | |||||||||||||
| 475 | # RFC 7234 Section 3 #4 | ||||||||||||
| 476 | # | ||||||||||||
| 477 | # the "private" response directive (see Section 5.2.2.6) does not | ||||||||||||
| 478 | # appear in the response, if the cache is shared | ||||||||||||
| 479 | # | ||||||||||||
| 480 | 16 | ▶ | if ($self->is_shared) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 481 | 6 | ▶ | if (any { lc $_ eq 'private' } @resp_directives) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 482 | 1 | ▶ | carp "NO CACHE: 'private' appears in cache directives when shared\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 483 | if $DEBUG; | ||||||||||||
| 484 | 1 | return 0 | |||||||||||
| 485 | } | ||||||||||||
| 486 | }; | ||||||||||||
| 487 | |||||||||||||
| 488 | # RFC 7234 Section 3 #5 | ||||||||||||
| 489 | # | ||||||||||||
| 490 | # the Authorization header field (see Section 4.2 of [RFC7235]) does | ||||||||||||
| 491 | # not appear in the request, if the cache is shared, unless the | ||||||||||||
| 492 | # response explicitly allows it (see Section 3.2) | ||||||||||||
| 493 | # | ||||||||||||
| 494 | 15 | ▶ | if ($self->is_shared) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 495 | 5 | ▶ | if ($rqst->header('Authorization')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 496 | 4 | ▶ | if (any { lc $_ eq 'must-revalidate' } @resp_directives) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 497 | 1 | ▶ | carp "DO CACHE: 'Authorization' appears: must-revalidate\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 498 | if $DEBUG; | ||||||||||||
| 499 | 1 | return 1 | |||||||||||
| 500 | } | ||||||||||||
| 501 | 3 | ▶ | if (any { lc $_ eq 'public' } @resp_directives) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 502 | 1 | ▶ | carp "DO CACHE: 'Authorization' appears: public\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 503 | if $DEBUG; | ||||||||||||
| 504 | 1 | return 1 | |||||||||||
| 505 | } | ||||||||||||
| 506 | 2 | ▶ | if (any { lc $_ =~ m/^s-maxage=\d+$/ } @resp_directives) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 507 | 1 | ▶ | carp "DO CACHE: 'Authorization' appears: s-maxage\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 508 | if $DEBUG; | ||||||||||||
| 509 | 1 | return 1 | |||||||||||
| 510 | } | ||||||||||||
| 511 | 1 | ▶ | carp "NO CACHE: 'Authorization' appears in request when shared\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 512 | if $DEBUG; | ||||||||||||
| 513 | 1 | return 0 | |||||||||||
| 514 | } | ||||||||||||
| 515 | }; | ||||||||||||
| 516 | |||||||||||||
| 517 | |||||||||||||
| 518 | # RFC 7234 Section 3 #6 | ||||||||||||
| 519 | # | ||||||||||||
| 520 | # the response either: | ||||||||||||
| 521 | # | ||||||||||||
| 522 | # - contains an Expires header field (see Section 5.3) | ||||||||||||
| 523 | # | ||||||||||||
| 524 | 11 | do { | |||||||||||
| 525 | 11 | my $expires_at = $resp->header('Expires'); | |||||||||||
| 526 | |||||||||||||
| 527 | 11 | ▶ | if ($expires_at) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 528 | 1 | ▶ | carp "OK CACHE: 'Expires' at: $expires_at\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 529 | if $DEBUG; | ||||||||||||
| 530 | 1 | return 1 | |||||||||||
| 531 | } | ||||||||||||
| 532 | }; | ||||||||||||
| 533 | |||||||||||||
| 534 | # - contains a max-age response directive (see Section 5.2.2.8) | ||||||||||||
| 535 | # | ||||||||||||
| 536 | 10 | do { | |||||||||||
| 537 | 10 | ▶ | if (any { lc $_ =~ m/^max-age=\d+$/ } @resp_directives) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 538 | 1 | ▶ | carp "DO CACHE: 'max-age' appears in response cache directives\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 539 | if $DEBUG; | ||||||||||||
| 540 | 1 | return 1 | |||||||||||
| 541 | } | ||||||||||||
| 542 | }; | ||||||||||||
| 543 | |||||||||||||
| 544 | # - contains a s-maxage response directive (see Section 5.2.2.9) | ||||||||||||
| 545 | # and the cache is shared | ||||||||||||
| 546 | # | ||||||||||||
| 547 | 9 | ▶ | if ($self->is_shared) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 548 | 1 | ▶ | if (any { lc $_ =~ m/^s-maxage=\d+$/ } @resp_directives) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||
| 549 | 1 | ▶ | carp "DO CACHE: 's-maxage' appears in response cache directives\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 550 | if $DEBUG; | ||||||||||||
| 551 | 1 | return 1 | |||||||||||
| 552 | } | ||||||||||||
| 553 | }; | ||||||||||||
| 554 | |||||||||||||
| 555 | |||||||||||||
| 556 | # - contains a Cache Control Extension (see Section 5.2.3) that | ||||||||||||
| 557 | # allows it to be cache | ||||||||||||
| 558 | # | ||||||||||||
| 559 | # TODO it looks like this is only used for special defined cache-control | ||||||||||||
| 560 | # directives. As such, those need special treatment. | ||||||||||||
| 561 | # It does not seem a good idea to hardcode those here, a config would | ||||||||||||
| 562 | # be a better solution. | ||||||||||||
| 563 | |||||||||||||
| 564 | |||||||||||||
| 565 | # - has a status code that is defined as cacheable by default (see | ||||||||||||
| 566 | # Section 4.2.2) | ||||||||||||
| 567 | # | ||||||||||||
| 568 | 8 | do { | |||||||||||
| 569 | 8 | my $code = $resp->code; | |||||||||||
| 570 | |||||||||||||
| 571 | 8 | ▶ | if (HTTP::Status::is_cacheable_by_default($code)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 572 | 2 | ▶ | carp "DO CACHE: status code is cacheable by default: '$code'\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 573 | if $DEBUG; | ||||||||||||
| 574 | 2 | return 1 | |||||||||||
| 575 | } | ||||||||||||
| 576 | }; | ||||||||||||
| 577 | |||||||||||||
| 578 | # - contains a public response directive (see Section 5.2.2.5) | ||||||||||||
| 579 | # | ||||||||||||
| 580 | 6 | do { | |||||||||||
| 581 | 6 | ▶ | if (any { lc $_ eq 'public' } @resp_directives) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 582 | 1 | ▶ | carp "DO CACHE: 'public' appears in response cache directives\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 583 | if $DEBUG; | ||||||||||||
| 584 | 1 | return 1 | |||||||||||
| 585 | } | ||||||||||||
| 586 | }; | ||||||||||||
| 587 | |||||||||||||
| 588 | # Falls trough ... SHOULD NOT store in cache | ||||||||||||
| 589 | # | ||||||||||||
| 590 | 5 | ▶ | carp "NO CACHE: Does not match the six criteria above\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 591 | if $DEBUG; | ||||||||||||
| 592 | |||||||||||||
| 593 | 5 | return undef; | |||||||||||
| 594 | } | ||||||||||||
| 595 | |||||||||||||
| 596 | # HTTP::Status::is_cacheable_by_default | ||||||||||||
| 597 | # | ||||||||||||
| 598 | # that subroutine is missing. Until it's added there, it's been monkey-patched | ||||||||||||
| 599 | # here. | ||||||||||||
| 600 | # | ||||||||||||
| 601 | # RFC 7231 - HTTP/1.1 Semantics and Content | ||||||||||||
| 602 | # Section 6.1. Overview of Status Codes | ||||||||||||
| 603 | # | ||||||||||||
| 604 | # Responses with status codes that are defined as cacheable by default | ||||||||||||
| 605 | # (e.g., 200, 203, 204, 206, 300, 301, 404, 405, 410, 414, and 501 in | ||||||||||||
| 606 | # this specification) can be reused by a cache with heuristic | ||||||||||||
| 607 | # expiration unless otherwise indicated by the method definition or | ||||||||||||
| 608 | # explicit cache controls [RFC7234]; all other status codes are not | ||||||||||||
| 609 | # cacheable by default. | ||||||||||||
| 610 | # | ||||||||||||
| 611 | my $handle = Monkey::Patch::Action::patch_package ( | ||||||||||||
| 612 | 'HTTP::Status', 'is_cacheable_by_default', 'add', sub { | ||||||||||||
| 613 | 8 | ▶ | my $code = shift; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 614 | 78 | $code = $code +0; | |||||||||||
| 615 | |||||||||||||
| 616 | 5 | return any {$_ == $code} (200,203,204,206,300,301,404,405,410,414,501) | |||||||||||
| 617 | } | ||||||||||||
| 618 | ); | ||||||||||||
| 619 | |||||||||||||
| 620 | |||||||||||||
| 621 | 1; | ||||||||||||