| 1 | # -*- Mode: perl -*- | ||
| 2 | # | ||
| 3 | # $Id: Medium.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: Medium.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::Medium; | ||
| 22 | |||
| 23 | use Carp::Datum; | ||
| 24 | use Log::Agent; | ||
| 25 | |||
| 26 | # | ||
| 27 | # ->make | ||
| 28 | # | ||
| 29 | # Creation routine. | ||
| 30 | # | ||
| 31 | sub make { logconfess "deferred" } | ||
| 32 | |||
| 33 | # | ||
| 34 | # Attributes | ||
| 35 | # | ||
| 36 | |||
| 37 | # | ||
| 38 | # The `serializer' attribute is initialized by the session object, when | ||
| 39 | # it is created. | ||
| 40 | # | ||
| 41 | |||
| 42 | sub serializer { $_[0]->{serializer} } | ||
| 43 | sub set_serializer { $_[0]->{serializer} = $_[1] } | ||
| 44 | |||
| 45 | # | ||
| 46 | # Deferred features | ||
| 47 | # | ||
| 48 | |||
| 49 | # | ||
| 50 | # ->session_id | ||
| 51 | # | ||
| 52 | # Retrieve session ID from the CGI environment. | ||
| 53 | # This is completely medium-dependant. | ||
| 54 | # | ||
| 55 | # When no CGI parameter bering a session ID is found, this routine returns | ||
| 56 | # undef, as a signal to the caller that the is no existing session. | ||
| 57 | # | ||
| 58 | sub session_id { logconfess "deferred" } | ||
| 59 | |||
| 60 | # | ||
| 61 | # ->is_available | ||
| 62 | # | ||
| 63 | # Look whether ID is free to use as a session ID. | ||
| 64 | # If it is free, atomically reserve it. | ||
| 65 | # | ||
| 66 | # Returns true if ID is OK for use, false if it's not available. | ||
| 67 | # | ||
| 68 | sub is_available { logconfess "deferred" } | ||
| 69 | |||
| 70 | # | ||
| 71 | # ->retrieve | ||
| 72 | # | ||
| 73 | # Retrieve context by session ID. | ||
| 74 | # | ||
| 75 | sub retrieve { | ||
| 76 | DFEATURE my $f_; | ||
| 77 | my $self = shift; | ||
| 78 | my ($id) = @_; | ||
| 79 | |||
| 80 | logconfess "deferred"; | ||
| 81 | } | ||
| 82 | |||
| 83 | # | ||
| 84 | # ->store | ||
| 85 | # | ||
| 86 | # Store context with given session ID. | ||
| 87 | # | ||
| 88 | # Returns hash of (parameter => value) to be generated in the HTML | ||
| 89 | # to identify the session. | ||
| 90 | # | ||
| 91 | sub store { | ||
| 92 | DFEATURE my $f_; | ||
| 93 | my $self = shift; | ||
| 94 | my ($id, $context) = @_; | ||
| 95 | |||
| 96 | logconfess "deferred"; | ||
| 97 | } | ||
| 98 | |||
| 99 | # | ||
| 100 | # Other features | ||
| 101 | # | ||
| 102 | |||
| 103 | # | ||
| 104 | # ->allocate_id | ||
| 105 | # | ||
| 106 | # Allocate new session ID. | ||
| 107 | # Returns allocated ID, undef if none could be allocated. | ||
| 108 | # | ||
| 109 | sub allocate_id { | ||
| 110 | DFEATURE my $f_; | ||
| 111 | my $self = shift; | ||
| 112 | |||
| 113 | for (my $i = 0; $i < 100; $i++) { | ||
| 114 | my $id = $self->_generate_session_id; | ||
| 115 | return DVAL $id if $self->is_available($id); | ||
| 116 | } | ||
| 117 | |||
| 118 | logerr "could not find an unused session ID"; | ||
| 119 | return DVAL undef; | ||
| 120 | } | ||
| 121 | |||
| 122 | # | ||
| 123 | # ->_generate_session_id | ||
| 124 | # | ||
| 125 | # Generate a random session ID | ||
| 126 | # | ||
| 127 | sub _generate_session_id { | ||
| 128 | DFEATURE my $f_; | ||
| 129 | my $self = shift; | ||
| 130 | |||
| 131 | require Digest::MD5; | ||
| 132 | |||
| 133 | my $id = Digest::MD5::md5_hex(time() . {} . rand() . $$); | ||
| 134 | $id = Digest::MD5::md5_base64($id); | ||
| 135 | $id =~ tr|+/=|-_.|; | ||
| 136 | |||
| 137 | return DVAL $id; | ||
| 138 | } | ||
| 139 | |||
| 140 | 1; | ||
| 141 | |||
| 142 | =head1 NAME | ||
| 143 | |||
| 144 | CGI::MxScreen::Session::Medium - Abstract session saving medium | ||
| 145 | |||
| 146 | =head1 SYNOPSIS | ||
| 147 | |||
| 148 | # Not meant to be used directly | ||
| 149 | |||
| 150 | =head1 DESCRIPTION | ||
| 151 | |||
| 152 | This class is B<deferred>, and is meant to be inherited from by classes | ||
| 153 | implementing the various session media. | ||
| 154 | |||
| 155 | =head1 SUPPORTED MEDIA | ||
| 156 | |||
| 157 | The following session media are currently supported: | ||
| 158 | |||
| 159 | =over 4 | ||
| 160 | |||
| 161 | =item C<CGI::MxScreen::Session::Medium::Browser> | ||
| 162 | |||
| 163 | This saves the session within the browser, and therefore does not require | ||
| 164 | any storage on the server side. | ||
| 165 | See L<CGI::MxScreen::Session::Medium::Browser> for configuration details. | ||
| 166 | |||
| 167 | =item C<CGI::MxScreen::Session::Medium::File> | ||
| 168 | |||
| 169 | This saves the session within a file, on the server side. | ||
| 170 | See L<CGI::MxScreen::Session::Medium::File> for configuration details. | ||
| 171 | |||
| 172 | =item C<CGI::MxScreen::Session::Medium::Raw_File> | ||
| 173 | |||
| 174 | This saves the session within a file, on the server side, but bypasses | ||
| 175 | the C<CGI::MxScreen::Serializer> interface alltogether to make raw C<Storable> | ||
| 176 | calls, i.e. C<store()> and C<retrieve()>. | ||
| 177 | |||
| 178 | This is more efficient but prevents any compression (which a CPU intensive | ||
| 179 | task anyway). As often, we're sacrifying space and genericity for performance. | ||
| 180 | See L<CGI::MxScreen::Session::Medium::Raw_File> for configuration details. | ||
| 181 | |||
| 182 | =back | ||
| 183 | |||
| 184 | =head1 INTERFACE | ||
| 185 | |||
| 186 | This section is meant for developpers wishing to implement their own | ||
| 187 | session medium. You don't need to read it if you're only using | ||
| 188 | C<CGI::MxScreen>. | ||
| 189 | |||
| 190 | =head2 Deferred Features | ||
| 191 | |||
| 192 | These features need to be defined by heirs: | ||
| 193 | |||
| 194 | =over 4 | ||
| 195 | |||
| 196 | =item C<is_available> I<session_id> | ||
| 197 | |||
| 198 | Looks whether I<session_id> is a free session ID. If it is, atomically | ||
| 199 | reserve it. | ||
| 200 | |||
| 201 | Must return I<true> when the ID was reserved, I<false> if it is not | ||
| 202 | available. | ||
| 203 | |||
| 204 | =item C<make> | ||
| 205 | |||
| 206 | The creation routine, whose interface will necessarily be different | ||
| 207 | for every medium. | ||
| 208 | |||
| 209 | =item C<retrieve> I<session_id> | ||
| 210 | |||
| 211 | Retrieve context by session ID, and return a reference to it. | ||
| 212 | |||
| 213 | =item C<session_id> | ||
| 214 | |||
| 215 | Retrieve session ID from the CGI environment (probably in some hidden | ||
| 216 | parameter). If there's no session ID there, return C<undef>. | ||
| 217 | |||
| 218 | =item C<store> I<session_id>, I<context> | ||
| 219 | |||
| 220 | Store I<context> by session ID. | ||
| 221 | |||
| 222 | It returns a hash ref of (parameter => value) tuples to be generated | ||
| 223 | as hidden parameters to identify the session. | ||
| 224 | |||
| 225 | =back | ||
| 226 | |||
| 227 | =head2 Attributes | ||
| 228 | |||
| 229 | The only attribute is: | ||
| 230 | |||
| 231 | =over 4 | ||
| 232 | |||
| 233 | =item C<serializer> | ||
| 234 | |||
| 235 | The C<CGI::MxScreen::Serializer> object to use for serialization. | ||
| 236 | It is initialized by C<CGI::MxScreen::Session> with a C<set_serializer()> call. | ||
| 237 | |||
| 238 | =back | ||
| 239 | |||
| 240 | =head1 AUTHOR | ||
| 241 | |||
| 242 | Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>> | ||
| 243 | |||
| 244 | =head1 SEE ALSO | ||
| 245 | |||
| 246 | CGI::MxScreen::Session::Medium::Browser(3), | ||
| 247 | CGI::MxScreen::Session::Medium::File(3), | ||
| 248 | CGI::MxScreen::Session::Medium::Raw_File(3). | ||
| 249 | |||
| 250 | =cut | ||
| 251 |