Threaded/SessionCache.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 97 CC 9 SCAR 25.3
CC
9
Cov
0%
getSession
30.0
createSession
12.0
openSession
12.0
CRAP 12.5
1 =pod
2  
3 =begin classdoc
4  
5  
6 Abstract base class for SessionCache classes.
7 Provides an interface definition for
8 caching session context in a threads::shared container.
9 Provides a default implementation to create non-persistent
10 sessions. Also acts as a factory for Session objects.
11 <p>
12 <b>Note:</b> Applications needing to provide their own
13 session class implementations should subclass this class instead, and
14 provide an instance of the subclass as the HTTP::Daemon::Threaded
15 constructor's SessionCache parameter. Such subclass instances may then
16 manufacture session objects using their own Session
17 subclass.
18 <p>
19 Copyright&copy 2006-2008, Dean Arnold, Presicient Corp., USA<br>
20 All rights reserved.
21 <p>
22 Licensed under the Academic Free License version 3.0, as specified at
23 <a href='http://www.opensource.org/licenses/afl-3.0.php'>OpenSource.org</a>.
24  
25 @author D. Arnold
26 @since 2006-08-21
27 @self $self
28  
29  
30  
31 =end classdoc
32  
33 =cut
34 package HTTP::Daemon::Threaded::SessionCache;
35  
36 use threads;
37 use threads::shared;
38 use HTTP::Daemon::Threaded::Session;
39  
40 use strict;
41 use warnings;
42  
43 our $VERSION = '0.91';
44 =pod
45  
46 =begin classdoc
47  
48 Constructor. Creates threads::shared object to contain
49 any Session object that will be created.
50 <p>
51 Subclasses should extend this to open any session storage,
52 and possible pre-cache session contexts.
53  
54 @return HTTP::Daemon::Threaded::SessionCache object
55  
56  
57 =end classdoc
58  
59 =cut
60  
61 sub new {
62 my $class = shift;
63 my %sessions : shared = ();
64 my %self : shared = (
65 _cache => \%sessions
66 );
67 return bless \%self, $class;
68 }
69  
70 =pod
71  
72 =begin classdoc
73  
74 Add a new session to the cache.
75  
76 @param $session HTTP::Daemon::Threaded::Session object
77 @return the HTTP::Daemon::Threaded::Session object
78  
79  
80 =end classdoc
81  
82 =cut
83 sub addSession {
84 my ($self, $session) = @_;
85  
86 my $id = $session->getID();
87 my $cache = $self->{_cache};
88 $cache->{$id} = $session;
89 return $session;
90 }
91  
92 =pod
93  
94 =begin classdoc
95  
96 Remove a session from the cache.
97  
98 @param $id unique ID of session to be removed
99 @return the HTTP::Daemon::Threaded::SessionCache object
100  
101  
102 =end classdoc
103  
104 =cut
105 sub removeSession {
106 my ($self, $id) = @_;
107 my $cache = $self->{_cache};
108 delete $cache->{$id};
109 return $self;
110 }
111  
112 =pod
113  
114 =begin classdoc
115  
116 Get a session from the cache.
117  
118 @param $request HTTP::Request object for which a session is to be located
119 @return the HTTP::Daemon::Threaded::Session object if it exists; undef otherwise
120  
121  
122 =end classdoc
123  
124 =cut
125 sub getSession {
126 my ($self, $request) = @_;
127  
128 #
129 # strictly speaking, this can be multivalued...but we're
130 # not gonna deal with that for now
131 #
132 my $cookie = $request->header('Cookie');
133 # print STDERR "Sorry, no cookie\n" and
134 return undef
135 unless $cookie;
136 # print STDERR "Cookie is $cookie\n";
137 $cookie = ';' . $cookie; # normalize
138 return undef
139 unless ($cookie=~/;Session=([^;]+)/i);
140  
141 my $id = $1;
142 my $cache = $self->{_cache};
143  
144 # print STDERR "ID is $id\n";
145  
146 return $cache->{$id}->setLastAccessedTime()
147 if (exists $cache->{$id});
148  
149 # print STDERR "Session $id not found\n";
150 my $session = $self->openSession($cookie);
151 return $session ? $session->setLastAccessedTime() : undef;
152 }
153  
154 =pod
155  
156 =begin classdoc
157  
158 Create a new session and store in the cache.
159  
160 @param $id <i>(optional)</i> unique ID of session to retrieve; default is whatever
161 the session object class generates
162 @param $timeout <i>(optional)</i> max inactivity timeout; default is class specific
163 @param $dough <i>(optional)</i> any information to be included in the session's cookie;
164 the $id will be prepended to this information
165 @param $expires <i>(optional)</i> RFC1123 formatted cookie expiration date string, or
166 'Never'; default is single session (nonpersistent)
167  
168 @return undef if a session with the same ID already exists, or if the
169 session object cannot be created; otherwise,
170 the created HTTP::Daemon::Threaded::Session object
171  
172  
173 =end classdoc
174  
175 =cut
176 sub createSession {
177 my $self = shift;
178 my $id = shift;
179  
180 my $cache = $self->{_cache};
181 return undef
182 if defined($id) && (exists $cache->{$id});
183 my $session = HTTP::Daemon::Threaded::Session->new($self, $id, @_);
184 return undef
185 unless $session;
186 $id = $session->getID();
187 $cache->{$id} = $session;
188 $session->setLastAccessedTime();
189 return $session;
190 }
191  
192 =pod
193  
194 =begin classdoc
195  
196 Recover an existing session from persistent storage.
197  
198 @param $cookie <i>(required)</i> HTTP Cookie header containing unique ID of
199 session to retrieve
200  
201 @return undef if the session object cannot be recovered; otherwise,
202 the HTTP::Daemon::Threaded::Session object
203  
204  
205 =end classdoc
206  
207 =cut
208 sub openSession {
209 my ($self, $cookie) = @_;
210  
211 $cookie = ';' . $cookie; # normalize
212 my $id = ($cookie=~/;Session=([^;]+)/i);
213  
214 my $cache = $self->{_cache};
215 return $cache->{$id}
216 if exists $cache->{$id};
217  
218 my $session = HTTP::Daemon::Threaded::Session->open($id, $self);
219 return undef
220 unless $session;
221 $id = $session->getID();
222 $cache->{$id} = $session;
223 return $session;
224 }
225  
226 1;