File Coverage

blib/lib/Plack/Session/State/Cookie.pm
Criterion Covered Total %
statement 39 39 100.0
branch 10 12 83.3
condition 15 17 88.2
subroutine 11 11 100.0
pod 3 4 75.0
total 78 83 93.9


line stmt bran cond sub pod time code
1             package Plack::Session::State::Cookie;
2 11     11   291927 use strict;
  11         35  
  11         260  
3 11     11   47 use warnings;
  11         16  
  11         439  
4              
5             our $VERSION = '0.33';
6             our $AUTHORITY = 'cpan:STEVAN';
7              
8 11     11   403 use parent 'Plack::Session::State';
  11         268  
  11         62  
9 11     11   448 use Cookie::Baker;
  11         23  
  11         501  
10 11     11   1808 use Plack::Util;
  11         11618  
  11         292  
11              
12 11         51 use Plack::Util::Accessor qw[
13             path
14             domain
15             expires
16             secure
17             httponly
18             samesite
19 11     11   56 ];
  11         19  
20              
21             sub get_session_id {
22 24     24 1 68 my ($self, $env) = @_;
23 24         81 crush_cookie($env->{HTTP_COOKIE})->{$self->session_key};
24             }
25              
26             sub merge_options {
27 27     27 0 113 my($self, %options) = @_;
28              
29 27         66 delete $options{id};
30              
31 27 100 100     96 $options{path} = $self->path || '/' if !exists $options{path};
32 27 100 100     217 $options{domain} = $self->domain if !exists $options{domain} && defined $self->domain;
33 27 100 100     178 $options{secure} = $self->secure if !exists $options{secure} && defined $self->secure;
34 27 50 66     160 $options{httponly} = $self->httponly if !exists $options{httponly} && defined $self->httponly;
35 27 50 66     142 $options{samesite} = $self->samesite if !exists $options{samesite} && defined $self->samesite;
36              
37              
38 27 100 100     152 if (!exists $options{expires} && defined $self->expires) {
39 2         11 $options{expires} = time + $self->expires;
40             }
41              
42 27         191 return %options;
43             }
44              
45             sub expire_session_id {
46 2     2 1 11 my ($self, $id, $res, $options) = @_;
47 2         9 my %opts = $self->merge_options(%$options, expires => time);
48 2         9 $self->_set_cookie($id, $res, %opts);
49             }
50              
51             sub finalize {
52 22     22 1 103 my ($self, $id, $res, $options) = @_;
53 22         106 my %opts = $self->merge_options(%$options);
54 22         67 $self->_set_cookie($id, $res, %opts);
55             }
56              
57             sub _set_cookie {
58 24     24   54 my($self, $id, $res, %options) = @_;
59              
60 24         51 my $cookie = bake_cookie(
61             $self->session_key, {
62             value => $id,
63             %options,
64             }
65             );
66 24         1241 Plack::Util::header_push($res->[1], 'Set-Cookie', $cookie);
67             }
68              
69             1;
70              
71             __END__