| 1 | package Dancer::Cookies; | ||||||||||||||||||||||||||||
| 2 | 205 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||
| 3 | #ABSTRACT: a singleton storage for all cookies | ||||||||||||||||||||||||||||
| 4 | 205 | $Dancer::Cookies::VERSION = '1.3520'; | |||||||||||||||||||||||||||
| 5 | 205 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 205 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 205 | ▶ | use Dancer::Cookie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 205 | ▶ | use Dancer::SharedData; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 205 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # all cookies defined by the application are store in that singleton | ||||||||||||||||||||||||||||
| 14 | # this is a hashref the represent all key/value pairs to store as cookies | ||||||||||||||||||||||||||||
| 15 | 205 | my $COOKIES = {}; | |||||||||||||||||||||||||||
| 16 | 407 | ▶ | sub cookies {$COOKIES} | ||||||||||||||||||||||||||
|
Subroutine
cookies: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub init { | ||||||||||||||||||||||||||||
| 19 | 23 | ▶ | $COOKIES = parse_cookie_from_env(); | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub cookie { | ||||||||||||||||||||||||||||
| 23 | 101 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
cookie: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 101 | my $name = shift; | |||||||||||||||||||||||||||
| 25 | 101 | my $value = shift; | |||||||||||||||||||||||||||
| 26 | 101 | ▶ | defined $value && set_cookie( $class, $name, $value, @_ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 101 | ▶ | cookies->{$name} ? cookies->{$name}->value : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub parse_cookie_from_env { | ||||||||||||||||||||||||||||
| 31 | 23 | ▶ | my $request = Dancer::SharedData->request; | ||||||||||||||||||||||||||
|
Subroutine
parse_cookie_from_env: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 23 | ▶ | my $env = (defined $request) ? $request->env : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 23 | ▶ | my $env_str = $env->{COOKIE} || $env->{HTTP_COOKIE}; | ||||||||||||||||||||||||||
|
Condition100%3/3
$env->{'COOKIE'} || $env->{'HTTP_COOKIE'}
MC/DC100%2/2
$env->{'COOKIE'} || $env->{'HTTP_COOKIE'}
$env->{'COOKIE'}
$env->{'HTTP_COOKIE'}
Truth table100%3/3
A || B
A= $env->{'COOKIE'}B= $env->{'HTTP_COOKIE'}
| |||||||||||||||||||||||||||||
| 34 | 23 | ▶ | return {} unless defined $env_str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 18 | my $cookies = {}; | |||||||||||||||||||||||||||
| 37 | 18 | foreach my $cookie ( split( /[,;]\s?/, $env_str ) ) { | |||||||||||||||||||||||||||
| 38 | # here, we don't want more than the 2 first elements | ||||||||||||||||||||||||||||
| 39 | # a cookie string can contains something like: | ||||||||||||||||||||||||||||
| 40 | # cookie_name="foo=bar" | ||||||||||||||||||||||||||||
| 41 | # we want `cookie_name' as the value and `foo=bar' as the value | ||||||||||||||||||||||||||||
| 42 | 21 | my( $name, $value ) = split /\s*=\s*/, $cookie, 2; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # catch weird entries like 'cookie1=foo;;cookie2=bar' | ||||||||||||||||||||||||||||
| 45 | 21 | ▶ | next unless length $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 21 | my @values; | |||||||||||||||||||||||||||
| 48 | 21 | ▶ | if ( defined $value && $value ne '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value and $value ne ""
MC/DC50%1/2
defined $value and $value ne ""
defined $value
$value ne ""
Truth table66%2/3
A and B
A= defined $valueB= $value ne ""
| |||||||||||||||||||||||||||||
| 49 | 19 | @values = map { uri_unescape($_) } split( /[&;]/, $value ); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 21 | $cookies->{$name} = | |||||||||||||||||||||||||||
| 53 | Dancer::Cookie->new( name => $name, value => \@values ); | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 18 | return $cookies; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # set_cookie name => value, | ||||||||||||||||||||||||||||
| 60 | # expires => time() + 3600, domain => '.foo.com' | ||||||||||||||||||||||||||||
| 61 | # http_only => 0 # defaults to 1 | ||||||||||||||||||||||||||||
| 62 | sub set_cookie { | ||||||||||||||||||||||||||||
| 63 | 10 | ▶ | my ( $class, $name, $value, %options ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_cookie: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | 10 | my $cookie = Dancer::Cookie->new( | |||||||||||||||||||||||||||
| 65 | name => $name, | ||||||||||||||||||||||||||||
| 66 | value => $value, | ||||||||||||||||||||||||||||
| 67 | %options | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | 10 | Dancer::Cookies->set_cookie_object($name => $cookie); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub set_cookie_object { | ||||||||||||||||||||||||||||
| 73 | 44 | ▶ | my ($class, $name, $cookie) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_cookie_object: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 44 | Dancer::SharedData->response->add_cookie($name, $cookie); | |||||||||||||||||||||||||||
| 75 | 44 | Dancer::Cookies->cookies->{$name} = $cookie; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 205 | 1; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||||||||||