CGI/Cookie.pm

filter: Statementstmt 95.0% 153 / 161 Branchbran 84.4% 98 / 116 Conditioncond 52.1% 24 / 46 MC/DCmcdc 36.6% 11 / 30 Subroutinesub 100% 25 / 25 Podpod 52.6% 10 / 19 totaltotal 80.8% 321 / 397 CC 71 SCAR 21.2
CC
71
Cov
85%
new
17.5
get_raw_cookie
15.0
as_string
11.0
CRAP 8.3
« CGI/Carp.pm ↑ Summary CGI/Pretty.pm »
1 package CGI::Cookie;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION='5.01';
7  
8 4 ▶use CGI::Util qw(rearrange unescape escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use overload '""' => \&as_string, 'cmp' => \&compare, 'fallback' => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 my $PERLEX = 0;
12 # Turn on special checking for ActiveState's PerlEx
13 4 ▶$PERLEX++ if defined($ENV{'GATEWAY_INTERFACE'}) && $ENV{'GATEWAY_INTERFACE'} =~ /^CGI-PerlEx/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"GATEWAY_INTERFACE"} and $ENV{"GATEWAY_INTERFACE"} =~ /^CGI-PerlEx/ 0 4 4
Condition33%1/3
defined $ENV{"GATEWAY_INTERFACE"} and $ENV{"GATEWAY_INTERFACE"} =~ /^CGI-PerlEx/
!ll&&!rl&&r
400
MC/DC0%0/2
defined $ENV{"GATEWAY_INTERFACE"} and $ENV{"GATEWAY_INTERFACE"} =~ /^CGI-PerlEx/
defined $ENV{"GATEWAY_INTERFACE"} $ENV{"GATEWAY_INTERFACE"} =~ /^CGI-PerlEx/
Truth table33%1/3
A and B
A= defined $ENV{"GATEWAY_INTERFACE"}B= $ENV{"GATEWAY_INTERFACE"} =~ /^CGI-PerlEx/
ABresult
0X0
100
111
14  
15 # Turn on special checking for mod_perl
16 # PerlEx::DBI tries to fool DBI by setting MOD_PERL
17 4 my $MOD_PERL = 0;
18 4 ▶if (exists $ENV{MOD_PERL} && ! $PERLEX) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"MOD_PERL"} and not $PERLEX) 0 4 4
Condition33%1/3
exists $ENV{"MOD_PERL"} and not $PERLEX
!ll&&!rl&&r
400
MC/DC0%0/2
exists $ENV{"MOD_PERL"} and not $PERLEX
exists $ENV{"MOD_PERL"} not $PERLEX
Truth table33%1/3
A and not(B)
A= exists $ENV{"MOD_PERL"}B= not $PERLEX
ABresult
0X0
100
111
19 0 ▶ if (exists $ENV{MOD_PERL_API_VERSION} && $ENV{MOD_PERL_API_VERSION} == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{'MOD_PERL_API_VERSION'} and $ENV{'MOD_PERL_API_VERSION'} == 2) { } 0 0 0
Condition0%0/3
exists $ENV{"MOD_PERL_API_VERSION"} and $ENV{"MOD_PERL_API_VERSION"} == 2
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ENV{"MOD_PERL_API_VERSION"} and $ENV{"MOD_PERL_API_VERSION"} == 2
exists $ENV{"MOD_PERL_API_VERSION"} $ENV{"MOD_PERL_API_VERSION"} == 2
Truth table0%0/3
A and B
A= exists $ENV{"MOD_PERL_API_VERSION"}B= $ENV{"MOD_PERL_API_VERSION"} == 2
ABresult
0X0
100
111
20 0 $MOD_PERL = 2;
21 0 require Apache2::RequestUtil;
22 0 require APR::Table;
23 } else {
24 0 $MOD_PERL = 1;
25 0 require Apache;
26 }
27 }
28  
29 # fetch a list of cookies from the environment and
30 # return as a hash. the cookies are parsed as normal
31 # escaped URL data.
32 6 ▶sub fetch {
Subroutine fetch: called CC=2 SCAR=6.9
Pod undocumented
33 6 my $class = shift;
34 6 ▶ my $raw_cookie = get_raw_cookie(@_) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $raw_cookie = get_raw_cookie(@_) or return 4 2 6
35 4 return $class->parse($raw_cookie);
36 }
37  
38 # Fetch a list of cookies from the environment or the incoming headers and
39 # return as a hash. The cookie values are not unescaped or altered in any way.
40 6 ▶ sub raw_fetch {
Subroutine raw_fetch: called CC=5 SCAR=16.1
Pod undocumented
41 6 my $class = shift;
42 6 ▶ my $raw_cookie = get_raw_cookie(@_) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $raw_cookie = get_raw_cookie(@_) or return 4 2 6
43 4 my %results;
44 4 my($key,$value);
45
46 4 my @pairs = split("[;,] ?",$raw_cookie);
47 4 for my $pair ( @pairs ) {
48 14 $pair =~ s/^\s+|\s+$//g; # trim leading trailing whitespace
49 14 my ( $key, $value ) = split "=", $pair;
50  
51 14 ▶ $value = defined $value ? $value : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 12 2 14
52 14 $results{$key} = $value;
53 }
54 4 ▶ return wantarray ? %results : \%results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 1 4
55 }
56  
57 12 ▶sub get_raw_cookie {
Subroutine get_raw_cookie: called CC=8 SCAR=27.1
Pod undocumented
58 12 my $r = shift;
59 0 ▶ $r ||= eval { $MOD_PERL == 2 ?
Branch: 2 branches
BranchTrueFalseTotal
if $MOD_PERL 0 12 12
$MOD_PERL == 2 ? : 0 0 0
Condition0%0/3
$r ||= eval { do { $MOD_PERL == 2 ? "Apache2::RequestUtil"->request : "Apache"->request } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$r ||= eval { do { $MOD_PERL == 2 ? "Apache2::RequestUtil"->request : "Apache"->request } }
$r eval { do { $MOD_PERL == 2 ? "Apache2::RequestUtil"->request : "Apache"->request } }
Truth table0%0/3
A ||= B
A= $rB= eval { do { $MOD_PERL == 2 ? "Apache2::RequestUtil"->request : "Apache"->request } }
ABresult
1X1
011
000
60 Apache2::RequestUtil->request() :
61 Apache->request } if $MOD_PERL;
62  
63 12 ▶ return $r->headers_in->{'Cookie'} if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 12 12
64  
65 die "Run $r->subprocess_env; before calling fetch()"
66 12 ▶ if $MOD_PERL and !exists $ENV{REQUEST_METHOD};
Branch: 1 branch
BranchTrueFalseTotal
if $MOD_PERL and not exists $ENV{"REQUEST_METHOD"} 0 12 12
Condition33%1/3
$MOD_PERL and not exists $ENV{"REQUEST_METHOD"}
!ll&&!rl&&r
1200
MC/DC0%0/2
$MOD_PERL and not exists $ENV{"REQUEST_METHOD"}
$MOD_PERL not exists $ENV{"REQUEST_METHOD"}
Truth table33%1/3
A and not(B)
A= $MOD_PERLB= not exists $ENV{"REQUEST_METHOD"}
ABresult
0X0
100
111
67
68 12 ▶ return $ENV{HTTP_COOKIE} || $ENV{COOKIE};
Condition100%3/3
$ENV{'HTTP_COOKIE'} || $ENV{'COOKIE'}
l!l&&r!l&&!r
534
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
69 }
70  
71  
72 20 ▶sub parse {
Subroutine parse: called CC=9 SCAR=22.0
Pod undocumented
73 20 my ($self,$raw_cookie) = @_;
74 20 ▶ return wantarray ? () : {} unless $raw_cookie;
Branch: 2 branches
BranchTrueFalseTotal
unless $raw_cookie 4 16 20
wantarray ? : 2 2 4
75  
76 16 my %results;
77  
78 16 ▶ if ( $raw_cookie =~ /(expires)=([^;]+)/ixsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_cookie =~ /(expires)=([^;]+)/imsx) 10 6 16
79 # regex captures are untainted, so carry over the taint of the source
80 10 my $tainted = substr( $raw_cookie, 0, 0 );
81 10 my ($expires_name, $expires_value) = ($1 . $tainted, $2 . $tainted);
82 # remove the matched expires pair by position - the value is client
83 # supplied so must never be interpolated into a pattern
84 10 substr( $raw_cookie, $-[0], $+[0] - $-[0] ) = '';
85 10 $results{$expires_name} = $self->new(-name => $expires_name, -value => $expires_value);
86 }
87  
88  
89 16 my @pairs = split("[;,] ?",$raw_cookie);
90 16 for (@pairs) {
91 47 s/^\s+//;
92 47 s/\s+$//;
93  
94 47 my($key,$value) = split("=",$_,2);
95  
96 # Some foreign cookies are not in name=value format, so ignore
97 # them.
98 47 ▶ next if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 6 41 47
99 41 my @values = ();
100 41 ▶ if ($value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value ne "") 41 0 41
101 41 @values = map unescape($_),split(/[&;]/,$value.'&dmy');
102 41 pop @values;
103 }
104 41 $key = unescape($key);
105 # A bug in Netscape can cause several cookies with same name to
106 # appear. The FIRST one in HTTP_COOKIE is the most recent version.
107 41 ▶ $results{$key} ||= $self->new(-name=>$key,-value=>\@values);
Condition33%1/3
$results{$key} ||= $self->new("-name", $key, "-value", \@values)
l!l&&r!l&&!r
0410
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
108 }
109 16 ▶ return wantarray ? %results : \%results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13 3 16
110 }
111  
112 97 ▶sub new {
Subroutine new: called CC=17 SCAR=28.6
Pod undocumented
113 97 my ( $class, @params ) = @_;
114 97 ▶ $class = ref( $class ) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0970
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
115 # Ignore mod_perl request object--compatibility with Apache::Cookie.
116 shift if ref $params[0]
117 97 ▶ && eval { $params[0]->isa('Apache::Request::Req') || $params[0]->isa('Apache') };
Branch: 1 branch
BranchTrueFalseTotal
if ref $params[0] and eval { do { $params[0]->isa("Apache::Request::Req") or $params[0]->isa("Apache") } } 1 96 97
Condition83%5/6
ref $params[0] and eval { do { $params[0]->isa("Apache::Request::Req") or $params[0]->isa("Apache") } }
!ll&&!rl&&r
9511
$params[0]->isa('Apache::Request::Req') || $params[0]->isa('Apache')
l!l&&r!l&&!r
011
MC/DC100%2/2
ref $params[0] and eval { do { $params[0]->isa("Apache::Request::Req") or $params[0]->isa("Apache") } }
ref $params[0] eval { do { $params[0]->isa("Apache::Request::Req") or $params[0]->isa("Apache") } }
MC/DC50%1/2
$params[0]->isa('Apache::Request::Req') || $params[0]->isa('Apache')
$params[0]->isa('Apache::Request::Req') $params[0]->isa('Apache')
Truth table100%3/3
A and B
A= ref $params[0]B= eval { do { $params[0]->isa("Apache::Request::Req") or $params[0]->isa("Apache") } }
ABresult
0X0
100
111
Truth table66%2/3
A || B
A= $params[0]->isa('Apache::Request::Req')B= $params[0]->isa('Apache')
ABresult
1X1
011
000
118 97 my ( $name, $value, $path, $domain, $secure, $expires, $max_age, $httponly, $samesite, $priority, $partitioned )
119 = rearrange(
120 [
121 'NAME', [ 'VALUE', 'VALUES' ],
122 'PATH', 'DOMAIN',
123 'SECURE', 'EXPIRES',
124 'MAX-AGE','HTTPONLY','SAMESITE',
125 'PRIORITY', 'PARTITIONED',
126 ],
127 @params
128 );
129 97 ▶ return undef unless defined $name and defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and defined $value 0 97 97
Condition33%1/3
defined $name and defined $value
!ll&&!rl&&r
0097
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
130 97 my $self = {};
131 97 bless $self, $class;
132 97 $self->name( $name );
133 97 $self->value( $value );
134 97 ▶ $path ||= "/";
Condition100%2/2
$path ||= "/"
l!l
1879
MC/DC100%1/1
$path ||= "/"
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
135 97 ▶ $self->path( $path ) if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 97 0 97
136 89 ▶ $self->domain( $domain ) if defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain 14 75 89
137 84 ▶ $self->secure( $secure ) if defined $secure;
Branch: 1 branch
BranchTrueFalseTotal
if defined $secure 7 77 84
138 84 ▶ $self->expires( $expires ) if defined $expires;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expires 19 65 84
139 79 ▶ $self->max_age( $max_age ) if defined $max_age;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_age 3 76 79
140 79 ▶ $self->httponly( $httponly ) if defined $httponly;
Branch: 1 branch
BranchTrueFalseTotal
if defined $httponly 2 77 79
141 79 ▶ $self->samesite( $samesite ) if defined $samesite;
Branch: 1 branch
BranchTrueFalseTotal
if defined $samesite 4 75 79
142 79 ▶ $self->priority( $priority ) if defined $priority;
Branch: 1 branch
BranchTrueFalseTotal
if defined $priority 3 76 79
143 79 ▶ $self->partitioned( $partitioned ) if defined $partitioned;
Branch: 1 branch
BranchTrueFalseTotal
if defined $partitioned 2 77 79
144 79 return $self;
145 }
146  
147 82 ▶sub as_string {
Subroutine as_string: called CC=11 SCAR=24.0
Pod undocumented
148 82 my $self = shift;
149 82 ▶ return "" unless $self->name;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->name 0 82 82
150  
151 4 ▶ no warnings; # some things may be undefined, that's OK.
Subroutine BEGIN: called CC=1 SCAR=0.0
152  
153 82 my $name = escape( $self->name );
154 82 my $value = join "&", map { escape($_) } $self->value;
155 82 my @cookie = ( "$name=$value" );
156  
157 82 ▶ push @cookie,"domain=".$self->domain if $self->domain;
Branch: 1 branch
BranchTrueFalseTotal
if $self->domain 27 55 82
158 82 ▶ push @cookie,"path=".$self->path if $self->path;
Branch: 1 branch
BranchTrueFalseTotal
if $self->path 82 0 82
159 82 ▶ push @cookie,"expires=".$self->expires if $self->expires;
Branch: 1 branch
BranchTrueFalseTotal
if $self->expires 23 59 82
160 82 ▶ push @cookie,"max-age=".$self->max_age if $self->max_age;
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_age 11 71 82
161 82 ▶ push @cookie,"secure" if $self->secure;
Branch: 1 branch
BranchTrueFalseTotal
if $self->secure 21 61 82
162 82 ▶ push @cookie,"HttpOnly" if $self->httponly;
Branch: 1 branch
BranchTrueFalseTotal
if $self->httponly 11 71 82
163 82 ▶ push @cookie,"SameSite=".$self->samesite if $self->samesite;
Branch: 1 branch
BranchTrueFalseTotal
if $self->samesite 12 70 82
164 82 ▶ push @cookie,"Priority=".$self->priority if $self->priority;
Branch: 1 branch
BranchTrueFalseTotal
if $self->priority 11 71 82
165 82 ▶ push @cookie,"Partitioned" if $self->partitioned;
Branch: 1 branch
BranchTrueFalseTotal
if $self->partitioned 11 71 82
166  
167 82 return join "; ", @cookie;
168 }
169  
170 6 ▶sub compare {
Subroutine compare: called CC=1 SCAR=0.0
Pod undocumented
171 6 my ( $self, $value ) = @_;
172 6 return "$self" cmp $value;
173 }
174  
175 3 ▶sub bake {
Subroutine bake: called CC=5 SCAR=19.6
Pod undocumented
176 3 my ($self, $r) = @_;
177  
178 3 ▶ $r ||= eval {
Branch: 1 branch
BranchTrueFalseTotal
if $MOD_PERL 0 3 3
Condition0%0/3
$r ||= eval { do { $MOD_PERL == 2 ? "Apache2::RequestUtil"->request : "Apache"->request } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$r ||= eval { do { $MOD_PERL == 2 ? "Apache2::RequestUtil"->request : "Apache"->request } }
$r eval { do { $MOD_PERL == 2 ? "Apache2::RequestUtil"->request : "Apache"->request } }
Truth table0%0/3
A ||= B
A= $rB= eval { do { $MOD_PERL == 2 ? "Apache2::RequestUtil"->request : "Apache"->request } }
ABresult
1X1
011
000
179 0 ▶ $MOD_PERL == 2
Branch: 1 branch
BranchTrueFalseTotal
$MOD_PERL == 2 ? : 0 0 0
180 ? Apache2::RequestUtil->request()
181 : Apache->request
182 } if $MOD_PERL;
183 3 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 2 1 3
184 2 $r->err_headers_out->add('Set-Cookie' => $self->as_string);
185 } else {
186 1 require CGI;
187 1 print CGI::header(-cookie => $self);
188 }
189  
190 }
191  
192 # accessors
193 269 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
194 269 my ( $self, $name ) = @_;
195 269 ▶ $self->{'name'} = $name if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 98 171 269
196 269 return $self->{'name'};
197 }
198  
199 222 ▶sub value {
Subroutine value: called CC=5 SCAR=16.1
Pod documented
200 222 my ( $self, $value ) = @_;
201 222 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 98 124 222
202 my @values
203 98 ▶ = ref $value eq 'ARRAY' ? @$value
Branch: 2 branches
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 44 54 98
ref $value eq 'HASH' ? : 0 54 54
204 : ref $value eq 'HASH' ? %$value
205 : ( $value );
206 98 $self->{'value'} = [@values];
207 }
208 222 ▶ return wantarray ? @{ $self->{'value'} } : $self->{'value'}->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 83 139 222
209 }
210  
211 # attribute values are not escaped, so must be checked: per RFC 6265 they
212 # can't contain ";" or control characters (anything else would allow
213 # other attributes, e.g. "/; Domain=example.com", to be injected)
214 138 ▶sub _check_attribute {
Subroutine _check_attribute: called CC=3 SCAR=11.0
215 138 my ( $attribute,$value,$valid ) = @_;
216 138 ▶ $valid ||= qr/\A[^;\x00-\x1f\x7f]*\z/;
Condition100%2/2
$valid ||= qr/\A[^;\x00-\x1f\x7f]*\z/
l!l
16122
MC/DC100%1/1
$valid ||= qr/\A[^;\x00-\x1f\x7f]*\z/
$valid
Truth table100%2/2
A
A= $valid
Aresult
11
00
217 138 ▶ return $value if $value =~ $valid;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /$valid/ 116 22 138
218 22 ( my $shown = $value ) =~ s/([^\x20-\x7e])/sprintf( '\\x%02x',ord $1 )/ge;
219 22 die "Invalid cookie $attribute (contains ';' or invalid characters): $shown\n";
220 }
221  
222 130 ▶sub domain {
Subroutine domain: called CC=2 SCAR=6.9
Pod documented
223 130 my ( $self, $domain ) = @_;
224 130 ▶ $self->{'domain'} = lc _check_attribute( domain => $domain,qr/\A\.?[A-Za-z0-9._-]*\z/ )
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain 16 114 130
225 if defined $domain;
226 125 return $self->{'domain'};
227 }
228  
229 94 ▶sub secure {
Subroutine secure: called CC=2 SCAR=6.9
Pod documented
230 94 my ( $self, $secure ) = @_;
231 94 ▶ $self->{'secure'} = $secure if defined $secure;
Branch: 1 branch
BranchTrueFalseTotal
if defined $secure 8 86 94
232 94 return $self->{'secure'};
233 }
234  
235 142 ▶sub expires {
Subroutine expires: called CC=2 SCAR=6.9
Pod documented
236 142 my ( $self, $expires ) = @_;
237 # CGI::Util::expires passes through anything it doesn't understand
238 142 ▶ $self->{'expires'} = _check_attribute( expires => CGI::Util::expires($expires) )
Branch: 1 branch
BranchTrueFalseTotal
if defined $expires 20 122 142
239 if defined $expires;
240 137 return $self->{'expires'};
241 }
242  
243 105 ▶sub max_age {
Subroutine max_age: called CC=2 SCAR=6.9
Pod documented
244 105 my ( $self, $max_age ) = @_;
245 105 ▶ $self->{'max-age'} = CGI::Util::expire_calc($max_age)-time() if defined $max_age;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_age 5 100 105
246 105 return $self->{'max-age'};
247 }
248  
249 276 ▶sub path {
Subroutine path: called CC=2 SCAR=6.9
Pod documented
250 276 my ( $self, $path ) = @_;
251 276 ▶ $self->{'path'} = _check_attribute( path => $path ) if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 102 174 276
252 264 return $self->{'path'};
253 }
254  
255 86 ▶sub httponly { # HttpOnly
Subroutine httponly: called CC=2 SCAR=6.9
Pod documented
256 86 my ( $self, $httponly ) = @_;
257 86 ▶ $self->{'httponly'} = $httponly if defined $httponly;
Branch: 1 branch
BranchTrueFalseTotal
if defined $httponly 2 84 86
258 86 return $self->{'httponly'};
259 }
260  
261 86 ▶sub partitioned { # Partitioned
Subroutine partitioned: called CC=2 SCAR=6.9
Pod undocumented
262 86 my ( $self, $partitioned ) = @_;
263 86 ▶ $self->{'partitioned'} = $partitioned if defined $partitioned;
Branch: 1 branch
BranchTrueFalseTotal
if defined $partitioned 2 84 86
264 86 return $self->{'partitioned'};
265 }
266  
267 4 my %_legal_samesite = ( Strict => 1, Lax => 1, None => 1 );
268 107 ▶sub samesite { # SameSite
Subroutine samesite: called CC=4 SCAR=13.9
Pod documented
269 107 my $self = shift;
270 107 ▶ my $samesite = ucfirst lc +shift if @_; # Normalize casing.
Branch: 1 branch
BranchTrueFalseTotal
if @_ 7 100 107
271 107 ▶ $self->{'samesite'} = $samesite if $samesite and $_legal_samesite{$samesite};
Branch: 1 branch
BranchTrueFalseTotal
if $samesite and $_legal_samesite{$samesite} 6 101 107
Condition100%3/3
$samesite and $_legal_samesite{$samesite}
!ll&&!rl&&r
10016
MC/DC100%2/2
$samesite and $_legal_samesite{$samesite}
$samesite $_legal_samesite{$samesite}
Truth table100%3/3
A and B
A= $samesiteB= $_legal_samesite{$samesite}
ABresult
0X0
100
111
272 107 return $self->{'samesite'};
273 }
274  
275 4 my %_legal_priority = ( Low => 1, Medium => 1, High => 1 );
276 102 ▶sub priority {
Subroutine priority: called CC=4 SCAR=13.9
Pod documented
277 102 my $self = shift;
278 102 ▶ my $priority = ucfirst lc +shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 97 102
279 102 ▶ if ($priority && $_legal_priority{$priority}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($priority and $_legal_priority{$priority}) 4 98 102
Condition100%3/3
$priority and $_legal_priority{$priority}
!ll&&!rl&&r
9714
MC/DC100%2/2
$priority and $_legal_priority{$priority}
$priority $_legal_priority{$priority}
Truth table100%3/3
A and B
A= $priorityB= $_legal_priority{$priority}
ABresult
0X0
100
111
280 4 $self->{'priority'} = $priority;
281 }
282 102 return $self->{'priority'};
283 }
284  
285 4 1;
286  
287 =head1 NAME
288  
289 CGI::Cookie - Interface to HTTP Cookies
290  
291 =head1 SYNOPSIS
292  
293 use CGI qw/:standard/;
294 use CGI::Cookie;
295  
296 # Create new cookies and send them
297 $cookie1 = CGI::Cookie->new(-name=>'ID',-value=>123456);
298 $cookie2 = CGI::Cookie->new(-name=>'preferences',
299 -value=>{ font => Helvetica,
300 size => 12 }
301 );
302 print header(-cookie=>[$cookie1,$cookie2]);
303  
304 # fetch existing cookies
305 %cookies = CGI::Cookie->fetch;
306 $id = $cookies{'ID'}->value;
307  
308 # create cookies returned from an external source
309 %cookies = CGI::Cookie->parse($ENV{COOKIE});
310  
311 =head1 DESCRIPTION
312  
313 CGI::Cookie is an interface to HTTP/1.1 cookies, a mechanism
314 that allows Web servers to store persistent information on
315 the browser's side of the connection. Although CGI::Cookie is
316 intended to be used in conjunction with CGI.pm (and is in fact used by
317 it internally), you can use this module independently.
318  
319 For full information on cookies see
320  
321 https://tools.ietf.org/html/rfc6265
322  
323 =head1 USING CGI::Cookie
324  
325 CGI::Cookie is object oriented. Each cookie object has a name and a
326 value. The name is any scalar value. The value is any scalar or
327 array value (associative arrays are also allowed). Cookies also have
328 several optional attributes, including:
329  
330 =over 4
331  
332 =item B<1. expiration date>
333  
334 The expiration date tells the browser how long to hang on to the
335 cookie. If the cookie specifies an expiration date in the future, the
336 browser will store the cookie information in a disk file and return it
337 to the server every time the user reconnects (until the expiration
338 date is reached). If the cookie species an expiration date in the
339 past, the browser will remove the cookie from the disk file. If the
340 expiration date is not specified, the cookie will persist only until
341 the user quits the browser.
342  
343 =item B<2. domain>
344  
345 This is a partial or complete domain name for which the cookie is
346 valid. The browser will return the cookie to any host that matches
347 the partial domain name. For example, if you specify a domain name
348 of ".capricorn.com", then the browser will return the cookie to
349 Web servers running on any of the machines "www.capricorn.com",
350 "ftp.capricorn.com", "feckless.capricorn.com", etc. Domain names
351 must contain at least two periods to prevent attempts to match
352 on top level domains like ".edu". If no domain is specified, then
353 the browser will only return the cookie to servers on the host the
354 cookie originated from.
355  
356 =item B<3. path>
357  
358 If you provide a cookie path attribute, the browser will check it
359 against your script's URL before returning the cookie. For example,
360 if you specify the path "/cgi-bin", then the cookie will be returned
361 to each of the scripts "/cgi-bin/tally.pl", "/cgi-bin/order.pl", and
362 "/cgi-bin/customer_service/complain.pl", but not to the script
363 "/cgi-private/site_admin.pl". By default, the path is set to "/", so
364 that all scripts at your site will receive the cookie.
365  
366 =item B<4. secure flag>
367  
368 If the "secure" attribute is set, the cookie will only be sent to your
369 script if the CGI request is occurring on a secure channel, such as SSL.
370  
371 =item B<5. httponly flag>
372  
373 If the "httponly" attribute is set, the cookie will only be accessible
374 through HTTP Requests. This cookie will be inaccessible via JavaScript
375 (to prevent XSS attacks).
376  
377 This feature is supported by nearly all modern browsers.
378  
379 See these URLs for more information:
380  
381 http://msdn.microsoft.com/en-us/library/ms533046.aspx
382 http://www.browserscope.org/?category=security&v=top
383  
384 =item B<6. samesite flag>
385  
386 Allowed settings are C<Strict>, C<Lax> and C<None>.
387  
388 As of June 2016, support is limited to recent releases of Chrome and Opera.
389  
390 L<https://tools.ietf.org/html/draft-west-first-party-cookies-07>
391  
392 =item B<7. priority flag>
393  
394 Allowed settings are C<Low>, C<Medium> and C<High>.
395  
396 Support is limited to recent releases of Chrome.
397  
398 =back
399  
400 =head2 Creating New Cookies
401  
402 my $c = CGI::Cookie->new(-name => 'foo',
403 -value => 'bar',
404 -expires => '+3M',
405 '-max-age' => '+3M',
406 -domain => '.capricorn.com',
407 -path => '/cgi-bin/database',
408 -secure => 1,
409 -samesite=> "Lax",
410 -priority=> "High",
411 );
412  
413 Create cookies from scratch with the B<new> method. The B<-name> and
414 B<-value> parameters are required. The name must be a scalar value.
415 The value can be a scalar, an array reference, or a hash reference.
416 (At some point in the future cookies will support one of the Perl
417 object serialization protocols for full generality).
418  
419 B<-expires> accepts any of the relative or absolute date formats
420 recognized by CGI.pm, for example "+3M" for three months in the
421 future. See CGI.pm's documentation for details.
422  
423 B<-max-age> accepts the same data formats as B<< -expires >>, but sets a
424 relative value instead of an absolute like B<< -expires >>. This is intended to be
425 more secure since a clock could be changed to fake an absolute time. In
426 practice, as of 2011, C<< -max-age >> still does not enjoy the widespread support
427 that C<< -expires >> has. You can set both, and browsers that support
428 C<< -max-age >> should ignore the C<< Expires >> header. The drawback
429 to this approach is the bit of bandwidth for sending an extra header on each cookie.
430  
431 B<-domain> points to a domain name or to a fully qualified host name.
432 If not specified, the cookie will be returned only to the Web server
433 that created it.
434  
435 B<-path> points to a partial URL on the current server. The cookie
436 will be returned to all URLs beginning with the specified path. If
437 not specified, it defaults to '/', which returns the cookie to all
438 pages at your site.
439  
440 Unlike the name and value, which are URL escaped, the B<-path>, B<-domain>
441 and B<-expires> values are used as given, so since v5.00 CGI::Cookie dies if
442 they contain a ";" or control characters (which would allow other attributes
443 to be injected), or if B<-domain> isn't a valid host name.
444  
445 B<-secure> if set to a true value instructs the browser to return the
446 cookie only when a cryptographic protocol is in use.
447  
448 B<-httponly> if set to a true value, the cookie will not be accessible
449 via JavaScript.
450  
451 B<-samesite> may be C<Lax>, C<Strict>, or C<None> and is an evolving part
452 of the standards for cookies. Please refer to current documentation
453 regarding it.
454  
455 For compatibility with Apache::Cookie, you may optionally pass in
456 a mod_perl request object as the first argument to C<new()>. It will
457 simply be ignored:
458  
459 my $c = CGI::Cookie->new($r,
460 -name => 'foo',
461 -value => ['bar','baz']);
462  
463 =head2 Sending the Cookie to the Browser
464  
465 The simplest way to send a cookie to the browser is by calling the bake()
466 method:
467  
468 $c->bake;
469  
470 This will print the Set-Cookie HTTP header to STDOUT using CGI.pm. CGI.pm
471 will be loaded for this purpose if it is not already. Otherwise CGI.pm is not
472 required or used by this module.
473  
474 Under mod_perl, pass in an Apache request object:
475  
476 $c->bake($r);
477  
478 If you want to set the cookie yourself, Within a CGI script you can send
479 a cookie to the browser by creating one or more Set-Cookie: fields in the
480 HTTP header. Here is a typical sequence:
481  
482 my $c = CGI::Cookie->new(-name => 'foo',
483 -value => ['bar','baz'],
484 -expires => '+3M');
485  
486 print "Set-Cookie: $c\n";
487 print "Content-Type: text/html\n\n";
488  
489 To send more than one cookie, create several Set-Cookie: fields.
490  
491 If you are using CGI.pm, you send cookies by providing a -cookie
492 argument to the header() method:
493  
494 print header(-cookie=>$c);
495  
496 Mod_perl users can set cookies using the request object's header_out()
497 method:
498  
499 $r->err_headers_out->add('Set-Cookie' => $c);
500  
501 Internally, Cookie overloads the "" operator to call its as_string()
502 method when incorporated into the HTTP header. as_string() turns the
503 Cookie's internal representation into an RFC-compliant text
504 representation. You may call as_string() yourself if you prefer:
505  
506 print "Set-Cookie: ",$c->as_string,"\n";
507  
508 =head2 Recovering Previous Cookies
509  
510 %cookies = CGI::Cookie->fetch;
511  
512 B<fetch> returns an associative array consisting of all cookies
513 returned by the browser. The keys of the array are the cookie names. You
514 can iterate through the cookies this way:
515  
516 %cookies = CGI::Cookie->fetch;
517 for (keys %cookies) {
518 do_something($cookies{$_});
519 }
520  
521 In a scalar context, fetch() returns a hash reference, which may be more
522 efficient if you are manipulating multiple cookies.
523  
524 CGI.pm uses the URL escaping methods to save and restore reserved characters
525 in its cookies. If you are trying to retrieve a cookie set by a foreign server,
526 this escaping method may trip you up. Use raw_fetch() instead, which has the
527 same semantics as fetch(), but performs no unescaping.
528  
529 You may also retrieve cookies that were stored in some external
530 form using the parse() class method:
531  
532 $COOKIES = `cat /usr/tmp/Cookie_stash`;
533 %cookies = CGI::Cookie->parse($COOKIES);
534  
535 If you are in a mod_perl environment, you can save some overhead by
536 passing the request object to fetch() like this:
537  
538 CGI::Cookie->fetch($r);
539  
540 If the value passed to parse() is undefined, an empty array will returned in list
541 context, and an empty hashref will be returned in scalar context.
542  
543 =head2 Manipulating Cookies
544  
545 Cookie objects have a series of accessor methods to get and set cookie
546 attributes. Each accessor has a similar syntax. Called without
547 arguments, the accessor returns the current value of the attribute.
548 Called with an argument, the accessor changes the attribute and
549 returns its new value.
550  
551 =over 4
552  
553 =item B<name()>
554  
555 Get or set the cookie's name. Example:
556  
557 $name = $c->name;
558 $new_name = $c->name('fred');
559  
560 =item B<value()>
561  
562 Get or set the cookie's value. Example:
563  
564 $value = $c->value;
565 @new_value = $c->value(['a','b','c','d']);
566  
567 B<value()> is context sensitive. In a list context it will return
568 the current value of the cookie as an array. In a scalar context it
569 will return the B<first> value of a multivalued cookie.
570  
571 =item B<domain()>
572  
573 Get or set the cookie's domain.
574  
575 =item B<path()>
576  
577 Get or set the cookie's path.
578  
579 =item B<expires()>
580  
581 Get or set the cookie's expiration time.
582  
583 =item B<max_age()>
584  
585 Get or set the cookie's max_age value.
586  
587 =back
588  
589  
590 =head1 AUTHOR INFORMATION
591  
592 The CGI.pm distribution is copyright 1995-2007, Lincoln D. Stein. It is
593 distributed under the Artistic License 2.0. It is currently
594 maintained by Lee Johnson with help from many contributors.
595  
596 Address bug reports and comments to: https://github.com/leejo/CGI.pm/issues
597  
598 The original bug tracker can be found at: https://rt.cpan.org/Public/Dist/Display.html?Queue=CGI.pm
599  
600 When sending bug reports, please provide the version of CGI.pm, the version of
601 Perl, the name and version of your Web server, and the name and version of the
602 operating system you are using. If the problem is even remotely browser
603 dependent, please provide information about the affected browsers as well.
604  
605 =head1 BUGS
606  
607 This section intentionally left blank.
608  
609 =head1 SEE ALSO
610  
611 L<CGI::Carp>, L<CGI>
612  
613 L<RFC 2109|http://www.ietf.org/rfc/rfc2109.txt>, L<RFC 2695|http://www.ietf.org/rfc/rfc2965.txt>
614  
615 =cut
« CGI/Carp.pm ↑ Summary CGI/Pretty.pm »