line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::UserAgent::CookieJar; |
2
|
54
|
|
|
54
|
|
874
|
use Mojo::Base -base; |
|
54
|
|
|
|
|
131
|
|
|
54
|
|
|
|
|
379
|
|
3
|
|
|
|
|
|
|
|
4
|
54
|
|
|
54
|
|
17850
|
use Mojo::Cookie::Request; |
|
54
|
|
|
|
|
164
|
|
|
54
|
|
|
|
|
559
|
|
5
|
54
|
|
|
54
|
|
16819
|
use Mojo::Path; |
|
54
|
|
|
|
|
154
|
|
|
54
|
|
|
|
|
391
|
|
6
|
54
|
|
|
54
|
|
391
|
use Scalar::Util qw(looks_like_number); |
|
54
|
|
|
|
|
128
|
|
|
54
|
|
|
|
|
69962
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has 'ignore'; |
9
|
|
|
|
|
|
|
has max_cookie_size => 4096; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub add { |
12
|
132
|
|
|
132
|
1
|
317
|
my ($self, @cookies) = @_; |
13
|
|
|
|
|
|
|
|
14
|
132
|
|
|
|
|
402
|
my $size = $self->max_cookie_size; |
15
|
132
|
|
|
|
|
355
|
for my $cookie (@cookies) { |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Convert max age to expires |
18
|
139
|
|
|
|
|
367
|
my $age = $cookie->max_age; |
19
|
139
|
100
|
|
|
|
515
|
$cookie->expires($age <= 0 ? 0 : $age + time) if looks_like_number $age; |
|
|
100
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Check cookie size |
22
|
139
|
100
|
100
|
|
|
351
|
next if length($cookie->value // '') > $size; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Replace cookie |
25
|
138
|
100
|
100
|
|
|
343
|
next unless my $domain = lc($cookie->domain // ''); |
26
|
137
|
100
|
|
|
|
352
|
next unless my $path = $cookie->path; |
27
|
135
|
100
|
100
|
|
|
360
|
next unless length(my $name = $cookie->name // ''); |
28
|
134
|
|
100
|
|
|
546
|
my $jar = $self->{jar}{$domain} //= []; |
29
|
134
|
|
|
|
|
333
|
@$jar = (grep({ _compare($_, $path, $name, $domain) } @$jar), $cookie); |
|
453
|
|
|
|
|
860
|
|
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
132
|
|
|
|
|
418
|
return $self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub all { |
36
|
14
|
|
|
14
|
1
|
34
|
my $jar = shift->{jar}; |
37
|
14
|
|
|
|
|
89
|
return [map { @{$jar->{$_}} } sort keys %$jar]; |
|
10
|
|
|
|
|
13
|
|
|
10
|
|
|
|
|
73
|
|
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub collect { |
41
|
953
|
|
|
953
|
1
|
2231
|
my ($self, $tx) = @_; |
42
|
|
|
|
|
|
|
|
43
|
953
|
|
|
|
|
2474
|
my $url = $tx->req->url; |
44
|
953
|
|
|
|
|
1767
|
for my $cookie (@{$tx->res->cookies}) { |
|
953
|
|
|
|
|
2214
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Validate domain |
47
|
138
|
|
|
|
|
546
|
my $host = lc $url->ihost; |
48
|
138
|
100
|
|
|
|
385
|
$cookie->domain($host)->host_only(1) unless $cookie->domain; |
49
|
138
|
|
|
|
|
315
|
my $domain = lc $cookie->domain; |
50
|
138
|
100
|
|
|
|
406
|
if (my $cb = $self->ignore) { next if $cb->($cookie) } |
|
50
|
100
|
|
|
|
146
|
|
51
|
129
|
100
|
100
|
|
|
674
|
next if $host ne $domain && ($host !~ /\Q.$domain\E$/ || $host =~ /\.\d+$/); |
|
|
|
100
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Validate path |
54
|
122
|
|
66
|
|
|
695
|
my $path = $cookie->path // $url->path->to_dir->to_abs_string; |
55
|
122
|
|
|
|
|
481
|
$path = Mojo::Path->new($path)->trailing_slash(0)->to_abs_string; |
56
|
122
|
100
|
|
|
|
575
|
next unless _path($path, $url->path->to_abs_string); |
57
|
119
|
|
|
|
|
430
|
$self->add($cookie->path($path)); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
7
|
|
|
7
|
1
|
54
|
sub empty { delete shift->{jar} } |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub find { |
64
|
246
|
|
|
246
|
1
|
624
|
my ($self, $url) = @_; |
65
|
|
|
|
|
|
|
|
66
|
246
|
|
|
|
|
4123
|
my @found; |
67
|
246
|
|
|
|
|
618
|
my $domain = my $host = lc $url->ihost; |
68
|
246
|
|
|
|
|
750
|
my $path = $url->path->to_abs_string; |
69
|
246
|
|
|
|
|
866
|
while ($domain) { |
70
|
926
|
100
|
|
|
|
2417
|
next unless my $old = $self->{jar}{$domain}; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Grab cookies |
73
|
249
|
|
|
|
|
669
|
my $new = $self->{jar}{$domain} = []; |
74
|
249
|
|
|
|
|
669
|
for my $cookie (@$old) { |
75
|
550
|
100
|
100
|
|
|
1500
|
next if $cookie->host_only && $host ne $cookie->domain; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Check if cookie has expired |
78
|
549
|
100
|
|
|
|
1414
|
if (defined(my $expires = $cookie->expires)) { next if time > $expires } |
|
323
|
100
|
|
|
|
820
|
|
79
|
545
|
|
|
|
|
1105
|
push @$new, $cookie; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Taste cookie |
82
|
545
|
100
|
100
|
|
|
1240
|
next if $cookie->secure && $url->protocol ne 'https'; |
83
|
544
|
100
|
|
|
|
1224
|
next unless _path($cookie->path, $path); |
84
|
410
|
|
|
|
|
1173
|
my $name = $cookie->name; |
85
|
410
|
|
|
|
|
1000
|
my $value = $cookie->value; |
86
|
410
|
|
|
|
|
1452
|
push @found, Mojo::Cookie::Request->new(name => $name, value => $value); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Remove another part |
91
|
926
|
|
|
|
|
3067
|
continue { $domain =~ s/^[^.]*\.*// } |
92
|
|
|
|
|
|
|
|
93
|
246
|
|
|
|
|
1401
|
return \@found; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub prepare { |
97
|
959
|
|
|
959
|
1
|
2351
|
my ($self, $tx) = @_; |
98
|
959
|
100
|
|
|
|
1629
|
return unless keys %{$self->{jar}}; |
|
959
|
|
|
|
|
4403
|
|
99
|
225
|
|
|
|
|
623
|
my $req = $tx->req; |
100
|
225
|
|
|
|
|
468
|
$req->cookies(@{$self->find($req->url)}); |
|
225
|
|
|
|
|
629
|
|
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub _compare { |
104
|
453
|
|
|
453
|
|
880
|
my ($cookie, $path, $name, $domain) = @_; |
105
|
453
|
|
66
|
|
|
854
|
return $cookie->path ne $path || $cookie->name ne $name || $cookie->domain ne $domain; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
666
|
100
|
100
|
666
|
|
3115
|
sub _path { $_[0] eq '/' || $_[0] eq $_[1] || index($_[1], "$_[0]/") == 0 } |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
1; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=encoding utf8 |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 NAME |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Mojo::UserAgent::CookieJar - Cookie jar for HTTP user agents |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 SYNOPSIS |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
use Mojo::UserAgent::CookieJar; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Add response cookies |
123
|
|
|
|
|
|
|
my $jar = Mojo::UserAgent::CookieJar->new; |
124
|
|
|
|
|
|
|
$jar->add( |
125
|
|
|
|
|
|
|
Mojo::Cookie::Response->new( |
126
|
|
|
|
|
|
|
name => 'foo', |
127
|
|
|
|
|
|
|
value => 'bar', |
128
|
|
|
|
|
|
|
domain => 'localhost', |
129
|
|
|
|
|
|
|
path => '/test' |
130
|
|
|
|
|
|
|
) |
131
|
|
|
|
|
|
|
); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# Find request cookies |
134
|
|
|
|
|
|
|
for my $cookie (@{$jar->find(Mojo::URL->new('http://localhost/test'))}) { |
135
|
|
|
|
|
|
|
say $cookie->name; |
136
|
|
|
|
|
|
|
say $cookie->value; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 DESCRIPTION |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
L is a minimalistic and relaxed cookie jar used by L, based on L
|
142
|
|
|
|
|
|
|
6265|https://tools.ietf.org/html/rfc6265>. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
L implements the following attributes. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 ignore |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
my $ignore = $jar->ignore; |
151
|
|
|
|
|
|
|
$jar = $jar->ignore(sub {...}); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
A callback used to decide if a cookie should be ignored by L"collect">. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# Ignore all cookies |
156
|
|
|
|
|
|
|
$jar->ignore(sub { 1 }); |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# Ignore cookies for domains "com", "net" and "org" |
159
|
|
|
|
|
|
|
$jar->ignore(sub ($cookie) { |
160
|
|
|
|
|
|
|
return undef unless my $domain = $cookie->domain; |
161
|
|
|
|
|
|
|
return $domain eq 'com' || $domain eq 'net' || $domain eq 'org'; |
162
|
|
|
|
|
|
|
}); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head2 max_cookie_size |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
my $size = $jar->max_cookie_size; |
167
|
|
|
|
|
|
|
$jar = $jar->max_cookie_size(4096); |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Maximum cookie size in bytes, defaults to C<4096> (4KiB). |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 METHODS |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
L inherits all methods from L and implements the following new ones. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 add |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
$jar = $jar->add(@cookies); |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Add multiple L objects to the jar. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head2 all |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
my $cookies = $jar->all; |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Return all L objects that are currently stored in the jar. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# Names of all cookies |
188
|
|
|
|
|
|
|
say $_->name for @{$jar->all}; |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head2 collect |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
$jar->collect(Mojo::Transaction::HTTP->new); |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Collect response cookies from transaction. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head2 empty |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
$jar->empty; |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Empty the jar. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head2 find |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
my $cookies = $jar->find(Mojo::URL->new); |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Find L objects in the jar for L object. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# Names of all cookies found |
209
|
|
|
|
|
|
|
say $_->name for @{$jar->find(Mojo::URL->new('http://example.com/foo'))}; |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head2 prepare |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
$jar->prepare(Mojo::Transaction::HTTP->new); |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Prepare request cookies for transaction. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head1 SEE ALSO |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
L, L, L. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=cut |