File Coverage

blib/lib/Plack/Session/State/Cookie.pm
Criterion Covered Total %
statement 38 38 100.0
branch 9 10 90.0
condition 13 14 92.8
subroutine 11 11 100.0
pod 3 4 75.0
total 74 77 96.1


line stmt bran cond sub pod time code
1             package Plack::Session::State::Cookie;
2 11     11   177621 use strict;
  11         24  
  11         469  
3 11     11   59 use warnings;
  11         17  
  11         640  
4              
5             our $VERSION = '0.30';
6             our $AUTHORITY = 'cpan:STEVAN';
7              
8 11     11   723 use parent 'Plack::Session::State';
  11         403  
  11         82  
9 11     11   6156 use Cookie::Baker;
  11         13298  
  11         686  
10 11     11   2559 use Plack::Util;
  11         11711  
  11         349  
11              
12 11         75 use Plack::Util::Accessor qw[
13             path
14             domain
15             expires
16             secure
17             httponly
18 11     11   72 ];
  11         17  
19              
20             sub get_session_id {
21 24     24 1 34 my ($self, $env) = @_;
22 24         87 crush_cookie($env->{HTTP_COOKIE})->{$self->session_key};
23             }
24              
25             sub merge_options {
26 27     27 0 100 my($self, %options) = @_;
27              
28 27         51 delete $options{id};
29              
30 27 100 100     105 $options{path} = $self->path || '/' if !exists $options{path};
31 27 100 100     260 $options{domain} = $self->domain if !exists $options{domain} && defined $self->domain;
32 27 100 100     259 $options{secure} = $self->secure if !exists $options{secure} && defined $self->secure;
33 27 50 66     237 $options{httponly} = $self->httponly if !exists $options{httponly} && defined $self->httponly;
34              
35              
36 27 100 100     196 if (!exists $options{expires} && defined $self->expires) {
37 2         17 $options{expires} = time + $self->expires;
38             }
39              
40 27         207 return %options;
41             }
42              
43             sub expire_session_id {
44 2     2 1 17 my ($self, $id, $res, $options) = @_;
45 2         11 my %opts = $self->merge_options(%$options, expires => time);
46 2         11 $self->_set_cookie($id, $res, %opts);
47             }
48              
49             sub finalize {
50 22     22 1 108 my ($self, $id, $res, $options) = @_;
51 22         78 my %opts = $self->merge_options(%$options);
52 22         67 $self->_set_cookie($id, $res, %opts);
53             }
54              
55             sub _set_cookie {
56 24     24   41 my($self, $id, $res, %options) = @_;
57              
58 24         55 my $cookie = bake_cookie(
59             $self->session_key, {
60             value => $id,
61             %options,
62             }
63             );
64 24         1178 Plack::Util::header_push($res->[1], 'Set-Cookie', $cookie);
65             }
66              
67             1;
68              
69             __END__