| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | for my $cookie (@{$cookies}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | croak('Can\'t store cookie without domain') unless $cookie->domain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | ▶ | if ($store) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
$candidate->domain eq $cookie->domain and $candidate->path eq $cookie->path and $candidate->name eq $cookie->name
$candidate->domain eq $cookie->domain and $candidate->path eq $cookie->path
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/9
defined $cookie->max_age and $cookie->max_age == 0 or defined $cookie->expires and $cookie->expires->epoch < time
defined $cookie->max_age and $cookie->max_age == 0
defined $cookie->expires and $cookie->expires->epoch < time
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | my @cookies = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | my $urlobj = Mojo::URL->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | ref $url && $url->isa('Mojo::URL') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $url and $url->isa("Mojo::URL")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ? $urlobj = $url | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | : $urlobj->parse($url); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | croak 'Url must be absolute' unless $urlobj->is_abs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | my $domain = $urlobj->host; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$domain =~ s/^[\w\-]+\.(.*)/$1/x and $domain =~ /([\w\-]+\.[\w\-]+)$/x
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | my $store = $self->_jar->{$domain}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | ▶ | if ($store) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | my $store_size = scalar @{$store}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | my @not_expired; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | while (my $candidate = shift @{$store}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # Check for expiry while we're here | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | (defined $candidate->expires | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $candidate->expires and $candidate->expires->epoch < time
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | unless ($candidate->port) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | push @cookies, $candidate; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | my $port = $urlobj->port || '80'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$urlobj->port || '80'
MC/DC0%0/1
$urlobj->port || '80'
$urlobj->port
Truth table0%0/2
A
A= $urlobj->port
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | push @cookies, $candidate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||