blib/lib/Apache/FakeCookie.pm

filter: Statementstmt 96.0% 121 / 126 Branchbran 70.5% 48 / 68 Conditioncond 56.5% 13 / 23 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 100% 21 / 21 Podpod n/a totaltotal 81.4% 206 / 253 CC 46 SCAR 17.1
CC
46
Cov
84%
new
9.4
as_string
9.1
_expire_calc
7.5
CRAP 5.6
↑ Summary
1 package Apache::FakeCookie;
2  
3 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 $VERSION = do { my @r = (q$Revision: 0.08 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
5  
6 # Oh!, we really don't live in this package
7  
8 package Apache::Cookie;
9 1 ▶use vars qw($Cookies);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 $Cookies = {};
13  
14 # emluation is fairly complete
15 # cookies can be created, altered and removed
16 #
17 10 ▶sub fetch { return wantarray ? %{$Cookies} : $Cookies; }
Subroutine fetch: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 7 10
18 4 ▶sub path {&do_this;}
Subroutine path: called CC=1 SCAR=0.0
19 4 ▶sub secure {&do_this;}
Subroutine secure: called CC=1 SCAR=0.0
20 18 ▶sub name {&do_this;}
Subroutine name: called CC=1 SCAR=0.0
21 4 ▶sub domain {&do_this;}
Subroutine domain: called CC=1 SCAR=0.0
22 5 ▶sub value {
Subroutine value: called CC=5 SCAR=17.2
23 5 my ($self, $val) = @_;
24 5 ▶ $self->{-value} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 2 3 5
25 5 ▶ if (defined $self->{-value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'-value'}) { } 5 0 5
26 5 ▶ return wantarray ? @{$self->{-value}} : $self->{-value}->[0]
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 2 5
27 } else {
28 0 ▶ return wantarray ? () : '';
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
29 }
30 }
31 11 ▶sub new {
Subroutine new: called CC=8 SCAR=22.4
32 11 my $proto = shift; # bless into Apache::Cookie
33 11 shift; # waste reference to $r;
34 11 my @vals = @_;
35 11 my $self = {@vals};
36 11 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1100
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
37 # make sure values are in array format
38 11 my $val = $self->{-value};;
39 11 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 10 1 11
40 10 $val = $self->{-value};
41 10 ▶ if (ref($val) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 10 0 10
elsif (ref $val eq 'HASH') { } 0 0 0
elsif (not ref $val) { } 0 0 0
42 10 @vals = @$val;
43 } elsif (ref($val) eq 'HASH') {
44 0 @vals = %$val;
45 } elsif (!ref($val)) {
46 0 @vals = ($val); # it's a plain SCALAR
47 } # hmm.... must be a SCALAR ref or CODE ref
48 10 $self->{-value} = [@vals];
49 }
50 $self->{-expires} = _expires($self->{-expires})
51 11 ▶ if exists $self->{-expires} && defined $self->{-expires};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"-expires"} and defined $self->{"-expires"} 5 6 11
Condition66%2/3
exists $self->{"-expires"} and defined $self->{"-expires"}
!ll&&!rl&&r
605
MC/DC50%1/2
exists $self->{"-expires"} and defined $self->{"-expires"}
exists $self->{"-expires"} defined $self->{"-expires"}
Truth table66%2/3
A and B
A= exists $self->{"-expires"}B= defined $self->{"-expires"}
ABresult
0X0
100
111
52 11 bless $self, $class;
53 11 return $self;
54 }
55 8 ▶sub bake {
Subroutine bake: called CC=2 SCAR=6.9
56 8 my $self = shift;
57 8 ▶ if ( defined $self->{-value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'-value'}) { } 7 1 8
58 7 $Cookies->{$self->{-name}} = $self;
59 } else {
60 1 delete $Cookies->{$self->{-name}};
61 }
62 }
63 3 ▶sub parse { # adapted from CGI::Cookie v1.20 by Lincoln Stein
Subroutine parse: called CC=7 SCAR=19.7
64 3 my ($self,$raw_cookie) = @_;
65 3 ▶ if ($raw_cookie) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_cookie) 2 1 3
66 2 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
67 2 my %results;
68  
69 2 my(@pairs) = split("; ?",$raw_cookie);
70 2 foreach (@pairs) {
71 3 s/\s*(.*?)\s*/$1/;
72 3 my($key,$value) = split("=",$_,2);
73 # Some foreign cookies are not in name=value format, so ignore
74 # them.
75 3 ▶ next if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 3 3
76 3 my @values = ();
77 3 ▶ if ($value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value ne "") 3 0 3
78 3 @values = map unescape($_),split(/[&;]/,$value.'&dmy');
79 3 pop @values;
80 }
81 3 $key = unescape($key);
82 # A bug in Netscape can cause several cookies with same name to
83 # appear. The FIRST one in HTTP_COOKIE is the most recent version.
84 3 ▶ $results{$key} ||= $self->new(undef,-name=>$key,-value=>\@values);
Condition33%1/3
$results{$key} ||= $self->new(undef, "-name", $key, "-value", \@values)
l!l&&r!l&&!r
030
MC/DC0%0/2
$results{$key} ||= $self->new(undef, "-name", $key, "-value", \@values)
$results{$key} $self->new(undef, "-name", $key, "-value", \@values)
Truth table33%1/3
A ||= B
A= $results{$key}B= $self->new(undef, "-name", $key, "-value", \@values)
ABresult
1X1
011
000
85 }
86 2 $self = \%results;
87 2 bless $self, $class;
88 2 $Cookies = $self;
89 }
90 3 @_ = ($self);
91 3 goto &fetch;
92 }
93 22 ▶sub expires {
Subroutine expires: called CC=4 SCAR=13.9
94 22 my $self = shift;
95 22 ▶ $self->{-expires} = _expires(shift)
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 20 22
96 if @_;
97 return (exists $self->{-expires} &&
98 defined $self->{-expires})
99 22 ▶ ? $self->{-expires} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'-expires'} && defined $self->{'-expires'} ? : 14 8 22
Condition66%2/3
exists $self->{"-expires"} and defined $self->{"-expires"}
!ll&&!rl&&r
8014
MC/DC50%1/2
exists $self->{"-expires"} and defined $self->{"-expires"}
exists $self->{"-expires"} defined $self->{"-expires"}
Truth table66%2/3
A and B
A= exists $self->{"-expires"}B= defined $self->{"-expires"}
ABresult
0X0
100
111
100 }
101 # Adapted from CGI::Cookie v1.20 by Lincoln Stein
102 # This internal routine creates date strings suitable for use in
103 # cookies and HTTP headers. (They differ, unfortunately.)
104 # Thanks to Mark Fisher for this.
105 7 ▶sub _expires {
Subroutine _expires: called CC=2 SCAR=6.9
106 7 my($time) = @_;
107 7 my(@MON)=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
108 7 my(@WDAY) = qw/Sun Mon Tue Wed Thu Fri Sat/;
109  
110 # pass through preformatted dates for the sake of expire_calc()
111 7 $time = _expire_calc($time);
112 7 ▶ return $time unless $time =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $time =~ /^\d+$/ 0 7 7
113 7 my $sc = '-';
114 7 my($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime($time);
115 7 $year += 1900;
116 7 return sprintf("%s, %02d$sc%s$sc%04d %02d:%02d:%02d GMT",
117 $WDAY[$wday],$mday,$MON[$mon],$year,$hour,$min,$sec);
118 }
119 # Copied directly from CGI::Cookie v1.20 by Lincoln Stein
120 # This internal routine creates an expires time exactly some number of
121 # hours from the current time. It incorporates modifications from
122 # Mark Fisher.
123 7 ▶sub _expire_calc {
Subroutine _expire_calc: called CC=6 SCAR=20.2
124 7 my($time) = @_;
125 7 my(%mult) = ('s'=>1,
126 'm'=>60,
127 'h'=>60*60,
128 'd'=>60*60*24,
129 'M'=>60*60*24*30,
130 'y'=>60*60*24*365);
131 # format for time can be in any of the forms...
132 # "now" -- expire immediately
133 # "+180s" -- in 180 seconds
134 # "+2m" -- in 2 minutes
135 # "+12h" -- in 12 hours
136 # "+1d" -- in 1 day
137 # "+3M" -- in 3 months
138 # "+2y" -- in 2 years
139 # "-3m" -- 3 minutes ago(!)
140 # If you don't supply one of these forms, we assume you are
141 # specifying the date yourself
142 7 my($offset);
143 7 ▶ if (!$time || (lc($time) eq 'now')) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $time or lc $time eq 'now') { } 0 7 7
elsif ($time =~ /^\d+/) { } 2 5 7
elsif ($time =~ /^([+-]?(?:\d+|\d*\.\d*))([mhdMy]?)/) { } 5 0 5
Condition33%1/3
not $time or lc $time eq "now"
l!l&&r!l&&!r
007
MC/DC0%0/2
not $time or lc $time eq "now"
not $time lc $time eq "now"
Truth table33%1/3
not(A) or B
A= not $timeB= lc $time eq "now"
ABresult
1X1
011
000
144 0 $offset = 0;
145 } elsif ($time=~/^\d+/) {
146 2 return $time;
147 } elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([mhdMy]?)/) {
148 5 ▶ $offset = ($mult{$2} || 1)*$1;
Condition50%1/2
$mult{$2} || 1
l!l
50
MC/DC0%0/1
$mult{$2} || 1
$mult{$2}
Truth table50%1/2
A
A= $mult{$2}
Aresult
11
00
149 } else {
150 0 return $time;
151 }
152 5 return (time+$offset);
153 }
154 3 ▶sub remove {
Subroutine remove: called CC=4 SCAR=14.2
155 3 my ($self,$name) = @_;
156 3 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 2 1 3
157 2 ▶ delete $Cookies->{$name} if exists $Cookies->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Apache::Cookie::Cookies->{$name} 2 0 2
158 } else {
159 delete $Cookies->{$self->{-name}}
160 1 ▶ if exists $Cookies->{$self->{-name}};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$Apache::Cookie::Cookies{$self->{"-name"}} 1 0 1
161 }
162 }
163 17 ▶sub as_string {
Subroutine as_string: called CC=9 SCAR=22.1
164 17 my $self = shift;
165 17 ▶ return '' unless $self->name;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->name 0 17 17
166 17 my %cook = %$self;
167 17 ▶ my $cook = ($cook{-name}) ? escape($cook{-name}) . '=' : '';
Branch: 1 branch
BranchTrueFalseTotal
$cook{'-name'} ? : 17 0 17
168 17 ▶ if ($cook{-value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cook{"-value"}) 17 0 17
169 17 my $i = '';
170 17 foreach(@{$cook{-value}}) {
171 20 $cook .= $i . escape($_);
172 20 $i = '&';
173 }
174 }
175 17 foreach(qw(domain path)) {
176 34 ▶ $cook .= "; $_=" . $cook{"-$_"} if $cook{"-$_"};
Branch: 1 branch
BranchTrueFalseTotal
if $cook{"-$_"} 13 21 34
177 }
178 17 ▶ $cook .= "; expires=$_" if ($_ = expires(\%cook));
Branch: 1 branch
BranchTrueFalseTotal
if $_ = expires(\%cook) 9 8 17
179 17 ▶ $cook .= ($cook{-secure}) ? '; secure' : '';
Branch: 1 branch
BranchTrueFalseTotal
$cook{'-secure'} ? : 9 8 17
180 }
181  
182 ### helpers
183 30 ▶sub do_this {
Subroutine do_this: called CC=1 SCAR=0.0
184 30 (caller(1))[3] =~ /[^:]+$/;
185 30 splice(@_,1,0,'-'.$&);
186 30 goto &cookie_item;
187 }
188 # get or set a named item in cookie hash
189 30 ▶sub cookie_item {
Subroutine cookie_item: called CC=5 SCAR=16.1
190 30 my($self,$item,$val) = @_;
191 30 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 4 26 30
192 #
193 # Darn! this modifies a cookie item if user is generating
194 # a replacement cookie and has not yet "baked" it...
195 # Don't see how this can hurt in the real world... MAR 9-2-02
196 4 ▶ if ( $item eq '-name' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($item eq "-name" and exists $$Apache::Cookie::Cookies{$self->{"-name"}}) 1 3 4
Condition66%2/3
$item eq "-name" and exists $$Apache::Cookie::Cookies{$self->{"-name"}}
!ll&&!rl&&r
301
MC/DC50%1/2
$item eq "-name" and exists $$Apache::Cookie::Cookies{$self->{"-name"}}
$item eq "-name" exists $$Apache::Cookie::Cookies{$self->{"-name"}}
Truth table66%2/3
A and B
A= $item eq "-name"B= exists $$Apache::Cookie::Cookies{$self->{"-name"}}
ABresult
0X0
100
111
197 exists $Cookies->{$self->{-name}} ) {
198 1 $Cookies->{$val} = $Cookies->{$self->{-name}};
199 1 delete $Cookies->{$self->{-name}};
200 }
201 4 $self->{$item} = $val;
202 }
203 30 ▶ return (exists $self->{$item}) ? $self->{$item} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$item} ? : 29 1 30
204 }
205 78 ▶sub escape {
Subroutine escape: called CC=2 SCAR=7.0
206 78 my ($x) = @_;
207 78 ▶ return undef unless defined($x);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 78 78
208 78 $x =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
209 78 return $x;
210 }
211 # unescape URL-data, but leave +'s alone
212 12 ▶sub unescape {
Subroutine unescape: called CC=2 SCAR=7.0
213 12 my ($x) = @_;
214 12 ▶ return undef unless defined($x);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 12 12
215 12 $x =~ tr/+/ /; # pluses become spaces
216 12 $x =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
217 12 return $x;
218 }
219 1 1
220 __END__
↑ Summary