« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module/SocialMedia/Line.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/Log/Dispatch/FileRotate.pm »
| 1 | package Dwarf::Plugin::HTTP::Session; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use Dwarf::Util qw/add_method/; | ||
| 4 | use Dwarf::Session; | ||
| 5 | use Dwarf::Session::State::Cookie; | ||
| 6 | use Dwarf::Session::Store::DBI; | ||
| 7 | |||
| 8 | sub init { | ||
| 9 | my ($class, $c, $conf) = @_; | ||
| 10 | $conf ||= {}; | ||
| 11 | |||
| 12 | my $session_key = $conf->{session_key} || 'SESSION_ID'; | ||
| 13 | my $session_table = $conf->{session_table} || 'sessions'; | ||
| 14 | my $session_expires = $conf->{session_expires} || 3600; | ||
| 15 | my $session_clean_thres = $conf->{session_clean_thres} || 0; | ||
| 16 | my $param_name = $conf->{param_name} || 'session_id'; | ||
| 17 | my $cookie_path = $conf->{cookie_path} || '/'; | ||
| 18 | my $cookie_domain = $conf->{cookie_domain} || undef; | ||
| 19 | my $cookie_expires = $conf->{cookie_expires} || undef; | ||
| 20 | my $cookie_secure = $conf->{cookie_secure} // 0; | ||
| 21 | my $cookie_httponly = $conf->{cookie_httponly} // 1; | ||
| 22 | |||
| 23 | if ($cookie_expires) { | ||
| 24 | $cookie_expires += time; | ||
| 25 | } | ||
| 26 | |||
| 27 | add_method($c, session => sub { | ||
| 28 | my $self = shift; | ||
| 29 | $self->{'dwarf.session'} ||= Dwarf::Session->new( | ||
| 30 | state => Dwarf::Session::State::Cookie->new( | ||
| 31 | param_name => $param_name, | ||
| 32 | name => $session_key, | ||
| 33 | path => $cookie_path, | ||
| 34 | domain => $cookie_domain, | ||
| 35 | expires => $cookie_expires, | ||
| 36 | secure => $cookie_secure, | ||
| 37 | httponly => $cookie_httponly, | ||
| 38 | ), | ||
| 39 | store => Dwarf::Session::Store::DBI->new( | ||
| 40 | dbh => $self->dbh, | ||
| 41 | sid_table => $session_table, | ||
| 42 | expires => $session_expires, | ||
| 43 | clean_thres => $session_clean_thres, | ||
| 44 | ), | ||
| 45 | request => $self->request, | ||
| 46 | ); | ||
| 47 | }); | ||
| 48 | |||
| 49 | add_method($c, refresh_session => sub { | ||
| 50 | my ($self) = @_; | ||
| 51 | my $session = $self->session; | ||
| 52 | $session->regenerate_session_id(1); | ||
| 53 | }); | ||
| 54 | |||
| 55 | add_method($c, delete_session => sub { | ||
| 56 | my $self = shift; | ||
| 57 | if (my $session = delete $self->{'dwarf.session'}) { | ||
| 58 | $session->store->delete($session->session_id); | ||
| 59 | } | ||
| 60 | }); | ||
| 61 | |||
| 62 | add_method($c, store_params_on_session => sub { | ||
| 63 | my $self = shift; | ||
| 64 | }); | ||
| 65 | |||
| 66 | add_method($c, restore_params_from_session => sub { | ||
| 67 | my $self = shift; | ||
| 68 | }); | ||
| 69 | |||
| 70 | $c->add_trigger('AFTER_DISPATCH' => sub { | ||
| 71 | my ($self, $res) = @_; | ||
| 72 | if (my $session = $self->{'dwarf.session'}) { | ||
| 73 | if (ref($session->store) eq 'Dwarf::Session::Store::DBI') { | ||
| 74 | $session->store->cleanup; # Expire したセッションの掃除 | ||
| 75 | } | ||
| 76 | $session->response_filter($res); | ||
| 77 | $session->finalize(); | ||
| 78 | |||
| 79 | $res->header(P3P => 'CP="UNI CUR OUR"'); # IE のクッキー対策 | ||
| 80 | } | ||
| 81 | }); | ||
| 82 | } | ||
| 83 | |||
| 84 | 1; |