blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/CGI/Session.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 167 CC 15 SCAR 53.0
CC
15
Cov
0%
init
240.0
BEGIN
2.0
BEGIN
2.0
CRAP 200.3
1 package Dwarf::Plugin::CGI::Session;
2 use Dwarf::Pragma;
3 use Dwarf::Util qw/add_method/;
4 use CGI::Session;
5  
6 sub init {
7 my ($class, $c, $conf) = @_;
8 $conf ||= {};
9  
10 my $session_key = $conf->{session_key} || 'SESSION_ID';
11 my $session_driver = $conf->{session_driver} || 'driver:PostgreSQL';
12 my $session_opts = $conf->{session_opts} || { ColumnType => 'binary' };
13 my $param_name = $conf->{param_name} || $session_key;
14 my $use_cookie = $conf->{use_cookie} || 1;
15 my $cookie_path = $conf->{cookie_path} || '/';
16 my $cookie_domain = $conf->{cookie_domain} || undef;
17 my $cookie_expires = $conf->{cookie_expires} || undef;
18 my $cookie_httponly = $conf->{cookie_httponly} || 0;
19 my $cookie_secure = $conf->{cookie_secure} || 0;
20 my $on_init = $conf->{on_init} || sub {};
21  
22 add_method($c, session => sub {
23 my $self = shift;
24  
25 $self->{'dwarf.session'} ||= do {
26 my $session_id = undef;
27  
28 if ($use_cookie) {
29 my $cookie = $self->request->cookies->{$session_key};
30  
31 if (defined $cookie and $cookie ne '') {
32 $session_id = $cookie;
33 }
34 }
35  
36 my $query_param = $self->request->param($param_name);
37  
38 if (defined $query_param and $query_param ne '') {
39 $session_id = $query_param;
40 }
41  
42 my $session = CGI::Session->new(
43 $session_driver,
44 $session_id,
45 {
46 Handle => $self->dbh,
47 %{ $session_opts }
48 }
49 ) or die CGI::Session->errstr();
50 $on_init->($session);
51 $session->flush;
52  
53 $session;
54 };
55 });
56  
57 add_method($c, refresh_session => sub {
58 my ($self, $will_copy) = @_;
59 $will_copy = 1 unless defined $will_copy;
60  
61 my $session = $self->session;
62  
63 my $new_session = CGI::Session->new(
64 $session_driver,
65 undef,
66 {
67 Handle => $self->dbh,
68 %{ $session_opts }
69 }
70 ) or die CGI::Session->errstr();
71 $on_init->($new_session);
72  
73 if ($will_copy) {
74 my %params = %{ $session->dataref };
75 for my $key (keys %params) {
76 next if $key =~ m/^_SESSION_/;
77 $new_session->param($key, $params{$key});
78 }
79 $new_session->flush;
80 }
81  
82 $session->clear;
83 $session->delete;
84 $session->flush;
85  
86 $self->{'dwarf.session'} = $new_session;
87 });
88  
89 add_method($c, delete_session => sub {
90 my $self = shift;
91 delete $self->{'dwarf.session'} if exists $self->{'dwarf.session'};
92 });
93  
94 return unless $use_cookie;
95  
96 $c->add_trigger('AFTER_DISPATCH' => sub {
97 my ($self, $res) = @_;
98  
99 return unless exists $self->{'dwarf.session'};
100 my $value = { value => $self->session->id };
101 $value->{path} = $cookie_path if defined $cookie_path;
102 $value->{domain} = $cookie_domain if defined $cookie_domain;
103 $value->{expires} = time + $cookie_expires if defined $cookie_expires;
104 $value->{httponly} = 1 if $cookie_httponly;
105 $value->{secure} = 1 if $cookie_secure;
106 my $cookies = $res->cookies;
107 $cookies->{$session_key} = $value;
108 $res->cookies($cookies);
109 $res->header(P3P => 'CP="UNI CUR OUR"'); # IE のクッキー対策
110 });
111 }
112  
113 1;