Simple/Cookie.pm

filter: Statementstmt 100% 125 / 125 Branchbran 89.7% 79 / 88 Conditioncond 65.2% 15 / 23 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 100% 22 / 22 Podpod 64.7% 11 / 17 totaltotal 89.3% 259 / 290 CC 55 SCAR 19.1
CC
55
Cov
93%
new
14.4
as_string
11.0
parse
7.3
CRAP 6.8
« Simple/Util.pm ↑ Summary
1 package CGI::Simple::Cookie;
2  
3 # Original version Copyright 1995-1999, Lincoln D. Stein. All rights reserved.
4 # It may be used and modified freely, but I do request that this copyright
5 # notice remain attached to the file. You may modify this module as you
6 # wish, but if you redistribute a modified version, please attach a note
7 # listing the modifications you have made.
8  
9 # This version Copyright 2001, Dr James Freeman. All rights reserved.
10 # Renamed, strictified, and generally hacked code. Now 30% shorter.
11 # Interface remains identical and passes all original CGI::Cookie tests
12  
13 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 $VERSION = '1.282';
17 2 ▶use CGI::Simple::Util qw(rearrange unescape escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use overload '""' => \&as_string, 'cmp' => \&compare, 'fallback' => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # fetch a list of cookies from the environment and return as a hash.
21 # the cookies are parsed as normal escaped URL data.
22 5 ▶sub fetch {
Subroutine fetch: called CC=3 SCAR=11.0
Pod undocumented
23 5 my $self = shift;
24 5 ▶ my $raw_cookie = $ENV{HTTP_COOKIE} || $ENV{COOKIE};
Condition100%3/3
$ENV{'HTTP_COOKIE'} || $ENV{'COOKIE'}
l!l&&r!l&&!r
122
MC/DC100%2/2
$ENV{'HTTP_COOKIE'} || $ENV{'COOKIE'}
$ENV{'HTTP_COOKIE'} $ENV{'COOKIE'}
Truth table100%3/3
A || B
A= $ENV{'HTTP_COOKIE'}B= $ENV{'COOKIE'}
ABresult
1X1
011
000
25 5 ▶ return () unless $raw_cookie;
Branch: 1 branch
BranchTrueFalseTotal
unless $raw_cookie 2 3 5
26 3 return $self->parse( $raw_cookie );
27 }
28  
29 7 ▶sub parse {
Subroutine parse: called CC=7 SCAR=19.9
Pod undocumented
30 7 my ( $self, $raw_cookie ) = @_;
31 7 ▶ return () unless $raw_cookie;
Branch: 1 branch
BranchTrueFalseTotal
unless $raw_cookie 0 7 7
32 7 my %results;
33 7 my @pairs = split "[;,] ?", $raw_cookie;
34 7 for my $pair ( @pairs ) {
35 # trim leading trailing whitespace
36 25 $pair =~ s/^\s+//;
37 25 $pair =~ s/\s+$//;
38 25 my ( $key, $value ) = split( "=", $pair, 2 );
39 25 ▶ next if !defined( $value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 25 25
40 25 my @values = ();
41 25 ▶ if ( $value ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value ne "") 25 0 25
42 25 @values = map unescape( $_ ), split( /[&;]/, $value . '&dmy' );
43 25 pop @values;
44 }
45 25 $key = unescape( $key );
46  
47 # A bug in Netscape can cause several cookies with same name to
48 # appear. The FIRST one in HTTP_COOKIE is the most recent version.
49 25 ▶ $results{$key} ||= $self->new( -name => $key, -value => \@values );
Condition33%1/3
$results{$key} ||= $self->new("-name", $key, "-value", \@values)
l!l&&r!l&&!r
0250
MC/DC0%0/2
$results{$key} ||= $self->new("-name", $key, "-value", \@values)
$results{$key} $self->new("-name", $key, "-value", \@values)
Truth table33%1/3
A ||= B
A= $results{$key}B= $self->new("-name", $key, "-value", \@values)
ABresult
1X1
011
000
50 }
51 7 ▶ return wantarray ? %results : \%results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 2 7
52 }
53  
54 # fetch a list of cookies from the environment and return as a hash.
55 # the cookie values are not unescaped or altered in any way.
56 5 ▶sub raw_fetch {
Subroutine raw_fetch: called CC=6 SCAR=17.9
Pod undocumented
57 5 ▶ my $raw_cookie = $ENV{HTTP_COOKIE} || $ENV{COOKIE};
Condition100%3/3
$ENV{'HTTP_COOKIE'} || $ENV{'COOKIE'}
l!l&&r!l&&!r
122
MC/DC100%2/2
$ENV{'HTTP_COOKIE'} || $ENV{'COOKIE'}
$ENV{'HTTP_COOKIE'} $ENV{'COOKIE'}
Truth table100%3/3
A || B
A= $ENV{'HTTP_COOKIE'}B= $ENV{'COOKIE'}
ABresult
1X1
011
000
58 5 ▶ return () unless $raw_cookie;
Branch: 1 branch
BranchTrueFalseTotal
unless $raw_cookie 2 3 5
59 3 my %results;
60 3 my @pairs = split "; ?", $raw_cookie;
61 3 for my $pair ( @pairs ) {
62 10 $pair =~ s/^\s+|\s+$//; # trim leading trailing whitespace
63 10 my ( $key, $value ) = split "=", $pair;
64  
65 # fixed bug that does not allow 0 as a cookie value thanks Jose Mico
66 # $value ||= 0;
67 10 ▶ $value = defined $value ? $value : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 10 0 10
68 10 $results{$key} = $value;
69 }
70 3 ▶ return wantarray ? %results : \%results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
71 }
72  
73 44 ▶sub new {
Subroutine new: called CC=14 SCAR=26.7
Pod undocumented
74 44 my ( $class, @params ) = @_;
75 44 ▶ $class = ref( $class ) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0440
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
76 my (
77 44 $name, $value, $path, $domain,
78 $secure, $expires, $max_age, $httponly, $samesite,
79 $priority, $partitioned
80 )
81 = rearrange(
82 [
83 'NAME', [ 'VALUE', 'VALUES' ],
84 'PATH', 'DOMAIN',
85 'SECURE', 'EXPIRES',
86 'MAX-AGE', 'HTTPONLY', 'SAMESITE',
87 'PRIORITY', 'PARTITIONED',
88 ],
89 @params
90 );
91 44 ▶ return undef unless defined $name and defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and defined $value 0 44 44
Condition33%1/3
defined $name and defined $value
!ll&&!rl&&r
0044
MC/DC0%0/2
defined $name and defined $value
defined $name defined $value
Truth table33%1/3
A and B
A= defined $nameB= defined $value
ABresult
0X0
100
111
92 44 my $self = {};
93 44 bless $self, $class;
94 44 $self->name( $name );
95 44 $self->value( $value );
96 44 ▶ $path ||= "/";
Condition100%2/2
$path ||= "/"
l!l
935
MC/DC100%1/1
$path ||= "/"
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
97 44 ▶ $self->path( $path ) if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 44 0 44
98 44 ▶ $self->domain( $domain ) if defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain 10 34 44
99 44 ▶ $self->secure( $secure ) if defined $secure;
Branch: 1 branch
BranchTrueFalseTotal
if defined $secure 9 35 44
100 44 ▶ $self->expires( $expires ) if defined $expires;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expires 11 33 44
101 44 ▶ $self->max_age( $max_age ) if defined $max_age;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_age 3 41 44
102 44 ▶ $self->httponly( $httponly ) if defined $httponly;
Branch: 1 branch
BranchTrueFalseTotal
if defined $httponly 9 35 44
103 44 ▶ $self->samesite( $samesite ) if defined $samesite;
Branch: 1 branch
BranchTrueFalseTotal
if defined $samesite 3 41 44
104 44 ▶ $self->priority( $priority ) if defined $priority;
Branch: 1 branch
BranchTrueFalseTotal
if defined $priority 2 42 44
105 44 ▶ $self->partitioned( $partitioned ) if defined $partitioned;
Branch: 1 branch
BranchTrueFalseTotal
if defined $partitioned 2 42 44
106 44 return $self;
107 }
108  
109 42 ▶sub as_string {
Subroutine as_string: called CC=11 SCAR=24.0
Pod undocumented
110 42 my $self = shift;
111 42 ▶ return "" unless $self->name;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->name 0 42 42
112 42 my $name = escape( $self->name );
113 42 my $value = join "&", map { escape( $_ ) } $self->value;
114 42 my @cookie = ( "$name=$value" );
115 42 ▶ push @cookie, "domain=" . $self->domain if $self->domain;
Branch: 1 branch
BranchTrueFalseTotal
if $self->domain 28 14 42
116 42 ▶ push @cookie, "path=" . $self->path if $self->path;
Branch: 1 branch
BranchTrueFalseTotal
if $self->path 42 0 42
117 42 ▶ push @cookie, "expires=" . $self->expires if $self->expires;
Branch: 1 branch
BranchTrueFalseTotal
if $self->expires 23 19 42
118 42 ▶ push @cookie, "max-age=" . $self->max_age if $self->max_age;
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_age 11 31 42
119 42 ▶ push @cookie, "secure" if $self->secure;
Branch: 1 branch
BranchTrueFalseTotal
if $self->secure 23 19 42
120 42 ▶ push @cookie, "HttpOnly" if $self->httponly;
Branch: 1 branch
BranchTrueFalseTotal
if $self->httponly 23 19 42
121 42 ▶ push @cookie, "SameSite=" . $self->samesite if $self->samesite;
Branch: 1 branch
BranchTrueFalseTotal
if $self->samesite 11 31 42
122 42 ▶ push @cookie,"Priority=".$self->priority if $self->priority;
Branch: 1 branch
BranchTrueFalseTotal
if $self->priority 11 31 42
123 42 ▶ push @cookie,"Partitioned" if $self->partitioned;
Branch: 1 branch
BranchTrueFalseTotal
if $self->partitioned 11 31 42
124 42 return join "; ", @cookie;
125 }
126  
127 8 ▶sub compare {
Subroutine compare: called CC=1 SCAR=0.0
Pod undocumented
128 8 my ( $self, $value ) = @_;
129 8 return "$self" cmp $value;
130 }
131  
132 # accessors subs
133 135 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
134 135 my ( $self, $name ) = @_;
135 135 ▶ $self->{'name'} = $name if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 45 90 135
136 135 return $self->{'name'};
137 }
138  
139 109 ▶sub value {
Subroutine value: called CC=5 SCAR=16.1
Pod documented
140 109 my ( $self, $value ) = @_;
141 109 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 45 64 109
142 my @values
143 45 ▶ = ref $value eq 'ARRAY' ? @$value
Branch: 2 branches
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 30 15 45
ref $value eq 'HASH' ? : 0 15 15
144 : ref $value eq 'HASH' ? %$value
145 : ( $value );
146 45 $self->{'value'} = [@values];
147 }
148 109 ▶ return wantarray ? @{ $self->{'value'} } : $self->{'value'}->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 45 64 109
149 }
150  
151 87 ▶sub domain {
Subroutine domain: called CC=2 SCAR=6.9
Pod documented
152 87 my ( $self, $domain ) = @_;
153 87 ▶ $self->{'domain'} = $domain if defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain 12 75 87
154 87 return $self->{'domain'};
155 }
156  
157 56 ▶sub secure {
Subroutine secure: called CC=2 SCAR=6.9
Pod documented
158 56 my ( $self, $secure ) = @_;
159 56 ▶ $self->{'secure'} = $secure if defined $secure;
Branch: 1 branch
BranchTrueFalseTotal
if defined $secure 10 46 56
160 56 return $self->{'secure'};
161 }
162  
163 89 ▶sub expires {
Subroutine expires: called CC=2 SCAR=6.9
Pod documented
164 89 my ( $self, $expires ) = @_;
165 89 ▶ $self->{'expires'} = CGI::Simple::Util::expires( $expires, 'cookie' )
Branch: 1 branch
BranchTrueFalseTotal
if defined $expires 12 77 89
166 if defined $expires;
167 89 return $self->{'expires'};
168 }
169  
170 65 ▶sub max_age {
Subroutine max_age: called CC=2 SCAR=6.9
Pod documented
171 65 my ( $self, $max_age ) = @_;
172 65 ▶ $self->{'max-age'}
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_age 5 60 65
173 = CGI::Simple::Util::_expire_calc( $max_age ) - time()
174 if defined $max_age;
175 65 return $self->{'max-age'};
176 }
177  
178 135 ▶sub path {
Subroutine path: called CC=2 SCAR=6.9
Pod documented
179 135 my ( $self, $path ) = @_;
180 135 ▶ $self->{'path'} = $path if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 45 90 135
181 135 return $self->{'path'};
182 }
183  
184 56 ▶sub httponly {
Subroutine httponly: called CC=2 SCAR=6.9
Pod documented
185 56 my ( $self, $httponly ) = @_;
186 56 ▶ $self->{'httponly'} = $httponly if defined $httponly;
Branch: 1 branch
BranchTrueFalseTotal
if defined $httponly 10 46 56
187 56 return $self->{'httponly'};
188 }
189  
190 46 ▶sub partitioned { # Partitioned
Subroutine partitioned: called CC=2 SCAR=6.9
Pod documented
191 46 my ( $self, $partitioned ) = @_;
192 46 ▶ $self->{'partitioned'} = $partitioned if defined $partitioned;
Branch: 1 branch
BranchTrueFalseTotal
if defined $partitioned 2 44 46
193 46 return $self->{'partitioned'};
194 }
195  
196 2 my %_legal_samesite = ( Strict => 1, Lax => 1, None => 1 );
197 63 ▶sub samesite {
Subroutine samesite: called CC=4 SCAR=13.9
Pod documented
198 63 my $self = shift;
199 63 ▶ my $samesite = ucfirst lc +shift if @_; # Normalize casing.
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 58 63
200 63 ▶ $self->{'samesite'} = $samesite if $samesite and $_legal_samesite{$samesite};
Branch: 1 branch
BranchTrueFalseTotal
if $samesite and $_legal_samesite{$samesite} 5 58 63
Condition66%2/3
$samesite and $_legal_samesite{$samesite}
!ll&&!rl&&r
5805
MC/DC50%1/2
$samesite and $_legal_samesite{$samesite}
$samesite $_legal_samesite{$samesite}
Truth table66%2/3
A and B
A= $samesiteB= $_legal_samesite{$samesite}
ABresult
0X0
100
111
201 63 return $self->{'samesite'};
202 }
203  
204 2 my %_legal_priority = ( Low => 1, Medium => 1, High => 1 );
205 56 ▶sub priority {
Subroutine priority: called CC=4 SCAR=13.9
Pod documented
206 56 my $self = shift;
207 56 ▶ my $priority = ucfirst lc +shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 54 56
208 56 ▶ if ($priority && $_legal_priority{$priority}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($priority and $_legal_priority{$priority}) 2 54 56
Condition66%2/3
$priority and $_legal_priority{$priority}
!ll&&!rl&&r
5402
MC/DC50%1/2
$priority and $_legal_priority{$priority}
$priority $_legal_priority{$priority}
Truth table66%2/3
A and B
A= $priorityB= $_legal_priority{$priority}
ABresult
0X0
100
111
209 2 $self->{'priority'} = $priority;
210 }
211 56 return $self->{'priority'};
212 }
213  
214 2 1;
215  
216 __END__
« Simple/Util.pm ↑ Summary