Reactor/Sessions.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 17 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 151 CC 12 SCAR 22.8
CC
12
Cov
0%
create
30.0
compose_key_from_id
12.0
load
6.0
CRAP 9.8
1 ##############################################################################
2 ##
3 ## Web::Reactor application machinery
4 ## 2013 (c) Vladi Belperchinov-Shabanski "Cade"
5 ## <cade@bis.bg> <cade@biscom.net> <cade@cpan.org>
6 ##
7 ## LICENSE: GPLv2
8 ##
9 ##############################################################################
10 package Web::Reactor::Sessions;
11 use strict;
12 use Carp;
13  
14 sub new
15 {
16 my $class = shift;
17 my %env = @_;
18
19 $class = ref( $class ) || $class;
20 my $self = {
21 'ENV' => \%env,
22 };
23 bless $self, $class;
24
25 return $self;
26 }
27  
28 ##############################################################################
29 ##
30 ## public interface methods, should be used via Reactor object, see specs
31 ##
32  
33 # create new session id of given type and length and allocate storage for it
34 # args:
35 # type -- alphanumeric type name (selects storage only)
36 # len -- session id length
37 # returns:
38 # new session id or undef when failed
39 sub create
40 {
41 my $self = shift;
42 my $type = uc shift;
43 my $len = shift || 128;
44  
45 die "Web::Reactor::Sess::create: invalid type, expected ALPHANUMERIC, got [$type]" unless $type =~ /^[A-Z0-9]+$/;
46 die "Web::Reactor::Sess::create: invalid length, expected len > 0, got [$len]" unless $len > 0;
47  
48 my $env = $self->_renv();
49  
50 my $id;
51 my $t = time();
52 my $to = $env->{ 'SESS_CREATE_TIMEOUT' } || 5; # seconds
53 while(4)
54 {
55 $id = $self->create_id( $len );
56  
57 my @key = $self->compose_key_from_id( $type, $id );
58 last if $self->_storage_create( @key );
59  
60 if ( time() - $t > $to )
61 {
62 $id = undef;
63 # FIXME: report error
64 # re_log( "error: cannot create new session: timeout, key[@key]" );
65 return undef;
66 }
67 }
68
69 return $id;
70 };
71  
72 # loads session data from the storage
73 # args:
74 # type -- alphanumeric type name (selects storage only)
75 # id -- session id
76 # returns:
77 # hashref of session data or undef if error
78 sub load
79 {
80 my $self = shift;
81 my $type = shift;
82 my $id = shift;
83  
84 return 0 unless $id;
85  
86 my @key = $self->compose_key_from_id( $type, $id );
87  
88 return $self->_storage_load( @key );
89 }
90  
91 # saves session data to the storage
92 # args:
93 # type -- alphanumeric type name (selects storage only)
94 # id -- session id
95 # data -- hashref of session data
96 # returns:
97 # 1 if successful, undef if failed
98 sub save
99 {
100 my $self = shift;
101 my $type = shift;
102 my $id = shift;
103 my $data = shift;
104  
105 return 0 unless $id;
106  
107 my @key = $self->compose_key_from_id( $type, $id );
108  
109 return $self->_storage_save( $data, @key );
110 }
111  
112 # deletes a session from the storage
113 # args:
114 # type -- alphanumeric type name (selects storage only)
115 # id -- session id
116 # returns:
117 # 1 if successful, undef if failed
118 sub delete
119 {
120 my $self = shift;
121 my $type = shift;
122 my $id = shift;
123  
124 return 0 unless $id;
125  
126 my @key = $self->compose_key_from_id( $type, $id );
127  
128 return $self->_storage_delete( @key );
129 }
130  
131 # checks if session exists in the storage
132 # args:
133 # type -- alphanumeric type name (selects storage only)
134 # id -- session id
135 # returns:
136 # 1 if exists, undef if not
137 sub exists
138 {
139 my $self = shift;
140 my $type = shift;
141 my $id = shift;
142  
143 return 0 unless $id;
144  
145 my @key = $self->compose_key_from_id( $type, $id );
146  
147 return $self->_storage_exists( @key );
148 }
149  
150  
151 ##############################################################################
152 ##
153 ## methods, which must be implemented in sub-classes!
154 ## they are all internal to this package
155 ##
156  
157 # create new session storage indexed by the given key components
158 # it is important that this function try to do atomic create in the storage.
159 # it must (and expected to) fail if session with the same key exists and never
160 # overwrite existing session storage!
161 # args:
162 # @key (i.e. @_) -- key components array, usually filled with 2 or 3 elements
163 # when 2: TYPE, SESS7c87y32d78asa4
164 # when 3: TYPE, SESS187yc5v87thccf, SESS2jdhfh74yc3847
165 # usually it is simple to $key = join '.' @_;
166 #
167 # returns:
168 # 1 if successful or 0 or undef if not possible or session id already exists
169 sub _storage_create { die "Web::Reactor::Sess::*::_storage_create() is not implemented!"; }
170  
171 # loads session data from the storage
172 # args:
173 # @key (i.e. @_) -- key components array, example: $key = join '.' @_;
174 # returns:
175 # hashref of session data or undef if error
176 sub _storage_load { die "Web::Reactor::Sess::*::_storage_load() is not implemented!"; }
177  
178 # saves session data to the storage
179 # args:
180 # data -- hashref of session data
181 # @key (i.e. @_) -- key components array, example: $key = join '.' @_;
182 # returns:
183 # 1 if successful, undef if failed
184 sub _storage_save { die "Web::Reactor::Sess::*::_storage_save() is not implemented!"; }
185  
186 # checks if session exists in the storage
187 # args:
188 # @key (i.e. @_) -- key components array, example: $key = join '.' @_;
189 # returns:
190 # 1 if exists, undef if not
191 sub _storage_exists { die "Web::Reactor::Sess::*::_storage_exists() is not implemented!"; }
192  
193 ##############################################################################
194 ##
195 ##
196 ##
197  
198 # return string with new session id with given LEN argument or default length
199 # args:
200 # len -- session id length (optional, 0 or undef for default length)
201 # letters -- letters to be used for session id creation (optional)
202 # must be non-whitespace characters string with no duplicates
203 # returns:
204 # id -- session id string
205 sub create_id
206 {
207 my $self = shift;
208 my $env = $self->_renv();
209
210 my $len = shift() || $env->{ 'SESS_LENGTH' } || 128;
211 my $let = shift() || $env->{ 'SESS_LETTERS' } || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
212  
213 my $l = length( $let );
214 my $id;
215 # FIXME: move this line to function in Utils.pm
216 $id .= substr( $let, int(rand() * $l), 1 ) for ( 1 .. $len );
217 return $id;
218 };
219  
220 sub compose_key_from_id
221 {
222 my $self = shift;
223 my $type = uc shift;
224 my $id = shift;
225  
226 confess "Web::Reactor::Sess::compose_key_from_id: invalid type, expected ALPHANUMERIC" unless $type =~ /^[A-Z0-9]+$/;
227
228 my @key;
229
230 push @key, $type;
231 push @key, $self->get_user_sid() if $type ne 'USER'; # FIXME: not only! UPDs also! (user permanent data)
232 push @key, $id;
233
234 return @key;
235 }
236  
237 ##############################################################################
238 ##
239 ## helpers
240 ##
241  
242 sub get_user_sid
243 {
244 my $self = shift;
245 my $user_sid = $self->{ 'REO_REACTOR' }{ 'SESSIONS' }{ 'SID' }{ 'USER' };
246  
247 confess "missing USER SESSION" unless $user_sid;
248  
249 return $user_sid;
250 }
251  
252 # return ENV hash reference from the reactor
253 sub _renv
254 {
255 my $self = shift;
256
257 }
258  
259 ##############################################################################
260 1;
261 ###EOF########################################################################