GX/HTTP/Response/Cookie.pm

filter: Statementstmt 98.1% 106 / 108 Branchbran 87.8% 58 / 66 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 4 / 4 Podpod 100% 2 / 2 totaltotal 92.4% 171 / 185 CC 35 SCAR 28.8
CC
35
Cov
93%
parse
21.1
as_string
15.4
BEGIN
1.0
CRAP 17.7
« GX/Component.pm ↑ Summary GX/View/Template/TT.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/HTTP/Response/Cookie.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::HTTP::Response::Cookie;
7  
8 12 ▶use GX::HTTP::Util qw( quote url_encode url_decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 # ----------------------------------------------------------------------------------------------------------------------
12 # Class setup
13 # ----------------------------------------------------------------------------------------------------------------------
14  
15 12 ▶use GX::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 12 extends 'GX::HTTP::Cookie';
18  
19 # RFC 2109, RFC 2965
20 12 has 'comment' => (
21 isa => 'Scalar'
22 );
23  
24 # RFC 2965
25 12 has 'comment_url' => (
26 isa => 'Scalar'
27 );
28  
29 # RFC 2965
30 12 has 'discard' => (
31 isa => 'Bool'
32 );
33  
34 # Netscape
35 12 has 'expires' => (
36 isa => 'Scalar'
37 );
38  
39 # RFC 2965
40 12 has 'http_only' => (
41 isa => 'Bool'
42 );
43  
44 # RFC 2109, RFC 2965
45 12 has 'max_age' => (
46 isa => 'Scalar'
47 );
48  
49 # RFC 2109, RFC 2965
50 12 has 'secure' => (
51 isa => 'Bool'
52 );
53  
54 12 build;
55  
56  
57 # ----------------------------------------------------------------------------------------------------------------------
58 # Public methods
59 # ----------------------------------------------------------------------------------------------------------------------
60  
61 2 ▶sub as_string {
Subroutine as_string: called CC=15 SCAR=27.3
Pod documented
62  
63 2 my $self = shift;
64  
65 # Note: Some user agents (e.g. IE, LWP::UserAgent) have problems with quoted attribute values
66  
67 2 my $string = '';
68  
69 2 ▶ if ( ! defined $self->{'name'} || ! length $self->{'name'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"name"} and length $self->{"name"}) 0 2 2
Condition33%1/3
defined $self->{"name"} and length $self->{"name"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self->{"name"} and length $self->{"name"}
defined $self->{"name"} length $self->{"name"}
Truth table33%1/3
A and B
A= defined $self->{"name"}B= length $self->{"name"}
ABresult
0X0
100
111
70 0 return $string;
71 }
72  
73 2 $string .= url_encode( $self->{'name'} ) . '=';
74  
75 2 ▶ if ( defined $self->{'value'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"value"}) 2 0 2
76 2 $string .= quote( $self->{'value'} );
77 }
78  
79 2 ▶ if ( defined $self->{'comment'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"comment"}) 1 1 2
80 1 $string .= '; Comment=' . quote( $self->{'comment'} );
81 }
82  
83 2 ▶ if ( defined $self->{'comment_url'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"comment_url"}) 1 1 2
84 # Must be quoted (RFC 2965)
85 1 $string .= '; CommentURL=' . quote( $self->{'comment_url'} );
86 }
87  
88 2 ▶ if ( $self->{'discard'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"discard"}) 1 1 2
89 1 $string .= '; Discard';
90 }
91  
92 2 ▶ if ( defined $self->{'domain'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"domain"}) 1 1 2
93 1 $string .= '; Domain=' . $self->{'domain'};
94 }
95  
96 2 ▶ if ( defined $self->{'expires'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"expires"}) 0 2 2
97 0 $string .= '; Expires=' . $self->{'expires'};
98 }
99  
100 2 ▶ if ( $self->{'http_only'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"http_only"}) 1 1 2
101 1 $string .= '; HttpOnly';
102 }
103  
104 2 ▶ if ( defined $self->{'max_age'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"max_age"}) 1 1 2
105 1 $string .= '; Max-Age=' . $self->{'max_age'};
106 }
107  
108 2 ▶ if ( defined $self->{'path'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"path"}) 1 1 2
109 1 $string .= '; Path=' . $self->{'path'};
110 }
111  
112 2 ▶ if ( defined $self->{'port'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"port"}) 1 1 2
113 # Must be quoted (RFC 2965)
114 1 $string .= '; Port=' . quote( $self->{'port'} );
115 }
116  
117 2 ▶ if ( $self->{'secure'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"secure"}) 1 1 2
118 1 $string .= '; Secure';
119 }
120  
121 2 ▶ if ( defined $self->{'version'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'version'}) { } 1 1 2
122 1 $string .= '; Version=' . $self->{'version'};
123 }
124 else {
125 1 $string .= '; Version=1';
126 }
127  
128 2 return $string;
129  
130 }
131  
132 6 ▶sub parse {
Subroutine parse: called CC=21 SCAR=30.5
Pod documented
133  
134 6 my $class = shift;
135 6 my $string = shift;
136  
137 6 ▶ defined $string or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $string or return 6 0 6
138  
139 6 my @parts;
140  
141 6 ▶ while ( length $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string) 25 6 31
142  
143 25 ▶ $string =~ s/^\s*([^=;,]+)\s*=?\s*// or return;
Branch: 1 branch
BranchTrueFalseTotal
$string =~ s/^\s*([^=;,]+)\s*=?\s*// or return 25 0 25
144 25 my $key = $1;
145  
146 25 push @parts, $key;
147  
148 25 ▶ if ( $key =~ /^expires$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^expires$/i) { } 4 21 25
149  
150 # Attribute format: Wdy, DD-Mon-YYYY HH:MM:SS GMT
151 4 ▶ if ( $string =~ s/^([^;,]+,+[^;,]+)\s*(?:;|,)*// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^([^;,]+,+[^;,]+)\s*(?:;|,)*//) 4 0 4
152 4 push @parts, $1;
153 }
154  
155 }
156 else {
157  
158 21 ▶ if ( $string =~ s/^"([^"\\]*(?:\\.[^"\\]*)*)"\s*(?:;|,)*// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($string =~ s/^"([^"\\]*(?:\\.[^"\\]*)*)"\s*(?:;|,)*//) { } 2 19 21
elsif ($string =~ s/^([^;,]*)\s*(?:;|,)*//) { } 19 0 19
159 2 my $value = $1;
160 2 $value =~ s/\\"/"/g;
161 2 $value =~ s/\\\\/\\/g;
162 2 push @parts, $value;
163 }
164 elsif ( $string =~ s/^([^;,]*)\s*(?:;|,)*// ) {
165 19 push @parts, $1;
166 }
167  
168 }
169  
170 }
171  
172 6 my $cookie;
173 my @cookies;
174  
175 6 ▶ while ( @parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 25 6 31
176  
177 25 my $part = shift @parts;
178  
179 25 ▶ if ( $cookie ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookie) 19 6 25
180  
181 19 my $key = lc $part;
182  
183 19 ▶ if ( $key eq 'version' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "version") 2 17 19
184 2 $cookie->{'version'} = shift @parts;
185 2 next;
186 }
187  
188 17 ▶ if ( $key eq 'max-age' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "max-age") 1 16 17
189 1 $cookie->{'max_age'} = shift @parts;
190 1 next;
191 }
192  
193 16 ▶ if ( $key eq 'domain' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "domain") 1 15 16
194 1 $cookie->{'domain'} = shift @parts;
195 1 next;
196 }
197  
198 15 ▶ if ( $key eq 'path' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "path") 5 10 15
199 5 $cookie->{'path'} = shift @parts;
200 5 next;
201 }
202  
203 10 ▶ if ( $key eq 'secure' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "secure") 1 9 10
204 1 shift @parts;
205 1 $cookie->{'secure'} = 1;
206 1 next;
207 }
208  
209 9 ▶ if ( $key eq 'httponly' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "httponly") 1 8 9
210 1 shift @parts;
211 1 $cookie->{'http_only'} = 1;
212 1 next;
213 }
214  
215 8 ▶ if ( $key eq 'discard' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "discard") 1 7 8
216 1 shift @parts;
217 1 $cookie->{'discard'} = 1;
218 1 next;
219 }
220  
221 7 ▶ if ( $key eq 'port' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "port") 1 6 7
222 1 $cookie->{'port'} = shift @parts;
223 1 next;
224 }
225  
226 6 ▶ if ( $key eq 'expires' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "expires") 4 2 6
227 4 $cookie->{'expires'} = shift @parts;
228 4 next;
229 }
230  
231 2 ▶ if ( $key eq 'comment' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "comment") 1 1 2
232 1 $cookie->{'comment'} = shift @parts;
233 1 next;
234 }
235  
236 1 ▶ if ( $key eq 'commenturl' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "commenturl") 1 0 1
237 1 $cookie->{'comment_url'} = shift @parts;
238 1 next;
239 }
240  
241 }
242  
243 6 $cookie = $class->new(
244 name => url_decode( $part ),
245 value => shift( @parts )
246 );
247  
248 6 push @cookies, $cookie;
249  
250 }
251  
252 6 return @cookies;
253  
254 }
255  
256  
257 12 1;
258  
259 __END__
« GX/Component.pm ↑ Summary GX/View/Template/TT.pm »