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   1930 use strict;
  4         5  
  4         130  
3 4     4   1495 use HTTP::Session::State::Base;
  4         11  
  4         29  
4 4     4   1008 use Carp ();
  4         7  
  4         47  
5 4     4   14 use Scalar::Util ();
  4         5  
  4         2655  
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   95 my $class = shift;
15 49 100       81 unless ($required) {
16 4         20 (my $klass = $COOKIE_CLASS) =~ s!::!/!g;
17 4         8 $klass .= ".pm";
18 4         2463 require $klass;
19 4         11236 $required++;
20             }
21 49         264 return $COOKIE_CLASS
22             }
23             }
24              
25             sub new {
26 30     30 1 320704 my $class = shift;
27 30 50       116 my %args = ref($_[0]) ? %{$_[0]} : @_;
  0         0  
28             # set default values
29 30   100     127 $args{name} ||= 'http_session_sid';
30 30   100     141 $args{path} ||= '/';
31 30         294 bless {%args}, $class;
32             }
33              
34             sub get_session_id {
35 30     30 1 662 my ($self, $req) = @_;
36              
37 30   100     139 my $cookie_header = $ENV{HTTP_COOKIE} || (Scalar::Util::blessed($req) ? $req->header('Cookie') : $req->{HTTP_COOKIE});
38 30 100       671 return unless $cookie_header;
39              
40 28         84 my %jar = _cookie_class()->parse($cookie_header);
41 28         8410 my $cookie = $jar{$self->name};
42 28 100       243 return $cookie ? $cookie->value : undef;
43             }
44              
45             sub response_filter {
46 21     21 1 512 my ($self, $session_id, $res) = @_;
47 21 100       448 Carp::croak "missing session_id" unless $session_id;
48              
49 19         49 $self->header_filter($session_id, $res);
50             }
51              
52             sub header_filter {
53 21     21 1 78 my ($self, $session_id, $res) = @_;
54 21 50       40 Carp::croak "missing session_id" unless $session_id;
55              
56             my $cookie = _cookie_class()->new(
57             sub {
58 21     21   399 my %options = (
59             -name => $self->name,
60             -value => $session_id,
61             -path => $self->path,
62             );
63 21 100       834 $options{'-domain'} = $self->domain if $self->domain;
64 21 100       442 $options{'-expires'} = $self->expires if $self->expires;
65 21 100       462 $options{'-secure'} = $self->secure if $self->secure;
66 21 100       446 $options{'-samesite'} = $self->samesite if $self->samesite;
67 21 100       441 $options{'-httponly'} = $self->httponly if $self->httponly;
68 21         201 %options;
69 21         43 }->()
70             );
71 21 100       3125 if (Scalar::Util::blessed($res)) {
72 20         44 $res->header( 'Set-Cookie' => $cookie->as_string );
73 20         2965 $res;
74             } else {
75 1         2 push @{$res->[1]}, 'Set-Cookie' => $cookie->as_string;
  1         4  
76 1         95 $res;
77             }
78             }
79              
80             1;
81             __END__