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.3520';
5 184     184   1290 use strict;
  184         428  
  184         5546  
6 184     184   1084 use warnings;
  184         501  
  184         4884  
7              
8 184     184   81286 use Dancer::Cookie;
  184         578  
  184         5269  
9 184     184   2198 use Dancer::SharedData;
  184         488  
  184         3776  
10              
11 184     184   988 use URI::Escape;
  184         487  
  184         102499  
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 3665 sub cookies {$COOKIES}
17              
18             sub init {
19 21     21 1 100 $COOKIES = parse_cookie_from_env();
20             }
21              
22             sub cookie {
23 101     101 1 216 my $class = shift;
24 101         176 my $name = shift;
25 101         180 my $value = shift;
26 101 100       333 defined $value && set_cookie( $class, $name, $value, @_ );
27 101 100       240 cookies->{$name} ? cookies->{$name}->value : undef;
28             }
29              
30             sub parse_cookie_from_env {
31 21     21 1 53 my $request = Dancer::SharedData->request;
32 21 50       80 my $env = (defined $request) ? $request->env : {};
33 21   100     87 my $env_str = $env->{COOKIE} || $env->{HTTP_COOKIE};
34 21 100       51 return {} unless defined $env_str;
35              
36 18         40 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         115 my( $name, $value ) = split /\s*=\s*/, $cookie, 2;
43              
44             # catch weird entries like 'cookie1=foo;;cookie2=bar'
45 21 50       55 next unless length $name;
46              
47 21         32 my @values;
48 21 100 66     79 if ( defined $value && $value ne '' ) {
49 19         57 @values = map { uri_unescape($_) } split( /[&;]/, $value );
  22         82  
50             }
51              
52 21         240 $cookies->{$name} =
53             Dancer::Cookie->new( name => $name, value => \@values );
54             }
55              
56 18         76 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 32 my ( $class, $name, $value, %options ) = @_;
64 10         70 my $cookie = Dancer::Cookie->new(
65             name => $name,
66             value => $value,
67             %options
68             );
69 10         34 Dancer::Cookies->set_cookie_object($name => $cookie);
70             }
71              
72             sub set_cookie_object {
73 44     44 0 171 my ($class, $name, $cookie) = @_;
74 44         196 Dancer::SharedData->response->add_cookie($name, $cookie);
75 44         187 Dancer::Cookies->cookies->{$name} = $cookie;
76             }
77              
78             1;
79              
80             __END__