| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||||||||||||||||||||
| 3 | # File: GX/HTTP/Request/Cookie.pm | ||||||||||||||||||||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package GX::HTTP::Request::Cookie; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 16 | ▶ | 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 | 16 | ▶ | use GX::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 16 | extends 'GX::HTTP::Cookie'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 16 | build; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 23 | # Public methods | ||||||||||||||||||||||||||||
| 24 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 2 | my $string = ''; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 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"}
| |||||||||||||||||||||||||||||
| 33 | 0 | return $string; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | if ( defined $self->{'version'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 1 | $string .= '$Version=' . $self->{'version'} . '; '; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | else { | ||||||||||||||||||||||||||||
| 40 | 1 | $string .= '$Version=1; '; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | $string .= url_encode( $self->{'name'} ) . '='; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | if ( defined $self->{'value'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 2 | $string .= quote( $self->{'value'} ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | ▶ | if ( defined $self->{'path'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 1 | $string .= '; $Path=' . $self->{'path'}; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | ▶ | if ( defined $self->{'domain'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1 | $string .= '; $Domain=' . $self->{'domain'}; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 2 | ▶ | if ( defined $self->{'port'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | # Must be quoted (RFC 2965) | ||||||||||||||||||||||||||||
| 59 | 1 | $string .= '; $Port=' . quote( $self->{'port'} ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | return $string; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 5 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=13 SCAR=25.9
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | my $class = shift; | |||||||||||||||||||||||||||
| 70 | 5 | my $string = shift; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 5 | ▶ | defined $string or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 5 | my @parts; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 5 | ▶ | while ( length $string ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 25 | ▶ | $string =~ s/^\s*([^=;,]+)\s*=\s*// or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 25 | push @parts, $1; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 25 | ▶ | if ( $string =~ s/^"([^"\\]*(?:\\.[^"\\]*)*)"\s*(?:;|,)*// ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 83 | 21 | my $value = $1; | |||||||||||||||||||||||||||
| 84 | 21 | $value =~ s/\\"/"/g; | |||||||||||||||||||||||||||
| 85 | 21 | $value =~ s/\\\\/\\/g; | |||||||||||||||||||||||||||
| 86 | 21 | push @parts, $value; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | elsif ( $string =~ s/^([^;,]*)\s*(?:;|,)*// ) { | ||||||||||||||||||||||||||||
| 89 | 4 | push @parts, $1; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||
| 92 | 0 | return; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # See RFC 2109, section 4.4 | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 5 | my @cookies; | |||||||||||||||||||||||||||
| 100 | my $cookie; | ||||||||||||||||||||||||||||
| 101 | 5 | my %attributes; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 5 | ▶ | while ( @parts ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 25 | my $part = shift @parts; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 25 | ▶ | if ( substr( $part, 0, 1 ) eq '$' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 16 | my $attribute = lc substr( $part, 1 ); | |||||||||||||||||||||||||||
| 110 | 16 | my $value = shift @parts; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 16 | ▶ | if ( $cookie ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 11 | ▶ | if ( $attribute eq 'version' ) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 115 | 0 | $cookie->{'version'} = $value; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | elsif ( $attribute eq 'path' ) { | ||||||||||||||||||||||||||||
| 118 | 9 | $cookie->{'path'} = $value; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | elsif ( $attribute eq 'domain' ) { | ||||||||||||||||||||||||||||
| 121 | 1 | $cookie->{'domain'} = $value; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | elsif ( $attribute eq 'port' ) { | ||||||||||||||||||||||||||||
| 124 | 1 | $cookie->{'port'} = $value; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | else { | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Global attribute | ||||||||||||||||||||||||||||
| 131 | 5 | $attributes{$attribute} = $value; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | $cookie = $class->new( | ||||||||||||||||||||||||||||
| 139 | name => url_decode( $part ), | ||||||||||||||||||||||||||||
| 140 | value => shift( @parts ), | ||||||||||||||||||||||||||||
| 141 | domain => $attributes{'domain'}, | ||||||||||||||||||||||||||||
| 142 | path => $attributes{'path'}, | ||||||||||||||||||||||||||||
| 143 | 9 | version => $attributes{'version'} | |||||||||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 9 | push @cookies, $cookie; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 5 | return @cookies; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 16 | 1; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||