blib/lib/Cookie/Baker.pm

filter: Statementstmt 95.9% 94 / 98 Branchbran 84.4% 49 / 58 Conditioncond 100% 17 / 17 MC/DCmcdc 100% 11 / 11 Subroutinesub 100% 11 / 11 Podpod 33.3% 1 / 3 totaltotal 92.4% 183 / 198 CC 38 SCAR 25.2
CC
38
Cov
92%
bake_cookie
22.0
pp_crush_cookie
6.0
BEGIN
5.2
CRAP 12.4
↑ Summary
1 package Cookie::Baker;
2  
3 3 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=16.5
10 3 our $VERSION = "0.13";
11 3 our @EXPORT = qw/bake_cookie crush_cookie/;
12 3 my $use_pp = $ENV{COOKIE_BAKER_PP};
13 3 ▶ if (!$use_pp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_pp) 3 0 3
14 3 eval {
15 3 require Cookie::Baker::XS;
16 0 ▶ if ( $Cookie::Baker::XS::VERSION < $VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Cookie::Baker::XS::VERSION < $Cookie::Baker::VERSION) 0 0 0
17 0 warn "Cookie::Baker::XS $VERSION is require. fallback to PP version";
18 0 die;
19 }
20 };
21 3 $use_pp = !!$@;
22 }
23 3 ▶ if ($use_pp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_pp) { } 3 0 3
24 3 *crush_cookie = \&pp_crush_cookie;
25 }
26 else {
27 0 *crush_cookie = \&Cookie::Baker::XS::crush_cookie;
28 }
29 }
30  
31  
32 6 ▶sub valid_path {
Subroutine valid_path: called CC=5 SCAR=16.3
Pod undocumented
33 6 my $path = shift;
34 6 for my $s (split //, $path) {
35 8 my $hex = hex(ord($s));
36 8 ▶ return 0 if 0x20 > $hex;
Branch: 1 branch
BranchTrueFalseTotal
if 32 > $hex 0 8 8
37 8 ▶ return 0 if $hex >= 0x7f;
Branch: 1 branch
BranchTrueFalseTotal
if $hex >= 127 1 7 8
38 7 ▶ return 0 if $hex == 0x3b; # 0x3b is ';'
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 59 0 7 7
39 }
40 5 return 1;
41 }
42  
43 35 ▶sub bake_cookie {
Subroutine bake_cookie: called CC=22 SCAR=30.9
Pod documented
44 35 my ($name,$val) = @_;
45  
46 35 ▶ return '' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 35 35
47 35 ▶ my %args = ref $val ? %{$val} : (value => $val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 34 1 35
48 35 ▶ if ($args{partitioned}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"partitioned"}) 2 33 35
49 # enforce SameSite=None; and secure; on CHIPS (Cookies Having Independent Partitioned State)
50 2 $args{samesite} = 'none';
51 2 $args{secure} = 1;
52 }
53  
54 # validation for domain, path and max-age
55 35 ▶ if (exists $args{domain} && $args{domain} !~ /^[a-zA-Z0-9\.\-]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"domain"} and not $args{"domain"} =~ /^[a-zA-Z0-9\.\-]+$/) 1 34 35
Condition100%3/3
exists $args{"domain"} and not $args{"domain"} =~ /^[a-zA-Z0-9\.\-]+$/
!ll&&!rl&&r
3311
MC/DC100%2/2
exists $args{"domain"} and not $args{"domain"} =~ /^[a-zA-Z0-9\.\-]+$/
exists $args{"domain"} not $args{"domain"} =~ /^[a-zA-Z0-9\.\-]+$/
Truth table100%3/3
A and not(B)
A= exists $args{"domain"}B= not $args{"domain"} =~ /^[a-zA-Z0-9\.\-]+$/
ABresult
0X0
100
111
56 1 warn "Invalid domain: $args{domain}";
57 1 delete $args{domain};
58 }
59 35 ▶ if (exists $args{path} && !valid_path($args{path})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"path"} and not valid_path($args{"path"})) 1 34 35
Condition100%3/3
exists $args{"path"} and not valid_path($args{"path"})
!ll&&!rl&&r
2951
MC/DC100%2/2
exists $args{"path"} and not valid_path($args{"path"})
exists $args{"path"} not valid_path($args{"path"})
Truth table100%3/3
A and not(B)
A= exists $args{"path"}B= not valid_path($args{"path"})
ABresult
0X0
100
111
60 1 warn "Invalid path: $args{path}";
61 1 delete $args{path};
62 }
63 35 ▶ if (exists $args{"max-age"} && $args{"max-age"} !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"max-age"} and not $args{"max-age"} =~ /^\d+$/) 1 34 35
Condition100%3/3
exists $args{"max-age"} and not $args{"max-age"} =~ /^\d+$/
!ll&&!rl&&r
3221
MC/DC100%2/2
exists $args{"max-age"} and not $args{"max-age"} =~ /^\d+$/
exists $args{"max-age"} not $args{"max-age"} =~ /^\d+$/
Truth table100%3/3
A and not(B)
A= exists $args{"max-age"}B= not $args{"max-age"} =~ /^\d+$/
ABresult
0X0
100
111
64 1 warn "Invalid max-age: " . $args{"max-age"};
65 1 delete $args{"max-age"};
66 }
67  
68 35 ▶ $name = URI::Escape::uri_escape($name) if $name =~ m![^a-zA-Z\-\._~]!;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /[^a-zA-Z\-\._~]/ 0 35 35
69 35 my $cookie = "$name=" . URI::Escape::uri_escape($args{value}) . '; ';
70 35 ▶ $cookie .= 'domain=' . $args{domain} . '; ' if $args{domain};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"domain"} 1 34 35
71 35 ▶ $cookie .= 'path='. $args{path} . '; ' if $args{path};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"path"} 5 30 35
72 35 ▶ $cookie .= 'expires=' . _date($args{expires}) . '; ' if exists $args{expires} && defined $args{expires};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"expires"} and defined $args{"expires"} 13 22 35
Condition100%3/3
exists $args{"expires"} and defined $args{"expires"}
!ll&&!rl&&r
21113
MC/DC100%2/2
exists $args{"expires"} and defined $args{"expires"}
exists $args{"expires"} defined $args{"expires"}
Truth table100%3/3
A and B
A= exists $args{"expires"}B= defined $args{"expires"}
ABresult
0X0
100
111
73 35 ▶ $cookie .= 'max-age=' . $args{"max-age"} . '; ' if exists $args{"max-age"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"max-age"} 2 33 35
74 35 ▶ if (exists $args{samesite} && $args{samesite} =~ m/\A(?:lax|strict|none)\Z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"samesite"} and $args{"samesite"} =~ /\A(?:lax|strict|none)\Z/i) 6 29 35
Condition100%3/3
exists $args{"samesite"} and $args{"samesite"} =~ /\A(?:lax|strict|none)\Z/i
!ll&&!rl&&r
2726
MC/DC100%2/2
exists $args{"samesite"} and $args{"samesite"} =~ /\A(?:lax|strict|none)\Z/i
exists $args{"samesite"} $args{"samesite"} =~ /\A(?:lax|strict|none)\Z/i
Truth table100%3/3
A and B
A= exists $args{"samesite"}B= $args{"samesite"} =~ /\A(?:lax|strict|none)\Z/i
ABresult
0X0
100
111
75 6 $cookie .= 'SameSite=' . ucfirst(lc($args{samesite})) . '; ';
76 # secure flag must be set when SameSite=None
77 6 ▶ $args{secure} = 1 if $cookie =~ m/SameSite=None; /;
Branch: 1 branch
BranchTrueFalseTotal
if $cookie =~ /SameSite=None; / 4 2 6
78 }
79 35 ▶ $cookie .= 'secure; ' if $args{secure};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"secure"} 5 30 35
80 35 ▶ $cookie .= 'HttpOnly; ' if $args{httponly};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"httponly"} 1 34 35
81 35 ▶ $cookie .= 'Partitioned; ' if $args{partitioned};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"partitioned"} 2 33 35
82  
83 35 substr($cookie,-2,2,'');
84 35 $cookie;
85 }
86  
87 3 my @MON = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
88 3 my @WDAY = qw( Sun Mon Tue Wed Thu Fri Sat );
89  
90 3 my %term = (
91 's' => 1,
92 'm' => 60,
93 'h' => 3600,
94 'd' => 86400,
95 'M' => 86400 * 30,
96 'y' => 86400 * 365,
97 );
98  
99 13 ▶sub _date {
Subroutine _date: called CC=5 SCAR=16.1
100 13 my $expires = shift;
101  
102 13 my $expires_at;
103 13 ▶ if ($expires =~ /^\d+$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($expires =~ /^\d+$/) { } 2 11 13
elsif ($expires =~ /^([-+]?(?:\d+|\d*\.\d*))([smhdMy]?)/) { } 9 2 11
elsif ($expires eq 'now') { } 1 1 2
104 # all numbers -> epoch date
105 2 $expires_at = $expires;
106 }
107 elsif ( $expires =~ /^([-+]?(?:\d+|\d*\.\d*))([smhdMy]?)/ ) {
108 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 9 ▶ my $offset = ($term{$2} || 1) * $1;
Condition100%2/2
$term{$2} || 1
l!l
72
MC/DC100%1/1
$term{$2} || 1
$term{$2}
Truth table100%2/2
A
A= $term{$2}
Aresult
11
00
110 9 $expires_at = time + $offset;
111 }
112 elsif ( $expires eq 'now' ) {
113 1 $expires_at = time;
114 }
115 else {
116 1 return $expires;
117 }
118 12 my($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($expires_at);
119 12 $year += 1900;
120 # (cookies use '-' as date separator, HTTP uses ' ')
121 12 return sprintf("%s, %02d-%s-%04d %02d:%02d:%02d GMT",
122 $WDAY[$wday], $mday, $MON[$mon], $year, $hour, $min, $sec);
123 }
124  
125 52 ▶sub pp_crush_cookie {
Subroutine pp_crush_cookie: called CC=6 SCAR=17.9
Pod undocumented
126 52 my $cookie_string = shift;
127 52 my $raw_key = shift;
128 52 ▶ return {} unless $cookie_string;
Branch: 1 branch
BranchTrueFalseTotal
unless $cookie_string 4 48 52
129 48 my %results;
130 48 my @pairs = grep m/=/, split /; ?/, $cookie_string;
131 48 for my $pair ( @pairs ) {
132 # trim leading trailing whitespace
133 138 $pair =~ s/^\s+//; $pair =~ s/\s+$//;
134  
135 138 my ($key, $value) = split( "=", $pair, 2 );
136  
137 138 ▶ if (!$raw_key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raw_key) 116 22 138
138 116 $key = URI::Escape::uri_unescape($key);
139 }
140  
141 # Values can be quoted
142 138 ▶ $value = "" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 138 138
143 138 $value =~ s/\A"(.*)"\z/$1/;
144 138 $value = URI::Escape::uri_unescape($value);
145  
146 # Take the first one like CGI.pm or rack do
147 138 ▶ $results{$key} = $value unless exists $results{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $results{$key} 126 12 138
148 }
149 48 return \%results;
150 }
151  
152 3 1;
153 __END__
↑ Summary