Mojo/UserAgent/CookieJar.pm

filter: Statementstmt 100% 104 / 104 Branchbran 98.3% 59 / 60 Conditioncond 94.5% 35 / 37 MC/DCmcdc 85.7% 18 / 21 Subroutinesub 100% 16 / 16 Podpod 100% 9 / 9 totaltotal 97.5% 241 / 247 CC 50 SCAR 20.9
CC
50
Cov
99%
add
12.0
find
11.0
collect
10.0
CRAP 8.0
« Mojo/SSE.pm ↑ Summary Mojolicious/Controller.pm »
1 package Mojo::UserAgent::CookieJar;
2 24 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 24 ▶use Mojo::Cookie::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use Mojo::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 24 has [qw(file ignore)];
10 24 has max_cookie_size => 4096;
11  
12 24 my $COMMENT = "# Netscape HTTP Cookie File\n# This file was generated by Mojolicious! Edit at your own risk.\n\n";
13  
14 39 ▶sub add {
Subroutine add: called CC=12 SCAR=24.8
Pod documented
15 39 my ($self, @cookies) = @_;
16  
17 39 my $size = $self->max_cookie_size;
18 39 for my $cookie (@cookies) {
19  
20 # Convert max age to expires
21 47 my $age = $cookie->max_age;
22 47 ▶ $cookie->expires($age <= 0 ? 0 : $age + time) if looks_like_number $age;
Branch: 2 branches
BranchTrueFalseTotal
if &looks_like_number($age) 3 44 47
$age <= 0 ? : 2 1 3
23  
24 # Check cookie size
25 47 ▶ next if length($cookie->value // '') > $size;
Branch: 1 branch
BranchTrueFalseTotal
if length($cookie->value // "") > $size 1 46 47
Condition100%2/2
$cookie->value // ""
l!l
434
MC/DC100%1/1
$cookie->value // ""
$cookie->value
Truth table100%2/2
A
A= $cookie->value
Aresult
11
00
26  
27 # Replace cookie
28 46 ▶ next unless my $domain = lc($cookie->domain // '');
Branch: 1 branch
BranchTrueFalseTotal
unless my $domain = lc($cookie->domain // "") 1 45 46
Condition100%2/2
$cookie->domain // ""
l!l
451
MC/DC100%1/1
$cookie->domain // ""
$cookie->domain
Truth table100%2/2
A
A= $cookie->domain
Aresult
11
00
29 45 ▶ next unless my $path = $cookie->path;
Branch: 1 branch
BranchTrueFalseTotal
unless my $path = $cookie->path 2 43 45
30 43 ▶ next unless length(my $name = $cookie->name // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length(my $name = $cookie->name // "") 1 42 43
Condition100%2/2
$cookie->name // ""
l!l
421
MC/DC100%1/1
$cookie->name // ""
$cookie->name
Truth table100%2/2
A
A= $cookie->name
Aresult
11
00
31 42 ▶ my $jar = $self->{jar}{$domain} //= [];
Condition100%2/2
$self->{"jar"}{$domain} //= []
l!l
1230
MC/DC100%1/1
$self->{"jar"}{$domain} //= []
$self->{"jar"}{$domain}
Truth table100%2/2
A
A= $self->{"jar"}{$domain}
Aresult
11
00
32 42 @$jar = (grep({ _compare($_, $path, $name, $domain) } @$jar), $cookie);
33 }
34  
35 39 return $self;
36 }
37  
38 27 ▶sub all {
Subroutine all: called CC=1 SCAR=0.0
Pod documented
39 27 my $jar = shift->{jar};
40 27 return [map { @{$jar->{$_}} } sort keys %$jar];
41 }
42  
43 171 ▶sub collect {
Subroutine collect: called CC=10 SCAR=23.0
Pod documented
44 171 my ($self, $tx) = @_;
45  
46 171 my $url = $tx->req->url;
47 171 for my $cookie (@{$tx->res->cookies}) {
48  
49 # Validate domain
50 26 my $host = lc $url->ihost;
51 26 ▶ $cookie->domain($host)->host_only(1) unless $cookie->domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $cookie->domain 8 18 26
52 26 my $domain = lc $cookie->domain;
53 26 ▶ if (my $cb = $self->ignore) { next if $cb->($cookie) }
Branch: 2 branches
BranchTrueFalseTotal
if (my $cb = $self->ignore) 2 24 26
if &$cb($cookie) 1 1 2
54 25 ▶ next if $host ne $domain && ($host !~ /\Q.$domain\E$/ || $host =~ /\.\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $host ne $domain and !($host =~ /\Q.$domain\E$/u) || $host =~ /\.\d+$/u 8 17 25
Condition100%6/6
$host ne $domain and !($host =~ /\Q.$domain\E$/u) || $host =~ /\.\d+$/u
!ll&&!rl&&r
1438
not $host =~ /\Q.$domain\E$/u or $host =~ /\.\d+$/u
l!l&&r!l&&!r
533
MC/DC100%3/3
$host ne $domain and !($host =~ /\Q.$domain\E$/u) || $host =~ /\.\d+$/u
$host ne $domain not $host =~ /\Q.$domain\E$/u $host =~ /\.\d+$/u
Truth table100%4/4
A and not(B) or C
A= $host ne $domainB= not $host =~ /\Q.$domain\E$/uC= $host =~ /\.\d+$/u
ABCresult
0XX0
1000
11X1
1011
55  
56 # Validate path
57 17 ▶ my $path = $cookie->path // $url->path->to_dir->to_abs_string;
Condition66%2/3
$cookie->path // $url->path->to_dir->to_abs_string
l!l&&r!l&&!r
980
MC/DC0%0/2
$cookie->path // $url->path->to_dir->to_abs_string
$cookie->path $url->path->to_dir->to_abs_string
Truth table66%2/3
A // B
A= $cookie->pathB= $url->path->to_dir->to_abs_string
ABresult
1X1
011
000
58 17 $path = Mojo::Path->new($path)->trailing_slash(0)->to_abs_string;
59 17 ▶ next unless _path($path, $url->path->to_abs_string);
Branch: 1 branch
BranchTrueFalseTotal
unless _path($path, $url->path->to_abs_string) 3 14 17
60 14 $self->add($cookie->path($path));
61 }
62 }
63  
64 2 ▶sub empty {
Subroutine empty: called CC=1 SCAR=0.0
Pod documented
65 2 my $self = shift;
66 2 delete $self->{jar};
67 2 return $self;
68 }
69  
70 33 ▶sub find {
Subroutine find: called CC=11 SCAR=24.0
Pod documented
71 33 my ($self, $url) = @_;
72  
73 33 my @found;
74 33 my $domain = my $host = lc $url->ihost;
75 33 my $path = $url->path->to_abs_string;
76 33 ▶ while ($domain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain) 74 33 107
77 74 ▶ next unless my $old = $self->{jar}{$domain};
Branch: 1 branch
BranchTrueFalseTotal
unless my $old = $self->{"jar"}{$domain} 37 37 74
78  
79 # Grab cookies
80 37 my $new = $self->{jar}{$domain} = [];
81 37 for my $cookie (@$old) {
82 47 ▶ next if $cookie->host_only && $host ne $cookie->domain;
Branch: 1 branch
BranchTrueFalseTotal
if $cookie->host_only and $host ne $cookie->domain 1 46 47
Condition100%3/3
$cookie->host_only and $host ne $cookie->domain
!ll&&!rl&&r
4331
MC/DC100%2/2
$cookie->host_only and $host ne $cookie->domain
$cookie->host_only $host ne $cookie->domain
Truth table100%3/3
A and B
A= $cookie->host_onlyB= $host ne $cookie->domain
ABresult
0X0
100
111
83  
84 # Check if cookie has expired
85 46 ▶ if (defined(my $expires = $cookie->expires)) { next if time > $expires }
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $expires = $cookie->expires)) 2 44 46
if time > $expires 2 0 2
86 44 push @$new, $cookie;
87  
88 # Taste cookie
89 44 ▶ next if $cookie->secure && $url->protocol ne 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $cookie->secure and $url->protocol ne "https" 1 43 44
Condition100%3/3
$cookie->secure and $url->protocol ne "https"
!ll&&!rl&&r
4211
MC/DC100%2/2
$cookie->secure and $url->protocol ne "https"
$cookie->secure $url->protocol ne "https"
Truth table100%3/3
A and B
A= $cookie->secureB= $url->protocol ne "https"
ABresult
0X0
100
111
90 43 ▶ next unless _path($cookie->path, $path);
Branch: 1 branch
BranchTrueFalseTotal
unless _path($cookie->path, $path) 2 41 43
91 41 my $name = $cookie->name;
92 41 my $value = $cookie->value;
93 41 push @found, Mojo::Cookie::Request->new(name => $name, value => $value);
94 }
95 }
96  
97 # Remove another part
98 74 continue { $domain =~ s/^[^.]*\.*// }
99  
100 33 return \@found;
101 }
102  
103 6 ▶sub load {
Subroutine load: called CC=10 SCAR=23.0
Pod documented
104 6 my $self = shift;
105  
106 6 my $file = $self->file;
107 6 ▶ return $self unless $file && -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file and -r $file 2 4 6
Condition100%3/3
$file and -r $file
!ll&&!rl&&r
114
MC/DC100%2/2
$file and -r $file
$file -r $file
Truth table100%3/3
A and B
A= $fileB= -r $file
ABresult
0X0
100
111
108  
109 4 for my $line (split "\n", path($file)->slurp) {
110  
111 # Prefix used by curl for HttpOnly cookies
112 20 ▶ my $httponly = $line =~ s/^#HttpOnly_// ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/^#HttpOnly_//u ? : 2 18 20
113 20 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u 9 11 20
114  
115 11 my @values = split "\t", $line;
116 11 ▶ next if @values != 7;
Branch: 1 branch
BranchTrueFalseTotal
if @values != 7 3 8 11
117  
118 8 ▶ $self->add(Mojo::Cookie::Response->new({
Branch: 3 branches
BranchTrueFalseTotal
$values[1] eq 'FALSE' ? : 1 7 8
$values[3] eq 'FALSE' ? : 3 5 8
$values[4] eq '0' ? : 4 4 8
119 domain => $values[0] =~ s/^\.//r,
120 host_only => $values[1] eq 'FALSE' ? 1 : 0,
121 path => $values[2],
122 secure => $values[3] eq 'FALSE' ? 0 : 1,
123 expires => $values[4] eq '0' ? undef : $values[4],
124 name => $values[5],
125 value => $values[6],
126 httponly => $httponly
127 }));
128 }
129  
130 4 return $self;
131 }
132  
133 174 ▶sub prepare {
Subroutine prepare: called CC=2 SCAR=6.9
Pod documented
134 174 my ($self, $tx) = @_;
135 174 ▶ return unless keys %{$self->{jar}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"jar"};} 160 14 174
136 14 my $req = $tx->req;
137 14 $req->cookies(@{$self->find($req->url)});
138 }
139  
140 4 ▶sub save {
Subroutine save: called CC=2 SCAR=6.9
Pod documented
141 4 my $self = shift;
142 4 ▶ return $self unless my $file = $self->file;
Branch: 1 branch
BranchTrueFalseTotal
unless my $file = $self->file 1 3 4
143  
144 3 my $final = path($file);
145 3 my $tmp = path("$file.$$");
146 3 $tmp->spew($COMMENT . $self->to_string)->move_to($final);
147  
148 3 return $self;
149 }
150  
151 5 ▶sub to_string {
Subroutine to_string: called CC=5 SCAR=16.1
Pod documented
152 5 my $self = shift;
153  
154 5 my @lines;
155 5 for my $cookie (@{$self->all}) {
156 5 ▶ my $line = [
Branch: 2 branches
BranchTrueFalseTotal
$cookie->host_only ? : 1 4 5
$cookie->secure ? : 1 4 5
Condition100%2/2
$cookie->expires // 0
l!l
14
MC/DC100%1/1
$cookie->expires // 0
$cookie->expires
Truth table100%2/2
A
A= $cookie->expires
Aresult
11
00
157 $cookie->domain, $cookie->host_only ? 'FALSE' : 'TRUE',
158 $cookie->path, $cookie->secure ? 'TRUE' : 'FALSE',
159 $cookie->expires // 0, $cookie->name,
160 $cookie->value
161 ];
162 5 push @lines, join "\t", @$line;
163 }
164  
165 5 return join "\n", @lines, '';
166 }
167  
168 13 ▶sub _compare {
Subroutine _compare: called CC=3 SCAR=11.0
169 13 my ($cookie, $path, $name, $domain) = @_;
170 13 ▶ return $cookie->path ne $path || $cookie->name ne $name || $cookie->domain ne $domain;
Condition83%5/6
$cookie->path ne $path || $cookie->name ne $name || $cookie->domain ne $domain
l!l&&r!l&&!r
904
$cookie->path ne $path or $cookie->name ne $name
l!l&&r!l&&!r
274
MC/DC66%2/3
$cookie->path ne $path || $cookie->name ne $name || $cookie->domain ne $domain
$cookie->path ne $path $cookie->name ne $name $cookie->domain ne $domain
Truth table75%3/4
A or B || C
A= $cookie->path ne $pathB= $cookie->name ne $nameC= $cookie->domain ne $domain
ABCresult
1XX1
01X1
0011
0000
171 }
172  
173 60 ▶sub _path { $_[0] eq '/' || $_[0] eq $_[1] || index($_[1], "$_[0]/") == 0 }
Subroutine _path: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq "/" or $_[0] eq $_[1] or index($_[1], "$_[0]/") == 0 45 15 60
Condition100%3/3
$_[0] eq "/" or $_[0] eq $_[1]
l!l&&r!l&&!r
172815
MC/DC100%2/2
$_[0] eq "/" or $_[0] eq $_[1]
$_[0] eq "/" $_[0] eq $_[1]
Truth table100%3/3
A or B
A= $_[0] eq "/"B= $_[0] eq $_[1]
ABresult
1X1
011
000
174  
175 24 1;
176  
177 =encoding utf8
178  
179 =head1 NAME
180  
181 Mojo::UserAgent::CookieJar - Cookie jar for HTTP user agents
182  
183 =head1 SYNOPSIS
184  
185 use Mojo::UserAgent::CookieJar;
186  
187 # Add response cookies
188 my $jar = Mojo::UserAgent::CookieJar->new;
189 $jar->add(
190 Mojo::Cookie::Response->new(
191 name => 'foo',
192 value => 'bar',
193 domain => 'localhost',
194 path => '/test'
195 )
196 );
197  
198 # Find request cookies
199 for my $cookie (@{$jar->find(Mojo::URL->new('http://localhost/test'))}) {
200 say $cookie->name;
201 say $cookie->value;
202 }
203  
204 =head1 DESCRIPTION
205  
206 L<Mojo::UserAgent::CookieJar> is a minimalistic and relaxed cookie jar used by L<Mojo::UserAgent>, based on L<RFC
207 6265|https://tools.ietf.org/html/rfc6265>.
208  
209 =head1 ATTRIBUTES
210  
211 L<Mojo::UserAgent::CookieJar> implements the following attributes.
212  
213 =head2 file
214  
215 my $file = $jar->file;
216 $jar = $jar->file('/home/sri/cookies.txt');
217  
218 File to L</"load"> cookies from and L</"save"> cookies to in Netscape format.
219  
220 # Save cookies to file
221 $jar->file('cookies.txt')->save;
222  
223 # Empty cookie jar and load cookies from file
224 $jar->file('cookies.txt')->empty->load;
225  
226 =head2 ignore
227  
228 my $ignore = $jar->ignore;
229 $jar = $jar->ignore(sub {...});
230  
231 A callback used to decide if a cookie should be ignored by L</"collect">.
232  
233 # Ignore all cookies
234 $jar->ignore(sub { 1 });
235  
236 # Ignore cookies for domains "com", "net" and "org"
237 $jar->ignore(sub ($cookie) {
238 return undef unless my $domain = $cookie->domain;
239 return $domain eq 'com' || $domain eq 'net' || $domain eq 'org';
240 });
241  
242 =head2 max_cookie_size
243  
244 my $size = $jar->max_cookie_size;
245 $jar = $jar->max_cookie_size(4096);
246  
247 Maximum cookie size in bytes, defaults to C<4096> (4KiB).
248  
249 =head1 METHODS
250  
251 L<Mojo::UserAgent::CookieJar> inherits all methods from L<Mojo::Base> and implements the following new ones.
252  
253 =head2 add
254  
255 $jar = $jar->add(@cookies);
256  
257 Add multiple L<Mojo::Cookie::Response> objects to the jar.
258  
259 =head2 all
260  
261 my $cookies = $jar->all;
262  
263 Return all L<Mojo::Cookie::Response> objects that are currently stored in the jar.
264  
265 # Names of all cookies
266 say $_->name for @{$jar->all};
267  
268 =head2 collect
269  
270 $jar->collect(Mojo::Transaction::HTTP->new);
271  
272 Collect response cookies from transaction.
273  
274 =head2 empty
275  
276 $jar = $jar->empty;
277  
278 Empty the jar.
279  
280 =head2 find
281  
282 my $cookies = $jar->find(Mojo::URL->new);
283  
284 Find L<Mojo::Cookie::Request> objects in the jar for L<Mojo::URL> object.
285  
286 # Names of all cookies found
287 say $_->name for @{$jar->find(Mojo::URL->new('http://example.com/foo'))};
288  
289 =head2 load
290  
291 $jar = $jar->load;
292  
293 Load cookies from L</"file">.
294  
295 =head2 prepare
296  
297 $jar->prepare(Mojo::Transaction::HTTP->new);
298  
299 Prepare request cookies for transaction.
300  
301 =head2 save
302  
303 $jar = $jar->save;
304  
305 Save cookies to L</"file">.
306  
307 =head2 to_string
308  
309 my $string = $jar->to_string;
310  
311 Stringify cookies in Netscape format.
312  
313  
314 =head1 SEE ALSO
315  
316 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
317  
318 =cut
« Mojo/SSE.pm ↑ Summary Mojolicious/Controller.pm »