Dancer::Cookies - a singleton storage for all cookies


Dancer documentation Contained in the Dancer distribution.

Index


Code Index:

NAME

Top

Dancer::Cookies - a singleton storage for all cookies

SYNOPSIS

Top

    use Dancer::Cookies;

    my $cookies = Dancer::Cookies->cookies;

    foreach my $name ( keys %{$cookies} ) {
        my $cookie = $cookies->{$name};
        my $value  = $cookie->value;
        print "$name => $value\n";
    }




    cookie lang => "fr-FR"; #set a cookie and return its value
    cookie lang => "fr-FR", expires => "2 hours";
    cookie "lang"           #return a cookie value

DESCRIPTION

Top

Dancer::Cookies keeps all the cookies defined by the application and makes them accessible and provides a few helper functions for cookie handling with regards to the stored cookies.

METHODS

Top

init

This method is called when ->new() is called. It creates a storage of cookies parsed from the environment using parse_cookies_from_env described below.

cookies

Returns a hash reference of all cookies, all objects of Dancer::Cookie type.

The key is the cookie name, the value is the Dancer::Cookie object.

AUTHOR

Top

Alexis Sukrieh

LICENSE AND COPYRIGHT

Top


Dancer documentation Contained in the Dancer distribution.

package Dancer::Cookies;
use strict;
use warnings;

use Dancer::Cookie;
use Dancer::SharedData;

use URI::Escape;

# all cookies defined by the application are store in that singleton
# this is a hashref the represent all key/value pairs to store as cookies
my $COOKIES = {};
sub cookies {$COOKIES}

sub init {
    $COOKIES = parse_cookie_from_env();
}

sub cookie {
    my $class = shift;
    my $name  = shift;
    my $value = shift;
    defined $value && set_cookie( $class, $name, $value, @_ );
    cookies->{$name} ? cookies->{$name}->value : undef;
}

sub parse_cookie_from_env {
    my $request = Dancer::SharedData->request;
    my $env     = (defined $request) ? $request->env : {};
    my $env_str = $env->{COOKIE} || $env->{HTTP_COOKIE};
    return {} unless defined $env_str;

    my $cookies = {};
    foreach my $cookie ( split( /[,;]\s/, $env_str ) ) {
        my ( $name, $value ) = split( '=', $cookie );
        my @values;
        if ( $value ne '' ) {
            @values = map { uri_unescape($_) } split( /[&;]/, $value );
        }
        $cookies->{$name} =
          Dancer::Cookie->new( name => $name, value => \@values );
    }

    return $cookies;
}

# set_cookie name => value,
#     expires => time() + 3600, domain => '.foo.com'
#     http_only => 0 # defaults to 1
sub set_cookie {
    my ( $class, $name, $value, %options ) = @_;
    my $cookie =  Dancer::Cookie->new(
        name  => $name,
        value => $value,
        %options
    );
    Dancer::Cookies->set_cookie_object($name => $cookie);
}

sub set_cookie_object {
    my ($class, $name, $cookie) = @_;
    Dancer::SharedData->response->push_header(
        'Set-Cookie' => $cookie->to_header);
    Dancer::Cookies->cookies->{$name} = $cookie;
}

1;

__END__