File Coverage

blib/lib/Dancer/Cookies.pm
Criterion Covered Total %
statement 42 42 100.0
branch 10 12 83.3
condition 5 6 83.3
subroutine 11 11 100.0
pod 4 6 66.6
total 72 77 93.5


line stmt bran cond sub pod time code
1             package Dancer::Cookies;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: a singleton storage for all cookies
4             $Dancer::Cookies::VERSION = '1.3514_04'; # TRIAL
5             $Dancer::Cookies::VERSION = '1.351404';
6 185     185   1611 use strict;
  185         322  
  185         4380  
7 185     185   795 use warnings;
  185         328  
  185         3649  
8              
9 185     185   65403 use Dancer::Cookie;
  185         607  
  185         4755  
10 185     185   1885 use Dancer::SharedData;
  185         346  
  185         3289  
11              
12 185     185   797 use URI::Escape;
  185         450  
  185         82558  
13              
14             # all cookies defined by the application are store in that singleton
15             # this is a hashref the represent all key/value pairs to store as cookies
16             my $COOKIES = {};
17 407     407 1 2785 sub cookies {$COOKIES}
18              
19             sub init {
20 21     21 1 70 $COOKIES = parse_cookie_from_env();
21             }
22              
23             sub cookie {
24 101     101 1 195 my $class = shift;
25 101         163 my $name = shift;
26 101         168 my $value = shift;
27 101 100       236 defined $value && set_cookie( $class, $name, $value, @_ );
28 101 100       201 cookies->{$name} ? cookies->{$name}->value : undef;
29             }
30              
31             sub parse_cookie_from_env {
32 21     21 1 42 my $request = Dancer::SharedData->request;
33 21 50       48 my $env = (defined $request) ? $request->env : {};
34 21   100     49 my $env_str = $env->{COOKIE} || $env->{HTTP_COOKIE};
35 21 100       43 return {} unless defined $env_str;
36              
37 18         24 my $cookies = {};
38 18         62 foreach my $cookie ( split( /[,;]\s?/, $env_str ) ) {
39             # here, we don't want more than the 2 first elements
40             # a cookie string can contains something like:
41             # cookie_name="foo=bar"
42             # we want `cookie_name' as the value and `foo=bar' as the value
43 21         93 my( $name, $value ) = split /\s*=\s*/, $cookie, 2;
44              
45             # catch weird entries like 'cookie1=foo;;cookie2=bar'
46 21 50       47 next unless length $name;
47              
48 21         22 my @values;
49 21 100 66     88 if ( defined $value && $value ne '' ) {
50 19         46 @values = map { uri_unescape($_) } split( /[&;]/, $value );
  22         80  
51             }
52              
53 21         175 $cookies->{$name} =
54             Dancer::Cookie->new( name => $name, value => \@values );
55             }
56              
57 18         70 return $cookies;
58             }
59              
60             # set_cookie name => value,
61             # expires => time() + 3600, domain => '.foo.com'
62             # http_only => 0 # defaults to 1
63             sub set_cookie {
64 10     10 0 34 my ( $class, $name, $value, %options ) = @_;
65 10         48 my $cookie = Dancer::Cookie->new(
66             name => $name,
67             value => $value,
68             %options
69             );
70 10         23 Dancer::Cookies->set_cookie_object($name => $cookie);
71             }
72              
73             sub set_cookie_object {
74 44     44 0 112 my ($class, $name, $cookie) = @_;
75 44         146 Dancer::SharedData->response->add_cookie($name, $cookie);
76 44         125 Dancer::Cookies->cookies->{$name} = $cookie;
77             }
78              
79             1;
80              
81             __END__