/usr/share/perl5/MasonX/Request/WithMultiSession.pm is in libmasonx-request-withapachesession-perl 0.30-3.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | package MasonX::Request::WithMultiSession;
use strict;
use Digest::SHA ();
use Time::HiRes;
use base qw(MasonX::Request::WithApacheSession);
use HTML::Mason::Exceptions ( abbr => [ qw( param_error error ) ] );
use Params::Validate qw( validate SCALAR );
Params::Validate::validation_options( on_fail => sub { param_error( join '', @_ ) } );
__PACKAGE__->valid_params
( multi_session_args_param =>
{ type => SCALAR,
default => 'sub_session_id',
descr => 'The parameter name which contains the sub-session id',
},
multi_session_expire =>
{ type => Params::Validate::SCALAR,
default => undef,
descr => 'How long a sub-session stays valid',
},
);
sub session
{
my $self = shift;
return $self->parent_request->session(@_) if $self->is_subrequest;
my %p = @_;
my %super_p = exists $p{session_id} ? ( session_id => $p{session_id} ) : ();
my $session = $self->SUPER::session(%super_p);
my %sub_session_p =
exists $p{sub_session_id} ? ( sub_session_id => $p{sub_session_id} ) : ();
my $id = $self->sub_session_id(%sub_session_p);
if ( $p{clone} || $p{new} )
{
# forces creation of a new id
delete $self->{sub_session_id};
my $new_id = $self->_make_new_sub_session_id;
if ( $p{clone} )
{
# shallow copy of old session
$session->{sub_sessions}{$new_id} = { %{ $session->{sub_sessions}{$id} } };
}
$id = $new_id;
}
$session->{sub_session_ids}{$id} = int(time);
return $session->{sub_sessions}{$id};
}
sub sub_session_id
{
my $self = shift;
my %p = validate( @_,
{ sub_session_id =>
{ type => SCALAR,
optional => 1,
},
} );
unless ( exists $self->{sub_session_id} )
{
my $args = $self->request_args;
my $args_key = $self->{multi_session_args_param};
my $session = $self->SUPER::session;
if ( exists $p{sub_session_id} )
{
unless ( exists $session->{sub_session_ids}{ $p{sub_session_id} } )
{
$session->{sub_sessions}{ $p{sub_session_id} } = {};
}
$self->{sub_session_id} = $p{sub_session_id};
}
elsif ( exists $args->{$args_key} &&
exists $session->{sub_session_ids}{ $args->{$args_key} } )
{
$self->{sub_session_id} = $args->{$args_key};
}
else
{
$self->_make_new_sub_session_id;
}
}
return $self->{sub_session_id};
}
sub _make_new_sub_session_id
{
my $self = shift;
my $session = $self->SUPER::session;
my $new_id;
do
{
# using Time::HiRes means that we get times with very high
# floating point resolutions (to 10 or 11 decimal places), so
# this is a good seed for a hashing algorithm
$new_id = Digest::SHA::sha1_hex( time() . {} . rand() . $$ );
} while ( exists $session->{sub_session_ids}{$new_id} );
$session->{sub_sessions}{$new_id} = {};
$self->{sub_session_id} = $new_id;
return $new_id;
}
sub delete_sub_session
{
my $self = shift;
my $session = $self->SUPER::session;
my %p = validate( @_,
{ sub_session_id =>
{ type => SCALAR,
optional => 1,
},
} );
my $sub_id = $p{sub_session_id} ? $p{sub_session_id} : delete $self->{sub_session_id};
delete $session->{sub_sessions}{$sub_id};
delete $session->{sub_session_ids}{$sub_id};
}
sub delete_session
{
my $self = shift;
$self->SUPER::delete_session;
delete $self->{sub_session_id};
}
sub DESTROY
{
my $self = shift;
return unless defined $self->{multi_session_expire};
my $session = $self->SUPER::session;
my $cutoff = int(time) - $self->{multi_session_expire};
foreach my $id ( keys %{ $session->{sub_session_ids} } )
{
if ( $session->{sub_session_ids}{$id} < $cutoff )
{
delete $session->{sub_sessions}{$id};
delete $session->{sub_session_ids}{$id};
}
}
}
1;
__END__
=head1 NAME
MasonX::Request::WithMultiSession - Multiple sub-sessions within one "parent" session
=head1 SYNOPSIS
PerlSetVar MasonRequestClass MasonX::Request::WithMultiSession
=head1 DESCRIPTION
This module subclasses C<MasonX::Request::WithApacheSession> in order
to allow multiple "sub-sessions" to exist within one parent session.
This can be quite useful for a web app where you want to allow the
user to open multiple windows, each with a different session, but
session ids are stored in a cookie.
Like C<MasonX::Request::WithApacheSession>, sub-sessions are shared
between a request and any subrequests it creates.
=head1 METHODS
This class has an interface quite similar to that of
C<MasonX::Request::WithApacheSession>.
=over 4
=item * session
The primary interface to this class is through the C<session()>
method. When this method is called without any parameters, the module
looks for an existing sub-session specified by the sub-session id
argument parameter (which can be in a query string or POST). This
value can be overridden by explicitly passing a "sub_session_id"
parameter.
If this parameter is found, an existing sub-session is returned. If
this parameter is not found, a new sub-session is created.
If the C<session()> method is called as C<< session( clone => 1 ) >>
then a new sub-session will be created, and its contents will be the
same as that of the current sub-session. This is a shallow copy of
the old session hash, so objects and references are shared between
them.
If C<< session( new => 1 ) >> is called, then a new, empty,
sub-session is created.
You can specify the main session id to use via the "session_id"
parameter.
=item * sub_session_id
This method returns the currently active sub-session's id. Use this
method to put this id into URL parameters, forms, etc. as needed.
If given a "sub_session_id" parameter, it will set the current
sub-session id.
=item * delete_sub_session
By default, this simply defaults the current sub-session. You can
pass a "sub_session_id" parameter to delete a specific session.
=back
=head2 Parameters
This module takes two parameters besides those inherited from
C<MasonX::Request::WithApacheSession>:
=over 4
=item * multi_session_args_param / MultiSessionArgsParam
This parameter can be used to specify which parameter contains the
sub-session id. By default, the module will look for a parameter
called "sub_session_id".
=item * multi_session_expire / MultiSessionExpire
This parameter specifies the number of seconds after a sub-session is
accessed until it is purged. If not specified, then sub-sessions are
never purged.
Sub-sessions expiration is checked when the request object goes out of
scope.
=back
=head1 USAGE
You will need to manually set the sub-session id argument parameter
for each request. The easiest way to do this is to make sure that all
URLs contain the sub-session id. This can be done by using a C<<
<%filter> >> block in a top-level autohandler (although this won't
catch redirects), or by making sure all URLs are generated by a single
component/function.
=head1 SUPPORT
Bug reports and requests for help should be sent to the mason-users
list. See http://www.masonhq.com/resources/mailing_lists.html for
more details.
=head1 AUTHOR
Dave Rolsky, <autarch@urth.org>
Development funded by Marigold Technologies.
=head1 SEE ALSO
HTML::Mason
=cut
|