| 1 | =pod | ||
| 2 | |||
| 3 | =begin classdoc | ||
| 4 | |||
| 5 | |||
| 6 | Abstract base class for Session classes. | ||
| 7 | Provides an interface definition for | ||
| 8 | a single threads::shared session context container. | ||
| 9 | Loosely based on the <a href='http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html'> | ||
| 10 | Java Servlets</a> <a href='http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.html'> | ||
| 11 | HttpSession</a> class. | ||
| 12 | <p> | ||
| 13 | <b>Note:</b> This implementation does not provide interfaces for user | ||
| 14 | authorization/authentication. The intent is to outsource such functionality | ||
| 15 | to the application level, which can populate and retrieve attributes | ||
| 16 | as needed, and apply any encryption or privileges in an application specific | ||
| 17 | manner. Future releases may provide stock objects providing common auth&auth | ||
| 18 | functionality. | ||
| 19 | <p> | ||
| 20 | <b>Note2:</b> The default implementation does not support Set-Cookie2/Cookie2 | ||
| 21 | HTTP headers. | ||
| 22 | <p> | ||
| 23 | Copyright© 2006-2008, Dean Arnold, Presicient Corp., USA<br> | ||
| 24 | All rights reserved. | ||
| 25 | <p> | ||
| 26 | Licensed under the Academic Free License version 3.0, as specified in the | ||
| 27 | License.txt file included in this software package, or at | ||
| 28 | <a href='http://www.opensource.org/licenses/afl-3.0.php'>OpenSource.org</a>. | ||
| 29 | |||
| 30 | @author D. Arnold | ||
| 31 | @since 2006-08-21 | ||
| 32 | @self $self | ||
| 33 | @see <a href='http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.html'>HttpSession</a> | ||
| 34 | @see <a href='http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html'>Java Servlets</a> | ||
| 35 | |||
| 36 | |||
| 37 | =end classdoc | ||
| 38 | |||
| 39 | =cut | ||
| 40 | package HTTP::Daemon::Threaded::Session; | ||
| 41 | |||
| 42 | use threads; | ||
| 43 | use threads::shared; | ||
| 44 | use Time::HiRes qw(time); | ||
| 45 | |||
| 46 | use strict; | ||
| 47 | use warnings; | ||
| 48 | |||
| 49 | our $VERSION = '0.91'; | ||
| 50 | |||
| 51 | our $id_gen : shared = 0; | ||
| 52 | |||
| 53 | =pod | ||
| 54 | |||
| 55 | =begin classdoc | ||
| 56 | |||
| 57 | Constructor. Uses input ID, Inactivity timeout, cookie "dough" | ||
| 58 | and expiration timestamp to create a new session context. | ||
| 59 | |||
| 60 | @param $cache <i>(required)</i> parent SessionCache object | ||
| 61 | @param $id <i>(optional)</i> unique identifier for the session; default is generated from | ||
| 62 | an internal integer generator | ||
| 63 | @param $timeout <i>(optional)</i> max inactivity timeout; default is 10 minutes | ||
| 64 | @param $dough <i>(optional)</i> any information to be included in the session cookie; | ||
| 65 | the $id will be prepended to this information | ||
| 66 | @param $expires <i>(optional)</i> RFC1123 formatted cookie expiration date string, or 'Never'; default | ||
| 67 | is single session (nonpersistent) | ||
| 68 | |||
| 69 | @return HTTP::Response object | ||
| 70 | |||
| 71 | |||
| 72 | =end classdoc | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub new { | ||
| 77 | my ($class, $cache, $id, $timeout, $dough, $expires) = @_; | ||
| 78 | unless (defined($id)) { | ||
| 79 | # | ||
| 80 | # if none provided, create our own | ||
| 81 | # | ||
| 82 | lock($id_gen); | ||
| 83 | $id_gen = int(time()) | ||
| 84 | unless $id_gen; | ||
| 85 | $id = ++$id_gen; | ||
| 86 | } | ||
| 87 | |||
| 88 | my $cookie = (defined($dough) ? "Session=$id;$dough" : "Session=$id"); | ||
| 89 | $cookie .= ";Expires=$expires" if $expires; | ||
| 90 | my %attrs : shared = (); | ||
| 91 | my %self : shared = ( | ||
| 92 | _created => time(), | ||
| 93 | _id => $id, | ||
| 94 | _attributes => \%attrs, | ||
| 95 | _max_inactive => $timeout || 600, | ||
| 96 | _cookie_sent => undef, | ||
| 97 | _cache => $cache, | ||
| 98 | _last_access => time(), | ||
| 99 | _expires => $expires, | ||
| 100 | _cookie => $cookie | ||
| 101 | ); | ||
| 102 | return bless \%self, $class; | ||
| 103 | } | ||
| 104 | |||
| 105 | =pod | ||
| 106 | |||
| 107 | =begin classdoc | ||
| 108 | |||
| 109 | Constructor. Uses input identifier to load a session context | ||
| 110 | from persistent storage. | ||
| 111 | <p> | ||
| 112 | <b>NOTE:</b> this default implementation simply returns undef, | ||
| 113 | as it does not use persistent storage | ||
| 114 | |||
| 115 | @param $id unique identifier for the session to be recovered | ||
| 116 | @param $cache parent SessionCache object | ||
| 117 | |||
| 118 | @return undef if the session cannot be recovered, otherwise, the | ||
| 119 | HTTP::Daemon::Threaded::Session object | ||
| 120 | |||
| 121 | |||
| 122 | =end classdoc | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub open { | ||
| 127 | my ($class, $id, $cache) = @_; | ||
| 128 | return undef; | ||
| 129 | } | ||
| 130 | |||
| 131 | =pod | ||
| 132 | |||
| 133 | =begin classdoc | ||
| 134 | |||
| 135 | Closes this session and removes it from the parent SessionCache. | ||
| 136 | May cause any underlying persistent session store to delete its | ||
| 137 | version of the session. | ||
| 138 | |||
| 139 | @return undef (to optimize clearing any containers) | ||
| 140 | |||
| 141 | |||
| 142 | =end classdoc | ||
| 143 | |||
| 144 | =cut | ||
| 145 | sub close { | ||
| 146 | my $self = shift; | ||
| 147 | lock(%$self); | ||
| 148 | $self->{_cache}->removeSession($self->{_id}); | ||
| 149 | delete $self->{_cache}; | ||
| 150 | return undef; | ||
| 151 | } | ||
| 152 | |||
| 153 | =pod | ||
| 154 | |||
| 155 | =begin classdoc | ||
| 156 | |||
| 157 | Returns the object bound with the specified name. | ||
| 158 | |||
| 159 | @param $name name of attribute to retrieve | ||
| 160 | |||
| 161 | @return the value of the named attribute (if any) | ||
| 162 | |||
| 163 | |||
| 164 | =end classdoc | ||
| 165 | |||
| 166 | =cut | ||
| 167 | sub getAttribute { | ||
| 168 | my ($self, $name) = @_; | ||
| 169 | lock(%$self); | ||
| 170 | return $self->{_attributes}{$name}; | ||
| 171 | } | ||
| 172 | |||
| 173 | =pod | ||
| 174 | |||
| 175 | =begin classdoc | ||
| 176 | |||
| 177 | Returns the list of attributes bound to this session. | ||
| 178 | |||
| 179 | @returnlist the alphabetically sorted list of attribute names | ||
| 180 | |||
| 181 | |||
| 182 | =end classdoc | ||
| 183 | |||
| 184 | =cut | ||
| 185 | sub getAttributeNames { | ||
| 186 | my $self = shift; | ||
| 187 | lock(%$self); | ||
| 188 | return sort keys %{$self->{_attributes}}; | ||
| 189 | } | ||
| 190 | |||
| 191 | =pod | ||
| 192 | |||
| 193 | =begin classdoc | ||
| 194 | |||
| 195 | Removes the object bound to the specified name from this session. | ||
| 196 | |||
| 197 | @param $name name of attribute to remove | ||
| 198 | @return the value of the removed attribute | ||
| 199 | |||
| 200 | |||
| 201 | =end classdoc | ||
| 202 | |||
| 203 | =cut | ||
| 204 | sub removeAttribute { | ||
| 205 | my ($self, $name) = @_; | ||
| 206 | my ($cookie, $key, $val) = ('', '', ''); | ||
| 207 | |||
| 208 | lock(%$self); | ||
| 209 | my $attrs = $self->{_attributes}; | ||
| 210 | my $expires = $self->{_expires}; | ||
| 211 | my $old = delete $attrs->{$name}; | ||
| 212 | $cookie .= "$key=$val;" | ||
| 213 | while (($key, $val) = each %$attrs); | ||
| 214 | |||
| 215 | chop $cookie; | ||
| 216 | $cookie .= ";Expires=$expires" if $expires; | ||
| 217 | $self->{_cookie} = $cookie; | ||
| 218 | return $old; | ||
| 219 | } | ||
| 220 | |||
| 221 | =pod | ||
| 222 | |||
| 223 | =begin classdoc | ||
| 224 | |||
| 225 | Set an attribute on this session, using the name specified. | ||
| 226 | <p> | ||
| 227 | <b>NOTE:</b> Since Session objects are threads shared, their | ||
| 228 | attributes hash is also threads::shared, which means that | ||
| 229 | any non-scalar values to be assigned to an attribute must | ||
| 230 | also be threads::shared. | ||
| 231 | |||
| 232 | @param $name name of attribute to set | ||
| 233 | @param $value value of the attribute to set | ||
| 234 | @return the Session object | ||
| 235 | |||
| 236 | |||
| 237 | =end classdoc | ||
| 238 | |||
| 239 | =cut | ||
| 240 | sub setAttribute { | ||
| 241 | my ($self, $name, $value) = @_; | ||
| 242 | my ($cookie, $key, $val) = ('', '', ''); | ||
| 243 | |||
| 244 | lock(%$self); | ||
| 245 | my $attrs = $self->{_attributes}; | ||
| 246 | my $expires = $self->{_expires}; | ||
| 247 | $attrs->{$name} = $value; | ||
| 248 | $cookie .= "$key=$val;" | ||
| 249 | while (($key, $val) = each %$attrs); | ||
| 250 | |||
| 251 | chop $cookie; | ||
| 252 | $cookie .= ";Expires=$expires" if $expires; | ||
| 253 | $self->{_cookie} = $cookie; | ||
| 254 | |||
| 255 | return $self; | ||
| 256 | } | ||
| 257 | |||
| 258 | =pod | ||
| 259 | |||
| 260 | =begin classdoc | ||
| 261 | |||
| 262 | Get expiration on this session. | ||
| 263 | |||
| 264 | @return the RFC1123 formatted expiration (if any) | ||
| 265 | |||
| 266 | |||
| 267 | =end classdoc | ||
| 268 | |||
| 269 | =cut | ||
| 270 | sub getExpiration { | ||
| 271 | my $self = shift; | ||
| 272 | |||
| 273 | lock(%$self); | ||
| 274 | return $self->{_expires}; | ||
| 275 | } | ||
| 276 | |||
| 277 | =pod | ||
| 278 | |||
| 279 | =begin classdoc | ||
| 280 | |||
| 281 | Set expiration on this session. If no date is given, | ||
| 282 | then the cookie is expired. | ||
| 283 | |||
| 284 | @param $expires RFC1123 expiration timestamp | ||
| 285 | |||
| 286 | @return the Session object | ||
| 287 | |||
| 288 | |||
| 289 | =end classdoc | ||
| 290 | |||
| 291 | =cut | ||
| 292 | sub setExpiration { | ||
| 293 | my ($self, $expires) = @_; | ||
| 294 | |||
| 295 | lock(%$self); | ||
| 296 | $self->{_expires} = $expires; | ||
| 297 | my $cookie = $self->{_cookie}; | ||
| 298 | $cookie=~s/;Expires=.*$//; | ||
| 299 | $cookie .= ";Expires=$expires" | ||
| 300 | if $expires; | ||
| 301 | $self->{_cookie} = $cookie; | ||
| 302 | |||
| 303 | return $self; | ||
| 304 | } | ||
| 305 | |||
| 306 | =pod | ||
| 307 | |||
| 308 | =begin classdoc | ||
| 309 | |||
| 310 | Returns the creation time of this session as a fractional number. | ||
| 311 | <p> | ||
| 312 | <b>NOTE:</b> As a read operation on a static value, no lock is required. | ||
| 313 | |||
| 314 | @return the creation time of the session as a floating point number of seconds | ||
| 315 | since the epoch. | ||
| 316 | |||
| 317 | |||
| 318 | =end classdoc | ||
| 319 | |||
| 320 | =cut | ||
| 321 | sub getCreationTime { | ||
| 322 | my $self = shift; | ||
| 323 | return $self->{_created}; | ||
| 324 | } | ||
| 325 | |||
| 326 | =pod | ||
| 327 | |||
| 328 | =begin classdoc | ||
| 329 | |||
| 330 | Returns the unique ID time of this session | ||
| 331 | <p> | ||
| 332 | <b>NOTE:</b> As a read operation on a static value, no lock is required. | ||
| 333 | |||
| 334 | @return the ID string | ||
| 335 | |||
| 336 | |||
| 337 | =end classdoc | ||
| 338 | |||
| 339 | =cut | ||
| 340 | sub getID { | ||
| 341 | my $self = shift; | ||
| 342 | return $self->{_id}; | ||
| 343 | } | ||
| 344 | |||
| 345 | =pod | ||
| 346 | |||
| 347 | =begin classdoc | ||
| 348 | |||
| 349 | Returns the last time the client sent a request for this session. | ||
| 350 | |||
| 351 | @return the time as a floating point number of seconds sinc the epoch | ||
| 352 | |||
| 353 | |||
| 354 | =end classdoc | ||
| 355 | |||
| 356 | =cut | ||
| 357 | sub getLastAccessedTime { | ||
| 358 | my $self = shift; | ||
| 359 | lock(%$self); | ||
| 360 | return $self->{_last_access}; | ||
| 361 | } | ||
| 362 | |||
| 363 | =pod | ||
| 364 | |||
| 365 | =begin classdoc | ||
| 366 | |||
| 367 | Set the last time the client sent a request for this session. | ||
| 368 | |||
| 369 | @return this Session object | ||
| 370 | |||
| 371 | |||
| 372 | =end classdoc | ||
| 373 | |||
| 374 | =cut | ||
| 375 | sub setLastAccessedTime { | ||
| 376 | my $self = shift; | ||
| 377 | lock(%$self); | ||
| 378 | $self->{_last_access} = time(); | ||
| 379 | return $self; | ||
| 380 | } | ||
| 381 | |||
| 382 | =pod | ||
| 383 | |||
| 384 | =begin classdoc | ||
| 385 | |||
| 386 | Returns the last time the client sent a request for this session. | ||
| 387 | |||
| 388 | @return the time as a floating point number of seconds sinc the epoch | ||
| 389 | |||
| 390 | |||
| 391 | =end classdoc | ||
| 392 | |||
| 393 | =cut | ||
| 394 | sub getMaxInactiveInterval { | ||
| 395 | my $self = shift; | ||
| 396 | lock(%$self); | ||
| 397 | return $self->{_max_inactive}; | ||
| 398 | } | ||
| 399 | |||
| 400 | =pod | ||
| 401 | |||
| 402 | =begin classdoc | ||
| 403 | |||
| 404 | Set the max inactivity interval, in seconds, for this session. | ||
| 405 | |||
| 406 | @param $timeout inactivity interval in seconds | ||
| 407 | @return this Session object | ||
| 408 | |||
| 409 | |||
| 410 | =end classdoc | ||
| 411 | |||
| 412 | =cut | ||
| 413 | sub setMaxInactiveInterval { | ||
| 414 | my $self = shift; | ||
| 415 | lock(%$self); | ||
| 416 | $self->{_max_inactive} = $_[0]; | ||
| 417 | return $self; | ||
| 418 | } | ||
| 419 | |||
| 420 | # | ||
| 421 | # should we also apply the cookie expiration here ? | ||
| 422 | # | ||
| 423 | =pod | ||
| 424 | |||
| 425 | =begin classdoc | ||
| 426 | |||
| 427 | Has this session timed out ? | ||
| 428 | |||
| 429 | @return boolean true if the inactivity timeout has expired; else false | ||
| 430 | |||
| 431 | |||
| 432 | =end classdoc | ||
| 433 | |||
| 434 | =cut | ||
| 435 | sub isInactive { | ||
| 436 | my ($self, $idle) = @_; | ||
| 437 | lock(%$self); | ||
| 438 | return ($self->{_max_inactive} < (time() - $idle)); | ||
| 439 | } | ||
| 440 | |||
| 441 | =pod | ||
| 442 | |||
| 443 | =begin classdoc | ||
| 444 | |||
| 445 | Returns true if the cookie for the session has not yet been sent to the client. | ||
| 446 | Note that this session instance may have been revivified from persistent storage, | ||
| 447 | and has not yet sent its cookie to the client, but the client has previously | ||
| 448 | stored the cookie, in which case this method should return false. | ||
| 449 | |||
| 450 | @return 1 if the cookie has not been sent, undef otherwise | ||
| 451 | |||
| 452 | |||
| 453 | =end classdoc | ||
| 454 | |||
| 455 | =cut | ||
| 456 | sub isNew { | ||
| 457 | my $self = shift; | ||
| 458 | lock(%$self); | ||
| 459 | return !$self->{_cookie_sent}; | ||
| 460 | } | ||
| 461 | |||
| 462 | =pod | ||
| 463 | |||
| 464 | =begin classdoc | ||
| 465 | |||
| 466 | Called to indicate that the cookie has been sent to the client. | ||
| 467 | <p> | ||
| 468 | <b>Note:</b> Subclasses which implement <code>open()</code> should | ||
| 469 | already set this flag when a session is successfully recovered. | ||
| 470 | |||
| 471 | @return this Session object | ||
| 472 | |||
| 473 | |||
| 474 | =end classdoc | ||
| 475 | |||
| 476 | =cut | ||
| 477 | sub cookieSent { | ||
| 478 | my $self = shift; | ||
| 479 | lock(%$self); | ||
| 480 | $self->{_cookie_sent} = 1; | ||
| 481 | return $self; | ||
| 482 | } | ||
| 483 | |||
| 484 | =pod | ||
| 485 | |||
| 486 | =begin classdoc | ||
| 487 | |||
| 488 | Returns this session's cookie | ||
| 489 | |||
| 490 | @return the cookie string, including the unique session ID, and any dough and/or | ||
| 491 | expiration date supplied when the session was created | ||
| 492 | |||
| 493 | |||
| 494 | =end classdoc | ||
| 495 | |||
| 496 | =cut | ||
| 497 | sub getCookie { | ||
| 498 | my $self = shift; | ||
| 499 | lock(%$self); | ||
| 500 | return $self->{_cookie}; | ||
| 501 | } | ||
| 502 | |||
| 503 | 1; |