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.3521';
5 184     184   1312 use strict;
  184         443  
  184         5399  
6 184     184   1054 use warnings;
  184         531  
  184         5124  
7              
8 184     184   79284 use Dancer::Cookie;
  184         564  
  184         12849  
9 184     184   2169 use Dancer::SharedData;
  184         475  
  184         3739  
10              
11 184     184   971 use URI::Escape;
  184         455  
  184         99076  
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             my $COOKIES = {};
16 407     407 1 2969 sub cookies {$COOKIES}
17              
18             sub init {
19 21     21 1 103 $COOKIES = parse_cookie_from_env();
20             }
21              
22             sub cookie {
23 101     101 1 209 my $class = shift;
24 101         204 my $name = shift;
25 101         195 my $value = shift;
26 101 100       273 defined $value && set_cookie( $class, $name, $value, @_ );
27 101 100       259 cookies->{$name} ? cookies->{$name}->value : undef;
28             }
29              
30             sub parse_cookie_from_env {
31 21     21 1 58 my $request = Dancer::SharedData->request;
32 21 50       80 my $env = (defined $request) ? $request->env : {};
33 21   100     76 my $env_str = $env->{COOKIE} || $env->{HTTP_COOKIE};
34 21 100       61 return {} unless defined $env_str;
35              
36 18         32 my $cookies = {};
37 18         83 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         118 my( $name, $value ) = split /\s*=\s*/, $cookie, 2;
43              
44             # catch weird entries like 'cookie1=foo;;cookie2=bar'
45 21 50       63 next unless length $name;
46              
47 21         29 my @values;
48 21 100 66     81 if ( defined $value && $value ne '' ) {
49 19         56 @values = map { uri_unescape($_) } split( /[&;]/, $value );
  22         86  
50             }
51              
52 21         253 $cookies->{$name} =
53             Dancer::Cookie->new( name => $name, value => \@values );
54             }
55              
56 18         83 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     10 0 36 my ( $class, $name, $value, %options ) = @_;
64 10         58 my $cookie = Dancer::Cookie->new(
65             name => $name,
66             value => $value,
67             %options
68             );
69 10         32 Dancer::Cookies->set_cookie_object($name => $cookie);
70             }
71              
72             sub set_cookie_object {
73 44     44 0 161 my ($class, $name, $cookie) = @_;
74 44         207 Dancer::SharedData->response->add_cookie($name, $cookie);
75 44         212 Dancer::Cookies->cookies->{$name} = $cookie;
76             }
77              
78             1;
79              
80             __END__