File Coverage

blib/lib/Apache/Session/Generate/AutoIncrement.pm
Criterion Covered Total %
statement 26 30 86.6
branch 7 10 70.0
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 38 47 80.8


line stmt bran cond sub pod time code
1             #############################################################################
2             #
3             # Apache::Session::Generate::AutoIncrement;
4             # Generates session identifier tokens using a monotonically increasing number
5             # The current number is stored in a file.
6             # Copyright(c) 2001 Pascal Fleury (fleury@users.sourceforge.net)
7             # Distribute under the same terms as Perl itself.
8             #
9             ############################################################################
10              
11             package Apache::Session::Generate::AutoIncrement;
12              
13 1     1   1068 use strict;
  1         3  
  1         51  
14 1     1   6 use vars qw($VERSION);
  1         1  
  1         88  
15 1     1   1638 use File::CounterFile;
  1         130454  
  1         508  
16              
17             $VERSION = "0.9";
18              
19             our $DEFAULT_LENGTH = 10;
20              
21             sub generate {
22 5     5 0 432 my $session = shift;
23            
24 5 50       19 if (! exists $session->{args}->{CounterFile}) {
25 0         0 require Carp;
26 0         0 Carp::croak "You need to specify a 'CounterFile' argument to the session.";
27             }
28 5         14 my $initcount = $session->{args}->{CounterInitial};
29 5         37 my $syncfile = new File::CounterFile($session->{args}->{CounterFile}, $initcount);
30 5         1680 my $count = $syncfile->inc();
31            
32 5         1175 my $length = $DEFAULT_LENGTH;
33 5 100       19 if (exists $session->{args}->{IDLength}) {
34 2         5 $length = $session->{args}->{IDLength};
35             }
36            
37 5         17 my $cntstr = '0' x $length . "$count";
38 5         35 $session->{data}->{_session_id} = substr($cntstr, length($cntstr)-$length);
39            
40              
41             }
42              
43             sub validate {
44             #This routine checks to ensure that the session ID is in the form
45             #we expect. This must be called before we start diddling around
46             #in the database or the disk.
47              
48 3     3 0 62 my $session = shift;
49             # Check content
50 3 50       24 if ($session->{data}->{_session_id} !~ /^[0-9]+$/) {
51 0         0 die;
52             }
53             #check length
54 3         7 my $length = $DEFAULT_LENGTH;
55 3 100       12 if (exists $session->{args}->{IDLength}) {
56 1         86 $length = $session->{args}->{IDLength};
57             }
58 3 50       12 if ( length($session->{data}->{_session_id})!=$length ) {
59 0         0 die;
60             }
61 3         10 1; # This is for the test routines
62             }
63              
64             1;
65              
66             =pod
67              
68             =head1 NAME
69              
70             Apache::Session::Generate::AutoIncrement - Use monotonically increasing IDs
71              
72             =head1 SYNOPSIS
73              
74             use Apache::Session::Generate::AutoIncrement;
75            
76             $id = Apache::Session::Generate::AutoIncrement::generate();
77              
78             =head1 DESCRIPTION
79              
80             This module fulfills the ID generation interface of Apache::Session. The
81             IDs are generated using a monotonically increasing counter value. This
82             counter is file-based using the File::Counter module, so it is probably
83             not very efficient and fast.
84              
85             This module can also examine session IDs to ensure that they are, indeed,
86             session ID numbers and not evil attacks. The reader is encouraged to
87             consider the effect of bogus session ID numbers in a system which uses
88             these ID numbers to access disks and databases.
89              
90             This modules takes two arguments in the usual Apache::Session style.
91             The first argument is IDLength, and the value, between 0 and 32, tells
92             this modulevwhere to truncate the session ID. Without this argument,
93             the session ID will be 10 digits.
94             The second argument is CounterFile, which is the file in which the
95             counted value will reside. This parameter is given directly to the
96             File::Counter module.
97              
98             =head1 BUGS
99              
100             This module relies on File::CounterFile, so the same limitations
101             as that module do apply here (about locking the file).
102              
103             =head1 AUTHOR
104              
105             This module was written by Pascal Fleury
106             but heavily based on Jeffrey William Baker's module.
107              
108             =head1 COPYRIGHT
109              
110             Copyright(c) 2001-2002 by Pascal Fleury (fleury@users.sourceforge.net)
111             Distribute under the same terms as Perl itself.
112              
113              
114             =head1 SEE ALSO
115              
116             L, L