Neaf/X/Session/File.pm

filter: Statementstmt 95.7% 68 / 71 Branchbran 59.3% 19 / 32 Conditioncond 33.3% 5 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 15 / 15 Podpod 100% 9 / 9 totaltotal 78.1% 118 / 151 CC 22 SCAR 18.1
CC
22
Cov
78%
delete_session
14.9
atomic_read
9.9
atomic_write
5.3
CRAP 6.1
« Neaf/Request/Apache2.pm ↑ Summary Neaf/View/JS.pm »
1 package MVC::Neaf::X::Session::File;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '0.2901';
6  
7 =head1 NAME
8  
9 MVC::Neaf::X::Session::File - File-based sessions for Not Even A Framework.
10  
11 =head1 DESCRIPTION
12  
13 This module implements session storage, as described in
14 L<MVC::Neaf::X::Session>.
15  
16 It will store session data inside a single directory.
17 The file format is JSON but MAY change in the future.
18  
19 Uses flock() to avoid collisions.
20  
21 If session_ttl was specified, old session files will be deleted.
22  
23 B<NOTE> The file-locking MAY be prone to race conditions. If you want real secure
24 expiration, please specify expiration INSIDE the session, or use a database.
25  
26 =head1 SYNOPSIS
27  
28 use strict;
29 use warnings;
30 use MVC::Neaf;
31 use MVC::Neaf::X::Session::File;
32  
33 MVC::Neaf->set_session_engine(
34 engine => MVC::Neaf::X::Session::File->new( dir => $mydir )
35 );
36 # ... define your application here
37  
38 =head1 METHODS
39  
40 =cut
41  
42 2 ▶use Fcntl qw(:flock :seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use URI::Escape qw(uri_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 2 ▶use MVC::Neaf::Util qw(JSON encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use parent qw(MVC::Neaf::X::Session);
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 =head2 new( %options )
49  
50 Constructor. %options may include:
51  
52 =over
53  
54 =item * session_ttl - how long to store session data.
55  
56 =item * dir (required) - where to store files.
57  
58 =back
59  
60 =cut
61  
62 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
63 3 my $class = shift;
64 3 my $self = $class->SUPER::new( @_ );
65  
66 $self->my_croak( "dir option is mandatory" )
67 3 ▶ unless $self->{dir} and -d $self->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dir"} and -d $self->{"dir"} 1 2 3
Condition66%2/3
$self->{"dir"} and -d $self->{"dir"}
!ll&&!rl&&r
102
MC/DC50%1/2
$self->{"dir"} and -d $self->{"dir"}
$self->{"dir"} -d $self->{"dir"}
Truth table66%2/3
A and B
A= $self->{"dir"}B= -d $self->{"dir"}
ABresult
0X0
100
111
68  
69 2 return $self;
70 };
71  
72 =head2 save_session( $id, \%data )
73  
74 Save session data to a file.
75  
76 =cut
77  
78 2 ▶sub save_session {
Subroutine save_session: called CC=2 SCAR=7.0
Pod documented
79 2 my ($self, $id, $data) = @_;
80  
81 2 my $raw = $self->encode_content( $data );
82 2 my $expire = $self->atomic_write( $id, $raw );
83 2 ▶ $expire = $self->{session_ttl} ? $self->{session_ttl}+$expire : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'session_ttl'} ? : 2 0 2
84  
85 return {
86 2 id => $id,
87 expire => $expire,
88 };
89 };
90  
91 =head2 load_session( $id )
92  
93 Load session data from file.
94 Will DELETE session if session_ttl was specified and exceeded.
95  
96 =cut
97  
98 3 ▶sub load_session {
Subroutine load_session: called CC=2 SCAR=6.9
Pod documented
99 3 my ($self, $id) = @_;
100  
101 3 my ($raw, $expire) = $self->atomic_read( $id );
102 3 ▶ return $raw
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 1 2 3
103 ? { data => $self->decode_content( $raw ) }
104 : $raw;
105 };
106  
107 =head2 delete_session( $id )
108  
109 Remove a session, if such session is stored at all.
110  
111 =cut
112  
113 1 ▶sub delete_session {
Subroutine delete_session: called CC=5 SCAR=27.0
Pod documented
114 1 my ($self, $id) = @_;
115  
116 1 ▶ if (!unlink $self->get_file_name( $id )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $self->get_file_name($id)) 0 1 1
117 0 ▶ return 0 if $!{ENOENT} or $!{EPERM} && $^O eq 'MSWin32'; # missing = ok, locked+mswin = ok
Branch: 1 branch
BranchTrueFalseTotal
if $!{"ENOENT"} or $!{"EPERM"} and $^O eq "MSWin32" 0 0 0
Condition0%0/6
$!{"ENOENT"} or $!{"EPERM"} and $^O eq "MSWin32"
l!l&&r!l&&!r
000
$!{"EPERM"} and $^O eq "MSWin32"
!ll&&!rl&&r
000
MC/DC0%0/3
$!{"ENOENT"} or $!{"EPERM"} and $^O eq "MSWin32"
$!{"ENOENT"} $!{"EPERM"} $^O eq "MSWin32"
Truth table0%0/4
A or B and C
A= $!{"ENOENT"}B= $!{"EPERM"}C= $^O eq "MSWin32"
ABCresult
1XX1
0111
00X0
0100
118 0 $self->my_croak( "Failed to delete file ".($self->get_file_name( $id ))
119 .": $!" );
120 };
121 1 return 1;
122 };
123  
124 =head2 atomic_read( $id )
125  
126 Internal mechanism beyond load_file.
127  
128 =cut
129  
130 3 ▶sub atomic_read {
Subroutine atomic_read: called CC=9 SCAR=22.9
Pod documented
131 3 my ($self, $id) = @_;
132  
133 3 my $fname = $self->get_file_name( $id );
134 3 my $ok = open (my $fd, "<", $fname);
135 3 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 2 3
136 1 ▶ $!{ENOENT} and return; # file missing = OK
Branch: 1 branch
BranchTrueFalseTotal
$!{"ENOENT"} and return 1 0 1
137 0 $self->my_croak( "Failed to open(r) $fname: $!" );
138 };
139  
140 2 ▶ flock $fd, LOCK_SH
Branch: 1 branch
BranchTrueFalseTotal
flock $fd, 1 or $self->my_croak("Failed to lock(r) ${fname}: $!") 2 0 2
141 or $self->my_croak( "Failed to lock(r) $fname: $!" );
142  
143 # Remove stale sessions
144 2 my $ttl = $self->session_ttl;
145 2 ▶ my $expire = $ttl && [stat $fd]->[9] + $ttl;
Condition33%1/3
$ttl && [stat $fd]->[9] + $ttl
!ll&&!rl&&r
002
MC/DC0%0/2
$ttl && [stat $fd]->[9] + $ttl
$ttl [stat $fd]->[9] + $ttl
Truth table33%1/3
A && B
A= $ttlB= [stat $fd]->[9] + $ttl
ABresult
0X0
100
111
146 2 ▶ if ($expire && $expire < time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expire and $expire < time) 1 1 2
Condition66%2/3
$expire and $expire < time
!ll&&!rl&&r
011
MC/DC50%1/2
$expire and $expire < time
$expire $expire < time
Truth table66%2/3
A and B
A= $expireB= $expire < time
ABresult
0X0
100
111
147 1 ▶ close $fd if $^O eq 'MSWin32'; # won't delete under windows
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 1 1
148 1 $self->delete_session( $id );
149 1 return;
150 };
151  
152 1 local $/;
153 1 my $raw = <$fd>;
154 1 ▶ defined $raw
Branch: 1 branch
BranchTrueFalseTotal
defined $raw or $self->my_croak("Failed to read from ${fname}: $!") 1 0 1
155 or $self->my_croak( "Failed to read from $fname: $!" );
156  
157 1 close $fd; # ignore errors
158 1 return ($raw, $expire);
159 };
160  
161 =head2 atomic_write( $id, $content )
162  
163 Internal mechanism beyond save_session.
164  
165 =cut
166  
167 2 ▶sub atomic_write {
Subroutine atomic_write: called CC=5 SCAR=16.7
Pod documented
168 2 my ($self, $id, $raw) = @_;
169  
170 2 my $fname = $self->get_file_name( $id );
171 2 ▶ open (my $fd, ">>", $fname)
Branch: 1 branch
BranchTrueFalseTotal
open my $fd, ">>", $fname or $self->my_croak("Failed to open(w) ${fname}: $!") 2 0 2
172 or $self->my_croak( "Failed to open(w) $fname: $!" );
173  
174 2 ▶ flock $fd, LOCK_EX
Branch: 1 branch
BranchTrueFalseTotal
flock $fd, 2 or $self->my_croak("Failed to lock(w) ${fname}: $!") 2 0 2
175 or $self->my_croak( "Failed to lock(w) $fname: $!" );
176  
177 # Have exclusive permissions of fname, truncate & print
178 2 truncate $fd, 0;
179 2 seek $fd, 0, SEEK_SET;
180 2 ▶ print $fd $raw
Branch: 1 branch
BranchTrueFalseTotal
print $fd $raw or $self->my_croak("Failed to write to ${fname}: $!") 2 0 2
181 or $self->my_croak( "Failed to write to $fname: $!" );
182  
183 2 ▶ close $fd
Branch: 1 branch
BranchTrueFalseTotal
close $fd or $self->my_croak("Failed to sync(w) ${fname}: $!") 2 0 2
184 or $self->my_croak( "Failed to sync(w) $fname: $!" );
185  
186 2 return time;
187 };
188  
189 =head2 get_file_name( $id )
190  
191 Convert id into filename.
192  
193 =cut
194  
195 6 ▶sub get_file_name {
Subroutine get_file_name: called CC=2 SCAR=7.1
Pod documented
196 6 my ($self, $id) = @_;
197  
198 $self->my_croak("Storage directory not set")
199 6 ▶ unless $self->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dir"} 0 6 6
200 6 return join '/', $self->{dir}, uri_escape( $id );
201 };
202  
203 =head2 encode_content( $data )
204  
205 =head2 decode_content( $raw )
206  
207 Currently JSON is used.
208  
209 =cut
210  
211 2 ▶sub encode_content {
Subroutine encode_content: called CC=1 SCAR=0.0
Pod documented
212 2 my ($self, $data) = @_;
213  
214 2 return encode_json( $data );
215 };
216  
217 1 ▶sub decode_content {
Subroutine decode_content: called CC=1 SCAR=0.0
Pod documented
218 1 my ($self, $raw) = @_;
219  
220 1 return decode_json( $raw );
221 };
222  
223 =head1 LICENSE AND COPYRIGHT
224  
225 This module is part of L<MVC::Neaf> suite.
226  
227 Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>.
228  
229 This program is free software; you can redistribute it and/or modify it
230 under the terms of either: the GNU General Public License as published
231 by the Free Software Foundation; or the Artistic License.
232  
233 See L<http://dev.perl.org/licenses/> for more information.
234  
235 =cut
236  
237 2 1;
« Neaf/Request/Apache2.pm ↑ Summary Neaf/View/JS.pm »