Simple/Cookie.pm

filter: Statementstmt 100% 112 / 112 Branchbran 87.8% 65 / 74 Conditioncond 65.0% 13 / 20 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 100% 20 / 20 Podpod 60.0% 9 / 15 totaltotal 88.5% 225 / 254 CC 47 SCAR 18.1
CC
47
Cov
92%
new
12.5
as_string
9.0
parse
7.3
CRAP 6.1
« 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.27';
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 sub fetch {
23 5 ▶ my $self = shift;
Subroutine fetch: called CC=3 SCAR=11.0
Pod undocumented
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 sub parse {
30 7 ▶ my ( $self, $raw_cookie ) = @_;
Subroutine parse: called CC=7 SCAR=19.9
Pod undocumented
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 sub raw_fetch {
57 5 ▶ my $raw_cookie = $ENV{HTTP_COOKIE} || $ENV{COOKIE};
Subroutine raw_fetch: called CC=6 SCAR=17.9
Pod undocumented
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 sub new {
74 44 ▶ my ( $class, @params ) = @_;
Subroutine new: called CC=12 SCAR=25.2
Pod undocumented
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 )
80 = rearrange(
81 [
82 'NAME', [ 'VALUE', 'VALUES' ],
83 'PATH', 'DOMAIN',
84 'SECURE', 'EXPIRES',
85 'MAX-AGE', 'HTTPONLY', 'SAMESITE'
86 ],
87 @params
88 );
89 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
90 44 my $self = {};
91 44 bless $self, $class;
92 44 $self->name( $name );
93 44 $self->value( $value );
94 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
95 44 ▶ $self->path( $path ) if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 44 0 44
96 44 ▶ $self->domain( $domain ) if defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain 10 34 44
97 44 ▶ $self->secure( $secure ) if defined $secure;
Branch: 1 branch
BranchTrueFalseTotal
if defined $secure 9 35 44
98 44 ▶ $self->expires( $expires ) if defined $expires;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expires 11 33 44
99 44 ▶ $self->max_age( $max_age ) if defined $max_age;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_age 3 41 44
100 44 ▶ $self->httponly( $httponly ) if defined $httponly;
Branch: 1 branch
BranchTrueFalseTotal
if defined $httponly 9 35 44
101 44 ▶ $self->samesite( $samesite ) if defined $samesite;
Branch: 1 branch
BranchTrueFalseTotal
if defined $samesite 3 41 44
102 44 return $self;
103 }
104  
105 sub as_string {
106 64 ▶ my $self = shift;
Subroutine as_string: called CC=9 SCAR=22.0
Pod undocumented
107 64 ▶ return "" unless $self->name;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->name 0 64 64
108 64 my $name = escape( $self->name );
109 64 my $value = join "&", map { escape( $_ ) } $self->value;
110 64 my @cookie = ( "$name=$value" );
111 64 ▶ push @cookie, "domain=" . $self->domain if $self->domain;
Branch: 1 branch
BranchTrueFalseTotal
if $self->domain 27 37 64
112 64 ▶ push @cookie, "path=" . $self->path if $self->path;
Branch: 1 branch
BranchTrueFalseTotal
if $self->path 64 0 64
113 64 ▶ push @cookie, "expires=" . $self->expires if $self->expires;
Branch: 1 branch
BranchTrueFalseTotal
if $self->expires 22 42 64
114 64 ▶ push @cookie, "max-age=" . $self->max_age if $self->max_age;
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_age 9 55 64
115 64 ▶ push @cookie, "secure" if $self->secure;
Branch: 1 branch
BranchTrueFalseTotal
if $self->secure 22 42 64
116 64 ▶ push @cookie, "HttpOnly" if $self->httponly;
Branch: 1 branch
BranchTrueFalseTotal
if $self->httponly 22 42 64
117 64 ▶ push @cookie, "SameSite=" . $self->samesite if $self->samesite;
Branch: 1 branch
BranchTrueFalseTotal
if $self->samesite 9 55 64
118 64 return join "; ", @cookie;
119 }
120  
121 sub compare {
122 8 ▶ my ( $self, $value ) = @_;
Subroutine compare: called CC=1 SCAR=0.0
Pod undocumented
123 8 return "$self" cmp $value;
124 }
125  
126 # accessors subs
127 sub name {
128 179 ▶ my ( $self, $name ) = @_;
Subroutine name: called CC=2 SCAR=6.9
Pod documented
129 179 ▶ $self->{'name'} = $name if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 45 134 179
130 179 return $self->{'name'};
131 }
132  
133 sub value {
134 131 ▶ my ( $self, $value ) = @_;
Subroutine value: called CC=5 SCAR=16.1
Pod documented
135 131 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 45 86 131
136 my @values
137 45 ▶ = ref $value eq 'ARRAY' ? @$value
Branch: 2 branches
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 30 15 45
ref $value eq 'HASH' ? : 0 15 15
138 : ref $value eq 'HASH' ? %$value
139 : ( $value );
140 45 $self->{'value'} = [@values];
141 }
142 131 ▶ return wantarray ? @{ $self->{'value'} } : $self->{'value'}->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 67 64 131
143 }
144  
145 sub domain {
146 108 ▶ my ( $self, $domain ) = @_;
Subroutine domain: called CC=2 SCAR=6.9
Pod documented
147 108 ▶ $self->{'domain'} = $domain if defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain 12 96 108
148 108 return $self->{'domain'};
149 }
150  
151 sub secure {
152 78 ▶ my ( $self, $secure ) = @_;
Subroutine secure: called CC=2 SCAR=6.9
Pod documented
153 78 ▶ $self->{'secure'} = $secure if defined $secure;
Branch: 1 branch
BranchTrueFalseTotal
if defined $secure 10 68 78
154 78 return $self->{'secure'};
155 }
156  
157 sub expires {
158 110 ▶ my ( $self, $expires ) = @_;
Subroutine expires: called CC=2 SCAR=6.9
Pod documented
159 110 ▶ $self->{'expires'} = CGI::Simple::Util::expires( $expires, 'cookie' )
Branch: 1 branch
BranchTrueFalseTotal
if defined $expires 12 98 110
160 if defined $expires;
161 110 return $self->{'expires'};
162 }
163  
164 sub max_age {
165 85 ▶ my ( $self, $max_age ) = @_;
Subroutine max_age: called CC=2 SCAR=6.9
Pod documented
166 85 ▶ $self->{'max-age'}
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_age 5 80 85
167 = CGI::Simple::Util::_expire_calc( $max_age ) - time()
168 if defined $max_age;
169 85 return $self->{'max-age'};
170 }
171  
172 sub path {
173 179 ▶ my ( $self, $path ) = @_;
Subroutine path: called CC=2 SCAR=6.9
Pod documented
174 179 ▶ $self->{'path'} = $path if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 45 134 179
175 179 return $self->{'path'};
176 }
177  
178 sub httponly {
179 78 ▶ my ( $self, $httponly ) = @_;
Subroutine httponly: called CC=2 SCAR=6.9
Pod documented
180 78 ▶ $self->{'httponly'} = $httponly if defined $httponly;
Branch: 1 branch
BranchTrueFalseTotal
if defined $httponly 10 68 78
181 78 return $self->{'httponly'};
182 }
183  
184 2 my %_legal_samesite = ( Strict => 1, Lax => 1, None => 1 );
185 sub samesite {
186 83 ▶ my $self = shift;
Subroutine samesite: called CC=4 SCAR=13.9
Pod documented
187 83 ▶ my $samesite = ucfirst lc +shift if @_; # Normalize casing.
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 78 83
188 83 ▶ $self->{'samesite'} = $samesite if $samesite and $_legal_samesite{$samesite};
Branch: 1 branch
BranchTrueFalseTotal
if $samesite and $_legal_samesite{$samesite} 5 78 83
Condition66%2/3
$samesite and $_legal_samesite{$samesite}
!ll&&!rl&&r
7805
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
189 83 return $self->{'samesite'};
190 }
191  
192 2 1;
193  
194 __END__
« Simple/Util.pm ↑ Summary