| 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.280'; | |||||||||||||||||||||||||||
| 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'}
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'}
| |||||||||||||||||||||||||||||
| 25 | 5 | ▶ | return () unless $raw_cookie; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 40 | 25 | my @values = (); | |||||||||||||||||||||||||||
| 41 | 25 | ▶ | if ( $value ne '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 7 | ▶ | return wantarray ? %results : \%results; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'}
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'}
| |||||||||||||||||||||||||||||
| 58 | 5 | ▶ | return () unless $raw_cookie; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | 10 | $results{$key} = $value; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 3 | ▶ | return wantarray ? %results : \%results; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $name and defined $value
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
| |||||||||||||||||||||||||||||
| 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 ||= "/"
MC/DC100%1/1
$path ||= "/"
$path
Truth table100%2/2
A
A= $path
| |||||||||||||||||||||||||||||
| 95 | 44 | ▶ | $self->path( $path ) if defined $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 44 | ▶ | $self->domain( $domain ) if defined $domain; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 44 | ▶ | $self->secure( $secure ) if defined $secure; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 44 | ▶ | $self->expires( $expires ) if defined $expires; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 44 | ▶ | $self->max_age( $max_age ) if defined $max_age; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 44 | ▶ | $self->httponly( $httponly ) if defined $httponly; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 44 | ▶ | $self->samesite( $samesite ) if defined $samesite; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 112 | 64 | ▶ | push @cookie, "path=" . $self->path if $self->path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 64 | ▶ | push @cookie, "expires=" . $self->expires if $self->expires; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 64 | ▶ | push @cookie, "max-age=" . $self->max_age if $self->max_age; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 64 | ▶ | push @cookie, "secure" if $self->secure; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 64 | ▶ | push @cookie, "HttpOnly" if $self->httponly; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 64 | ▶ | push @cookie, "SameSite=" . $self->samesite if $self->samesite; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 136 | my @values | ||||||||||||||||||||||||||||
| 137 | 45 | ▶ | = ref $value eq 'ARRAY' ? @$value | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 188 | 83 | ▶ | $self->{'samesite'} = $samesite if $samesite and $_legal_samesite{$samesite}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$samesite and $_legal_samesite{$samesite}
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}
| |||||||||||||||||||||||||||||
| 189 | 83 | return $self->{'samesite'}; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 2 | 1; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | __END__ | ||||||||||||||||||||||||||||