Role/CHICaching.pm

filter: Statementstmt 96.3% 53 / 55 Branchbran 54.1% 13 / 24 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 80.0% 76 / 95 CC 15 SCAR 26.5
CC
15
Cov
81%
__ANON__
20.2
BEGIN
1.0
BEGIN
1.0
CRAP 14.1
↑ Summary Role/CHICaching/VaryNotAsterisk.pm »
1 package LWP::UserAgent::Role::CHICaching;
2  
3 3 ▶use 5.006000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Types::Standard qw(Str Bool Ref InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Types::URI -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $AUTHORITY = 'cpan:KJETILK';
10 3 our $VERSION = '0.04';
11  
12 =pod
13  
14 =encoding utf-8
15  
16 =head1 NAME
17  
18 LWP::UserAgent::Role::CHICaching - A role to allow LWP::UserAgent to cache with CHI
19  
20 =head1 SYNOPSIS
21  
22 Compose it into a class, e.g.
23  
24 package LWP::UserAgent::MyCacher;
25 use Moo;
26 extends 'LWP::UserAgent';
27 with 'LWP::UserAgent::Role::CHICaching',
28 'LWP::UserAgent::Role::CHICaching::SimpleKeyGen',
29 'LWP::UserAgent::Role::CHICaching::SimpleMungeResponse';
30  
31  
32 =head1 DESCRIPTION
33  
34 This is a role for creating caching user agents. When the client makes
35 a request to the server, sometimes the response should be cached, so
36 that no actual request has to be sent at all, or possibly just a
37 request to validate the cache. HTTP 1.1 defines how to do this. This
38 role makes it possible to use the very flexible L<CHI> module to
39 manage such a cache. See L<LWP::UserAgent::CHICaching> for a finished
40 class you can use.
41  
42  
43 =head2 Attributes and Methods
44  
45 =over
46  
47 =item C<< cache >>
48  
49 Used to set the C<CHI> object to be used as cache in the constructor.
50  
51  
52  
53 =item C<< request_uri >>
54  
55 The Request-URI of the request. When set, it will clear the C<key>,
56 but should probably be left to be used internally for now.
57  
58 =item C<< request >>
59  
60 Wrapping L<LWP::UserAgent>'s request method.
61  
62 =item C<< is_shared >>
63  
64 A boolean value to set whether the cache can be shared. The default is
65 that it is.
66  
67 =item C<< heuristics_opts >>
68  
69 A hashref that is passed to the C<freshness_lifetime> method of
70 L<HTTP::Response>, and used to determine the behaviour of the
71 heuristic lifetime. By default, heuristic freshness lifetime is off,
72 only standards-compliant freshness lifetime (i.e. based on the
73 C<Cache-Control> and C<Expires> headers) are used.
74  
75 =back
76  
77 =head2 Implemented elsewhere
78  
79 The following are required by this role, but implemented
80 elsewhere. See L<LWP::UserAgent::Role::CHICaching::SimpleKeyGen> and
81 L<LWP::UserAgent::Role::CHICaching::SimpleMungeResponse> for further explanations.
82  
83 =over
84  
85 =item C<< key >>, C<< clear_key >>
86  
87 The key to use for a response.
88  
89 =item C<< cache_vary($response) >>
90  
91 A method that returns true if the response may be cached even if it
92 contains a C<Vary> header, false otherwise. The L<HTTP::Response>
93 object will be passed to it as a parameter.
94  
95 =item C<< cache_set($response, $expires_in) >>
96  
97 A method that takes the L<HTTP::Response> from the client and an
98 expires time in seconds and set the actual cache.
99  
100 =item C<< finalize($cached) >>
101  
102 A method that takes the cached entry as an argument, and will return a
103 L<HTTP::Response> to return to the client.
104  
105 =back
106  
107 =cut
108  
109 3 has cache => (
110 is => 'ro',
111 isa => InstanceOf['CHI::Driver'],
112 required => 1,
113 );
114  
115  
116 3 requires 'key';
117 3 requires 'cache_vary';
118 3 requires 'finalize';
119 3 requires 'cache_set';
120  
121 has request_uri => (
122 is =>'rw',
123 isa => Uri,
124 coerce => 1,
125 9 ▶ trigger => sub { shift->clear_key },
Subroutine __ANON__: called CC=1 SCAR=0.0
126 3 );
127  
128 3 has is_shared => (
129 is => 'rw',
130 isa => Bool,
131 default => 1);
132  
133 has heuristics_opts => (
134 is => 'rw',
135 isa => Ref['HASH'],
136 7 ▶ default => sub {return {heuristic_expiry => 0}}
Subroutine __ANON__: called CC=1 SCAR=0.0
137 3 );
138  
139 9 ▶around request => sub {
Subroutine __ANON__: called CC=15 SCAR=30.1
140 9 my ($orig, $self) = (shift, shift);
141 9 my @args = @_;
142 9 my $request = $args[0];
143  
144 9 ▶ return $self->$orig(@args) if $request->method ne 'GET';
Branch: 1 branch
BranchTrueFalseTotal
if $request->method ne "GET" 0 9 9
145  
146 9 $self->request_uri($request->uri);
147  
148 9 my $cached = $self->cache->get($self->key); # CHI will take care of expiration
149  
150 9 my $expires_in = 0;
151 9 ▶ if (defined($cached)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cached) { } 2 7 9
152 ######## Here, we decide whether to reuse a cached response.
153 ######## The standard describing this is:
154 ######## http://tools.ietf.org/html/rfc7234#section-4
155 2 $cached->header('Age' => $cached->current_age);
156 2 return $self->finalize($cached); # TODO: Deal with no-transform
157 } else {
158 7 my $res = $self->$orig(@args);
159  
160 ######## Here, we decide whether to store a response
161 ######## This is defined in:
162 ######## http://tools.ietf.org/html/rfc7234#section-3
163 # Quoting the standard
164  
165 ## A cache MUST NOT store a response to any request, unless:
166
167 ## o The request method is understood by the cache and defined as being
168 ## cacheable, and
169 # TODO: Ok, only GET supported, see above
170  
171 ## o the response status code is understood by the cache, and
172 7 ▶ if ($res->is_success) { # TODO: Cache only successful responses for now
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 7 0 7
173  
174 # First, we deal superficially with the Vary header, for the
175 # full complexity see
176 # http://tools.ietf.org/html/rfc7234#section-4.1
177 7 ▶ return $res unless ($self->cache_vary($res));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cache_vary($res) 3 4 7
178  
179 4 my $cc = join('|',$res->header('Cache-Control')); # Since we only do string matching, this should be ok
180 4 ▶ if (defined($cc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cc) 4 0 4
181 ## o the "no-store" cache directive (see Section 5.2) does not appear
182 ## in request or response header fields, and
183 4 ▶ return $res if ($cc =~ m/no-store|no-cache/); # TODO: Improve no-cache use
Branch: 1 branch
BranchTrueFalseTotal
if $cc =~ /no-store|no-cache/ 0 4 4
184 4 ▶ if ($self->is_shared) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_shared) 4 0 4
185 ## o the "private" response directive (see Section 5.2.2.6) does not
186 ## appear in the response, if the cache is shared, and
187 4 ▶ return $res if ($cc =~ m/private/);
Branch: 1 branch
BranchTrueFalseTotal
if $cc =~ /private/ 0 4 4
188 ## o the Authorization header field (see Section 4.2 of [RFC7235]) does
189 ## not appear in the request, if the cache is shared, unless the
190 ## response explicitly allows it (see Section 3.2), and
191 4 ▶ if ($request->header('Authorization')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->header("Authorization")) 0 4 4
192 0 ▶ return $res unless ($cc =~ m/public|must-revalidate|s-maxage/);
Branch: 1 branch
BranchTrueFalseTotal
unless $cc =~ /public|must-revalidate|s-maxage/ 0 0 0
193 }
194 }
195 ## o the response either:
196 ##
197 ## * contains an Expires header field (see Section 5.3), or
198 ## * contains a max-age response directive (see Section 5.2.2.8), or
199 # This is implemented in HTTP::Response, but it relates to the old RFC2616
200 # and doesn't support shared caches.
201 4 ▶ $expires_in = $res->freshness_lifetime(%{$self->heuristics_opts}) || 0;
Condition50%1/2
$res->freshness_lifetime(%{$self->heuristics_opts;}) || 0
l!l
40
MC/DC0%0/1
$res->freshness_lifetime(%{$self->heuristics_opts;}) || 0
$res->freshness_lifetime(%{$self->heuristics_opts;})
Truth table50%1/2
A
A= $res->freshness_lifetime(%{$self->heuristics_opts;})
Aresult
11
00
202  
203 ## * contains a s-maxage response directive (see Section 5.2.2.9)
204 ## and the cache is shared, or
205  
206 4 ▶ if ($self->is_shared && ($cc =~ m/s-maxage\s*=\s*(\d+)/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_shared and $cc =~ /s-maxage\s*=\s*(\d+)/) 0 4 4
Condition33%1/3
$self->is_shared and $cc =~ /s-maxage\s*=\s*(\d+)/
!ll&&!rl&&r
040
MC/DC0%0/2
$self->is_shared and $cc =~ /s-maxage\s*=\s*(\d+)/
$self->is_shared $cc =~ /s-maxage\s*=\s*(\d+)/
Truth table33%1/3
A and B
A= $self->is_sharedB= $cc =~ /s-maxage\s*=\s*(\d+)/
ABresult
0X0
100
111
207 0 $expires_in = $1;
208 }
209  
210  
211  
212 ## * contains a Cache Control Extension (see Section 5.2.3) that
213 ## allows it to be cached, or
214 # TODO
215  
216 ## * has a status code that is defined as cacheable by default (see
217 ## Section 4.2.2), or
218 # TODO: We only do GET
219  
220 ## * contains a public response directive (see Section 5.2.2.5).
221 # We do not specifically deal with this
222  
223 }
224 4 ▶ if ($expires_in > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires_in > 0) 4 0 4
225 4 $self->cache_set($res, $expires_in);
226 }
227 }
228 4 return $res;
229 }
230 3 };
231  
232 3 1;
233  
234 __END__
↑ Summary Role/CHICaching/VaryNotAsterisk.pm »