UserAgent/CookieJar.pm

filter: Statementstmt 76.0% 54 / 71 Branchbran 41.1% 14 / 34 Conditioncond 19.2% 5 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 53.2% 82 / 154 CC 29 SCAR 36.4
CC
29
Cov
56%
cookies_for_url
49.4
store
38.9
BEGIN
1.0
CRAP 38.0
↑ Summary UserAgent.pm »
1 # Copyright (C) 2009, Pascal Gaudette.
2  
3 package MojoX::UserAgent::CookieJar;
4  
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use base 'Mojo::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 __PACKAGE__->attr('size' => 0);
13  
14 1 ▶__PACKAGE__->attr('_jar' => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 1 ▶sub store {
Subroutine store: called CC=15 SCAR=36.6
Pod documented
17 1 my $self = shift;
18 1 ▶ my $cookies = (ref $_[0] eq 'ARRAY') ? shift : [@_];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 1 1
19  
20 1 for my $cookie (@{$cookies}) {
21  
22 3 ▶ croak('Can\'t store cookie without domain') unless $cookie->domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $cookie->domain 0 3 3
23  
24 3 my $domain = $cookie->domain;
25 3 my $store = $self->_jar->{$domain};
26  
27 # max-age wins over expires
28 3 ▶ $cookie->expires($cookie->max_age + time) if $cookie->max_age;
Branch: 1 branch
BranchTrueFalseTotal
if $cookie->max_age 3 0 3
29  
30 3 ▶ if ($store) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store) { } 1 2 3
31  
32 # Do we already have this cookie?
33 1 my $found = 0;
34 1 for my $i (0 .. $#{$store}) {
35  
36 1 my $candidate = $store->[$i];
37 1 ▶ if ( $candidate->domain eq $cookie->domain
Branch: 1 branch
BranchTrueFalseTotal
if ($candidate->domain eq $cookie->domain and $candidate->path eq $cookie->path and $candidate->name eq $cookie->name) 0 1 1
Condition33%2/6
$candidate->domain eq $cookie->domain and $candidate->path eq $cookie->path and $candidate->name eq $cookie->name
!ll&&!rl&&r
100
$candidate->domain eq $cookie->domain and $candidate->path eq $cookie->path
!ll&&!rl&&r
010
MC/DC0%0/3
$candidate->domain eq $cookie->domain and $candidate->path eq $cookie->path and $candidate->name eq $cookie->name
$candidate->domain eq $cookie->domain $candidate->path eq $cookie->path $candidate->name eq $cookie->name
Truth table25%1/4
A and B and C
A= $candidate->domain eq $cookie->domainB= $candidate->path eq $cookie->pathC= $candidate->name eq $cookie->name
ABCresult
0XX0
10X0
1100
1111
38 && $candidate->path eq $cookie->path
39 && $candidate->name eq $cookie->name)
40 {
41  
42 0 $found = 1;
43  
44 # Check for unset
45 0 ▶ if ((defined $cookie->max_age && $cookie->max_age == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cookie->max_age and $cookie->max_age == 0 or defined $cookie->expires and $cookie->expires->epoch < time) { } 0 0 0
Condition0%0/9
defined $cookie->max_age and $cookie->max_age == 0 or defined $cookie->expires and $cookie->expires->epoch < time
l!l&&r!l&&!r
000
defined $cookie->max_age and $cookie->max_age == 0
!ll&&!rl&&r
000
defined $cookie->expires and $cookie->expires->epoch < time
!ll&&!rl&&r
000
MC/DC0%0/4
defined $cookie->max_age and $cookie->max_age == 0 or defined $cookie->expires and $cookie->expires->epoch < time
defined $cookie->max_age $cookie->max_age == 0 defined $cookie->expires $cookie->expires->epoch < time
Truth table0%0/7
A and B or C and D
A= defined $cookie->max_ageB= $cookie->max_age == 0C= defined $cookie->expiresD= $cookie->expires->epoch < time
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
46 || (defined $cookie->expires
47 && $cookie->expires->epoch < time)
48 )
49 {
50 0 splice @{$store}, $i, 1;
51 0 $self->{size}--;
52 }
53 else {
54  
55 # Got a match: replace.
56 # Should this be in-place (as below), or should we
57 # delete the old one and push new one?
58 0 $store->[$i] = $cookie;
59 }
60  
61 0 last;
62 }
63 }
64  
65 1 ▶ unless ($found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 1 0 1
66 # push may not be enough here, might want to order by
67 # longest path?
68 1 push @{$store}, $cookie;
69 1 $self->{size}++;
70 }
71 }
72 else {
73 2 $self->_jar->{$domain} = [$cookie];
74 2 $self->{size}++;
75 }
76 }
77 1 return $self->size;
78 }
79  
80 1 ▶sub cookies_for_url {
Subroutine cookies_for_url: called CC=15 SCAR=39.0
Pod documented
81 1 my $self = shift;
82 1 my $url = shift;
83  
84 1 ▶ croak 'Must provide url' unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 1 1
85  
86 1 my @cookies = ();
87 1 my $urlobj = Mojo::URL->new;
88  
89 1 ▶ ref $url && $url->isa('Mojo::URL')
Branch: 1 branch
BranchTrueFalseTotal
ref $url && $url->isa('Mojo::URL') ? : 0 1 1
Condition33%1/3
ref $url and $url->isa("Mojo::URL")
!ll&&!rl&&r
100
MC/DC0%0/2
ref $url and $url->isa("Mojo::URL")
ref $url $url->isa("Mojo::URL")
Truth table33%1/3
A and B
A= ref $urlB= $url->isa("Mojo::URL")
ABresult
0X0
100
111
90 ? $urlobj = $url
91 : $urlobj->parse($url);
92  
93 1 ▶ croak 'Url must be absolute' unless $urlobj->is_abs;
Branch: 1 branch
BranchTrueFalseTotal
unless $urlobj->is_abs 0 1 1
94  
95 1 my $domain = $urlobj->host;
96  
97 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$domain =~ s/^[\w\-]+\.(.*)/$1/x and $domain =~ /([\w\-]+\.[\w\-]+)$/x and do { do { my $store = $self->_jar->{$domain}; if ($store) { my $store_size = scalar @{$store;}; my @not_expired; while (my $candidate = shift @{$store;}) { do { defined $candidate->expires && $candidate->expires->epoch < time ? next : push(@not_expired, $candidate); my $path = $candidate->path; if ($urlobj->path =~ /^$path/) { if ($candidate->port) { my $port = $urlobj->port || 80; push @cookies, $candidate if $candidate->port =~ /\b$port\b/; } else { push @cookies, $candidate; } ; } }; } ; push @{$store;}, @not_expired; $self->size($self->size + scalar(@not_expired) - $store_size); } } } 1 0 1
Condition33%1/3
$domain =~ s/^[\w\-]+\.(.*)/$1/x and $domain =~ /([\w\-]+\.[\w\-]+)$/x
!ll&&!rl&&r
001
MC/DC0%0/2
$domain =~ s/^[\w\-]+\.(.*)/$1/x and $domain =~ /([\w\-]+\.[\w\-]+)$/x
$domain =~ s/^[\w\-]+\.(.*)/$1/x $domain =~ /([\w\-]+\.[\w\-]+)$/x
Truth table33%1/3
A and B
A= $domain =~ s/^[\w\-]+\.(.*)/$1/xB= $domain =~ /([\w\-]+\.[\w\-]+)$/x
ABresult
0X0
100
111
98 2 my $store = $self->_jar->{$domain};
99  
100 2 ▶ if ($store) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store) 1 1 2
101  
102 1 my $store_size = scalar @{$store};
103 1 my @not_expired;
104  
105 1 ▶ while (my $candidate = shift @{$store}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $candidate = shift @{$store;}) 1 0 1
106  
107 # Check for expiry while we're here
108 1 ▶ (defined $candidate->expires
Branch: 1 branch
BranchTrueFalseTotal
defined $candidate->expires && $candidate->expires->epoch < time ? : 0 0 0
Condition33%1/3
defined $candidate->expires and $candidate->expires->epoch < time
!ll&&!rl&&r
010
MC/DC0%0/2
defined $candidate->expires and $candidate->expires->epoch < time
defined $candidate->expires $candidate->expires->epoch < time
Truth table33%1/3
A and B
A= defined $candidate->expiresB= $candidate->expires->epoch < time
ABresult
0X0
100
111
109 && $candidate->expires->epoch < time)
110 ? next
111 : push @not_expired, $candidate;
112  
113 0 my $path = $candidate->path;
114  
115 0 ▶ if ($urlobj->path =~ m{^$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($urlobj->path =~ /^$path/) 0 0 0
116 0 ▶ unless ($candidate->port) {
Branch: 1 branch
BranchTrueFalseTotal
if ($candidate->port) { } 0 0 0
117 0 push @cookies, $candidate;
118 }
119 else {
120 0 ▶ my $port = $urlobj->port || '80';
Condition0%0/2
$urlobj->port || '80'
l!l
00
MC/DC0%0/1
$urlobj->port || '80'
$urlobj->port
Truth table0%0/2
A
A= $urlobj->port
Aresult
11
00
121 0 ▶ push @cookies, $candidate
Branch: 1 branch
BranchTrueFalseTotal
if $candidate->port =~ /\b$port\b/ 0 0 0
122 if ($candidate->port =~ m{\b$port\b});
123 }
124 }
125 }
126  
127 0 push @{$store}, @not_expired;
128 0 $self->size($self->size + scalar(@not_expired) - $store_size);
129  
130 }
131 } while ( $domain =~ s{^[\w\-]+\.(.*)}{$1}x
132 && $domain =~ m{([\w\-]+\.[\w\-]+)$}x);
133 # Note to self: check DNS spec(s) for use of extended characters
134 # in domain names... (ie [\w\-] might not cut it...)
135  
136 0 return [@cookies];
137 }
138  
139  
140 3 1;
141 __END__
↑ Summary UserAgent.pm »