HTTPServer/Session.pm

filter: Statementstmt 100% 83 / 83 Branchbran 63.3% 19 / 30 Conditioncond 58.3% 7 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 16 / 16 Podpod 83.3% 5 / 6 totaltotal 85.7% 132 / 154 CC 20 SCAR 11.6
CC
20
Cov
87%
new
6.2
_load
5.1
Set
3.1
CRAP 3.2
« HTTPServer/CaptureSTDOUT.pm ↑ Summary
1 ##############################################################################
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Library General Public
5 # License as published by the Free Software Foundation; either
6 # version 2 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Library General Public License for more details.
12 #
13 # You should have received a copy of the GNU Library General Public
14 # License along with this library; if not, write to the
15 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 # Boston, MA 02111-1307, USA.
17 #
18 # Copyright (C) 2003-2005 Ryan Eatmon
19 #
20 ##############################################################################
21 package Net::HTTPServer::Session;
22  
23 =head1 NAME
24  
25 Net::HTTPServer::Session
26  
27 =head1 SYNOPSIS
28  
29 Net::HTTPServer::Session handles server side client sessions
30
31 =head1 DESCRIPTION
32  
33 Net::HTTPServer::Session provides a server side data store for client
34 specific sessions. It uses a cookie stored on the browser to tell
35 the server which session to restore to the user. This is modelled
36 after the PHP session concept. The session is valid for 4 hours from
37 the last time the cookie was sent.
38  
39 =head1 EXAMPLES
40  
41 sub pageHandler
42 {
43 my $request = shift;
44
45 my $session = $request->Session();
46  
47 my $response = $request->Response();
48  
49 # Logout
50 $session->Destroy() if $request->Env("logout");
51  
52 $response->Print("<html><head><title>Hi there</title></head><body>");
53
54 # If the user specified a username on the URL, then save it.
55 if ($request->Env("username"))
56 {
57 $session->Set("username",$request->Env("username"));
58 }
59
60 # If there is a saved username, then use it.
61 if ($session->Get("username"))
62 {
63 $response->Print("Hello, ",$session->Get("username"),"!");
64 }
65 else
66 {
67 $response->Print("Hello, stranger!");
68 }
69  
70 $response->Print("</body></html>");
71  
72 return $response;
73 }
74  
75 The above would behave as follows:
76  
77 http://server/page - Hello, stranger!
78 http://server/page?username=Bob - Hello, Bob!
79 http://server/page - Hello, Bob!
80 http://server/page?username=Fred - Hello, Fred!
81 http://server/page - Hello, Fred!
82 http://server/page?logout=1 - Hello, stranger!
83 http://server/page - Hello, stranger!
84  
85 =head1 METHODS
86  
87 =head2 Delete(var)
88  
89 Delete the specified variable from the session.
90  
91 =head2 Destroy()
92  
93 Destroy the session. The server side data is deleted and the cookie
94 will be expired.
95  
96 =head2 Exists(var)
97  
98 Returns if the specified variable exists in the sesion.
99  
100 =head2 Get(var)
101  
102 Return the value of the specified variable from the session if it
103 exists, undef otherwise.
104  
105 =head2 Set(var,value)
106  
107 Store the specified value (scalar or reference to any Perl data
108 structure) in the session.
109  
110 =head1 AUTHOR
111  
112 Ryan Eatmon
113  
114 =head1 COPYRIGHT
115  
116 Copyright (c) 2003-2005 Ryan Eatmon <reatmon@mail.com>. All rights
117 reserved. This program is free software; you can redistribute it
118 and/or modify it under the same terms as Perl itself.
119  
120 =cut
121
122 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 4 ▶use vars qw ( $VERSION $SESSION_COUNT %data );
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 4 $VERSION = "1.0.3";
129  
130 4 $SESSION_COUNT = 0;
131  
132 sub new
133 3 ▶{
Subroutine new: called CC=6 SCAR=18.3
Pod undocumented
134 3 my $proto = shift;
135 3 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
136 3 my $self = { };
137
138 3 bless($self, $proto);
139  
140 3 my (%args) = @_;
141
142 3 $self->{ARGS} = \%args;
143  
144 3 $self->{KEY} = $self->_arg("key",undef);
145 3 $self->{SERVER} = $self->_arg("server",undef);
146  
147 3 ▶ return unless $self->{SERVER}->{CFG}->{SESSIONS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"SERVER"}{"CFG"}{"SESSIONS"} 0 3 3
148  
149 $self->{KEY} = $self->_genkey()
150 if (!defined($self->{KEY}) ||
151 ($self->{KEY} eq "") ||
152 3 ▶ ($self->{KEY} =~ /\//)
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"KEY"} or $self->{"KEY"} eq "" or $self->{"KEY"} =~ m[/] 1 2 3
Condition66%4/6
not defined $self->{"KEY"} or $self->{"KEY"} eq "" or $self->{"KEY"} =~ m[/]
l!l&&r!l&&!r
102
not defined $self->{"KEY"} or $self->{"KEY"} eq ""
l!l&&r!l&&!r
102
MC/DC33%1/3
not defined $self->{"KEY"} or $self->{"KEY"} eq "" or $self->{"KEY"} =~ m[/]
not defined $self->{"KEY"} $self->{"KEY"} eq "" $self->{"KEY"} =~ m[/]
Truth table50%2/4
not(A) or B or C
A= not defined $self->{"KEY"}B= $self->{"KEY"} eq ""C= $self->{"KEY"} =~ m[/]
ABCresult
1XX1
01X1
0011
0000
153 );
154  
155 3 $self->{FILE} = $self->{SERVER}->{CFG}->{DATADIR}."/".$self->{KEY};
156
157 #XXX Check that server (Net::HTTPServer object) is defined
158
159 3 $self->{VALID} = 1;
160 3 $self->{DATA} = {};
161 3 $self->_load();
162  
163 3 return $self;
164 }
165  
166  
167 sub Delete
168 1 ▶{
Subroutine Delete: called CC=2 SCAR=7.0
Pod documented
169 1 my $self = shift;
170 1 my $var = shift;
171  
172 1 ▶ return unless $self->Exists($var);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->Exists($var) 0 1 1
173 1 delete($self->{DATA}->{$var});
174 }
175  
176  
177 sub Destroy
178 1 ▶{
Subroutine Destroy: called CC=1 SCAR=0.0
Pod documented
179 1 my $self = shift;
180  
181 1 $self->{VALID} = 0;
182 }
183  
184  
185 sub Exists
186 25 ▶{
Subroutine Exists: called CC=2 SCAR=7.0
Pod documented
187 25 my $self = shift;
188 25 my $var = shift;
189  
190 25 ▶ return unless $self->_valid();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_valid 0 25 25
191 25 return exists($self->{DATA}->{$var});
192 }
193  
194  
195 sub Get
196 11 ▶{
Subroutine Get: called CC=2 SCAR=7.0
Pod documented
197 11 my $self = shift;
198 11 my $var = shift;
199  
200 11 ▶ return unless $self->Exists($var);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->Exists($var) 0 11 11
201 11 return $self->{DATA}->{$var};
202 }
203  
204  
205 sub Set
206 3 ▶{
Subroutine Set: called CC=3 SCAR=11.3
Pod documented
207 3 my $self = shift;
208 3 my $var = shift;
209 3 my $value = shift;
210  
211 3 ▶ return unless $self->_valid();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_valid 0 3 3
212 3 ▶ $self->{DATA}->{$var} = $value if defined($value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 3 0 3
213 }
214  
215  
216 ###############################################################################
217 #
218 # _arg - if the arg exists then use it, else use the default.
219 #
220 ###############################################################################
221 sub _arg
222 6 ▶{
Subroutine _arg: called CC=2 SCAR=6.9
223 6 my $self = shift;
224 6 my $arg = shift;
225 6 my $default = shift;
226  
227 6 ▶ return (exists($self->{ARGS}->{$arg}) ? $self->{ARGS}->{$arg} : $default);
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'ARGS'}{$arg} ? : 5 1 6
228 }
229  
230  
231 sub _genkey
232 1 ▶{
Subroutine _genkey: called CC=2 SCAR=7.0
233 1 my $self = shift;
234  
235 1 $SESSION_COUNT++;
236 1 my $key = "NetHTTPServerSession".$SESSION_COUNT.$$.time;
237  
238 1 ▶ if ($Net::HTTPServer::DigestMD5 == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::HTTPServer::DigestMD5 == 1) 1 0 1
239 {
240 1 $key = Digest::MD5::md5_hex($key);
241 }
242  
243 1 return $key;
244 }
245  
246  
247 sub _key
248 6 ▶{
Subroutine _key: called CC=1 SCAR=0.0
249 6 my $self = shift;
250  
251 6 return $self->{KEY};
252 }
253  
254  
255 sub _load
256 3 ▶{
Subroutine _load: called CC=5 SCAR=16.3
257 3 my $self = shift;
258  
259 3 ▶ return unless $self->_valid();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_valid 0 3 3
260  
261 3 ▶ return unless (-f $self->{FILE});
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->{"FILE"} 1 2 3
262  
263 2 undef(%data);
264
265 2 my $data;
266 2 ▶ open(DATA,$self->{FILE}) || return;
Branch: 1 branch
BranchTrueFalseTotal
open DATA, $self->{"FILE"} or return 2 0 2
267 2 read(DATA, $data, (-s DATA));
268 2 close(DATA);
269  
270 2 eval $data;
271
272 2 ▶ if (!$@)
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) 2 0 2
273 {
274 2 $self->{DATA} = \%data;
275 }
276 }
277  
278  
279 sub _save
280 2 ▶{
Subroutine _save: called CC=3 SCAR=11.0
281 2 my $self = shift;
282  
283 2 ▶ if (!$self->_valid())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_valid) 1 1 2
284 {
285 1 ▶ unlink($self->{FILE}) if (-f $self->{FILE});
Branch: 1 branch
BranchTrueFalseTotal
if -f $self->{"FILE"} 0 1 1
286 1 return;
287 }
288  
289 1 my $dumper = new Data::Dumper([$self->{DATA}],["*data"]);
290 1 $dumper->Purity(1);
291  
292 1 open(DATA,">".$self->{FILE});
293 1 print DATA $dumper->Dump();
294 1 close(DATA);
295 }
296  
297  
298 sub _valid
299 35 ▶{
Subroutine _valid: called CC=2 SCAR=7.1
300 35 my $self = shift;
301  
302 35 ▶ return (exists($self->{VALID}) && ($self->{VALID} == 1));
Condition66%2/3
exists $self->{'VALID'} && $self->{'VALID'} == 1
!ll&&!rl&&r
0233
MC/DC50%1/2
exists $self->{'VALID'} && $self->{'VALID'} == 1
exists $self->{'VALID'} $self->{'VALID'} == 1
Truth table66%2/3
A && B
A= exists $self->{'VALID'}B= $self->{'VALID'} == 1
ABresult
0X0
100
111
303 }
304  
305  
306 4 1;
307  
« HTTPServer/CaptureSTDOUT.pm ↑ Summary