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