Cookies.pm

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