Finger/Server.pm

filter: Statementstmt 92.2% 95 / 103 Branchbran 84.3% 27 / 32 Conditioncond 75.0% 9 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 81.8% 18 / 22 Podpod 100% 4 / 4 totaltotal 86.7% 157 / 181 CC 23 SCAR 15.6
CC
23
Cov
89%
start
9.1
new
7.7
__ANON__
6.0
CRAP 4.7
↑ Summary Finger/Client.pm »
1 package AnyEvent::Finger::Server;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use AnyEvent::Socket qw( tcp_server );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use AnyEvent::Finger::Transaction;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use AnyEvent::Finger::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use AnyEvent::Finger::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: Simple asynchronous finger server
14 5 our $VERSION = '0.14'; # VERSION
15  
16  
17 sub new
18 5 ▶{
Subroutine new: called CC=7 SCAR=20.4
Pod documented
19 5 my $class = shift;
20 0 ▶ my $args = ref $_[0] eq 'HASH' ? (\%{$_[0]}) : ({@_});
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 5 5
21 5 my $port = $args->{port};
22 5 ▶ $port = 79 unless defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 1 4 5
23 5 my $forward_deny = $args->{forward_deny};
24 5 ▶ $forward_deny = 0 unless defined $forward_deny;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $forward_deny 3 2 5
25 5 my $forward = $args->{forward};
26 5 ▶ $forward = 0 unless defined $forward;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $forward 4 1 5
27 bless {
28 hostname => $args->{hostname},
29 port => $port,
30 0 ▶ on_error => $args->{on_error} || sub { carp $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 0 ▶ on_bind => $args->{on_bind} || sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
32 5 ▶ forward_deny => $forward_deny,
Condition50%3/6
$args->{'on_error'} || sub { carp($_[0]); }
l!l&&r!l&&!r
050
$args->{'on_bind'} || sub { }
l!l&&r!l&&!r
410
MC/DC0%0/2
$args->{'on_error'} || sub { carp($_[0]); }
$args->{'on_error'} sub { carp($_[0]); }
MC/DC0%0/2
$args->{'on_bind'} || sub { }
$args->{'on_bind'} sub { }
Truth table33%1/3
A || B
A= $args->{'on_error'}B= sub { carp($_[0]); }
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $args->{'on_bind'}B= sub { }
ABresult
1X1
011
000
33 forward => $forward,
34 }, $class;
35 }
36  
37  
38 sub start
39 6 ▶{
Subroutine start: called CC=9 SCAR=22.1
Pod documented
40 6 my $self = shift;
41 6 my $callback = shift;
42 6 ▶ my $args = ref $_[0] eq 'HASH' ? (\%{$_[0]}) : ({@_});
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 1 5 6
43  
44 6 ▶ croak "already started" if $self->{guard};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"guard"} 0 6 6
45  
46 6 for(qw( hostname port on_error on_bind forward forward_deny ))
47 {
48 36 ▶ next if defined $args->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$_} 3 33 36
49 33 $args->{$_} = $self->{$_};
50 }
51  
52  
53 6 my $forward = $args->{forward};
54 6 ▶ $forward = $self->{forward} unless defined $forward;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $forward 0 6 6
55 6 ▶ if($forward)
Branch: 1 branch
BranchTrueFalseTotal
if ($forward) 1 5 6
56 {
57 1 ▶ unless(ref $forward)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $forward) 0 1 1
58 {
59 0 require AnyEvent::Finger::Client;
60 0 $args->{forward} = AnyEvent::Finger::Client->new;
61 }
62 }
63  
64 20 ▶ my $cb = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 20 my ($fh, $host, $port) = @_;
66  
67 20 my $handle;
68 $handle = AnyEvent::Handle->new(
69 fh => $fh,
70 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 my ($hdl, $fatal, $msg) = @_;
72 0 $args->{on_error}->($msg);
73 0 $_[0]->destroy;
74 },
75 0 ▶ on_eof => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 $handle->destroy;
77 },
78 20 );
79  
80 20 ▶ $handle->push_read( line => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
81 20 my($handle, $line) = @_;
82 20 $line =~ s/\015?\012//g;
83  
84 43 ▶ my $res = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
85 43 my $lines = shift;
86 43 ▶ $lines = [ $lines ] unless ref $lines eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $lines eq "ARRAY" 9 34 43
87 43 foreach my $line (@$lines)
88 {
89 65 ▶ if(defined $line)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) { } 45 20 65
90 {
91 45 $handle->push_write($line . "\015\012");
92 }
93 else
94 {
95 20 $handle->destroy;
96 20 return;
97 }
98 }
99 20 };
100  
101 20 bless $res, 'AnyEvent::Finger::Response';
102 20 my $req = AnyEvent::Finger::Request->new($line);
103  
104 my $tx = bless {
105 req => $req,
106 res => $res,
107 remote_port => $port,
108 local_port => $self->{bindport},
109 20 remote_address => $host,
110 }, 'AnyEvent::Finger::Transaction';
111  
112 20 ▶ if($args->{forward_deny} && $tx->req->forward_request)
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"forward_deny"} and $tx->req->forward_request) 4 16 20
Condition100%3/3
$args->{"forward_deny"} and $tx->req->forward_request
!ll&&!rl&&r
1154
MC/DC100%2/2
$args->{"forward_deny"} and $tx->req->forward_request
$args->{"forward_deny"} $tx->req->forward_request
Truth table100%3/3
A and B
A= $args->{"forward_deny"}B= $tx->req->forward_request
ABresult
0X0
100
111
113 {
114 4 $res->(['finger forwarding service denied', undef]);
115 4 return;
116 }
117  
118 16 ▶ if($forward && $req->forward_request)
Branch: 1 branch
BranchTrueFalseTotal
if ($forward and $req->forward_request) 5 11 16
Condition100%3/3
$forward and $req->forward_request
!ll&&!rl&&r
1015
MC/DC100%2/2
$forward and $req->forward_request
$forward $req->forward_request
Truth table100%3/3
A and B
A= $forwardB= $req->forward_request
ABresult
0X0
100
111
119 {
120 5 my $host = pop @{ $req->hostnames };
121 5 my $new_request = join '@', $req->username, @{ $req->hostnames };
122 5 ▶ $new_request = '/W ' . $new_request if $req->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $req->verbose 2 3 5
123 5 ▶ $forward->finger($new_request, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 5 my $lines = shift;
125 5 push @$lines, undef;
126 5 $res->($lines);
127 5 }, { hostname => $host });
128 5 return;
129 }
130  
131 11 $callback->($tx);
132 20 });
133 6 };
134  
135 6 my $port = $args->{port};
136 6 ▶ undef $port if $port == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $port == 0 6 0 6
137  
138 6 ▶ $self->{guard} = tcp_server $args->{hostname}, $port, $cb, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
139 6 my($fh, $host, $port) = @_;
140 6 $self->{bindport} = $port;
141 6 $args->{on_bind}->($self);
142 6 };
143  
144 6 $self;
145 }
146  
147  
148 10 ▶sub bindport { shift->{bindport} }
Subroutine bindport: called CC=1 SCAR=0.0
Pod documented
149  
150  
151 sub stop
152 1 ▶{
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
153 1 my($self) = @_;
154 1 delete $self->{guard};
155 1 delete $self->{bindport};
156 1 $self;
157 }
158  
159 5 1;
160  
161 __END__
↑ Summary Finger/Client.pm »