File Coverage

blib/lib/HTTP/Session/State/Cookie.pm
Criterion Covered Total %
statement 49 50 98.0
branch 22 24 91.6
condition 7 7 100.0
subroutine 10 10 100.0
pod 4 4 100.0
total 92 95 96.8


line stmt bran cond sub pod time code
1             package HTTP::Session::State::Cookie;
2 4     4   2106 use strict;
  4         8  
  4         135  
3 4     4   1559 use HTTP::Session::State::Base;
  4         12  
  4         42  
4 4     4   995 use Carp ();
  4         10  
  4         59  
5 4     4   14 use Scalar::Util ();
  4         6  
  4         2425  
6              
7             our $COOKIE_CLASS = 'CGI::Cookie';
8              
9             __PACKAGE__->mk_accessors(qw/name path domain expires secure samesite httponly/);
10              
11             {
12             my $required = 0;
13             sub _cookie_class {
14 49     49   121 my $class = shift;
15 49 100       204 unless ($required) {
16 4         27 (my $klass = $COOKIE_CLASS) =~ s!::!/!g;
17 4         10 $klass .= ".pm";
18 4         3375 require $klass;
19 4         15713 $required++;
20             }
21 49         423 return $COOKIE_CLASS
22             }
23             }
24              
25             sub new {
26 30     30 1 429201 my $class = shift;
27 30 50       212 my %args = ref($_[0]) ? %{$_[0]} : @_;
  0         0  
28             # set default values
29 30   100     219 $args{name} ||= 'http_session_sid';
30 30   100     192 $args{path} ||= '/';
31 30         474 bless {%args}, $class;
32             }
33              
34             sub get_session_id {
35 30     30 1 1305 my ($self, $req) = @_;
36              
37 30   100     222 my $cookie_header = $ENV{HTTP_COOKIE} || (Scalar::Util::blessed($req) ? $req->header('Cookie') : $req->{HTTP_COOKIE});
38 30 100       1292 return unless $cookie_header;
39              
40 28         99 my %jar = _cookie_class()->parse($cookie_header);
41 28         16281 my $cookie = $jar{$self->name};
42 28 100       345 return $cookie ? $cookie->value : undef;
43             }
44              
45             sub response_filter {
46 21     21 1 821 my ($self, $session_id, $res) = @_;
47 21 100       629 Carp::croak "missing session_id" unless $session_id;
48              
49 19         77 $self->header_filter($session_id, $res);
50             }
51              
52             sub header_filter {
53 21     21 1 172 my ($self, $session_id, $res) = @_;
54 21 50       63 Carp::croak "missing session_id" unless $session_id;
55              
56             my $cookie = _cookie_class()->new(
57             sub {
58 21     21   658 my %options = (
59             -name => $self->name,
60             -value => $session_id,
61             -path => $self->path,
62             );
63 21 100       1314 $options{'-domain'} = $self->domain if $self->domain;
64 21 100       787 $options{'-expires'} = $self->expires if $self->expires;
65 21 100       820 $options{'-secure'} = $self->secure if $self->secure;
66 21 100       721 $options{'-samesite'} = $self->samesite if $self->samesite;
67 21 100       800 $options{'-httponly'} = $self->httponly if $self->httponly;
68 21         440 %options;
69 21         64 }->()
70             );
71 21 100       5481 if (Scalar::Util::blessed($res)) {
72 20         88 $res->header( 'Set-Cookie' => $cookie->as_string );
73 20         5401 $res;
74             } else {
75 1         3 push @{$res->[1]}, 'Set-Cookie' => $cookie->as_string;
  1         6  
76 1         163 $res;
77             }
78             }
79              
80             1;
81             __END__