Cookies.pm

filter: Statementstmt 93.0% 350 / 376 Branchbran 82.7% 192 / 232 Conditioncond 76.2% 61 / 80 MC/DCmcdc 56.2% 27 / 48 Subroutinesub 95.6% 22 / 23 Podpod 92.3% 12 / 13 totaltotal 86.0% 664 / 772 CC 159 SCAR 34.9
CC
159
Cov
88%
extract_cookies
59.9
add_cookie_header
45.8
set_cookie
13.9
CRAP 32.7
« Cookies/Microsoft.pm ↑ Summary Cookies/Netscape.pm »
1 package HTTP::Cookies;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use HTTP::Date qw(str2time parse_date time2str);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use HTTP::Headers::Util qw(_split_header_words join_header_words);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $EPOCH_OFFSET;
8 6 our $VERSION = '6.11';
9  
10 # Legacy: because "use "HTTP::Cookies" used be the ONLY way
11 # to load the class HTTP::Cookies::Netscape.
12 6 require HTTP::Cookies::Netscape;
13  
14 6 $EPOCH_OFFSET = 0; # difference from Unix epoch
15  
16 # A HTTP::Cookies object is a hash. The main attribute is the
17 # COOKIES 3 level hash: $self->{COOKIES}{$domain}{$path}{$key}.
18  
19 sub new
20 {
21 33 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
22 33 my $self = bless {
23 COOKIES => {},
24 }, $class;
25 33 my %cnf = @_;
26 33 for (keys %cnf) {
27 7 $self->{lc($_)} = $cnf{$_};
28 }
29 33 $self->load;
30 33 $self;
31 }
32  
33  
34 sub add_cookie_header
35 {
36 70 ▶ my $self = shift;
Subroutine add_cookie_header: called CC=39 SCAR=38.2
Pod documented
37 70 ▶ my $request = shift || return;
Condition50%1/2
shift() || (return)
l!l
700
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
38 70 my $url = $request->uri;
39 70 my $scheme = $url->scheme;
40 70 ▶ unless ($scheme =~ /^https?\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scheme =~ /^https?\z/) 3 67 70
41 3 return;
42 }
43  
44 67 my $domain = _host($request, $url);
45 67 ▶ $domain = "$domain.local" unless $domain =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $domain =~ /\./ 4 63 67
46 67 my $secure_request = ($scheme eq "https");
47 67 my $req_path = _url_path($url);
48 67 my $req_port = $url->port;
49 67 my $now = time();
50 67 ▶ _normalize_path($req_path) if $req_path =~ /%/;
Branch: 1 branch
BranchTrueFalseTotal
if $req_path =~ /%/ 3 64 67
51  
52 67 my @cval; # cookie values for the "Cookie" header
53 my $set_ver;
54 67 my $netscape_only = 0; # An exact domain match applies to any cookie
55  
56 67 ▶ while ($domain =~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain =~ /\./) 252 67 319
57 # Checking $domain for cookies"
58 252 my $cookies = $self->{COOKIES}{$domain};
59 252 ▶ next unless $cookies;
Branch: 1 branch
BranchTrueFalseTotal
unless $cookies 195 57 252
60 57 ▶ if ($self->{delayload} && defined($cookies->{'//+delayload'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"delayload"} and defined $cookies->{"//+delayload"}) 0 57 57
Condition33%1/3
$self->{"delayload"} and defined $cookies->{"//+delayload"}
!ll&&!rl&&r
5700
MC/DC0%0/2
$self->{"delayload"} and defined $cookies->{"//+delayload"}
$self->{"delayload"} defined $cookies->{"//+delayload"}
Truth table33%1/3
A and B
A= $self->{"delayload"}B= defined $cookies->{"//+delayload"}
ABresult
0X0
100
111
61 0 my $cookie_data = $cookies->{'//+delayload'}{'cookie'};
62 0 delete $self->{COOKIES}{$domain};
63 0 $self->load_cookie($cookie_data->[1]);
64 0 $cookies = $self->{COOKIES}{$domain};
65 0 ▶ next unless $cookies; # should not really happen
Branch: 1 branch
BranchTrueFalseTotal
unless $cookies 0 0 0
66 }
67  
68 # Want to add cookies corresponding to the most specific paths
69 # first (i.e. longest path first)
70 57 my $path;
71 57 for $path (sort {length($b) <=> length($a) } keys %$cookies) {
72 67 ▶ if (index($req_path, $path) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($req_path, $path) != 0) 7 60 67
73 7 next;
74 }
75  
76 60 my($key,$array);
77 60 ▶ while (($key,$array) = each %{$cookies->{$path}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$cookies{$path};}) 75 60 135
78 75 my($version,$val,$port,$path_spec,$secure,$expires) = @$array;
79 75 ▶ if ($secure && !$secure_request) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secure and not $secure_request) 1 74 75
Condition100%3/3
$secure and not $secure_request
!ll&&!rl&&r
7311
MC/DC100%2/2
$secure and not $secure_request
$secure not $secure_request
Truth table100%3/3
A and not(B)
A= $secureB= not $secure_request
ABresult
0X0
100
111
80 1 next;
81 }
82 74 ▶ if ($expires && $expires < $now) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires and $expires < $now) 0 74 74
Condition66%2/3
$expires and $expires < $now
!ll&&!rl&&r
56180
MC/DC0%0/2
$expires and $expires < $now
$expires $expires < $now
Truth table66%2/3
A and B
A= $expiresB= $expires < $now
ABresult
0X0
100
111
83 0 next;
84 }
85 74 ▶ if ($port) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port) 7 67 74
86 7 my $found;
87 7 ▶ if ($port =~ s/^_//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port =~ s/^_//) { } 4 3 7
88 # The corresponding Set-Cookie attribute was empty
89 4 ▶ $found++ if $port eq $req_port;
Branch: 1 branch
BranchTrueFalseTotal
if $port eq $req_port 4 0 4
90 4 $port = "";
91 }
92 else {
93 3 my $p;
94 3 for $p (split(/,/, $port)) {
95 7 ▶ $found++, last if $p eq $req_port;
Branch: 1 branch
BranchTrueFalseTotal
if $p eq $req_port 3 4 7
96 }
97 }
98 7 ▶ unless ($found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 7 7
99 0 next;
100 }
101 }
102 74 ▶ if ($version > 0 && $netscape_only) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version > 0 and $netscape_only) 0 74 74
Condition66%2/3
$version > 0 and $netscape_only
!ll&&!rl&&r
43310
MC/DC0%0/2
$version > 0 and $netscape_only
$version > 0 $netscape_only
Truth table66%2/3
A and B
A= $version > 0B= $netscape_only
ABresult
0X0
100
111
103 0 next;
104 }
105  
106 # set version number of cookie header.
107 # XXX: What should it be if multiple matching
108 # Set-Cookie headers have different versions themselves
109 74 ▶ if (!$set_ver++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($set_ver++) 50 24 74
110 50 ▶ if ($version >= 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version >= 1) { } 18 32 50
elsif (not $self->{'hide_cookie2'}) { } 32 0 32
111 18 push(@cval, "\$Version=$version");
112 }
113 elsif (!$self->{hide_cookie2}) {
114 32 $request->header(Cookie2 => '$Version="1"');
115 }
116 }
117  
118 # do we need to quote the value
119 74 ▶ if ($val =~ /\W/ && $version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /\W/ and $version) 0 74 74
Condition66%2/3
$val =~ /\W/ and $version
!ll&&!rl&&r
7130
MC/DC0%0/2
$val =~ /\W/ and $version
$val =~ /\W/ $version
Truth table66%2/3
A and B
A= $val =~ /\W/B= $version
ABresult
0X0
100
111
120 0 $val =~ s/([\\\"])/\\$1/g;
121 0 $val = qq("$val");
122 }
123  
124 # and finally remember this cookie
125 74 push(@cval, "$key=$val");
126 74 ▶ if ($version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= 1) 31 43 74
127 31 ▶ push(@cval, qq(\$Path="$path")) if $path_spec;
Branch: 1 branch
BranchTrueFalseTotal
if $path_spec 16 15 31
128 31 ▶ push(@cval, qq(\$Domain="$domain")) if $domain =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $domain =~ /^\./ 8 23 31
129 31 ▶ if (defined $port) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port) 7 24 31
130 7 my $p = '$Port';
131 7 ▶ $p .= qq(="$port") if length $port;
Branch: 1 branch
BranchTrueFalseTotal
if length $port 3 4 7
132 7 push(@cval, $p);
133 }
134 }
135  
136 }
137 }
138  
139 } continue {
140 # Try with a more general domain, alternately stripping
141 # leading name components and leading dots. When this
142 # results in a domain with no leading dot, it is for
143 # Netscape cookie compatibility only:
144 #
145 # a.b.c.net Any cookie
146 # .b.c.net Any cookie
147 # b.c.net Netscape cookie only
148 # .c.net Any cookie
149  
150 252 ▶ if ($domain =~ s/^\.+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain =~ s/^\.+//) { } 126 126 252
151 126 $netscape_only = 1;
152 }
153 else {
154 126 $domain =~ s/[^.]*//;
155 126 $netscape_only = 0;
156 }
157 }
158  
159 67 ▶ if (@cval) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cval) 50 17 67
160 50 ▶ if (my $old = $request->header("Cookie")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $old = $request->header("Cookie")) 2 48 50
161 2 unshift(@cval, $old);
162 }
163 50 $request->header(Cookie => join("; ", @cval));
164 50 ▶ if (my $hash = $request->{_http_cookies}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hash = $request->{"_http_cookies"}) 8 42 50
165 8 %$hash = (map split(/=/, $_, 2), @cval);
166 }
167 }
168  
169 67 $request;
170 }
171  
172  
173 sub get_cookies
174 {
175 9 ▶ my $self = shift;
Subroutine get_cookies: called CC=4 SCAR=13.9
Pod documented
176 9 my $url = shift;
177 9 ▶ $url = "https://$url" unless $url =~ m,^[a-zA-Z][a-zA-Z0-9.+\-]*:,;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /^[a-zA-Z][a-zA-Z0-9.+\-]*:/ 6 3 9
178 9 require HTTP::Request;
179 9 my $req = HTTP::Request->new(GET => $url);
180 9 my $cookies = $req->{_http_cookies} = {};
181 9 $self->add_cookie_header($req);
182 9 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 1 9
183 8 ▶ return map $cookies->{$_}, @_ if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 7 8
184 7 return $cookies->{$_[0]};
185 }
186 1 return $cookies;
187 }
188  
189  
190 sub extract_cookies
191 {
192 70 ▶ my $self = shift;
Subroutine extract_cookies: called CC=58 SCAR=40.9
Pod documented
193 70 ▶ my $response = shift || return;
Condition50%1/2
shift() || (return)
l!l
700
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
194  
195 70 my @set = _split_header_words($response->_header("Set-Cookie2"));
196 70 my @ns_set = $response->_header("Set-Cookie");
197  
198 70 ▶ return $response unless @set || @ns_set; # quick exit
Branch: 1 branch
BranchTrueFalseTotal
unless @set or @ns_set 7 63 70
Condition100%3/3
@set or @ns_set
l!l&&r!l&&!r
32317
MC/DC100%2/2
@set or @ns_set
@set @ns_set
Truth table100%3/3
A or B
A= @setB= @ns_set
ABresult
1X1
011
000
199  
200 63 my $request = $response->request;
201 63 my $url = $request->uri;
202 63 my $req_host = _host($request, $url);
203 63 ▶ $req_host = "$req_host.local" unless $req_host =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $req_host =~ /\./ 3 60 63
204 63 my $req_port = $url->port;
205 63 my $req_path = _url_path($url);
206 63 ▶ _normalize_path($req_path) if $req_path =~ /%/;
Branch: 1 branch
BranchTrueFalseTotal
if $req_path =~ /%/ 2 61 63
207  
208 63 ▶ if (@ns_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ns_set) 33 30 63
209 # The old Netscape cookie format for Set-Cookie
210 # http://curl.haxx.se/rfc/cookie_spec.html
211 # can for instance contain an unquoted "," in the expires
212 # field, so we have to use this ad-hoc parser.
213 33 my $now = time();
214  
215 # Build a hash of cookies that was present in Set-Cookie2
216 # headers. We need to skip them if we also find them in a
217 # Set-Cookie header.
218 33 my %in_set2;
219 33 for (@set) {
220 2 $in_set2{$_->[0]}++;
221 }
222  
223 33 my $set;
224 33 for $set (@ns_set) {
225 43 $set =~ s/^\s+//;
226 43 my @cur;
227 my $param;
228 43 my $expires;
229 43 my $first_param = 1;
230 43 for $param (@{_split_text($set)}) {
231 108 ▶ next unless length($param);
Branch: 1 branch
BranchTrueFalseTotal
unless length $param 1 107 108
232 107 my($k,$v) = split(/\s*=\s*/, $param, 2);
233 107 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) { } 105 2 107
234 105 $v =~ s/\s+$//;
235 #print "$k => $v\n";
236 }
237 else {
238 2 $k =~ s/\s+$//;
239 #print "$k => undef";
240 }
241 107 ▶ if (!$first_param && lc($k) eq "expires") {
Branch: 3 branches
BranchTrueFalseTotal
if (not $first_param and lc $k eq 'expires') { } 14 93 107
elsif (not $first_param and lc $k eq 'max-age') { } 1 92 93
elsif (not $first_param and lc($k) =~ /^(?:version|discard|ns-cookie)/) { } 2 90 92
Condition100%3/3
not $first_param and lc $k eq "expires"
!ll&&!rl&&r
425114
MC/DC100%2/2
not $first_param and lc $k eq "expires"
not $first_param lc $k eq "expires"
Truth table100%3/3
not(A) and B
A= not $first_paramB= lc $k eq "expires"
ABresult
0X0
100
111
242 14 my $etime = str2time($v);
243 14 ▶ if (defined $etime) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $etime) { } 12 2 14
244 12 push(@cur, "Max-Age" => $etime - $now);
245 12 $expires++;
246 }
247 else {
248 # parse_date can deal with years outside the range of time_t,
249 2 my($year, $mon, $day, $hour, $min, $sec, $tz) = parse_date($v);
250 2 ▶ if ($year) {
Branch: 1 branch
BranchTrueFalseTotal
if ($year) 2 0 2
251 2 my $thisyear = (gmtime)[5] + 1900;
252 2 ▶ if ($year < $thisyear) {
Branch: 2 branches
BranchTrueFalseTotal
if ($year < $thisyear) { } 2 0 2
elsif ($year >= $thisyear + 10) { } 0 0 0
253 2 push(@cur, "Max-Age" => -1); # any negative value will do
254 2 $expires++;
255 }
256 elsif ($year >= $thisyear + 10) {
257 # the date is at least 10 years into the future, just replace
258 # it with something approximate
259 0 push(@cur, "Max-Age" => 10 * 365 * 24 * 60 * 60);
260 0 $expires++;
261 }
262 }
263 }
264 }
265 ▶ elsif (!$first_param && lc($k) eq 'max-age') {
Condition100%3/3
not $first_param and lc $k eq "max-age"
!ll&&!rl&&r
42501
MC/DC100%2/2
not $first_param and lc $k eq "max-age"
not $first_param lc $k eq "max-age"
Truth table100%3/3
not(A) and B
A= not $first_paramB= lc $k eq "max-age"
ABresult
0X0
100
111
266 1 $expires++;
267 }
268 ▶ elsif (!$first_param && lc($k) =~ /^(?:version|discard|ns-cookie)/) {
Condition100%3/3
not $first_param and lc($k) =~ /^(?:version|discard|ns-cookie)/
!ll&&!rl&&r
42482
MC/DC100%2/2
not $first_param and lc($k) =~ /^(?:version|discard|ns-cookie)/
not $first_param lc($k) =~ /^(?:version|discard|ns-cookie)/
Truth table100%3/3
not(A) and B
A= not $first_paramB= lc($k) =~ /^(?:version|discard|ns-cookie)/
ABresult
0X0
100
111
269 # ignore
270 }
271 else {
272 90 push(@cur, $k => $v);
273 }
274 107 $first_param = 0;
275 }
276 43 ▶ next unless @cur;
Branch: 1 branch
BranchTrueFalseTotal
unless @cur 1 42 43
277 42 ▶ next if $in_set2{$cur[0]};
Branch: 1 branch
BranchTrueFalseTotal
if $in_set2{$cur[0]} 1 41 42
278  
279 # push(@cur, "Port" => $req_port);
280 41 ▶ push(@cur, "Discard" => undef) unless $expires;
Branch: 1 branch
BranchTrueFalseTotal
unless $expires 26 15 41
281 41 push(@cur, "Version" => 0);
282 41 push(@cur, "ns-cookie" => 1);
283 41 push(@set, \@cur);
284 }
285 }
286  
287 SET_COOKIE:
288 63 for my $set (@set) {
289 77 ▶ next unless @$set >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$set >= 2 0 77 77
290  
291 77 my $key = shift @$set;
292 77 my $val = shift @$set;
293  
294 77 my %hash;
295 77 ▶ while (@$set) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$set) 236 77 313
296 236 my $k = shift @$set;
297 236 my $v = shift @$set;
298 236 my $lc = lc($k);
299 # don't loose case distinction for unknown fields
300 236 ▶ $k = $lc if $lc =~ /^(?:discard|domain|max-age|
Branch: 1 branch
BranchTrueFalseTotal
if $lc =~ /^(?:discard|domain|max-age| path|port|secure|version)$/x 192 44 236
301 path|port|secure|version)$/x;
302 236 ▶ if ($k eq "discard" || $k eq "secure") {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "discard" or $k eq "secure") 33 203 236
Condition100%3/3
$k eq "discard" or $k eq "secure"
l!l&&r!l&&!r
312203
MC/DC100%2/2
$k eq "discard" or $k eq "secure"
$k eq "discard" $k eq "secure"
Truth table100%3/3
A or B
A= $k eq "discard"B= $k eq "secure"
ABresult
1X1
011
000
303 33 ▶ $v = 1 unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 33 0 33
304 }
305 236 ▶ next if exists $hash{$k}; # only first value is significant
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{$k} 0 236 236
306 236 $hash{$k} = $v;
307 };
308  
309 77 my %orig_hash = %hash;
310 77 my $version = delete $hash{version};
311 77 ▶ $version = 1 unless defined($version);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 16 61 77
312 77 my $discard = delete $hash{discard};
313 77 my $secure = delete $hash{secure};
314 77 my $maxage = delete $hash{'max-age'};
315 77 my $ns_cookie = delete $hash{'ns-cookie'};
316  
317 # Check domain
318 77 my $domain = delete $hash{domain};
319 77 ▶ $domain = lc($domain) if defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain 25 52 77
320 77 ▶ if (defined($domain)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain and $domain ne $req_host and $domain ne ".$req_host") { } 17 60 77
Condition100%6/6
defined $domain and $domain ne $req_host and $domain ne ".$req_host"
!ll&&!rl&&r
54617
defined $domain and $domain ne $req_host
!ll&&!rl&&r
52223
MC/DC100%3/3
defined $domain and $domain ne $req_host and $domain ne ".$req_host"
defined $domain $domain ne $req_host $domain ne ".$req_host"
Truth table100%4/4
A and B and C
A= defined $domainB= $domain ne $req_hostC= $domain ne ".$req_host"
ABCresult
0XX0
10X0
1100
1111
321 && $domain ne $req_host && $domain ne ".$req_host") {
322 17 ▶ if ($domain !~ /\./ && $domain ne "local") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $domain =~ /\./ and $domain ne "local") 0 17 17
Condition33%1/3
not $domain =~ /\./ and $domain ne "local"
!ll&&!rl&&r
1700
MC/DC0%0/2
not $domain =~ /\./ and $domain ne "local"
not $domain =~ /\./ $domain ne "local"
Truth table33%1/3
not(A) and B
A= not $domain =~ /\./B= $domain ne "local"
ABresult
0X0
100
111
323 0 next SET_COOKIE;
324 }
325 17 ▶ $domain = ".$domain" unless $domain =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $domain =~ /^\./ 3 14 17
326 17 ▶ if ($domain =~ /\.\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain =~ /\.\d+$/) 1 16 17
327 1 next SET_COOKIE;
328 }
329 16 my $len = length($domain);
330 16 ▶ unless (substr($req_host, -$len) eq $domain) {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($req_host, -$len) eq $domain) 0 16 16
331 0 next SET_COOKIE;
332 }
333 16 my $hostpre = substr($req_host, 0, length($req_host) - $len);
334 16 ▶ if ($hostpre =~ /\./ && !$ns_cookie) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hostpre =~ /\./ and not $ns_cookie) 2 14 16
Condition100%3/3
$hostpre =~ /\./ and not $ns_cookie
!ll&&!rl&&r
1132
MC/DC100%2/2
$hostpre =~ /\./ and not $ns_cookie
$hostpre =~ /\./ not $ns_cookie
Truth table100%3/3
A and not(B)
A= $hostpre =~ /\./B= not $ns_cookie
ABresult
0X0
100
111
335 2 next SET_COOKIE;
336 }
337 }
338 else {
339 60 $domain = $req_host;
340 }
341  
342 74 my $path = delete $hash{path};
343 74 my $path_spec;
344 74 ▶ if (defined $path && $path ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path and $path ne '') { } 49 25 74
Condition100%3/3
defined $path and $path ne ""
!ll&&!rl&&r
24149
MC/DC100%2/2
defined $path and $path ne ""
defined $path $path ne ""
Truth table100%3/3
A and B
A= defined $pathB= $path ne ""
ABresult
0X0
100
111
345 49 $path_spec++;
346 49 ▶ _normalize_path($path) if $path =~ /%/;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /%/ 1 48 49
347 49 ▶ if (!$ns_cookie &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $ns_cookie and substr($req_path, 0, length $path) ne $path) 4 45 49
Condition100%3/3
not $ns_cookie and substr($req_path, 0, length $path) ne $path
!ll&&!rl&&r
29164
MC/DC100%2/2
not $ns_cookie and substr($req_path, 0, length $path) ne $path
not $ns_cookie substr($req_path, 0, length $path) ne $path
Truth table100%3/3
not(A) and B
A= not $ns_cookieB= substr($req_path, 0, length $path) ne $path
ABresult
0X0
100
111
348 substr($req_path, 0, length($path)) ne $path) {
349 4 next SET_COOKIE;
350 }
351 }
352 else {
353 25 $path = $req_path;
354 25 $path =~ s,/[^/]*$,,;
355 25 ▶ $path = "/" unless length($path);
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 24 1 25
356 }
357  
358 70 my $port;
359 70 ▶ if (exists $hash{port}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash{"port"}) 5 65 70
360 5 $port = delete $hash{port};
361 5 ▶ if (defined $port) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port) { } 3 2 5
362 3 $port =~ s/\s+//g;
363 3 my $found;
364 3 for my $p (split(/,/, $port)) {
365 7 ▶ unless ($p =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p =~ /^\d+$/) 0 7 7
366 0 next SET_COOKIE;
367 }
368 7 ▶ $found++ if $p eq $req_port;
Branch: 1 branch
BranchTrueFalseTotal
if $p eq $req_port 2 5 7
369 }
370 3 ▶ unless ($found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 1 2 3
371 1 next SET_COOKIE;
372 }
373 }
374 else {
375 2 $port = "_$req_port";
376 }
377 }
378 69 ▶ $self->set_cookie($version,$key,$val,$path,$domain,$port,$path_spec,$secure,$maxage,$discard, \%hash)
Branch: 1 branch
BranchTrueFalseTotal
if $self->set_cookie_ok(\%orig_hash) 69 0 69
379 if $self->set_cookie_ok(\%orig_hash);
380 }
381  
382 63 $response;
383 }
384  
385 sub set_cookie_ok
386 {
387 69 ▶ 1;
Subroutine set_cookie_ok: called CC=1 SCAR=0.0
Pod undocumented
388 }
389  
390  
391 sub set_cookie
392 {
393 78 ▶ my $self = shift;
Subroutine set_cookie: called CC=13 SCAR=26.4
Pod documented
394 78 my($version,
395 $key, $val, $path, $domain, $port,
396 $path_spec, $secure, $maxage, $discard, $rest) = @_;
397  
398 # path and key can not be empty (key can't start with '$')
399 78 ▶ return $self if !defined($path) || $path !~ m,^/, ||
Branch: 1 branch
BranchTrueFalseTotal
if not defined $path && $path =~ m[^/] && defined $key or $key =~ /^\$/ 0 78 78
Condition33%3/9
not defined $path && $path =~ m[^/] && defined $key or $key =~ /^\$/
l!l&&r!l&&!r
0078
defined $path && $path =~ m[^/] && defined $key
!ll&&!rl&&r
0078
defined $path and $path =~ m[^/]
!ll&&!rl&&r
0078
MC/DC0%0/4
not defined $path && $path =~ m[^/] && defined $key or $key =~ /^\$/
defined $path $path =~ m[^/] defined $key $key =~ /^\$/
Truth table20%1/5
not(A and B && C) or D
A= defined $pathB= $path =~ m[^/]C= defined $keyD= $key =~ /^\$/
ABCDresult
0XXX1
10XX1
110X1
11111
11100
400 !defined($key) || $key =~ m,^\$,;
401  
402 # ensure legal port
403 78 ▶ if (defined $port) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port) 4 74 78
404 4 ▶ return $self unless $port =~ /^_?\d+(?:,\d+)*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $port =~ /^_?\d+(?:,\d+)*$/ 0 4 4
405 }
406  
407 78 my $expires;
408 78 ▶ if (defined $maxage) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maxage) 27 51 78
409 27 ▶ if ($maxage <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxage <= 0) 8 19 27
410 8 delete $self->{COOKIES}{$domain}{$path}{$key};
411 8 return $self;
412 }
413 19 $expires = time() + $maxage;
414 }
415 70 ▶ $version = 0 unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 6 64 70
416  
417 70 my @array = ($version, $val,$port,
418 $path_spec,
419 $secure, $expires, $discard);
420 70 ▶ push(@array, {%$rest}) if defined($rest) && %$rest;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rest and %$rest 3 67 70
Condition100%3/3
defined $rest and %$rest
!ll&&!rl&&r
6613
MC/DC100%2/2
defined $rest and %$rest
defined $rest %$rest
Truth table100%3/3
A and B
A= defined $restB= %$rest
ABresult
0X0
100
111
421 # trim off undefined values at end
422 70 ▶ pop(@array) while !defined $array[-1];
Branch: 1 branch
BranchTrueFalseTotal
defined $array[-1] or do { pop @array } 85 70 155
423  
424 70 $self->{COOKIES}{$domain}{$path}{$key} = \@array;
425 70 $self;
426 }
427  
428  
429 sub save
430 {
431 2 ▶ my $self = shift;
Subroutine save: called CC=6 SCAR=18.3
Pod documented
432 my %args = (
433 file => $self->{'file'},
434 2 ▶ ignore_discard => $self->{'ignore_discard'},
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 1 2
435 @_ == 1 ? ( file => $_[0] ) : @_
436 );
437 2 ▶ Carp::croak('Unexpected argument to save method') if keys %args > 2;
Branch: 1 branch
BranchTrueFalseTotal
if keys %args > 2 0 2 2
438 2 ▶ my $file = $args{'file'} || return;
Condition50%1/2
$args{'file'} || (return)
l!l
20
MC/DC0%0/1
$args{'file'} || (return)
$args{'file'}
Truth table50%1/2
A
A= $args{'file'}
Aresult
11
00
439 2 ▶ open(my $fh, '>', $file) or die "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Can't open ${file}: $!" 0 2 2
440 2 print {$fh} "#LWP-Cookies-1.0\n";
441 2 print {$fh} $self->as_string(!$args{'ignore_discard'});
442 2 ▶ close $fh or die "Can't close $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close ${file}: $!" 0 2 2
443 2 1;
444 }
445  
446  
447 sub load
448 {
449 30 ▶ my $self = shift;
Subroutine load: called CC=10 SCAR=23.2
Pod documented
450 30 ▶ my $file = shift || $self->{'file'} || return;
Condition80%4/5
shift() || $self->{'file'} || (return)
l!l
228
shift() or $self->{"file"}
l!l&&r!l&&!r
2028
MC/DC50%1/2
shift() || $self->{'file'} || (return)
shift() $self->{"file"}
Truth table66%2/3
A or B
A= shift()B= $self->{"file"}
ABresult
1X1
011
000
451  
452 2 local $/ = "\n"; # make sure we got standard record separator
453 2 ▶ open(my $fh, '<', $file) or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return 0 2 2
454  
455 # check that we have the proper header
456 2 my $magic = <$fh>;
457 2 chomp $magic;
458 2 ▶ unless ($magic =~ /^#LWP-Cookies-\d+\.\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($magic =~ /^#LWP-Cookies-\d+\.\d+/) 0 2 2
459 0 warn "$file does not seem to contain cookies";
460 0 return;
461 }
462  
463 # go through the file
464 2 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 7 2 9
465 7 chomp $line;
466 7 ▶ next unless $line =~ s/^Set-Cookie3:\s*//;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ s/^Set-Cookie3:\s*// 0 7 7
467 7 my $cookie;
468 7 for $cookie (_split_header_words($line)) {
469 7 my($key,$val) = splice(@$cookie, 0, 2);
470 7 my %hash;
471 7 ▶ while (@$cookie) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$cookie) 29 7 36
472 29 my $k = shift @$cookie;
473 29 my $v = shift @$cookie;
474 29 $hash{$k} = $v;
475 }
476 7 my $version = delete $hash{version};
477 7 my $path = delete $hash{path};
478 7 my $domain = delete $hash{domain};
479 7 my $port = delete $hash{port};
480 7 my $expires = str2time(delete $hash{expires});
481  
482 7 my $path_spec = exists $hash{path_spec}; delete $hash{path_spec};
483 7 my $secure = exists $hash{secure}; delete $hash{secure};
484 7 my $discard = exists $hash{discard}; delete $hash{discard};
485  
486 7 my @array = ($version, $val, $port, $path_spec, $secure, $expires,
487 $discard);
488 7 ▶ push(@array, \%hash) if %hash;
Branch: 1 branch
BranchTrueFalseTotal
if %hash 1 6 7
489 7 $self->{COOKIES}{$domain}{$path}{$key} = \@array;
490 }
491 }
492 2 1;
493 }
494  
495  
496 sub revert
497 {
498 0 ▶ my $self = shift;
Subroutine revert: not called CC=1 SCAR=6.9
Pod documented
499 0 $self->clear->load;
500 0 $self;
501 }
502  
503  
504 sub clear
505 {
506 5 ▶ my $self = shift;
Subroutine clear: called CC=5 SCAR=26.2
Pod documented
507 5 ▶ if (@_ == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if (@_ == 0) { } 5 0 5
elsif (@_ == 1) { } 0 0 0
elsif (@_ == 2) { } 0 0 0
elsif (@_ == 3) { } 0 0 0
508 5 $self->{COOKIES} = {};
509 }
510 elsif (@_ == 1) {
511 0 delete $self->{COOKIES}{$_[0]};
512 }
513 elsif (@_ == 2) {
514 0 delete $self->{COOKIES}{$_[0]}{$_[1]};
515 }
516 elsif (@_ == 3) {
517 0 delete $self->{COOKIES}{$_[0]}{$_[1]}{$_[2]};
518 }
519 else {
520 0 require Carp;
521 0 Carp::carp('Usage: $c->clear([domain [,path [,key]]])');
522 }
523 5 $self;
524 }
525  
526  
527 sub clear_temporary_cookies
528 {
529 2 ▶ my($self) = @_;
Subroutine clear_temporary_cookies: called CC=1 SCAR=0.0
Pod documented
530  
531 $self->scan(sub {
532 6 ▶ if($_[9] or # "Discard" flag set
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if ($_[9] or not $_[8]) 3 3 6
Condition66%2/3
$_[9] or not $_[8]
l!l&&r!l&&!r
303
MC/DC50%1/2
$_[9] or not $_[8]
$_[9] not $_[8]
Truth table66%2/3
A or not(B)
A= $_[9]B= not $_[8]
ABresult
1X1
011
000
533 not $_[8]) { # No expire field?
534 3 $_[8] = -1; # Set the expire/max_age field
535 3 $self->set_cookie(@_); # Clear the cookie
536 }
537 2 });
538 }
539  
540  
541 sub DESTROY
542 {
543 29 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=7.1
544 29 local($., $@, $!, $^E, $?);
545 29 ▶ $self->save if $self->{'autosave'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"autosave"} 0 29 29
546 }
547  
548  
549 sub scan
550 {
551 60 ▶ my($self, $cb) = @_;
Subroutine scan: called CC=5 SCAR=16.1
Pod documented
552 60 my($domain,$path,$key);
553 60 for $domain (sort keys %{$self->{COOKIES}}) {
554 90 for $path (sort keys %{$self->{COOKIES}{$domain}}) {
555 110 for $key (sort keys %{$self->{COOKIES}{$domain}{$path}}) {
556 my($version,$val,$port,$path_spec,
557 $secure,$expires,$discard,$rest) =
558 134 @{$self->{COOKIES}{$domain}{$path}{$key}};
559 134 ▶ $rest = {} unless defined($rest);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 126 8 134
560 134 &$cb($version,$key,$val,$path,$domain,$port,
561 $path_spec,$secure,$expires,$discard,$rest);
562 }
563 }
564 }
565 }
566  
567  
568 sub as_string
569 {
570 23 ▶ my($self, $skip_discard) = @_;
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
571 23 my @res;
572 $self->scan(sub {
573 49 ▶ my($version,$key,$val,$path,$domain,$port,
Subroutine __ANON__: called CC=9 SCAR=22.1
574 $path_spec,$secure,$expires,$discard,$rest) = @_;
575 49 ▶ return if $discard && $skip_discard;
Branch: 1 branch
BranchTrueFalseTotal
if $discard and $skip_discard 0 49 49
Condition66%2/3
$discard and $skip_discard
!ll&&!rl&&r
31180
MC/DC0%0/2
$discard and $skip_discard
$discard $skip_discard
Truth table66%2/3
A and B
A= $discardB= $skip_discard
ABresult
0X0
100
111
576 49 my @h = ($key, $val);
577 49 push(@h, "path", $path);
578 49 push(@h, "domain" => $domain);
579 49 ▶ push(@h, "port" => $port) if defined $port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port 7 42 49
580 49 ▶ push(@h, "path_spec" => undef) if $path_spec;
Branch: 1 branch
BranchTrueFalseTotal
if $path_spec 24 25 49
581 49 ▶ push(@h, "secure" => undef) if $secure;
Branch: 1 branch
BranchTrueFalseTotal
if $secure 0 49 49
582 49 ▶ push(@h, "expires" => HTTP::Date::time2isoz($expires)) if $expires;
Branch: 1 branch
BranchTrueFalseTotal
if $expires 10 39 49
583 49 ▶ push(@h, "discard" => undef) if $discard;
Branch: 1 branch
BranchTrueFalseTotal
if $discard 18 31 49
584 49 my $k;
585 49 for $k (sort keys %$rest) {
586 3 push(@h, $k, $rest->{$k});
587 }
588 49 push(@h, "version" => $version);
589 49 push(@res, "Set-Cookie3: " . join_header_words(\@h));
590 23 });
591 23 join("\n", @res, "");
592 }
593  
594 sub _host
595 {
596 130 ▶ my($request, $url) = @_;
Subroutine _host: called CC=2 SCAR=6.9
597 130 ▶ if (my $h = $request->header("Host")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $request->header("Host")) 32 98 130
598 32 $h =~ s/:\d+$//; # might have a port as well
599 32 return lc($h);
600 }
601 98 return lc($url->host);
602 }
603  
604 sub _url_path
605 {
606 130 ▶ my $url = shift;
Subroutine _url_path: called CC=3 SCAR=11.2
607 130 my $path;
608 130 ▶ if($url->can('epath')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url->can('epath')) { } 0 130 130
609 0 $path = $url->epath; # URI::URL method
610 }
611 else {
612 130 $path = $url->path; # URI::_generic method
613 }
614 130 ▶ $path = "/" unless length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 37 93 130
615 130 $path;
616 }
617  
618 sub _normalize_path # so that plain string compare can be used
619 {
620 6 ▶ my $x;
Subroutine _normalize_path: called CC=3 SCAR=11.0
621 6 $_[0] =~ s/%([0-9a-fA-F][0-9a-fA-F])/
622 35 $x = uc($1);
623 35 ▶ $x eq "2F" || $x eq "25" ? "%$x" :
Branch: 1 branch
BranchTrueFalseTotal
$x eq '2F' || $x eq '25' ? : 9 26 35
Condition100%3/3
$x eq "2F" or $x eq 25
l!l&&r!l&&!r
4526
MC/DC100%2/2
$x eq "2F" or $x eq 25
$x eq "2F" $x eq 25
Truth table100%3/3
A or B
A= $x eq "2F"B= $x eq 25
ABresult
1X1
011
000
624 pack("C", hex($x));
625 /eg;
626 6 $_[0] =~ s/([\0-\x20\x7f-\xff])/sprintf("%%%02X",ord($1))/eg;
627 }
628  
629 # deals with splitting values by ; and the fact that they could
630 # be in quotes which can also have escaping.
631 sub _split_text {
632 45 ▶ my $val = shift;
Subroutine _split_text: called CC=10 SCAR=23.0
633 45 my @vals = grep { $_ ne q{} } split(/([;\\"])/, $val);
634 45 my @chunks;
635 # divide it up into chunks to be processed.
636 45 my $in_string = 0;
637 45 my @current_string;
638 45 ▶ for(my $i = 0; $i < @vals; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @vals) 206 45 251
639 206 my $chunk = $vals[$i];
640 206 ▶ if($in_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_string) { } 18 188 206
641 18 ▶ if($chunk eq q{\\}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chunk eq '\\') { } 2 16 18
elsif ($chunk eq '"') { } 4 12 16
642 # don't care about next char probably.
643 # having said that, probably need to be appending to the chunks
644 # just dropping this.
645 2 $i++;
646 2 ▶ if($i < @vals) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @vals) 2 0 2
647 2 push @current_string, $vals[$i];
648 }
649 } elsif($chunk eq q{"}) {
650 4 $in_string = 0;
651 }
652 else {
653 12 push @current_string, $chunk;
654 }
655 } else {
656 188 ▶ if($chunk eq q{"}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chunk eq '"') { } 4 184 188
elsif ($chunk eq ';') { } 72 112 184
657 4 $in_string = 1;
658 }
659 elsif($chunk eq q{;}) {
660 72 push @chunks, join(q{}, @current_string);
661 72 @current_string = ();
662 }
663 else {
664 112 push @current_string, $chunk;
665 }
666 }
667 }
668 45 ▶ push @chunks, join(q{}, @current_string) if @current_string;
Branch: 1 branch
BranchTrueFalseTotal
if @current_string 42 3 45
669 45 s/^\s+// for @chunks;
670 45 return \@chunks;
671 }
672  
673 6 1;
674  
675 =pod
676  
677 =encoding UTF-8
678  
679 =head1 NAME
680  
681 HTTP::Cookies - HTTP cookie jars
682  
683 =head1 VERSION
684  
685 version 6.11
686  
687 =head1 SYNOPSIS
688  
689 use HTTP::Cookies;
690 $cookie_jar = HTTP::Cookies->new(
691 file => "$ENV{'HOME'}/lwp_cookies.dat",
692 autosave => 1,
693 );
694  
695 use LWP;
696 my $browser = LWP::UserAgent->new;
697 $browser->cookie_jar($cookie_jar);
698  
699 Or for an empty and temporary cookie jar:
700  
701 use LWP;
702 my $browser = LWP::UserAgent->new;
703 $browser->cookie_jar( {} );
704  
705 =head1 DESCRIPTION
706  
707 This class is for objects that represent a "cookie jar" -- that is, a
708 database of all the HTTP cookies that a given LWP::UserAgent object
709 knows about.
710  
711 Cookies are a general mechanism which server side connections can use
712 to both store and retrieve information on the client side of the
713 connection. For more information about cookies refer to
714 L<Cookie Spec|http://curl.haxx.se/rfc/cookie_spec.html> and
715 L<Cookie Central|http://www.cookiecentral.com>. This module also implements the
716 new style cookies described in L<RFC 2965|https://tools.ietf.org/html/rfc2965>.
717 The two variants of cookies are supposed to be able to coexist happily.
718  
719 Instances of the class I<HTTP::Cookies> are able to store a collection
720 of Set-Cookie2: and Set-Cookie: headers and are able to use this
721 information to initialize Cookie-headers in I<HTTP::Request> objects.
722 The state of a I<HTTP::Cookies> object can be saved in and restored from
723 files.
724  
725 =head1 LIMITATIONS
726  
727 This module does not support L<< Public Suffix|https://publicsuffix.org/
728 >> encouraged by a more recent standard, L<< RFC
729 6265|https://tools.ietf.org/html/rfc6265 >>.
730  
731 This module's shortcomings mean that a malicious Web site can set
732 cookies to track your user agent across all sites under a top level
733 domain. See F<< t/publicsuffix.t >> in this module's distribution for
734 details.
735  
736 L<< HTTP::CookieJar::LWP >> supports Public Suffix, but only provides a
737 limited subset of this module's functionality and L<< does not
738 support|HTTP::CookieJar/LIMITATIONS-AND-CAVEATS >> standards older than
739 I<RFC 6265>.
740  
741 =head1 METHODS
742  
743 The following methods are provided:
744  
745 =over 4
746  
747 =item $cookie_jar = HTTP::Cookies->new
748  
749 The constructor takes hash style parameters. The following
750 parameters are recognized:
751  
752 file: name of the file to restore cookies from and save cookies to
753 autosave: save during destruction (bool)
754 ignore_discard: save even cookies that are requested to be discarded (bool)
755 hide_cookie2: do not add Cookie2 header to requests
756  
757 Future parameters might include (not yet implemented):
758  
759 max_cookies 300
760 max_cookies_per_domain 20
761 max_cookie_size 4096
762  
763 no_cookies list of domain names that we never return cookies to
764  
765 =item $cookie_jar->get_cookies( $url_or_domain )
766  
767 =item $cookie_jar->get_cookies( $url_or_domain, $cookie_key,... )
768  
769 Returns a hash of the cookies that applies to the given URL. If a
770 domainname is given as argument, then a prefix of "https://" is assumed.
771  
772 If one or more $cookie_key parameters are provided return the given values,
773 or C<undef> if the cookie isn't available.
774  
775 =item $cookie_jar->add_cookie_header( $request )
776  
777 The add_cookie_header() method will set the appropriate Cookie:-header
778 for the I<HTTP::Request> object given as argument. The $request must
779 have a valid url attribute before this method is called.
780  
781 =item $cookie_jar->extract_cookies( $response )
782  
783 The extract_cookies() method will look for Set-Cookie: and
784 Set-Cookie2: headers in the I<HTTP::Response> object passed as
785 argument. Any of these headers that are found are used to update
786 the state of the $cookie_jar.
787  
788 =item $cookie_jar->set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, \%rest )
789  
790 The set_cookie() method updates the state of the $cookie_jar. The
791 $key, $val, $domain, $port and $path arguments are strings. The
792 $path_spec, $secure, $discard arguments are boolean values. The $maxage
793 value is a number indicating number of seconds that this cookie will
794 live. A value of $maxage <= 0 will delete this cookie. The $version argument
795 sets the version of the cookie; the default value is 0 ( original Netscape
796 spec ). Setting $version to another value indicates the RFC to which the
797 cookie conforms (e.g. version 1 for RFC 2109). %rest defines various other
798 attributes like "Comment" and "CommentURL".
799  
800 =item $cookie_jar->save
801  
802 =item $cookie_jar->save( $file )
803  
804 =item $cookie_jar->save( file => $file, ignore_discard => $ignore_discard )
805  
806 This method saves the state of the $cookie_jar to a file.
807 The state can then be restored later using the load() method. If a
808 filename is not specified we will use the name specified during
809 construction. If the $ignore_discard value is true (or not specified,
810 but attribute I<ignore_discard> was set at cookie jar construction),
811 then we will even save cookies that are marked to be discarded.
812  
813 The default is to save a sequence of "Set-Cookie3" lines.
814 "Set-Cookie3" is a proprietary LWP format, not known to be compatible
815 with any browser. The I<HTTP::Cookies::Netscape> sub-class can
816 be used to save in a format compatible with Netscape.
817  
818 =item $cookie_jar->load
819  
820 =item $cookie_jar->load( $file )
821  
822 This method reads the cookies from the file and adds them to the
823 $cookie_jar. The file must be in the format written by the save()
824 method.
825  
826 =item $cookie_jar->revert
827  
828 This method empties the $cookie_jar and re-loads the $cookie_jar
829 from the last save file.
830  
831 =item $cookie_jar->clear
832  
833 =item $cookie_jar->clear( $domain )
834  
835 =item $cookie_jar->clear( $domain, $path )
836  
837 =item $cookie_jar->clear( $domain, $path, $key )
838  
839 Invoking this method without arguments will empty the whole
840 $cookie_jar. If given a single argument only cookies belonging to
841 that domain will be removed. If given two arguments, cookies
842 belonging to the specified path within that domain are removed. If
843 given three arguments, then the cookie with the specified key, path
844 and domain is removed.
845  
846 =item $cookie_jar->clear_temporary_cookies
847  
848 Discard all temporary cookies. Scans for all cookies in the jar
849 with either no expire field or a true C<discard> flag. To be
850 called when the user agent shuts down according to RFC 2965.
851  
852 =item $cookie_jar->scan( \&callback )
853  
854 The argument is a subroutine that will be invoked for each cookie
855 stored in the $cookie_jar. The subroutine will be invoked with
856 the following arguments:
857  
858 0 version
859 1 key
860 2 val
861 3 path
862 4 domain
863 5 port
864 6 path_spec
865 7 secure
866 8 expires
867 9 discard
868 10 hash
869  
870 =item $cookie_jar->as_string
871  
872 =item $cookie_jar->as_string( $skip_discardables )
873  
874 The as_string() method will return the state of the $cookie_jar
875 represented as a sequence of "Set-Cookie3" header lines separated by
876 "\n". If $skip_discardables is TRUE, it will not return lines for
877 cookies with the I<Discard> attribute.
878  
879 =back
880  
881 =head1 SEE ALSO
882  
883 L<HTTP::Cookies::Netscape>, L<HTTP::Cookies::Microsoft>
884  
885 =head1 AUTHOR
886  
887 Gisle Aas <gisle@activestate.com>
888  
889 =head1 COPYRIGHT AND LICENSE
890  
891 This software is copyright (c) 2002 by Gisle Aas.
892  
893 This is free software; you can redistribute it and/or modify it under
894 the same terms as the Perl 5 programming language system itself.
895  
896 =cut
897  
898 __END__
« Cookies/Microsoft.pm ↑ Summary Cookies/Netscape.pm »