blib/lib/AxKit/XSP/Minisession.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 148 CC 11 SCAR 26.0
CC
11
Cov
0%
get_session
42.0
parse_end
12.0
parse_start
12.0
CRAP 13.4
1 # $Id: /local/CPAN/AxKit-XSP-Minisession/lib/AxKit/XSP/Minisession.pm 1430 2008-03-08T21:44:59.461106Z claco $
2 package AxKit::XSP::Minisession;
3 use strict;
4 use warnings;
5 use vars qw/$VERSION $NS/;
6 use base 'Apache::AxKit::Language::XSP';
7  
8 $VERSION = "1.10000";
9 $NS = 'http://squash.oucs.ox.ac.uk/XSP/Minisession';
10  
11 sub start_document { 'use Apache::Log;' }
12  
13 sub parse_char {
14 my ($e, $text) = @_;
15 $text =~ s/^\s*//;
16 $text =~ s/\s*$//;
17  
18 return '' unless $text;
19  
20 $text = Apache::AxKit::Language::XSP::makeSingleQuoted($text);
21 return ". $text";
22 }
23  
24 sub parse_end {
25 my ($e, $tag, %attr) = @_;
26 $tag =~ s/-/_/g;
27 if ($tag eq "set_value") {
28 } elsif ($tag eq "get_value") {
29 $e->manage_text(0);
30 $e->append_to_script("\";AxKit::XSP::Minisession::Backend::get_value(\$r,
31 \$flange);\n");
32 $e->end_expr();
33 return "";
34 } else { die "Unknown tag $tag\n" }
35 }
36  
37 sub parse_start {
38 my ($e, $tag, %attr) = @_;
39 $tag =~ s/-/_/g;
40 if ($tag eq "get_value") {
41 $e->start_expr($tag);
42 $e->manage_text(1);
43 return 'my $flange = "';
44 } elsif ($tag eq "set_value") {
45 $e->manage_text(0);
46 my $buildup = '';
47 $buildup .= "\$r->log->debug('calling
48 put_value');AxKit::XSP::Minisession::Backend::put_value(\$r, ".
49 Apache::AxKit::Language::XSP::makeSingleQuoted($_)
50 .", ". Apache::AxKit::Language::XSP::makeSingleQuoted($attr{$_})
51 .");\n"
52 for keys %attr;
53 return $buildup;
54 }
55 }
56  
57 package AxKit::XSP::Minisession::Backend;
58 use Apache::Log;
59 use Apache::Session::File;
60 use Apache::Cookie;
61 use strict;
62 use warnings;
63  
64 sub get_session {
65 my $r = shift;
66 my $sid;
67  
68 if (!($sid = $r->pnotes("SESSION_ID"))) {
69 # Has it come from a cookie?
70 my %jar = Apache::Cookie->new($r)->parse;
71 if (exists $jar{sessionid}) {
72 $sid = $jar{sessionid}->value();
73 $r->log->debug("Got the session id ($sid) from a cookie");
74 }
75 }
76  
77 $sid = undef unless $sid; # Clear it, as 0 is a valid sid.
78 my %session = ();
79 my $new = !(defined $sid);
80  
81 $r->log->debug(defined $sid ? "Retrieving session $sid"
82 : "Creating a new session");
83  
84 tie %session, 'Apache::Session::File', $sid, {
85 Directory => $r->dir_config("MinisessionDir") || "/tmp/sessions",
86 LockDirectory => $r->dir_config("MinisessionLockDir") ||
87 "/tmp/sessionlock",
88 };
89  
90 $r->pnotes("SESSION_ID", $sid);
91 $r->log->debug("Session contains @{[%session]}");
92 if ($new) {
93 put_session($r, \%session);
94 }
95 return \%session;
96 }
97  
98 sub put_session {
99 my ($r, $sess_ref) = @_;
100 $r->log->debug("Returning session ".$sess_ref->{_session_id}." to the cookie
101 jar");
102 my $cookie = Apache::Cookie->new($r,
103 -name => "sessionid",
104 -value => $sess_ref->{_session_id},
105 -path => "/"
106 );
107 $cookie->bake();
108 $r->pnotes("SESSION_ID", $sess_ref->{_session_id});
109 }
110  
111 sub get_value {
112 my $r = shift;
113 my $key = shift;
114 my $sref = get_session($r);
115 $r->log->debug("get_value saw session ".$sref->{_session_id});
116 $r->log->debug("retrieving $key -> $sref->{$key}");
117 my $v = $sref->{$key};
118 # Make damn sure the locks are released.
119 my $obj = tied %$sref;
120 untie %$sref;
121 $obj->DESTROY;
122 return $v;
123 }
124  
125 sub put_value {
126 my $r = shift;
127 my $sref = get_session($r);
128 while (@_) {
129 my $key = shift;
130 my $val = shift;
131 $r->log->debug("set_value saw session ".$sref->{_session_id}. ", setting
132 $key to $val");
133 $sref->{$key} = $val;
134 }
135  
136 put_session($r, $sref);
137 # Make damn sure the locks are released.
138 my $obj = tied %$sref;
139 untie %$sref;
140 $obj->DESTROY;
141 return undef; # To stop xsp leakage.
142 }
143  
144  
145 1;
146  
147 =head1 NAME
148  
149 AxKit::XSP::Minisession - Yet Another Session Handling Library
150  
151 =head1 SYNOPSIS
152  
153 In your config file:
154  
155 PerlSetVar MinisessionDir /tmp/sessions
156 PerlSetVar MinisessionLockDir /tmp/sessionlock
157 AxAddXSPTaglib +AxKit::XSP::Minisession
158  
159 In your XSP code:
160  
161 <xsp:page
162 xmlns:session="http://squash.oucs.ox.ac.uk/XSP/Minisession"
163 >
164  
165 <session:set-value username="simon"/>
166 <session:get-value>username</s:get-value>
167  
168 In your Perl code:
169  
170 die "Already logged in" if
171 AxKit::XSP::Minisession::Backend::get_value($r, "username");
172 AxKit::XSP::Minisession::Backend::set_value($r, "username", $username);
173  
174 =head1 DESCRIPTION
175  
176 This is a very simple session library which sets state via a cookie and
177 uses C<Apache::Session::File> to store sessions in files on the
178 file system. If you need anything more complex than that, this module
179 isn't for you.
180  
181 The guts of the module are the two functions C<get_value> and
182 C<set_value> in the C<::Backend> module. The first parameter to these
183 should be an C<Apache::Request> object, and the second a hash key.
184  
185 These functions are wrapped by the C<set-value> and C<get-value> tags
186 from XSP.
187  
188 And that's it. I said it was very simple.
189  
190 =head1 TAG REFERENCE
191  
192 =head2 set-value
193  
194 Assigns the given name/value pair to the currenct session.
195  
196 =head2 get-value
197  
198 Returns the currennt session value for the itemd requested.
199  
200 =head1 AUTHOR
201  
202 Christopher H. Laco
203 CPAN ID: CLACO
204 claco@chrislaco.com
205 http://today.icantfocus.com/blog/
206  
207 =head1 AUTHOR EMERITUS
208  
209 The original version was created by Simon Cozens.