Server.pm

filter: Statementstmt 16.7% 94 / 563 Branchbran 0.0% 0 / 202 Conditioncond 4.4% 5 / 112 MC/DCmcdc 0.0% 0 / 66 Subroutinesub 36.5% 30 / 82 Podpod 12.5% 1 / 8 totaltotal 12.5% 130 / 1033 CC 148 SCAR 48.3
CC
148
Cov
11%
invoke_payload
702.0
_process_request
210.0
_create_listener_socket
182.0
CRAP 125.8
↑ Summary Server/ModperlOperator.pm »
1 package Yote::Server;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Lock::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Yote;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use UUID::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 $VERSION = '1.27';
23  
24 1 our $DEBUG = 0;
25  
26 1 ▶sub new {
Subroutine new: called CC=6 SCAR=23.5
Pod undocumented
27 1 my( $pkg, $args ) = @_;
28 1 ▶ my $class = ref( $pkg ) || $pkg;
Condition33%1/3
ref $pkg || $pkg
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
29 my $server = bless {
30 args => $args || {},
31  
32 # the following are the args currently used
33 yote_root_dir => $args->{yote_root_dir},
34 yote_host => $args->{yote_host} || '127.0.0.1',
35 yote_port => $args->{yote_port} || 8881,
36 pids => [],
37 _locker => new Lock::Server( {
38 port => $args->{lock_port},
39 host => $args->{lock_host} || '127.0.0.1',
40 lock_attempt_timeout => $args->{lock_attempt_timeout},
41 lock_timeout => $args->{lock_timeout},
42 } ),
43 1 ▶ STORE => Yote::ServerStore->_new( { root => $args->{yote_root_dir} } ),
Condition50%4/8
$args || {}
l!l
10
$args->{'yote_host'} || '127.0.0.1'
l!l
01
$args->{'yote_port'} || 8881
l!l
10
$args->{'lock_host'} || '127.0.0.1'
l!l
01
MC/DC0%0/1
$args || {}
$args
MC/DC0%0/1
$args->{'yote_host'} || '127.0.0.1'
$args->{'yote_host'}
MC/DC0%0/1
$args->{'yote_port'} || 8881
$args->{'yote_port'}
MC/DC0%0/1
$args->{'lock_host'} || '127.0.0.1'
$args->{'lock_host'}
Truth table50%1/2
A
A= $args
Aresult
11
00
Truth table50%1/2
A
A= $args->{'yote_host'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'yote_port'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'lock_host'}
Aresult
11
00
44 }, $class;
45 0 $server->{STORE}{_locker} = $server->{_locker};
46 0 $server;
47 } #new
48  
49 0 ▶sub store {
Subroutine store: not called CC=1 SCAR=6.9
Pod undocumented
50 0 shift->{STORE};
51 }
52  
53 0 ▶sub load_options {
Subroutine load_options: not called CC=8 SCAR=42.8
Pod undocumented
54  
55 0 my( $yote_root_dir ) = @_;
56  
57 0 my $confile = "$yote_root_dir/yote.conf";
58  
59 #
60 # set up default options
61 #
62 0 my $options = {
63 yote_root_dir => $yote_root_dir,
64 yote_host => '127.0.0.1',
65 yote_port => 8881,
66 lock_port => 8004,
67 lock_host => '127.0.0.1',
68 lock_attempt_timeout => 12,
69 lock_timeout => 10,
70 use_ssl => 0,
71 SSL_cert_file => '',
72 SSL_key_file => '',
73 };
74  
75 #
76 # override base defaults with those from conf file
77 #
78 0 ▶ if( -f $confile && -r $confile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $confile and -r $confile) 0 0 0
Condition0%0/3
-f $confile and -r $confile
!ll&&!rl&&r
000
MC/DC0%0/2
-f $confile and -r $confile
-f $confile -r $confile
Truth table0%0/3
A and B
A= -f $confileB= -r $confile
ABresult
0X0
100
111
79 # TODO - create conf with defaults and make it part of the install
80 0 ▶ open( IN, "<$confile" ) or die "Unable to open config file $@ $!";
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<$confile" or die "Unable to open config file $@ $!" 0 0 0
81 0 ▶ while( <IN> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 0 0 0
82 0 chomp;
83 0 s/\#.*//;
84 0 ▶ if( /^\s*([^=\s]+)\s*=\s*([^\s].*)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*([^=\s]+)\s*=\s*([^\s].*)\s*$/) 0 0 0
85 0 ▶ if( defined $options->{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{$1}) { } 0 0 0
86 0 ▶ $options->{$1} = $2 if defined $options->{$1};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{$1} 0 0 0
87 } else {
88 0 print STDERR "Warning: encountered '$1' in file. Ignoring";
89 }
90 }
91 }
92 0 close IN;
93 } #if config file is there
94  
95 0 return $options;
96 } #load_options
97  
98 0 ▶sub ensure_locker {
Subroutine ensure_locker: not called CC=2 SCAR=17.9
Pod undocumented
99 0 my $self = shift;
100 # if running as the server, this will not be called.
101 # if something else is managing forking ( like the CGI )
102 # this should be run to make sure the locker socket
103 # opens and closes
104 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
105 0 _log( "$0 got INT signal. Shutting down." );
106 0 ▶ $self->{_locker}->stop if $self->{_locker};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_locker"} 0 0 0
107 0 exit;
108 0 };
109  
110 0 ▶ if( ! $self->{_locker}->ping(1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_locker"}->ping(1)) 0 0 0
111 0 $self->{_locker}->start;
112 }
113 } #ensure_locker
114  
115 0 ▶sub start {
Subroutine start: not called CC=3 SCAR=24.8
Pod undocumented
116 0 my $self = shift;
117  
118 0 $self->{_locker}->start;
119  
120 0 my $listener_socket = $self->_create_listener_socket;
121 0 ▶ die "Unable to open socket " unless $listener_socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $listener_socket 0 0 0
122  
123 0 ▶ if( my $pid = fork ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) 0 0 0
124 # parent
125 0 $self->{server_pid} = $pid;
126 0 return $pid;
127 }
128  
129 # in child
130 0 $0 = "YoteServer process";
131 0 $self->_run_loop( $listener_socket );
132  
133 } #start
134  
135 0 ▶sub stop {
Subroutine stop: not called CC=2 SCAR=17.9
Pod undocumented
136 0 my $self = shift;
137 0 ▶ if( my $pid = $self->{server_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = $self->{"server_pid"}) 0 0 0
138 0 $self->{error} = "Sending INT signal to lock server of pid '$pid'";
139 0 kill 'INT', $pid;
140 0 return 1;
141 }
142 0 $self->{error} = "No Yote server running";
143 0 return 0;
144 }
145  
146  
147  
148 =head2 run
149  
150 Runs the lock server.
151  
152 =cut
153 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
154 0 my $self = shift;
155 0 my $listener_socket = $self->_create_listener_socket;
156 0 ▶ die "Unable to open socket " unless $listener_socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $listener_socket 0 0 0
157 0 $self->_run_loop( $listener_socket );
158 }
159  
160 0 ▶sub _create_listener_socket {
Subroutine _create_listener_socket: not called CC=13 SCAR=52.0
161 0 my $self = shift;
162  
163 0 my $listener_socket;
164 0 my $count = 0;
165  
166 0 ▶ if( $self->{use_ssl} && ( ! $self->{SSL_cert_file} || ! $self->{SSL_key_file} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_ssl"} and not $self->{"SSL_cert_file"} && $self->{"SSL_key_file"}) 0 0 0
Condition0%0/6
$self->{"use_ssl"} and not $self->{"SSL_cert_file"} && $self->{"SSL_key_file"}
!ll&&!rl&&r
000
$self->{'SSL_cert_file'} && $self->{'SSL_key_file'}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"use_ssl"} and not $self->{"SSL_cert_file"} && $self->{"SSL_key_file"}
$self->{"use_ssl"} $self->{'SSL_cert_file'} $self->{'SSL_key_file'}
Truth table0%0/4
A and not(B && C)
A= $self->{"use_ssl"}B= $self->{'SSL_cert_file'}C= $self->{'SSL_key_file'}
ABCresult
0XX0
1110
10X1
1101
167 0 die "Cannot start server. SSL selected but is missing filename for SSL_cert_file and/or SSL_key_file";
168 }
169 0 ▶ while( ! $listener_socket && ++$count < 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $listener_socket and ++$count < 10) 0 0 0
Condition0%0/3
not $listener_socket and ++$count < 10
!ll&&!rl&&r
000
MC/DC0%0/2
not $listener_socket and ++$count < 10
not $listener_socket ++$count < 10
Truth table0%0/3
not(A) and B
A= not $listener_socketB= ++$count < 10
ABresult
0X0
100
111
170 0 ▶ if( $self->{args}{use_ssl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'args'}{'use_ssl'}) { } 0 0 0
171 0 my $cert_file = $self->{args}{SSL_cert_file};
172 0 my $key_file = $self->{args}{SSL_key_file};
173 0 ▶ if( index( $cert_file, '/' ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($cert_file, "/") != 0) 0 0 0
174 0 $cert_file = "$self->{yote_root_dir}/$cert_file";
175 }
176 0 ▶ if( index( $key_file, '/' ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($key_file, "/") != 0) 0 0 0
177 0 $key_file = "$self->{yote_root_dir}/$key_file";
178 }
179 0 $listener_socket = new IO::Socket::SSL(
180 Listen => 10,
181 LocalAddr => "$self->{yote_host}:$self->{yote_port}",
182 SSL_cert_file => $cert_file,
183 SSL_key_file => $key_file,
184 );
185 } else {
186 0 $listener_socket = new IO::Socket::INET(
187 Listen => 10,
188 LocalAddr => "$self->{yote_host}:$self->{yote_port}",
189 );
190 }
191 0 ▶ last if $listener_socket;
Branch: 1 branch
BranchTrueFalseTotal
if $listener_socket 0 0 0
192
193 0 print STDERR "Unable to open the yote socket [$self->{yote_host}:$self->{yote_port}] ($!). Retry $count of 10\n";
194 0 sleep 5 * $count;
195 }
196  
197 0 ▶ unless( $listener_socket ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($listener_socket) 0 0 0
198 0 $self->{error} = "Unable to open yote socket on port '$self->{yote_port}' : $! $@\n";
199 0 $self->{_locker}->stop;
200 0 _log( "unable to start yote server : $@ $!." );
201 0 return 0;
202 }
203  
204 0 print STDERR "Starting yote server\n";
205  
206 0 ▶ unless( $self->{yote_root_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"yote_root_dir"}) 0 0 0
207 0 eval('use Yote::ConfigData');
208 0 ▶ $self->{yote_root_dir} = $@ ? '/opt/yote' : Yote::ConfigData->config( 'yote_root' );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
209 0 undef $@;
210 }
211  
212 # if this is cancelled, make sure all child procs are killed too
213 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
214 0 _log( "got INT signal. Shutting down." );
215 0 ▶ $listener_socket && $listener_socket->close;
Branch: 1 branch
BranchTrueFalseTotal
$listener_socket and $listener_socket->close 0 0 0
216 0 for my $pid ( @{ $self->{_pids} } ) {
217 0 kill 'HUP', $pid;
218 }
219 0 $self->{_locker}->stop;
220 0 exit;
221 0 };
222  
223 0 $SIG{CHLD} = 'IGNORE';
224  
225 0 return $listener_socket;
226 } #_create_listener_socket
227  
228 0 ▶sub _run_loop {
Subroutine _run_loop: not called CC=2 SCAR=17.9
229 0 my( $self, $listener_socket ) = @_;
230 0 ▶ while( my $connection = $listener_socket->accept ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $connection = $listener_socket->accept) 0 0 0
231 0 $self->_process_request( $connection );
232 }
233 }
234  
235 0 ▶sub _log {
Subroutine _log: not called CC=4 SCAR=30.0
236 0 my( $msg, $sev ) = @_;
237 0 ▶ $sev //= 1;
Condition0%0/2
$sev //= 1
l!l
00
MC/DC0%0/1
$sev //= 1
$sev
Truth table0%0/2
A
A= $sev
Aresult
11
00
238 0 ▶ if( $sev <= $DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sev <= $Yote::Server::DEBUG) 0 0 0
239 0 print STDERR "Yote::Server : $msg\n";
240 0 ▶ open my $out, ">>/opt/yote/log/yote.log" or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">>/opt/yote/log/yote.log" or return 0 0 0
241 0 print $out "$msg\n";
242 0 close $out;
243 }
244 }
245  
246 0 ▶sub _find_ids_in_data {
Subroutine _find_ids_in_data: not called CC=8 SCAR=42.8
247 0 my $data = shift;
248 0 my $r = ref( $data );
249 0 ▶ if( $r eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 0 0 0
elsif ($r eq 'HASH') { } 0 0 0
elsif ($r) { } 0 0 0
250 0 ▶ return grep { $_ && index($_,'v')!=0 } map { ref( $_ ) ? _find_ids_in_data($_) : $_ } @$data;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
Condition0%0/3
$_ && index($_, 'v') != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$_ && index($_, 'v') != 0
$_ index($_, 'v') != 0
Truth table0%0/3
A && B
A= $_B= index($_, 'v') != 0
ABresult
0X0
100
111
251 }
252 elsif( $r eq 'HASH' ) {
253 0 ▶ return grep { $_ && index($_,'v')!=0} map { ref( $_ ) ? _find_ids_in_data($_) : $_ } values %$data;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
Condition0%0/3
$_ && index($_, 'v') != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$_ && index($_, 'v') != 0
$_ index($_, 'v') != 0
Truth table0%0/3
A && B
A= $_B= index($_, 'v') != 0
ABresult
0X0
100
111
254 }
255 elsif( $r ) {
256 0 die "_find_ids_in_data encountered a non ARRAY or HASH reference";
257 }
258 } #_find_ids_in_data
259  
260 # EXPERIMETNAL - this will return the entire public tree. The idea is to program
261 # without having to explicitly shove data across. This errs on the side of much
262 # more data, so relies on private data and method calls (encapsulation) to
263 # mitigate this
264  
265 0 ▶sub _unroll_ids {
Subroutine _unroll_ids: not called CC=8 SCAR=42.8
266 0 my( $store, $ids, $seen ) = @_;
267 0 ▶ $seen //= {};
Condition0%0/2
$seen //= {}
l!l
00
MC/DC0%0/1
$seen //= {}
$seen
Truth table0%0/2
A
A= $seen
Aresult
11
00
268  
269 0 my( @items ) = ( map { $store->fetch($_) } @$ids );
270  
271 0 my @outids;
272 0 for my $item( @items ) {
273 0 my $iid = $store->_get_id($item);
274 0 my $r = ref( $item );
275 0 $seen->{$iid}++;
276 0 ▶ if( $r eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 0 0 0
elsif ($r eq 'HASH') { } 0 0 0
277 0 push @outids, grep { ! $seen->{$_}++ } map { $store->_get_id($_) } grep { ref($_) } @$item;
278 }
279 elsif( $r eq 'HASH' ) {
280 0 push @outids, grep { ! $seen->{$_}++ } map { $store->_get_id($_) } grep { ref($_) } values %$item;
281 }
282 else {
283 0 my $data = $item->{DATA};
284 0 ▶ push @outids, map { $data->{$_} } grep { /^[^_]/ && $data->{$_} != /^v/ && ! $seen->{$data->{$_}}++ } keys %$data;
Condition0%0/6
/^[^_]/ && $data->{$_} != /^v/ && !$$seen{$data->{$_}}++
!ll&&!rl&&r
000
/^[^_]/ and $data->{$_} != /^v/
!ll&&!rl&&r
000
MC/DC0%0/3
/^[^_]/ && $data->{$_} != /^v/ && !$$seen{$data->{$_}}++
/^[^_]/ $data->{$_} != /^v/ !$$seen{$data->{$_}}++
Truth table0%0/4
A and B && not(C)
A= /^[^_]/B= $data->{$_} != /^v/C= !$$seen{$data->{$_}}++
ABCresult
0XX0
10X0
1100
1111
285 }
286 }
287  
288 0 ▶ _unroll_ids( $store, \@outids, $seen ) if @outids;
Branch: 1 branch
BranchTrueFalseTotal
if @outids 0 0 0
289  
290  
291 0 [ keys %$seen ];
292 } #_unroll_ids
293  
294 0 ▶sub _process_request {
Subroutine _process_request: not called CC=14 SCAR=53.5
295 #
296 # Reads incomming request from the socket, parses it, performs it and
297 # prints the result back to the socket.
298 #
299 0 my( $self, $sock ) = @_;
300  
301  
302 0 ▶ if ( my $pid = fork ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) { } 0 0 0
303 # parent
304 0 push @{$self->{_pids}},$pid;
305 } else {
306 # use Devel::SimpleProfiler;Devel::SimpleProfiler::start;
307 0 my( $self, $sock ) = @_;
308 #child
309 0 $0 = "YoteServer processing request";
310 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
311 0 _log( " process $$ got INT signal. Shutting down." );
312 0 $sock->close;
313 0 exit;
314 0 };
315
316
317 0 my $req = <$sock>;
318 0 $ENV{REMOTE_HOST} = $sock->peerhost;
319 0 my( %headers, %cookies );
320 0 ▶ while( my $hdr = <$sock> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $hdr = readline $sock)) 0 0 0
321 0 $hdr =~ s/\s*$//s;
322 0 ▶ last if $hdr !~ /[a-zA-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr =~ /[a-zA-Z]/ 0 0 0
323 0 my( $key, $val ) = ( $hdr =~ /^([^:]+):(.*)/ );
324 0 $headers{$key} = $val;
325 }
326  
327 0 for my $cookie ( split( /\s*;\s*/, $headers{Cookie} ) ) {
328 0 $cookie =~ s/^\s*|^\s*$//g;
329 0 my( $key, $val ) = split( /\s*=\s*/, $cookie, 2 );
330 0 $cookies{ $key } = $val;
331 }
332
333 #
334 # read certain length from socket ( as many bytes as content length )
335 #
336 0 my $content_length = $headers{'Content-Length'};
337 0 my $data;
338 0 ▶ if ( $content_length > 0 && ! eof $sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_length > 0 and not eof $sock) 0 0 0
Condition0%0/3
$content_length > 0 and not eof $sock
!ll&&!rl&&r
000
MC/DC0%0/2
$content_length > 0 and not eof $sock
$content_length > 0 not eof $sock
Truth table0%0/3
A and not(B)
A= $content_length > 0B= not eof $sock
ABresult
0X0
100
111
339 0 read $sock, $data, $content_length;
340 }
341 0 my( $verb, $path ) = split( /\s+/, $req );
342  
343 # escape for serving up web pages
344 # the thought is that this should be able to be a stand alone webserver
345 # for testing and to provide the javascript
346 0 ▶ if ( $path =~ m!/__/! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[/__/]) 0 0 0
347 # TODO - make sure install script makes the directories properly
348 0 my $filename = "$self->{yote_root_dir}/html/" . substr( $path, 4 );
349 0 ▶ if ( -e $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename) { } 0 0 0
350 0 my @stat = stat $filename;
351  
352 0 ▶ my $content_type = $filename =~ /css$/ ? 'text/css' : 'text/html';
Branch: 1 branch
BranchTrueFalseTotal
$filename =~ /css$/ ? : 0 0 0
353 0 my @headers = (
354 "Content-Type: $content_type; charset=utf-8",
355 'Server: Yote',
356 "Content-Length: $stat[7]",
357 );
358  
359 0 open( IN, "<$filename" );
360  
361 0 $sock->print( "HTTP/1.1 200 OK\n" . join ("\n", @headers). "\n\n" );
362  
363 0 ▶ while ( $data = <IN> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = readline IN)) 0 0 0
364 0 $sock->print( $data );
365 }
366 0 close IN;
367 } else {
368 0 $sock->print( "HTTP/1.1 404 FILE NOT FOUND\n\n" );
369 }
370 0 $sock->close;
371 0 exit;
372 }
373
374  
375 # data has the input parmas in JSON format.
376 # POST /
377  
378 0 ▶ if ( $verb ne 'POST' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verb ne "POST") 0 0 0
379 0 $sock->print( "HTTP/1.1 400 BAD REQUEST\n\n" );
380 0 $sock->close;
381 }
382  
383 0 $data =~ s/^p=//;
384 0 my $out_json;
385 0 eval {
386 0 $out_json = $self->invoke_payload( $data );
387 };
388  
389 0 ▶ if( ref $@ eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $@ eq 'HASH') { } 0 0 0
elsif ($@) { } 0 0 0
390 0 $out_json = encode_json( $@ );
391 }
392 elsif( $@ ) {
393 0 $out_json = encode_json( {
394 err => $@,
395 } );
396 }
397 0 my @headers = (
398 'Content-Type: text/json; charset=utf-8',
399 'Server: Yote',
400 'Access-Control-Allow-Headers: accept, content-type, cookie, origin, connection, cache-control',
401 'Access-Control-Allow-Origin: *', #TODO - have this configurable
402 'Content-Length: ' . bytes::length( $out_json ),
403 );
404
405 0 $sock->print( "HTTP/1.1 200 OK\n" . join ("\n", @headers). "\n\n$out_json\n" );
406
407 0 $sock->close;
408  
409 0 exit;
410  
411 } #child
412 } #_process_request
413 0 ▶sub invoke_payload {
Subroutine invoke_payload: not called CC=26 SCAR=65.5
Pod undocumented
414 0 my( $self, $raw_req_data, $file_uploads ) = @_;
415  
416 0 my $req_data = decode_json( $raw_req_data );
417
418 0 my( $obj_id, $token, $action, $params ) = @$req_data{ 'i', 't', 'a', 'pl' };
419
420 0 my $server_root = $self->{STORE}->fetch_server_root;
421 0 my $server_root_id = $server_root->{ID};
422
423  
424 0 my $id_to_last_update_time;
425 0 ▶ my $session = $token && $token ne '_' ? $server_root->_fetch_session( $token ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$token && $token ne '_' ? : 0 0 0
Condition0%0/3
$token and $token ne "_"
!ll&&!rl&&r
000
MC/DC0%0/2
$token and $token ne "_"
$token $token ne "_"
Truth table0%0/3
A and B
A= $tokenB= $token ne "_"
ABresult
0X0
100
111
426  
427 0 ▶ if( $session ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) 0 0 0
428 0 $id_to_last_update_time = $session->get__has_ids2times;
429 }
430  
431 0 ▶ unless( $obj_id eq '_' || # either the object id that is acted upon is
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj_id eq "_" or $obj_id eq $server_root_id or $id_to_last_update_time->{$obj_id}) 0 0 0
Condition0%0/6
$obj_id eq "_" or $obj_id eq $server_root_id or $id_to_last_update_time->{$obj_id}
l!l&&r!l&&!r
000
$obj_id eq "_" or $obj_id eq $server_root_id
l!l&&r!l&&!r
000
MC/DC0%0/3
$obj_id eq "_" or $obj_id eq $server_root_id or $id_to_last_update_time->{$obj_id}
$obj_id eq "_" $obj_id eq $server_root_id $id_to_last_update_time->{$obj_id}
Truth table0%0/4
A or B or C
A= $obj_id eq "_"B= $obj_id eq $server_root_idC= $id_to_last_update_time->{$obj_id}
ABCresult
1XX1
01X1
0011
0000
432 $obj_id eq $server_root_id || # the server root or is known to the session
433 ( $id_to_last_update_time->{$obj_id} ) ) {
434 # tried to do an action on an object it wasn't handed. do a 404
435 0 die( "client with token [$token] and session ($session) tried to invoke on obj id '$obj_id' which it does not have" );
436 }
437 0 ▶ if( substr( $action, 0, 1 ) eq '_' || $action =~ /^[gs]et$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($action, 0, 1) eq "_" or $action =~ /^[gs]et$/) 0 0 0
Condition0%0/3
substr($action, 0, 1) eq "_" or $action =~ /^[gs]et$/
l!l&&r!l&&!r
000
MC/DC0%0/2
substr($action, 0, 1) eq "_" or $action =~ /^[gs]et$/
substr($action, 0, 1) eq "_" $action =~ /^[gs]et$/
Truth table0%0/3
A or B
A= substr($action, 0, 1) eq "_"B= $action =~ /^[gs]et$/
ABresult
1X1
011
000
438 0 die( "Private method called" );
439 }
440  
441 0 ▶ if ( $params && ref( $params ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params and ref $params ne "ARRAY") 0 0 0
Condition0%0/3
$params and ref $params ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$params and ref $params ne "ARRAY"
$params ref $params ne "ARRAY"
Truth table0%0/3
A and B
A= $paramsB= ref $params ne "ARRAY"
ABresult
0X0
100
111
442 0 die( "Bad Req Param Not Array : $params" );
443 }
444  
445 0 my $store = $self->{STORE};
446  
447 # now things are getting a bit more complicated. The params passed in
448 # are always a list, but they may contain other containers that are not
449 # yote objects. So, transform the incomming parameter list and check all
450 # yote objects inside for may. Use a recursive helper function for this.
451 0 my $in_params = $store->__transform_params( $params, $session, $file_uploads );
452  
453 #
454 # This obj is the object that the method call is on
455 #
456 0 ▶ my $obj = $obj_id eq '_' ? $server_root :
Branch: 1 branch
BranchTrueFalseTotal
$obj_id eq '_' ? : 0 0 0
457 $store->fetch( $obj_id );
458  
459 0 ▶ unless( $obj->can( $action ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->can($action)) 0 0 0
460 0 die( "Bad Req : invalid method :'$action'" );
461 }
462  
463 # if there is a session, attach it to the object
464 0 ▶ if( $session ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) 0 0 0
465 0 $obj->{SESSION} = $session;
466 0 $obj->{SESSION}{SERVER_ROOT} = $server_root;
467  
468 }
469  
470 #
471 # <<------------- the actual method call --------------->>
472 #
473 0 my(@res) = ($obj->$action( @$in_params ));
474  
475 #
476 # this is included in what is returned to the client
477 #
478 0 my $out_res = $store->_xform_in( \@res, 'allow datastructures' );
479  
480 #
481 # in case the method generated a new session, (re)set that now
482 #
483 0 $session = $obj->{SESSION};
484 0 ▶ if( $session ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) 0 0 0
485 0 $id_to_last_update_time = $session->get__has_ids2times;
486 }
487
488 # the ids that were referenced explicitly in the
489 # method call.
490 0 my @out_ids = _find_ids_in_data( $out_res );
491  
492 #
493 # Based on the return value of the method call,
494 # these ids are ones that the client should have.
495 # We will check to see if these need updates
496 #
497 0 my @should_have = ( @{ _unroll_ids( $store, [@out_ids, keys %$id_to_last_update_time] ) } );
498 0 my( @updates, %methods );
499  
500 #
501 # check if existing are in the session
502 #
503 0 for my $should_have_id ( @should_have, keys %$id_to_last_update_time ) {
504 0 my $needs_update = 1;
505
506 0 ▶ if( $session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) 0 0 0
507 #
508 # check if the client of this session needs an update, otherwise assume that it does
509 #
510 0 ▶ my( $client_s, $client_ms ) = @{ $id_to_last_update_time->{$should_have_id} || [] };
Condition0%0/2
$id_to_last_update_time->{$should_have_id} || []
l!l
00
MC/DC0%0/1
$id_to_last_update_time->{$should_have_id} || []
$id_to_last_update_time->{$should_have_id}
Truth table0%0/2
A
A= $id_to_last_update_time->{$should_have_id}
Aresult
11
00
511 0 my( $server_s, $server_ms ) = $store->_last_updated( $should_have_id );
512  
513 0 ▶ $needs_update = $client_s == 0 || $server_s > $client_s || ($server_s == $client_s && $server_ms > $client_ms );
Condition0%0/9
$client_s == 0 || $server_s > $client_s || $server_s == $client_s && $server_ms > $client_ms
l!l&&r!l&&!r
000
$client_s == 0 or $server_s > $client_s
l!l&&r!l&&!r
000
$server_s == $client_s and $server_ms > $client_ms
!ll&&!rl&&r
000
MC/DC0%0/4
$client_s == 0 || $server_s > $client_s || $server_s == $client_s && $server_ms > $client_ms
$client_s == 0 $server_s > $client_s $server_s == $client_s $server_ms > $client_ms
Truth table0%0/5
A or B || C and D
A= $client_s == 0B= $server_s > $client_sC= $server_s == $client_sD= $server_ms > $client_ms
ABCDresult
1XXX1
01XX1
00111
000X0
00100
514 }
515  
516 0 ▶ if( $needs_update ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_update) 0 0 0
517 0 my $should_have_obj = $store->fetch( $should_have_id );
518 0 my $ref = ref( $should_have_obj );
519 0 my $data;
520 0 ▶ if( $ref eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
521 0 $data = [ map { $store->_xform_in( $_ ) } @$should_have_obj ];
522 } elsif( $ref eq 'HASH' ) {
523 0 $data = { map { $_ => $store->_xform_in( $should_have_obj->{$_} ) } keys %$should_have_obj };
524 } else {
525 0 my $d = $should_have_obj->{DATA};
526 0 ▶ $data = { map { $_ => $d->{$_} } grep { index($_,"_") != 0 } keys %$d },
Condition0%0/3
$methods{$ref} ||= $should_have_obj->_callable_methods
l!l&&r!l&&!r
000
MC/DC0%0/2
$methods{$ref} ||= $should_have_obj->_callable_methods
$methods{$ref} $should_have_obj->_callable_methods
Truth table0%0/3
A ||= B
A= $methods{$ref}B= $should_have_obj->_callable_methods
ABresult
1X1
011
000
527 0 $methods{$ref} ||= $should_have_obj->_callable_methods;
528 }
529 0 my $update = {
530 id => $should_have_id,
531 cls => $ref,
532 data => $data,
533 };
534 0 push @updates, $update;
535 0 ▶ if( $session ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) 0 0 0
536 0 $id_to_last_update_time->{$should_have_id} = [Time::HiRes::gettimeofday];
537 }
538 } # if this needs an update
539
540 } #each object the client should have
541  
542  
543 0 my $out_json = to_json( { result => $out_res,
544 updates => \@updates,
545 methods => \%methods,
546 } );
547  
548 0 delete $obj->{SESSION};
549 0 $self->{STORE}->stow_all;
550
551 0 return $out_json;
552 } #invoke_payload
553  
554 # ------- END Yote::Server
555  
556 package Yote::ServerStore;
557  
558 1 ▶use Data::RecordStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
559  
560 1 ▶use base 'Yote::ObjStore';
Subroutine BEGIN: called CC=1 SCAR=0.0
561  
562 1 ▶sub _new { #Yote::ServerStore
Subroutine _new: called CC=1 SCAR=1.7
563 1 my( $pkg, $args ) = @_;
564 1 $args->{store} = "$args->{root}/DATA_STORE";
565 1 my $self = $pkg->SUPER::_new( $args );
566  
567 # keeps track of when any object had been last updated.
568 # use like $self->{OBJ_UPDATE_DB}->put_record( $obj_id, [ time ] );
569 # or my( $time ) = @{ $self->{OBJ_UPDATE_DB}->get_record( $obj_id ) };
570 0 $self->{OBJ_UPDATE_DB} = Data::RecordStore::FixedStore->open( "LL", "$args->{root}/OBJ_META" );
571  
572 0 my( $m, $ms ) = ( Time::HiRes::gettimeofday );
573 0 $self->{OBJ_UPDATE_DB}->put_record( $self->{ID}, [ $m, $ms ] );
574  
575 0 $self;
576 } #_new
577  
578 0 ▶sub _dirty {
Subroutine _dirty: not called CC=1 SCAR=6.9
579 0 my( $self, $ref, $id ) = @_;
580 0 $self->SUPER::_dirty( $ref, $id );
581 0 $self->{OBJ_UPDATE_DB}->ensure_entry_count( $id );
582  
583 0 my( $m, $ms ) = ( Time::HiRes::gettimeofday );
584 0 $self->{OBJ_UPDATE_DB}->put_record( $id, [ $m, $ms ] );
585 }
586  
587 0 ▶sub stow_all {
Subroutine stow_all: not called CC=2 SCAR=17.9
588 0 my $self = $_[0];
589 0 for my $obj (values %{$self->{_DIRTY}} ) {
590 0 my $obj_id = $self->_get_id( $obj );
591 0 $self->{OBJ_UPDATE_DB}->ensure_entry_count( $obj_id );
592 }
593 0 $self->SUPER::stow_all;
594 } #stow_all
595  
596 0 ▶sub _last_updated {
Subroutine _last_updated: not called CC=1 SCAR=6.9
597 0 my( $self, $obj_id ) = @_;
598 0 my( $s, $ms ) = @{ $self->{OBJ_UPDATE_DB}->get_record( $obj_id ) };
599 0 $s, $ms;
600 }
601  
602 0 ▶sub _log {
Subroutine _log: not called CC=1 SCAR=6.9
603 0 Yote::Server::_log(shift);
604 }
605  
606  
607 0 ▶sub __transform_params {
Subroutine __transform_params: not called CC=8 SCAR=42.8
608 #
609 # Recursively transforms incoming parameters into values, yote objects, or non yote containers.
610 # This checks to make sure that the parameters are allowed by the given token.
611 # Throws execptions if the parametsr are not allowed, or if a reference that is not a hash or array
612 # is encountered.
613 #
614 0 my( $self, $param, $session, $files ) = @_;
615  
616 0 ▶ if( ref( $param ) eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $param eq 'HASH') { } 0 0 0
elsif (ref $param eq 'ARRAY') { } 0 0 0
elsif (ref $param) { } 0 0 0
617 0 return { map { $_ => $self->__transform_params($param->{$_}, $session, $files) } keys %$param };
618 }
619 elsif( ref( $param ) eq 'ARRAY' ) {
620 0 return [ map { $self->__transform_params($_, $session, $files) } @$param ];
621 } elsif( ref( $param ) ) {
622 0 die "Transforming Params: got weird ref '" . ref( $param ) . "'";
623 }
624 0 ▶ if( ( index( $param, 'v' ) != 0 && index($param, 'f' ) != 0 ) && !$session->get__has_ids2times({})->{$param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($param, "v") != 0 and index($param, "f") != 0 and not $session->get__has_ids2times({})->{$param}) 0 0 0
Condition0%0/6
index($param, "v") != 0 and index($param, "f") != 0 and not $session->get__has_ids2times({})->{$param}
!ll&&!rl&&r
000
index($param, "v") != 0 and index($param, "f") != 0
!ll&&!rl&&r
000
MC/DC0%0/3
index($param, "v") != 0 and index($param, "f") != 0 and not $session->get__has_ids2times({})->{$param}
index($param, "v") != 0 index($param, "f") != 0 not $session->get__has_ids2times({})->{$param}
Truth table0%0/4
A and B and not(C)
A= index($param, "v") != 0B= index($param, "f") != 0C= not $session->get__has_ids2times({})->{$param}
ABCresult
0XX0
10X0
1100
1111
625 # obj id given, but the client should not have that id
626 0 ▶ if( $param ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param) 0 0 0
627 0 die { err => 'Sync Error', needs_resync => 1 };
628 }
629 0 return undef;
630 }
631 0 return $self->_xform_out( $param, $files );
632 } #__transform_params
633  
634 0 ▶sub _xform_out {
Subroutine _xform_out: not called CC=7 SCAR=40.3
635 0 my( $self, $val, $files ) = @_;
636 0 ▶ return undef unless defined( $val );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
637 0 ▶ if( index($val,'f') == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($val, "f") == 0) 0 0 0
638 # convert to file object
639 0 ▶ if( $val =~ /^f(\d+)_(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^f(\d+)_(\d+)$/) 0 0 0
640 0 my( $offset_start, $offset_end ) = ( $1, $2 );
641 0 ▶ for( my $i=$offset_start; $i < $offset_end; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $offset_end) 0 0 0
642 0 my $file = $files->[$i];
643 0 ▶ if( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 0 0 0
644 0 my( $orig_filename ) = ( $file =~ /([^\/]*)$/ );
645 0 my( $extension ) = ( $orig_filename =~ /\.([^.\/]+)$/ );
646
647 # TODO - cleanup, maybe use File::Temp or something
648 0 my $newname = "/tmp/".UUID::Tiny::create_uuid_as_string();
649 0 open (FILE, ">$newname");
650 0 my $fh = $file->fh;
651 0 ▶ while (read ($fh, my $Buffer, 1024)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $Buffer, 1024) 0 0 0
652 0 print FILE $Buffer;
653 }
654 0 close FILE;
655 # create yote object here that wraps the file name
656 0 return $self->newobj( {
657 file_path => $newname,
658 file_extension => $extension,
659 file_name => $orig_filename,
660 } );
661 }
662 } #finding the file
663 0 return undef;
664 }
665 }
666 0 return $self->SUPER::_xform_out( $val );
667 } #_xform_out
668  
669  
670 #
671 # Unlike the superclass version of this, this provides an arguemnt to
672 # allow non-yote datastructures to be returned. The contents of those
673 # data structures will all recursively be xformed in.
674 #
675 0 ▶sub _xform_in {
Subroutine _xform_in: not called CC=10 SCAR=47.0
676 0 my( $self, $val, $allow_datastructures ) = @_;
677  
678 0 my $r = ref $val;
679 0 ▶ if( $r ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
680 0 ▶ if( $allow_datastructures) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_datastructures) 0 0 0
681 # check if this is a yote object
682 0 ▶ if( ref( $val ) eq 'ARRAY' && ! tied( @$val ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY' and not tied @$val) { } 0 0 0
elsif (ref $val eq 'HASH' and not tied %$val) { } 0 0 0
Condition0%0/3
ref $val eq "ARRAY" and not tied @$val
!ll&&!rl&&r
000
MC/DC0%0/2
ref $val eq "ARRAY" and not tied @$val
ref $val eq "ARRAY" not tied @$val
Truth table0%0/3
A and not(B)
A= ref $val eq "ARRAY"B= not tied @$val
ABresult
0X0
100
111
683 0 ▶ return [ map { ref $_ ? $self->_xform_in( $_, $allow_datastructures ) : "v$_" } @$val ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
684 }
685 ▶ elsif( ref( $val ) eq 'HASH' && ! tied %$val ) {
Condition0%0/3
ref $val eq "HASH" and not tied %$val
!ll&&!rl&&r
000
MC/DC0%0/2
ref $val eq "HASH" and not tied %$val
ref $val eq "HASH" not tied %$val
Truth table0%0/3
A and not(B)
A= ref $val eq "HASH"B= not tied %$val
ABresult
0X0
100
111
686 0 ▶ return { map { $_ => ( ref( $val->{$_} ) ? $self->_xform_in( $val->{$_}, $allow_datastructures ) : "v$val->{$_}" ) } keys %$val };
Branch: 1 branch
BranchTrueFalseTotal
ref $val->{$_} ? : 0 0 0
687 }
688 }
689 0 return $self->_get_id( $val );
690 }
691  
692 0 ▶ return defined $val ? "v$val" : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 0 0
693 } #_xform_in
694  
695 0 ▶sub newobj {
Subroutine newobj: not called CC=2 SCAR=17.9
696 0 my( $self, $data, $class ) = @_;
697 0 ▶ $class ||= 'Yote::ServerObj';
Condition0%0/2
$class ||= "Yote::ServerObj"
l!l
00
MC/DC0%0/1
$class ||= "Yote::ServerObj"
$class
Truth table0%0/2
A
A= $class
Aresult
11
00
698 0 $class->_new( $self, $data );
699 } #newobj
700  
701 0 ▶sub fetch_server_root {
Subroutine fetch_server_root: not called CC=4 SCAR=30.0
702 0 my $self = shift;
703  
704 0 ▶ return $self->{SERVER_ROOT} if $self->{SERVER_ROOT};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"SERVER_ROOT"} 0 0 0
705  
706 0 my $system_root = $self->fetch_root;
707 0 my $server_root = $system_root->get_server_root;
708 0 ▶ unless( $server_root ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($server_root) 0 0 0
709 0 $server_root = Yote::ServerRoot->_new( $self );
710 0 $system_root->set_server_root( $server_root );
711 0 $self->stow_all;
712 }
713  
714 # some setup here? accounts/webapps/etc?
715 # or make it simple. if the webapp has an account, then pass that account
716 # with the rest of the arguments
717  
718 # then verify if the command can run on the app object with those args
719 # or even : $myapp->run( 'command', @args );
720  
721 0 ▶ $self->{SERVER_ROOT} ||= $server_root;
Condition0%0/3
$self->{"SERVER_ROOT"} ||= $server_root
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"SERVER_ROOT"} ||= $server_root
$self->{"SERVER_ROOT"} $server_root
Truth table0%0/3
A ||= B
A= $self->{"SERVER_ROOT"}B= $server_root
ABresult
1X1
011
000
722  
723 0 $server_root;
724
725 } #fetch_server_root
726  
727 0 ▶sub lock {
Subroutine lock: not called CC=2 SCAR=17.9
728 0 my( $self, $key ) = @_;
729 0 ▶ $self->{_lockerClient} ||= $self->{_locker}->client( $$ );
Condition0%0/3
$self->{"_lockerClient"} ||= $self->{"_locker"}->client($$)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_lockerClient"} ||= $self->{"_locker"}->client($$)
$self->{"_lockerClient"} $self->{"_locker"}->client($$)
Truth table0%0/3
A ||= B
A= $self->{"_lockerClient"}B= $self->{"_locker"}->client($$)
ABresult
1X1
011
000
730 0 $self->{_lockerClient}->lock( $key );
731 }
732  
733 0 ▶sub unlock {
Subroutine unlock: not called CC=1 SCAR=6.9
734 0 my( $self, $key ) = @_;
735 0 $self->{_lockerClient}->unlock( $key );
736 }
737  
738  
739 # ------- END Yote::ServerStore
740  
741 package Yote::ServerObj;
742  
743 1 ▶use base 'Yote::Obj';
Subroutine BEGIN: called CC=1 SCAR=0.0
744  
745 0 ▶sub _log {
Subroutine _log: not called CC=1 SCAR=6.9
746 0 Yote::Server::_log(shift);
747 }
748  
749 0 ▶sub _err {
Subroutine _err: not called CC=1 SCAR=6.9
750 0 shift; #self
751 0 die { err => shift };
752 }
753  
754 1 $Yote::ServerObj::PKG2METHS = {};
755 0 ▶sub __discover_methods {
Subroutine __discover_methods: not called CC=7 SCAR=38.2
756 0 my $pkg = shift;
757 0 my $meths = $Yote::ServerObj::PKG2METHS->{$pkg};
758 0 ▶ if( $meths ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meths) 0 0 0
759 0 return $meths;
760 }
761  
762 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
763 0 my @m = grep { $_ !~ /::/ } keys %{"${pkg}\::"};
764 0 ▶ if( $pkg eq 'Yote::ServerObj' ) { #the base, presumably
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg eq "Yote::ServerObj") 0 0 0
765 0 return [ sort grep { $_ !~ /^(_|[gs]et_|(can|[sg]et|VERSION|AUTOLOAD|DESTROY|CARP_TRACE|BEGIN|isa|import|PKG2METHS|ISA|add_to_|remove_from)$)/ } @m ];
766 }
767  
768 0 my %hasm = map { $_ => 1 } @m;
769 0 for my $class ( @{"${pkg}\::ISA" } ) {
770 0 ▶ next if $class eq 'Yote::ServerObj' || $class eq 'Yote::Obj';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Yote::ServerObj" or $class eq "Yote::Obj" 0 0 0
Condition0%0/3
$class eq "Yote::ServerObj" or $class eq "Yote::Obj"
l!l&&r!l&&!r
000
MC/DC0%0/2
$class eq "Yote::ServerObj" or $class eq "Yote::Obj"
$class eq "Yote::ServerObj" $class eq "Yote::Obj"
Truth table0%0/3
A or B
A= $class eq "Yote::ServerObj"B= $class eq "Yote::Obj"
ABresult
1X1
011
000
771 0 my $pm = __discover_methods( $class );
772 0 push @m, @$pm;
773 }
774
775 0 my $base_meths = __discover_methods( 'Yote::ServerObj' );
776 0 my( %base ) = map { $_ => 1 } 'AUTOLOAD', @$base_meths;
777 0 ▶ $meths = [ sort grep { $_ !~ /^(_|[gs]et_|(can|[sg]et|VERSION|AUTOLOAD|DESTROY|BEGIN|isa|import|PKG2METHS|ISA|add_to_|remove_from)$)/ && ! $base{$_} } @m ];
Condition0%0/3
$_ =~ /^(_|[gs]et_|(can|[sg]et|VERSION|AUTOLOAD|DESTROY|BEGIN|isa|import|PKG2METHS|ISA|add_to_|remove_from)$)/ || $base{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ =~ /^(_|[gs]et_|(can|[sg]et|VERSION|AUTOLOAD|DESTROY|BEGIN|isa|import|PKG2METHS|ISA|add_to_|remove_from)$)/ || $base{$_}
$_ =~ /^(_|[gs]et_|(can|[sg]et|VERSION|AUTOLOAD|DESTROY|BEGIN|isa|import|PKG2METHS|ISA|add_to_|remove_from)$)/ $base{$_}
Truth table0%0/3
A || B
A= $_ =~ /^(_|[gs]et_|(can|[sg]et|VERSION|AUTOLOAD|DESTROY|BEGIN|isa|import|PKG2METHS|ISA|add_to_|remove_from)$)/B= $base{$_}
ABresult
1X1
011
000
778 0 $Yote::ServerObj::PKG2METHS->{$pkg} = $meths;
779
780 0 $meths;
781 } #__discover_methods
782  
783 # when sending objects across, the format is like
784 # id : { data : { }, methods : [] }
785 # the methods exclude all the methods of Yote::Obj
786 0 ▶sub _callable_methods {
Subroutine _callable_methods: not called CC=1 SCAR=6.9
787 0 my $self = shift;
788 0 my $pkg = ref( $self );
789 0 __discover_methods( $pkg );
790 } # _callable_methods
791  
792  
793 0 ▶sub _get {
Subroutine _get: not called CC=4 SCAR=30.0
794 0 my( $self, $fld, $default ) = @_;
795 0 ▶ if( ! defined( $self->{DATA}{$fld} ) && defined($default) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"DATA"}{$fld} and defined $default) 0 0 0
Condition0%0/3
not defined $self->{"DATA"}{$fld} and defined $default
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $self->{"DATA"}{$fld} and defined $default
not defined $self->{"DATA"}{$fld} defined $default
Truth table0%0/3
not(A) and B
A= not defined $self->{"DATA"}{$fld}B= defined $default
ABresult
0X0
100
111
796 0 ▶ if( ref( $default ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $default) 0 0 0
797 0 $self->{STORE}->_dirty( $default, $self->{STORE}->_get_id( $default ) );
798 }
799 0 $self->{STORE}->_dirty( $self, $self->{ID} );
800 0 $self->{DATA}{$fld} = $self->{STORE}->_xform_in( $default );
801 }
802 0 $self->{STORE}->_xform_out( $self->{DATA}{$fld} );
803 } #_get
804  
805  
806 # ------- END Yote::ServerObj
807  
808 package Yote::ServerRoot;
809  
810 1 ▶use base 'Yote::ServerObj';
Subroutine BEGIN: called CC=1 SCAR=0.0
811  
812 0 ▶sub _init {
Subroutine _init: not called CC=1 SCAR=6.9
813 0 my $self = shift;
814 0 $self->set__doesHave_Token2objs({});
815 0 $self->set__apps({});
816 0 $self->set__token_timeslots([]);
817 0 $self->set__token_timeslots_metadata([]);
818 0 $self->set__token_mutex([]);
819 }
820  
821 0 ▶sub _log {
Subroutine _log: not called CC=1 SCAR=6.9
822 0 Yote::Server::_log(shift);
823 }
824  
825 #
826 # fetches or creates session which has a _token field
827 #
828 0 ▶sub fetch_session {
Subroutine fetch_session: not called CC=2 SCAR=17.9
829 0 my( $self, $token ) = @_;
830 0 ▶ my $session = $self->_fetch_session( $token ) || $self->_create_session;
Condition0%0/3
$self->_fetch_session($token) || $self->_create_session
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_fetch_session($token) || $self->_create_session
$self->_fetch_session($token) $self->_create_session
Truth table0%0/3
A || B
A= $self->_fetch_session($token)B= $self->_create_session
ABresult
1X1
011
000
831 0 $self->{SESSION} = $session;
832 0 $session;
833 }
834  
835 0 ▶sub _fetch_session {
Subroutine _fetch_session: not called CC=4 SCAR=30.0
836 0 my( $self, $token ) = @_;
837
838 0 $self->{STORE}->lock( 'token_mutex' );
839 0 my $slots = $self->get__token_timeslots();
840  
841 0 ▶ for( my $i=0; $i<@$slots; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$slots) 0 0 0
842 0 ▶ if( my $session = $slots->[$i]{$token} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session = $slots->[$i]{$token}) 0 0 0
843 0 ▶ if( $i > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 0 0 0
844 # make sure this is in the most current 'boat'
845 0 $slots->[0]{ $token } = $session;
846 }
847 0 $self->{STORE}->unlock( 'token_mutex' );
848 0 return $session;
849 }
850 }
851 0 $self->{STORE}->unlock( 'token_mutex' );
852 0 0;
853 } #_fetch_sesion
854  
855 0 ▶sub _create_session {
Subroutine _create_session: not called CC=6 SCAR=37.4
856 0 my $self = shift;
857 0 my $tries = shift;
858  
859 0 ▶ if( $tries > 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries > 3) 0 0 0
860 0 die "Error creating token. Got the same random number 4 times in a row";
861 }
862  
863 0 my $token = int( rand( 1_000_000_000 ) ); #TODO - find max this can be for long int
864
865 # make the token boat. tokens last at least 10 mins, so quantize
866 # 10 minutes via time 10 min = 600 seconds = 600
867 # or easy, so that 1000 seconds ( ~ 16 mins )
868 # todo - make some sort of quantize function here
869 0 my $current_time_chunk = int( time / 100 );
870 0 my $earliest_valid_time_chunk = $current_time_chunk - 7;
871  
872 0 $self->{STORE}->lock( 'token_mutex' );
873  
874 #
875 # A list of slot 'boats' which store token -> ip
876 #
877 0 my $slots = $self->get__token_timeslots();
878 #
879 # a list of times. the list index of these times corresponds
880 # to the slot 'boats'
881 #
882 0 my $slot_data = $self->get__token_timeslots_metadata();
883
884 #
885 # Check if the token is already used ( very unlikely ).
886 # If already used, try this again :/
887 #
888 0 ▶ for( my $i=0; $i<@$slot_data; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$slot_data) 0 0 0
889 0 ▶ return $self->_create_session( $tries++ ) if $slots->[ $i ]{ $token };
Branch: 1 branch
BranchTrueFalseTotal
if $slots->[$i]{$token} 0 0 0
890 }
891  
892 #
893 # See if the most recent time slot is current. If it is behind, create a new current slot
894 # create a new most recent boat.
895 #
896 my $session = $self->{STORE}->newobj( {
897 0 _has_ids2times => {},
898 _token => $token }, 'Yote::ServerSession' );
899
900 0 ▶ if( $slot_data->[ 0 ] == $current_time_chunk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot_data->[0] == $current_time_chunk) { } 0 0 0
901 0 $slots->[ 0 ]{ $token } = $session;
902 } else {
903 0 unshift @$slot_data, $current_time_chunk;
904 0 unshift @$slots, { $token => $session };
905 }
906
907 #
908 # remove this token from old boats so it doesn't get purged
909 # when in a valid boat.
910 #
911 0 ▶ for( my $i=1; $i<@$slot_data; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$slot_data) 0 0 0
912 0 delete $slots->[$i]{ $token };
913 }
914  
915 0 $self->{STORE}->_stow( $slots );
916 0 $self->{STORE}->_stow( $slot_data );
917 0 $self->{STORE}->unlock( 'token_mutex' );
918  
919  
920 0 $session;
921  
922 } #_create_session
923  
924 0 ▶sub _destroy_session {
Subroutine _destroy_session: not called CC=2 SCAR=17.9
925 0 my( $self, $token ) = @_;
926
927 0 $self->{STORE}->lock( 'token_mutex' );
928 0 my $slots = $self->get__token_timeslots();
929 0 ▶ for( my $i=0; $i<@$slots; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$slots) 0 0 0
930 0 delete $slots->[$i]{ $token };
931 }
932 0 $self->{STORE}->_stow( $slots );
933 0 $self->{STORE}->unlock( 'token_mutex' );
934 0 1;
935 } #_destroy_session
936  
937 #
938 # Needed for when no logins are going to happen
939 #
940 0 ▶sub create_token {
Subroutine create_token: not called CC=1 SCAR=6.9
941 0 shift->_create_session->get__token;
942 }
943  
944 #
945 # Returns the app and possibly a logged in account
946 #
947 0 ▶sub fetch_app {
Subroutine fetch_app: not called CC=4 SCAR=30.0
948 0 my( $self, $app_name ) = @_;
949 0 my $apps = $self->get__apps;
950 0 my $app = $apps->{$app_name};
951  
952 0 ▶ unless( $app ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($app) 0 0 0
953 0 eval("require $app_name");
954 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
955 # TODO - have/use a good logging system with clarity and stuff
956 # warnings, errors, etc
957 0 return undef;
958 }
959 0 $app = $app_name->_new( $self->{STORE} );
960 0 $apps->{$app_name} = $app;
961 }
962 0 ▶ my $acct = $self->{SESSION} ? $self->{SESSION}->get_acct : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'SESSION'} ? : 0 0 0
963  
964 0 return $app, $acct, $self->{SESSION};
965 } #fetch_app
966  
967 0 ▶sub fetch_root {
Subroutine fetch_root: not called CC=1 SCAR=6.9
968 0 return shift;
969 }
970  
971 0 ▶sub init_root {
Subroutine init_root: not called CC=2 SCAR=17.9
972 0 my $self = shift;
973 0 ▶ my $session = $self->{SESSION} || $self->_create_session;
Condition0%0/3
$self->{'SESSION'} || $self->_create_session
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'SESSION'} || $self->_create_session
$self->{'SESSION'} $self->_create_session
Truth table0%0/3
A || B
A= $self->{'SESSION'}B= $self->_create_session
ABresult
1X1
011
000
974 0 $self->{SESSION} = $session;
975 0 $session->set__has_ids2times({});
976 0 my $token = $session->get__token;
977 0 return $self, $token;
978 }
979  
980 # while this is a non-op, it will cause any updated contents to be
981 # transfered to the caller automatically
982 0 ▶sub update {
Subroutine update: not called CC=1 SCAR=0.0
983  
984 }
985  
986 # ------- END Yote::ServerRoot
987  
988 package Yote::ServerSession;
989  
990 1 ▶use base 'Yote::ServerObj';
Subroutine BEGIN: called CC=1 SCAR=0.0
991  
992 0 ▶sub fetch { # fetch scrambled id
Subroutine fetch: not called CC=2 SCAR=17.9
993 0 my( $self, $in_sess_id ) = @_;
994 0 ▶ return unless $in_sess_id > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $in_sess_id > 0 0 0 0
995 0 $self->get__ids([])->[$in_sess_id-1];
996 }
997  
998 0 ▶sub getid { #scramble id for object
Subroutine getid: not called CC=2 SCAR=17.9
999 0 my( $self, $obj ) = @_;
1000 0 my $o2i = $self->get__obj2id({});
1001 0 ▶ if( $o2i->{$obj} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o2i->{$obj}) 0 0 0
1002 0 return $o2i->{$obj};
1003 }
1004 0 my $ids = $self->get__ids([]);
1005 0 push @$ids, $obj;
1006 0 my $id = scalar @$ids;
1007 0 $o2i->{$obj} = $id;
1008 0 $id;
1009 } #id
1010  
1011 # ------- END Yote::ServerSession
1012  
1013 package Yote::Server::Acct;
1014  
1015 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1016 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1017  
1018 1 ▶use Yote::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
1019  
1020 1 ▶use base 'Yote::ServerObj';
Subroutine BEGIN: called CC=1 SCAR=0.0
1021  
1022 0 ▶sub _onLogin {}
Subroutine _onLogin: not called CC=1 SCAR=0.0
1023  
1024 0 ▶sub logout {
Subroutine logout: not called CC=1 SCAR=6.9
1025 0 my $self = shift;
1026 0 my $server = $self->{SESSION}{SERVER};
1027 0 $server->_destroy_session( $self->{SESSION}->get__token );
1028 } #logout
1029  
1030 # ------- END Yote::Server::Acct
1031  
1032 package Yote::Server::App;
1033  
1034 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1035 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1036  
1037 1 ▶use Yote::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
1038  
1039 1 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
1040  
1041 1 ▶use base 'Yote::ServerObj';
Subroutine BEGIN: called CC=1 SCAR=0.0
1042  
1043 0 ▶sub _acct_class { "Yote::Server::Acct" }
Subroutine _acct_class: not called CC=1 SCAR=6.9
1044  
1045 #
1046 # Override and call _create_account
1047 #
1048 0 ▶sub create_account {
Subroutine create_account: not called CC=1 SCAR=6.9
1049 0 die "May not create account via website";
1050 }
1051  
1052 0 ▶sub _create_account {
Subroutine _create_account: not called CC=3 SCAR=24.8
1053 0 my( $self, $un, $pw, $class_override ) = @_;
1054 0 my $accts = $self->get__accts({});
1055  
1056 0 ▶ if( $accts->{lc($un)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$accts{lc $un}) 0 0 0
1057 0 $self->_err( "Unable to create account" );
1058 }
1059  
1060 0 ▶ my $acct = $self->{STORE}->newobj( { user => $un }, $class_override || $self->_acct_class );
Condition0%0/3
$class_override || $self->_acct_class
l!l&&r!l&&!r
000
MC/DC0%0/2
$class_override || $self->_acct_class
$class_override $self->_acct_class
Truth table0%0/3
A || B
A= $class_overrideB= $self->_acct_class
ABresult
1X1
011
000
1061 0 $acct->set__password_hash( crypt( $pw, length( $pw ) . Digest::MD5::md5_hex($acct->{ID} ) ) );
1062  
1063 # TODO - create an email infrastructure for account validation
1064 0 $acct->set_app( $self );
1065
1066 0 $accts->{lc($un)} = $acct;
1067 0 $acct;
1068 } #_create_account
1069  
1070 0 ▶sub logout {
Subroutine logout: not called CC=2 SCAR=17.9
1071 0 my $self = shift;
1072 0 my $root = $self->{SESSION}{SERVER_ROOT};
1073 0 ▶ $root->_destroy_session( $self->{SESSION}->get__token ) if $root;
Branch: 1 branch
BranchTrueFalseTotal
if $root 0 0 0
1074 0 delete $self->{SESSION};
1075 0 1;
1076 } #logout
1077  
1078 0 ▶sub login {
Subroutine login: not called CC=4 SCAR=30.0
1079 0 my( $self, $un, $pw ) = @_;
1080  
1081 # returns account, cookie. only way to get account object
1082 0 my $acct = $self->get__accts({})->{lc($un)};
1083  
1084 # doing it like this so a failed attempt has about the same amount of time
1085 # as an attempt against a nonexistant account. maybe random microsleep?
1086 0 ▶ my $pwh = crypt( $pw, length( $pw ) . Digest::MD5::md5_hex($acct ? $acct->{ID} : $self->{ID} ) );
Branch: 1 branch
BranchTrueFalseTotal
$acct ? : 0 0 0
1087 0 ▶ if( $acct && $pwh eq $acct->get__password_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acct and $pwh eq $acct->get__password_hash) 0 0 0
Condition0%0/3
$acct and $pwh eq $acct->get__password_hash
!ll&&!rl&&r
000
MC/DC0%0/2
$acct and $pwh eq $acct->get__password_hash
$acct $pwh eq $acct->get__password_hash
Truth table0%0/3
A and B
A= $acctB= $pwh eq $acct->get__password_hash
ABresult
0X0
100
111
1088 # this and Yote::ServerRoot::fetch_app are the only ways to expose the account obj
1089 # to the UI. If the UI calls for an acct object it wasn't exposed to, Yote::Server
1090 # won't allow it. fetch_app only calls it if the correct cookie token is passed in
1091 0 $self->{SESSION}->set_acct( $acct );
1092 0 $acct->_onLogin;
1093 0 return $acct;
1094 }
1095 0 $self->_err( "Incorrect login" );
1096 } #login
1097  
1098 # ------- END Yote::Server::Acct
1099  
1100 1 1;
1101  
1102 __END__
↑ Summary Server/ModperlOperator.pm »