| 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
Condition33%1/3
defined $self->{"name"} and length $self->{"name"}
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"}
| |||||||||||||||||||||||||||||
| 70 | 0 | return $string; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 2 | $string .= url_encode( $self->{'name'} ) . '='; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 2 | ▶ | if ( defined $self->{'value'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 2 | $string .= quote( $self->{'value'} ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | if ( defined $self->{'comment'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | $string .= '; Comment=' . quote( $self->{'comment'} ); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | ▶ | if ( defined $self->{'comment_url'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | # Must be quoted (RFC 2965) | ||||||||||||||||||||||||||||
| 85 | 1 | $string .= '; CommentURL=' . quote( $self->{'comment_url'} ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | ▶ | if ( $self->{'discard'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 1 | $string .= '; Discard'; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | ▶ | if ( defined $self->{'domain'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 1 | $string .= '; Domain=' . $self->{'domain'}; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | ▶ | if ( defined $self->{'expires'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | $string .= '; Expires=' . $self->{'expires'}; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2 | ▶ | if ( $self->{'http_only'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 1 | $string .= '; HttpOnly'; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | ▶ | if ( defined $self->{'max_age'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 1 | $string .= '; Max-Age=' . $self->{'max_age'}; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | ▶ | if ( defined $self->{'path'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 1 | $string .= '; Path=' . $self->{'path'}; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | ▶ | if ( defined $self->{'port'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | # Must be quoted (RFC 2965) | ||||||||||||||||||||||||||||
| 114 | 1 | $string .= '; Port=' . quote( $self->{'port'} ); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2 | ▶ | if ( $self->{'secure'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 1 | $string .= '; Secure'; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | ▶ | if ( defined $self->{'version'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 6 | my @parts; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 6 | ▶ | while ( length $string ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 25 | ▶ | $string =~ s/^\s*([^=;,]+)\s*=?\s*// or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 25 | my $key = $1; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 25 | push @parts, $key; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 25 | ▶ | if ( $key =~ /^expires$/i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # Attribute format: Wdy, DD-Mon-YYYY HH:MM:SS GMT | ||||||||||||||||||||||||||||
| 151 | 4 | ▶ | if ( $string =~ s/^([^;,]+,+[^;,]+)\s*(?:;|,)*// ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 4 | push @parts, $1; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | else { | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 21 | ▶ | if ( $string =~ s/^"([^"\\]*(?:\\.[^"\\]*)*)"\s*(?:;|,)*// ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 25 | my $part = shift @parts; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 25 | ▶ | if ( $cookie ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 19 | my $key = lc $part; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 19 | ▶ | if ( $key eq 'version' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 2 | $cookie->{'version'} = shift @parts; | |||||||||||||||||||||||||||
| 185 | 2 | next; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 17 | ▶ | if ( $key eq 'max-age' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 1 | $cookie->{'max_age'} = shift @parts; | |||||||||||||||||||||||||||
| 190 | 1 | next; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 16 | ▶ | if ( $key eq 'domain' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 1 | $cookie->{'domain'} = shift @parts; | |||||||||||||||||||||||||||
| 195 | 1 | next; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 15 | ▶ | if ( $key eq 'path' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 5 | $cookie->{'path'} = shift @parts; | |||||||||||||||||||||||||||
| 200 | 5 | next; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 10 | ▶ | if ( $key eq 'secure' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 1 | shift @parts; | |||||||||||||||||||||||||||
| 205 | 1 | $cookie->{'secure'} = 1; | |||||||||||||||||||||||||||
| 206 | 1 | next; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 9 | ▶ | if ( $key eq 'httponly' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 1 | shift @parts; | |||||||||||||||||||||||||||
| 211 | 1 | $cookie->{'http_only'} = 1; | |||||||||||||||||||||||||||
| 212 | 1 | next; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 8 | ▶ | if ( $key eq 'discard' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 1 | shift @parts; | |||||||||||||||||||||||||||
| 217 | 1 | $cookie->{'discard'} = 1; | |||||||||||||||||||||||||||
| 218 | 1 | next; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 7 | ▶ | if ( $key eq 'port' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 1 | $cookie->{'port'} = shift @parts; | |||||||||||||||||||||||||||
| 223 | 1 | next; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 6 | ▶ | if ( $key eq 'expires' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 4 | $cookie->{'expires'} = shift @parts; | |||||||||||||||||||||||||||
| 228 | 4 | next; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 2 | ▶ | if ( $key eq 'comment' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 1 | $cookie->{'comment'} = shift @parts; | |||||||||||||||||||||||||||
| 233 | 1 | next; | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 1 | ▶ | if ( $key eq 'commenturl' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||