State/Cookie.pm

filter: Statementstmt 85.8% 151 / 176 Branchbran 59.6% 31 / 52 Conditioncond 49.0% 26 / 53 MC/DCmcdc 37.5% 12 / 32 Subroutinesub 86.3% 19 / 22 Podpod 83.3% 5 / 6 totaltotal 71.5% 244 / 341 CC 64 SCAR 41.2
CC
64
Cov
74%
hash_set_cookie
272.0
decode_set_cookie
22.2
encode_set_cookie
16.1
CRAP 61.5
« State.pm ↑ Summary
1 6 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTTP::State::Cookie;
4 6 ▶no warnings "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Logging
7 #
8 6 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Log::OK;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 6 ▶use builtin qw<trim>;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 6 my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
17 6 my $i=0;
18 6 my %months= map {$_,$i++} @months;
19  
20 6 $i=0;
21 6 my @days= qw(Sun Mon Tue Wed Thu Fri Sat);
22 6 my %days= map {$_,$i++} @days;
23  
24 6 my @names;
25 my @same_site_names;
26 #my @values;
27 6 my %const_names;
28 6 my @pairs;
29 6 my @same_site_pairs;
30  
31 6 my %reverse;
32 6 my %same_site_reverse;
33 6 my @forward;
34  
35 BEGIN {
36 6 ▶ @names=qw<
Subroutine BEGIN: called CC=3 SCAR=11.0
37 Undef
38 Name
39 Value
40 Expires
41 Max-Age
42 Domain
43 Path
44 Secure
45 HttpOnly
46 SameSite
47 Partitioned
48
49 Creation_Time
50 Last_Access_Time
51 Persistent
52 HostOnly
53 Key
54 >;
55  
56 #@values= 0 .. @names-1;
57  
58 6 @same_site_names = qw<Undef Lax Strict None Default>;
59  
60 6 for my ($i)(0..$#names){
61 96 $const_names{("COOKIE_".uc $names[$i])=~tr/-/_/r}= $i;
62 96 $reverse{lc $names[$i]}=$i;
63 }
64 6 $reverse{undef}=0; #catching
65  
66 #Additional keys in hash for HTTP::CookieJar support
67  
68  
69 6 for my ($i)(0..$#same_site_names){
70 30 $const_names{"SAME_SITE_".uc $same_site_names[$i]}=$i;
71 30 $same_site_reverse{lc $same_site_names[$i]}=$i;
72 }
73 6 $same_site_reverse{undef}=0; #catching
74 }
75  
76 6 ▶use constant::more \%const_names;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78  
79 use Export::These
80 6 "cookie_struct",
81 constants=>["cookie_struct", keys %const_names],
82 encode=>[qw<cookie_struct encode_set_cookie encode_cookies hash_set_cookie>],
83 decode=>[qw<cookie_struct decode_set_cookie decode_cookies>],
84 export_ok=>["hash_set_cookie"],
85 all=>
86 6 ▶ [keys(%const_names),qw<encode_cookies encode_set_cookie decode_set_cookie decode_cookies cookie_struct hash_set_cookie>];
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88  
89 6 ▶use Time::Local qw<timelocal_modern>;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 6 my $tz_offset;
92  
93  
94 BEGIN {
95 # Low memory timezone offset calculation.
96 # Removes the need to load Time::Piece just for timezone offset
97 6 ▶ my $now=time;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 6 my @g=gmtime $now;
99 6 my @l=localtime $now;
100 6 my $gs=$g[0]+ $g[1]*60 + $g[2]*3600 + $g[3]*86400;
101 6 my $ls=$l[0]+ $l[1]*60 + $l[2]*3600 + $l[3]*86400;
102 6 $tz_offset=$ls-$gs;
103 }
104  
105 6 ▶use constant::more TZ_OFFSET=>$tz_offset;
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107  
108 # Expects the name and value as the first pair of arguments
109 sub cookie_struct {
110  
111 6 ▶ no warnings "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
112 26 ▶ my @c=(1, shift, shift); # Reuse the first field as string/int marker
Subroutine cookie_struct: called CC=11 SCAR=24.4
Pod documented
113  
114  
115 26 ▶ die "Cookie must have a name" unless $c[COOKIE_NAME];
Branch: 1 branch
BranchTrueFalseTotal
unless $c[1] 0 26 26
116  
117 26 ▶ if(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 25 1 26
118 6 ▶ no warnings "uninitialized";
Subroutine BEGIN: called CC=1 SCAR=0.0
119 6 ▶ no warnings "numeric";
Subroutine BEGIN: called CC=1 SCAR=0.0
120 25 ▶ if($c[$_[0]]){
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$_[0]]) { } 25 0 25
121 # anticipate keys provided as string.
122 #
123 # If the first remaining argument is numeric (field constant) will be an undef value
124 # which when used in numeric constant will be 0. The $c[0] is set to one which is true
125 # which means we anticipate string names
126 25 for my ($k, $v)(@_){
127 48 $c[$reverse{lc $k}]=$v;
128 }
129 }
130 else{
131 # keys assumed to be integer constants
132 #
133 0 for my ($k, $v)(@_){
134 0 $c[$k]=$v;
135 }
136 }
137  
138  
139 25 ▶ $c[COOKIE_EXPIRES]-=TZ_OFFSET if defined $c[COOKIE_EXPIRES];
Branch: 1 branch
BranchTrueFalseTotal
if defined $c[3] 6 19 25
140 25 ▶ $c[COOKIE_DOMAIN]=scalar reverse lc $c[COOKIE_DOMAIN] if $c[COOKIE_DOMAIN];
Branch: 1 branch
BranchTrueFalseTotal
if $c[5] 12 13 25
141  
142 25 $c[COOKIE_SAMESITE]=$same_site_reverse{lc$c[COOKIE_SAMESITE]};
143 25 ▶ $c[COOKIE_HOSTONLY]//=0;
Condition50%1/2
$c[14] //= 0
l!l
025
MC/DC0%0/1
$c[14] //= 0
$c[14]
Truth table50%1/2
A
A= $c[14]
Aresult
11
00
144  
145 }
146  
147 26 ▶ $c[COOKIE_NAME]//="";
Condition50%1/2
$c[1] //= ""
l!l
260
MC/DC0%0/1
$c[1] //= ""
$c[1]
Truth table50%1/2
A
A= $c[1]
Aresult
11
00
148 26 ▶ $c[COOKIE_VALUE]//="";
Condition100%2/2
$c[2] //= ""
l!l
251
MC/DC100%1/1
$c[2] //= ""
$c[2]
Truth table100%2/2
A
A= $c[2]
Aresult
11
00
149  
150 # Remove any extra fields added in haste
151 #
152 #splice @c, COOKIE_KEY+1;
153  
154 26 \@c;
155 }
156  
157  
158 # Supports a simple scalar or an array ref of simple scalars to parse/decode
159 sub decode_cookies {
160 6 ▶ no warnings "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
161 0 ▶ my @values= map trim($_), #trim leading /trailing white space
Subroutine decode_cookies: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
162 map split("=", $_, 2), #Split fields into KV pairs
163 split /;\s*/, ref($_[0])
164 ? join("; ", $_[0]->@*)
165 : $_[0]; #Split input into fields
166 0 @values;
167 }
168  
169 # Returns a newly created cookie struct from a Set-Cookie string. Does not
170 # validate or create default values of attributess. Only processes what is
171 # given
172 #
173 sub decode_set_cookie{
174 21 ▶ return undef unless $_[0];
Subroutine decode_set_cookie: called CC=20 SCAR=31.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 21 21
175 6 ▶ no warnings "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
176 # $string, converter
177 21 my $input=$_[0];
178 21 my $key;
179 my $value;
180 21 my @values;
181 21 my $first=1;
182 21 my @fields;
183  
184 #Value needs to be the first field
185  
186 21 my $index=index $input, ";";
187 21 my $name_value;
188 21 ▶ if($index>=0){
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= 0) { } 21 0 21
189 # at least one ";" was found
190 21 $name_value=substr $input,0, $index;
191 21 substr $input, 0, $index+1, "";
192
193 }
194 else {
195 # No ";" found
196 0 $name_value=$input;
197 0 $input="";
198 }
199
200 21 Log::OK::TRACE and log_trace " decoding cookie name: name value: $name_value";
201  
202 21 $index=index $name_value, "=";
203  
204 #Abort unless has a name
205 21 ▶ return unless $index >0;
Branch: 1 branch
BranchTrueFalseTotal
unless $index > 0 0 21 21
206  
207 21 $values[1]= substr $name_value, 0, $index;
208 21 $values[2]= substr $name_value, $index+1;
209 21 Log::OK::TRACE and log_trace " decoding cookie name: $values[1] value:$values[2]";
210  
211  
212 # trip whitespace
213 21 $values[1]=trim($values[1]);
214 21 $values[2]=trim($values[2]);
215  
216 # TODO: test for controll characters
217
218  
219  
220 21 Log::OK::TRACE and log_trace " decoding cookie name: $values[1] value:$values[2]";
221  
222 #Process attributes if input remaining;
223 21 ▶ return \@values unless $input;
Branch: 1 branch
BranchTrueFalseTotal
unless $input 0 21 21
224  
225 21 @fields=split /;\s*/, $input;
226  
227 21 for(@fields){
228  
229 78 ($key, $value)=split "=", $_, 2;
230  
231 78 $key=trim($key);
232 78 ▶ $value=trim($value) if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 64 14 78
233  
234 # Attributes are processed with case insensitive names
235 #
236 78 $key=lc $key;
237  
238 # Look up the value key value pair
239 # unkown values are stored in the undef => 0 position
240 78 ▶ $values[$reverse{$key}]=$value//1;
Condition100%2/2
$value // 1
l!l
6414
MC/DC100%1/1
$value // 1
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
241 }
242  
243 # nuke unkown value
244 21 $values[0]=undef;
245  
246  
247 # Fix the date. Date is stored in seconds internally
248 #
249 21 ▶ for($values[COOKIE_EXPIRES]//()){
Condition66%2/3
$values[3] // ()
l!l&&r!l&&!r
10011
MC/DC50%1/2
$values[3] // ()
$values[3] ()
Truth table66%2/3
A // B
A= $values[3]B= ()
ABresult
1X1
011
000
250 10 Log::OK::TRACE and log_trace " converting cookie expires from stamp to epoch";
251 10 my ($wday_key, $mday, $mon_key, $year, $hour, $min, $sec, $tz)=
252 /([^,]+), (\d+).([^-]{3}).(\d{4}) (\d+):(\d+):(\d+) (\w+)/;
253 #TODO support parsing of other deprecated data formats
254  
255 10 ▶ if(70<=$year<=99){
Branch: 2 branches
BranchTrueFalseTotal
if (70 <= $year <= 99) { } 0 10 10
elsif (0 <= $year <= 69) { } 0 10 10
256 0 $year+=1900;
257 }
258 elsif(0<=$year<=69){
259 0 $year+=2000;
260 }
261 else{
262 #year as is
263 }
264 #NOTE: timelocal_modern DOES NOT add/subtract time offset. Which is what we want
265 #as the time is already gmt
266 #
267 10 $_ = timelocal_modern($sec, $min, $hour, $mday, $months{$mon_key}, $year);
268 }
269  
270 # adjust creation and last modified times
271 21 ▶ if(defined $_[1]){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 8 13 21
272 8 ▶ $values[COOKIE_CREATION_TIME]-=$_[1] if $values[COOKIE_CREATION_TIME];
Branch: 1 branch
BranchTrueFalseTotal
if $values[11] 8 0 8
273 8 ▶ $values[COOKIE_LAST_ACCESS_TIME]-=$_[1] if $values[COOKIE_LAST_ACCESS_TIME];
Branch: 1 branch
BranchTrueFalseTotal
if $values[12] 8 0 8
274  
275 }
276  
277  
278 # Fix leading/trailing dot
279 21 ▶ for($values[COOKIE_DOMAIN]//()){
Condition66%2/3
$values[5] // ()
l!l&&r!l&&!r
11010
MC/DC50%1/2
$values[5] // ()
$values[5] ()
Truth table66%2/3
A // B
A= $values[5]B= ()
ABresult
1X1
011
000
280 11 s/\.$//;
281 11 s/^\.//;
282 11 $_ = scalar reverse $_;
283 }
284  
285 # Fix same site
286
287 21 ▶ for($values[COOKIE_SAMESITE]//()){
Condition66%2/3
$values[9] // ()
l!l&&r!l&&!r
1209
MC/DC50%1/2
$values[9] // ()
$values[9] ()
Truth table66%2/3
A // B
A= $values[9]B= ()
ABresult
1X1
011
000
288 12 $_=$same_site_reverse{lc $_};
289 }
290 # Ensure host only is defined
291 21 ▶ $values[COOKIE_HOSTONLY]//=0;
Condition100%2/2
$values[14] //= 0
l!l
714
MC/DC100%1/1
$values[14] //= 0
$values[14]
Truth table100%2/2
A
A= $values[14]
Aresult
11
00
292  
293  
294
295  
296 21 \@values;
297 }
298  
299 # Encodes KV pairs from supplied cookie structs
300 sub encode_cookies {
301 0 ▶ join "; ", map "$_->[COOKIE_NAME]=".($_->[COOKIE_VALUE]//""), @_;
Subroutine encode_cookies: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_->[2] // ""
l!l
00
MC/DC0%0/1
$_->[2] // ""
$_->[2]
Truth table0%0/2
A
A= $_->[2]
Aresult
11
00
302 }
303  
304 sub encode_set_cookie {
305 43 ▶ my ($cookie, $store_flag, $partition_key)=@_;
Subroutine encode_set_cookie: called CC=16 SCAR=27.8
Pod documented
306 43 Log::OK::DEBUG and log_debug "Serializing set cookie";
307  
308 # Start with name and value
309 #
310 43 ▶ my $string= "$cookie->[COOKIE_NAME]=".($cookie->[COOKIE_VALUE]//"");
Condition50%1/2
$cookie->[2] // ""
l!l
430
MC/DC0%0/1
$cookie->[2] // ""
$cookie->[2]
Truth table50%1/2
A
A= $cookie->[2]
Aresult
11
00
311  
312  
313  
314
315 # Format date for expires. Internally the cookie structure stores this value
316 # in terms of GMT.
317 # Again only add the attribute if value is defined
318 #
319 #for($cookie->[COOKIE_PERSISTENT] &&
320 43 ▶ for($cookie->[COOKIE_EXPIRES]//()){
Condition100%3/3
$cookie->[3] // ()
l!l&&r!l&&!r
3535
MC/DC100%2/2
$cookie->[3] // ()
$cookie->[3] ()
Truth table100%3/3
A // B
A= $cookie->[3]B= ()
ABresult
1X1
011
000
321 #
322 #NOTE: localtime doesn't add/subtract offsets. This is what we want as it was manually adjusted.
323 #
324 35 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =localtime $_;
325 35 $string.="; $names[COOKIE_EXPIRES]=$days[$wday], ".sprintf("%02d",$mday)." $months[$mon] ".($year+1900) .sprintf(" %02d:%02d:%02d", $hour,$min,$sec)." GMT";
326 }
327 # Reverse the cookie domain (stored backwards) if preset. Don't add the attribute
328 # if not defined.
329 #
330 $string.= "; $names[COOKIE_DOMAIN]=".scalar reverse $_
331 43 ▶ for $cookie->[COOKIE_DOMAIN]//();
Condition100%3/3
$cookie->[5] // ()
l!l&&r!l&&!r
3526
MC/DC100%2/2
$cookie->[5] // ()
$cookie->[5] ()
Truth table100%3/3
A // B
A= $cookie->[5]B= ()
ABresult
1X1
011
000
332  
333 # Do Attributes with needing values. Only add them if the attribute is
334 # defined
335 #
336 43 for my $index (COOKIE_MAX_AGE, COOKIE_PATH){
337 86 ▶ for($cookie->[$index]//()){
Condition66%2/3
$cookie->[$index] // ()
l!l&&r!l&&!r
39470
MC/DC0%0/2
$cookie->[$index] // ()
$cookie->[$index] ()
Truth table66%2/3
A // B
A= $cookie->[$index]B= ()
ABresult
1X1
011
000
338 39 $string.="; $names[$index]=$_";
339 }
340 }
341  
342  
343 # Do flags (attibutes with no values)
344 #
345 43 ▶ $string.="; $names[COOKIE_SECURE]" if defined $cookie->[COOKIE_SECURE];
Branch: 1 branch
BranchTrueFalseTotal
if defined $cookie->[7] 13 30 43
346 43 ▶ $string.="; $names[COOKIE_HTTPONLY]" if defined $cookie->[COOKIE_HTTPONLY];
Branch: 1 branch
BranchTrueFalseTotal
if defined $cookie->[8] 0 43 43
347  
348 43 ▶ if(defined $store_flag){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $store_flag) 33 10 43
349 # If asked for storage format, give internal values
350 #
351 33 $string.="; Creation_Time=".($cookie->[COOKIE_CREATION_TIME]+$store_flag);
352 33 $string.="; Last_Access_Time=".($cookie->[COOKIE_LAST_ACCESS_TIME]+$store_flag);
353 33 ▶ $string.="; HostOnly" if $cookie->[COOKIE_HOSTONLY];
Branch: 1 branch
BranchTrueFalseTotal
if $cookie->[14] 24 9 33
354 33 ▶ $string.="; Partitioned=$partition_key" if $cookie->[COOKIE_PARTITIONED] and $partition_key; #Store the partition key in the partitioned field
Branch: 1 branch
BranchTrueFalseTotal
if $cookie->[10] and $partition_key 9 24 33
Condition100%3/3
$cookie->[10] and $partition_key
!ll&&!rl&&r
2229
MC/DC100%2/2
$cookie->[10] and $partition_key
$cookie->[10] $partition_key
Truth table100%3/3
A and B
A= $cookie->[10]B= $partition_key
ABresult
0X0
100
111
355 #$string.="; Persistent" if $cookie->[COOKIE_PERSISTENT];
356 }
357 43 ▶ $string.="; $names[COOKIE_SAMESITE]=".$same_site_names[$cookie->[COOKIE_SAMESITE]] if $cookie->[COOKIE_SAMESITE];
Branch: 1 branch
BranchTrueFalseTotal
if $cookie->[9] 32 11 43
358  
359 43 $string;
360  
361 }
362  
363 #mosty for compatibility with HTTP::CookieJar 'cookies_for' method
364 sub hash_set_cookie{
365 0 ▶ my ($cookie, $store_flag)=@_;
Subroutine hash_set_cookie: not called CC=16 SCAR=56.1
Pod undocumented
366 0 my %hash=(name=>$cookie->[COOKIE_NAME], value=>$cookie->[COOKIE_VALUE]);
367  
368 # Reverse the cookie domain (stored backwards) if preset. Don't add the attribute
369 # if not defined.
370 #
371 $hash{domain}=scalar reverse $_
372 0 ▶ for $cookie->[COOKIE_DOMAIN]//();
Condition0%0/3
$cookie->[5] // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$cookie->[5] // ()
$cookie->[5] ()
Truth table0%0/3
A // B
A= $cookie->[5]B= ()
ABresult
1X1
011
000
373  
374 # Do Attributes with needing values. Only add them if the attribute is
375 # defined
376 #
377 0 ▶ $hash{maxage}=$_ for $cookie->[COOKIE_MAX_AGE]//();
Condition0%0/3
$cookie->[4] // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$cookie->[4] // ()
$cookie->[4] ()
Truth table0%0/3
A // B
A= $cookie->[4]B= ()
ABresult
1X1
011
000
378 0 ▶ $hash{path}=$_ for $cookie->[COOKIE_PATH]//();
Condition0%0/3
$cookie->[6] // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$cookie->[6] // ()
$cookie->[6] ()
Truth table0%0/3
A // B
A= $cookie->[6]B= ()
ABresult
1X1
011
000
379 0 ▶ $hash{samesite}=$_ for $cookie->[COOKIE_SAMESITE]//();
Condition0%0/3
$cookie->[9] // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$cookie->[9] // ()
$cookie->[9] ()
Truth table0%0/3
A // B
A= $cookie->[9]B= ()
ABresult
1X1
011
000
380  
381
382 # Format date for expires. Internally the cookie structure stores this value
383 # in terms of GMT.
384 # Again only add the attribute if value is defined
385 #
386 0 ▶ for($cookie->[COOKIE_PERSISTENT] && $cookie->[COOKIE_EXPIRES]//()){
Condition0%0/6
$cookie->[13] && $cookie->[3] // ()
l!l&&r!l&&!r
000
$cookie->[13] and $cookie->[3]
!ll&&!rl&&r
000
MC/DC0%0/3
$cookie->[13] && $cookie->[3] // ()
$cookie->[13] $cookie->[3] ()
Truth table0%0/5
A and B // C
A= $cookie->[13]B= $cookie->[3]C= ()
ABCresult
11X1
0X11
1011
0X00
1000
387 0 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =localtime $_;
388 0 $hash{expires}="$days[$wday], ".sprintf("%02d",$mday)." $months[$mon] ".($year+1900) .sprintf(" %02d:%02d:%02d",$hour,$min,$sec)." GMT";
389 }
390  
391 # Do flags (attibutes with no values)
392 #
393 0 ▶ $hash{secure}=1 if defined $cookie->[COOKIE_SECURE];
Branch: 1 branch
BranchTrueFalseTotal
if defined $cookie->[7] 0 0 0
394 0 ▶ $hash{httponly}=1 if defined $cookie->[COOKIE_HTTPONLY];
Branch: 1 branch
BranchTrueFalseTotal
if defined $cookie->[8] 0 0 0
395  
396 0 ▶ if(defined $store_flag){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $store_flag) 0 0 0
397 # If asked for storage format, give internal values
398 #
399 0 ▶ $hash{hostonly}=1 if $cookie->[COOKIE_HOSTONLY];
Branch: 1 branch
BranchTrueFalseTotal
if $cookie->[14] 0 0 0
400 0 $hash{creation_time}=($cookie->[COOKIE_CREATION_TIME]+$store_flag);
401 0 $hash{access_time}=($cookie->[COOKIE_LAST_ACCESS_TIME]+$store_flag);
402 }
403  
404 0 \%hash;
405 }
406 6 1;
« State.pm ↑ Summary