| 1 | package LWP::UserAgent::Caching; | ||||||||||||
| 2 | |||||||||||||
| 3 | =head1 NAME | ||||||||||||
| 4 | |||||||||||||
| 5 | LWP::UserAgent::Caching - HTTP::Casing based UserAgent, finally done right | ||||||||||||
| 6 | |||||||||||||
| 7 | =cut | ||||||||||||
| 8 | |||||||||||||
| 9 | 1 | our $VERSION = '0.04'; | |||||||||||
| 10 | |||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 1 | ▶ | use parent 'LWP::UserAgent'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 1 | ▶ | use HTTP::Caching; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||
| 18 | |||||||||||||
| 19 | use LWP::UserAgent::Caching; | ||||||||||||
| 20 | |||||||||||||
| 21 | my $cache = CHI->new( | ||||||||||||
| 22 | driver => 'File', | ||||||||||||
| 23 | root_dir => '/tmp/LWP_UserAgent_Caching', | ||||||||||||
| 24 | file_extension => '.cache', | ||||||||||||
| 25 | l1_cache => { | ||||||||||||
| 26 | driver => 'Memory', | ||||||||||||
| 27 | global => 1, | ||||||||||||
| 28 | max_size => 1024*1024 | ||||||||||||
| 29 | }, | ||||||||||||
| 30 | ); | ||||||||||||
| 31 | |||||||||||||
| 32 | my $ua = LWP::UserAgent::Caching->new( | ||||||||||||
| 33 | http_caching => { | ||||||||||||
| 34 | cache => $cache, | ||||||||||||
| 35 | type => 'private', | ||||||||||||
| 36 | request_directives => ( | ||||||||||||
| 37 | 'max-age=86400', # 24hrs | ||||||||||||
| 38 | 'min-fresh=60', # not over due within the next minute | ||||||||||||
| 39 | ), | ||||||||||||
| 40 | }, | ||||||||||||
| 41 | # more LWP::UserAgent options | ||||||||||||
| 42 | ); | ||||||||||||
| 43 | |||||||||||||
| 44 | my $rqst = HTTP::Request->new( GET => 'http://example.com' ); | ||||||||||||
| 45 | |||||||||||||
| 46 | $rqst->header( cache_control => 'no-cache' ); # Oh... now we bypass it ? | ||||||||||||
| 47 | $rqst->header( accept_language => 'nl, en-GB; q=0.9, en; 0.8, *' ); | ||||||||||||
| 48 | |||||||||||||
| 49 | my $resp = $ua->request($rqst); | ||||||||||||
| 50 | |||||||||||||
| 51 | |||||||||||||
| 52 | =head1 DESCRIPTION | ||||||||||||
| 53 | |||||||||||||
| 54 | C<LWP::UserAgent::Caching> gives you RFC compliant caching. It respects the old | ||||||||||||
| 55 | HTTP/1 headerfields like 'Expires' but also implements the HTTP/1.1 | ||||||||||||
| 56 | 'Cache-Control' directives. | ||||||||||||
| 57 | |||||||||||||
| 58 | Unlike many other cachng useragents, this one does actually invalidate the cache | ||||||||||||
| 59 | after a non-error response returned by a non-safe request (like DELETE). | ||||||||||||
| 60 | |||||||||||||
| 61 | =head1 METHODS | ||||||||||||
| 62 | |||||||||||||
| 63 | Since it's a subclass of the standard LWP::UserAgent, it inherrits all those. In | ||||||||||||
| 64 | this module we also implemented the shortcuts from L<HTTP::Request::Common> so | ||||||||||||
| 65 | that tehy will not call the parrent class | ||||||||||||
| 66 | |||||||||||||
| 67 | =head1 SEE ALSO | ||||||||||||
| 68 | |||||||||||||
| 69 | L<HTTP::Caching> The RFC 7234 compliant brains | ||||||||||||
| 70 | - DO NEVER USE THAT MODULE DIRECTLY | ||||||||||||
| 71 | |||||||||||||
| 72 | =cut | ||||||||||||
| 73 | |||||||||||||
| 74 | sub new { | ||||||||||||
| 75 | 2 | ▶ | my ( $class, %params ) = @_; | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||
| 76 | |||||||||||||
| 77 | 2 | ▶ | my $http_caching = delete $params{http_caching} || {}; | ||||||||||
|
Condition50%1/2
delete $params{'http_caching'} || {}
MC/DC0%0/1
delete $params{'http_caching'} || {}
delete $params{'http_caching'}
Truth table50%1/2
A
A= delete $params{'http_caching'}
| |||||||||||||
| 78 | |||||||||||||
| 79 | 2 | my $self = $class->SUPER::new(@_); | |||||||||||
| 80 | |||||||||||||
| 81 | $self->{http_caching} = HTTP::Caching->new( | ||||||||||||
| 82 | cache => $http_caching->{cache}, | ||||||||||||
| 83 | # cache_meta => $http_caching->{cache_meta} || $params{cache}, | ||||||||||||
| 84 | cache_type => $http_caching->{type} || 'private', | ||||||||||||
| 85 | cache_control_request => $http_caching->{request_directives}, | ||||||||||||
| 86 | 0 | ▶ | forwarder => sub { $self->SUPER::request(@_) } | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 87 | 2 | ▶ | ); | ||||||||||
|
Condition50%1/2
$http_caching->{'type'} || 'private'
MC/DC0%0/1
$http_caching->{'type'} || 'private'
$http_caching->{'type'}
Truth table50%1/2
A
A= $http_caching->{'type'}
| |||||||||||||
| 88 | |||||||||||||
| 89 | 2 | return $self; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | sub request { | ||||||||||||
| 93 | 0 | ▶ | return shift->{http_caching}->make_request(@_); | ||||||||||
|
Subroutine
request: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | |||||||||||||
| 97 | # | ||||||||||||
| 98 | # Now the shortcuts... | ||||||||||||
| 99 | # | ||||||||||||
| 100 | sub get { | ||||||||||||
| 101 | 0 | ▶ | require HTTP::Request::Common; | ||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 102 | 0 | my($self, @parameters) = @_; | |||||||||||
| 103 | 0 | my @suff = $self->_process_colonic_headers(\@parameters,1); | |||||||||||
| 104 | 0 | return $self->request( HTTP::Request::Common::GET( @parameters ), @suff ); | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | sub post { | ||||||||||||
| 108 | 0 | ▶ | require HTTP::Request::Common; | ||||||||||
|
Subroutine
post: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 109 | 0 | my($self, @parameters) = @_; | |||||||||||
| 110 | 0 | ▶ | my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1)); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 111 | 0 | return $self->request( HTTP::Request::Common::POST( @parameters ), @suff ); | |||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | sub head { | ||||||||||||
| 115 | 0 | ▶ | require HTTP::Request::Common; | ||||||||||
|
Subroutine
head: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 116 | 0 | my($self, @parameters) = @_; | |||||||||||
| 117 | 0 | my @suff = $self->_process_colonic_headers(\@parameters,1); | |||||||||||
| 118 | 0 | return $self->request( HTTP::Request::Common::HEAD( @parameters ), @suff ); | |||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | sub put { | ||||||||||||
| 122 | 0 | ▶ | require HTTP::Request::Common; | ||||||||||
|
Subroutine
put: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 123 | 0 | my($self, @parameters) = @_; | |||||||||||
| 124 | 0 | ▶ | my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1)); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | 0 | return $self->request( HTTP::Request::Common::PUT( @parameters ), @suff ); | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | sub delete { | ||||||||||||
| 129 | 0 | ▶ | require HTTP::Request::Common; | ||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 130 | 0 | my($self, @parameters) = @_; | |||||||||||
| 131 | 0 | my @suff = $self->_process_colonic_headers(\@parameters,1); | |||||||||||
| 132 | 0 | return $self->request( HTTP::Request::Common::DELETE( @parameters ), @suff ); | |||||||||||
| 133 | } | ||||||||||||
| 134 | |||||||||||||
| 135 | |||||||||||||
| 136 | 1 | 1; | |||||||||||