MxScreen/Session.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 110 CC 6 SCAR 19.3
CC
6
Cov
0%
_allocate_context
20.0
restore
12.0
make
2.0
CRAP 6.9
1 # -*- Mode: perl -*-
2 #
3 # $Id: Session.pm,v 0.1 2001/04/22 17:57:03 ram Exp $
4 #
5 # Copyright (c) 1998-2001, Raphael Manfredi
6 # Copyright (c) 2000-2001, Christophe Dehaudt
7 #
8 # You may redistribute only under the terms of the Artistic License,
9 # as specified in the README file that comes with the distribution.
10 #
11 # HISTORY
12 # $Log: Session.pm,v $
13 # Revision 0.1 2001/04/22 17:57:03 ram
14 # Baseline for first Alpha release.
15 #
16 # $EndLog$
17 #
18  
19 use strict;
20  
21 package CGI::MxScreen::Session;
22  
23 use Carp::Datum;
24 use Log::Agent;
25 use Getargs::Long;
26  
27 require Digest::MD5;
28 require CGI;
29 require CGI::MxScreen::Config;
30 require CGI::MxScreen::Tie::Read_Checked;
31  
32 use CGI::MxScreen::Constant;
33  
34 #
35 # ->make
36 #
37 # Creation routine.
38 #
39 # Create a new session with identified serializer and medium.
40 #
41 sub make {
42 my $self = bless {}, shift;
43 my ($serializer, $medium) = xgetargs(@_,
44 -serializer => 'CGI::MxScreen::Serializer',
45 -medium => 'CGI::MxScreen::Session::Medium',
46 );
47  
48 $self->{medium} = $medium;
49 $medium->set_serializer($serializer);
50  
51 return DVAL $self;
52 }
53  
54 #
55 # Attribute access
56 #
57  
58 sub id { $_[0]->{id} }
59 sub context { $_[0]->{context} }
60 sub medium { $_[0]->{medium} }
61  
62 #
63 # ->_allocate_context
64 #
65 # Allocate new session context.
66 #
67 sub _allocate_context {
68 DFEATURE my $f_;
69 my $self = shift;
70  
71 DREQUIRE !defined $self->id, "no session yet";
72 DREQUIRE CGI::MxScreen::Config::is_configured();
73  
74 #
75 # Allocate context
76 #
77  
78 my %vars;
79 tie %vars, "CGI::MxScreen::Tie::Read_Checked"
80 if $CGI::MxScreen::cf::mx_check_vars;
81 my $context = [{}, \%vars, [], [], {}];
82  
83 #
84 # Before using an opaque session ID, try to use a human-readable one.
85 #
86  
87 my $id = CGI::remote_host() . "-" . int(time) . "-$$";
88 my $medium = $self->medium;
89 $id = $medium->allocate_id unless $medium->is_available($id);
90  
91 logdie "unable to allocate session ID" unless defined $id;
92  
93 $self->{context} = $context;
94 $self->{id} = $id;
95  
96 return DVOID;
97 }
98  
99 #
100 # ->restore
101 #
102 # Restore context from storing medium.
103 # Returns context reference on success, undef on failure.
104 #
105 # NB: a reference to the context is kept internally in `context', i.e. it
106 # is not necessary to give it on subsequent save().
107 #
108 sub restore {
109 DFEATURE my $f_;
110 my $self = shift;
111 my $medium = $self->medium;
112  
113 DREQUIRE !defined $self->id, "no session retrieved yet";
114  
115 #
116 # Read the session ID from the CGI parameters, which is medium-dependent.
117 #
118 # For instance, the ID could be stored in a "session_id" hidden parameter
119 # which would be a file name. If the context is inlined in the parameters,
120 # there may not be any ID defined at all, but session_id() must return
121 # something defined.
122 #
123 # If no ID is returned, then we're starting a new session.
124 #
125  
126 my $id = $medium->session_id();
127 unless (defined $id) {
128 $self->_allocate_context;
129 return DVAL $self->context; # Done
130 }
131  
132 #
133 # Attempt to retrieve context, using $id as the key.
134 #
135  
136 my $context = $medium->retrieve($id);
137 return DVAL undef unless defined $context;
138  
139 $self->{id} = $id;
140 $self->{context} = $context;
141  
142 return DVAL $context; # OK
143 }
144  
145 #
146 # ->save
147 #
148 # Save context onto medium.
149 #
150 # Returns string containing the hidden CGI parameters that need to be
151 # propagated to the browser.
152 #
153 sub save {
154 DFEATURE my $f_;
155 my $self = shift;
156 my $id = $self->id;
157 my $context = $self->context;
158 my $medium = $self->medium;
159  
160 DREQUIRE defined $id, "valid session ID";
161 DREQUIRE ref $context, "valid context";
162  
163 my $params = $medium->store($id, $context);
164 DASSERT ref $params eq 'HASH',
165 "store on $medium returned a HASH ref $params";
166  
167 my $hidden = '';
168 foreach my $key (sort keys %$params) {
169 $hidden .= CGI::hidden($key, $params->{$key}) . "\n";
170 }
171 chop $hidden; # trailing \n
172  
173 return DVAL $hidden;
174 }
175  
176 1;
177  
178 =head1 NAME
179  
180 CGI::MxScreen::Session - Handle session save and restore
181  
182 =head1 SYNOPSIS
183  
184 # Not meant to be used directly
185  
186 =head1 DESCRIPTION
187  
188 This class handles the context save and restore operations, based
189 on a serializer and a saving medium. Both can be configured
190 dynamically, as explained in L<CGI::MxScreen::Config>.
191  
192 =head1 AUTHOR
193  
194 Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>>
195  
196 =head1 SEE ALSO
197  
198 CGI::MxScreen::Serializer(3), CGI::MxScreen::Session::Medium(3).
199  
200 =cut
201