blib/lib/Linux/Event/WebSocket/Server.pm

filter: Statementstmt 81.8% 63 / 77 Branchbran 65.6% 21 / 32 Conditioncond 34.4% 10 / 29 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 60.0% 15 / 25 Podpod 0.0% 0 / 13 totaltotal 57.3% 109 / 190 CC 30 SCAR 18.4
CC
30
Cov
68%
new
11.2
_load_connection_class
10.5
_positive_integer
8.9
CRAP 6.3
« blib/lib/Linux/Event/WebSocket/Server/_HTTPConnection.pm ↑ Summary blib/lib/Linux/Event/WebSocket/Connection.pm »
1 package Linux::Event::WebSocket::Server;
2 6 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use Linux::Event::IO::Sock::Listener;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Linux::Event::WebSocket::Server::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Linux::Event::WebSocket::Server::_HTTPConnection;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 our $VERSION = '0.003';
13  
14 6 my $DEFAULT_MAX_MESSAGE_SIZE = 16 * 1024 * 1024;
15  
16 7 ▶sub _load_connection_class ($class) {
Subroutine _load_connection_class: called CC=6 SCAR=23.5
17 7 ▶ croak 'new(): connection_class must be a package name'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $class or ref $class or not $class =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/u 0 7 7
Condition33%2/6
not defined $class or ref $class or not $class =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/u
l!l&&r!l&&!r
007
not defined $class or ref $class
l!l&&r!l&&!r
007
MC/DC0%0/3
not defined $class or ref $class or not $class =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/u
not defined $class ref $class not $class =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/u
Truth table25%1/4
not(A) or B or not(C)
A= not defined $classB= ref $classC= not $class =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/u
ABCresult
1XX1
01X1
0011
0000
18 if !defined($class) || ref($class)
19 || $class !~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/;
20  
21 7 ▶ if (!$class->can('send_text')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("send_text")) 0 7 7
22 0 (my $file = "$class.pm") =~ s{::}{/}g;
23 0 require $file;
24 }
25  
26 7 ▶ croak 'new(): connection_class must inherit Linux::Event::WebSocket::Server::Connection'
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa("Linux::Event::WebSocket::Server::Connection") 0 7 7
27 if !$class->isa('Linux::Event::WebSocket::Server::Connection');
28 7 return $class;
29 }
30  
31 49 ▶sub _take_callback ($name, $option) {
Subroutine _take_callback: called CC=3 SCAR=11.0
32 49 ▶ return undef if !exists $option->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option->{$name} 27 22 49
33 22 my $callback = delete $option->{$name};
34 22 ▶ croak "new(): $name must be a coderef" if ref($callback) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback ne "CODE" 0 22 22
35 22 return $callback;
36 }
37  
38 7 ▶sub _subprotocols ($value) {
Subroutine _subprotocols: called CC=6 SCAR=20.7
39 7 ▶ croak 'new(): subprotocols must be an array reference'
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "ARRAY" 0 7 7
40 if ref($value) ne 'ARRAY';
41  
42 7 my @copy;
43 7 for my $token (@$value) {
44 1 ▶ croak 'new(): each subprotocol must be a WebSocket token'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $token or ref $token or not $token =~ /\A[!#\$%&'*+\-.^_`|~0-9A-Za-z]+\z/u 0 1 1
Condition33%2/6
not defined $token or ref $token or not $token =~ /\A[!#\$%&'*+\-.^_`|~0-9A-Za-z]+\z/u
l!l&&r!l&&!r
001
not defined $token or ref $token
l!l&&r!l&&!r
001
MC/DC0%0/3
not defined $token or ref $token or not $token =~ /\A[!#\$%&'*+\-.^_`|~0-9A-Za-z]+\z/u
not defined $token ref $token not $token =~ /\A[!#\$%&'*+\-.^_`|~0-9A-Za-z]+\z/u
Truth table25%1/4
not(A) or B or not(C)
A= not defined $tokenB= ref $tokenC= not $token =~ /\A[!#\$%&'*+\-.^_`|~0-9A-Za-z]+\z/u
ABCresult
1XX1
01X1
0011
0000
45 if !defined($token) || ref($token)
46 || $token !~ /\A[!#\$%&'*+\-.^_`|~0-9A-Za-z]+\z/;
47 1 push @copy, "$token";
48 }
49 7 return \@copy;
50 }
51  
52 7 ▶sub _positive_integer ($where, $value) {
Subroutine _positive_integer: called CC=5 SCAR=21.9
53 7 ▶ croak "$where must be a positive integer"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value or not "$value" =~ /\A[0-9]+\z/u or $value < 1 0 7 7
Condition33%3/9
not defined $value or ref $value or not "$value" =~ /\A[0-9]+\z/u or $value < 1
l!l&&r!l&&!r
007
not defined $value or ref $value or not "$value" =~ /\A[0-9]+\z/u
l!l&&r!l&&!r
007
not defined $value or ref $value
l!l&&r!l&&!r
007
MC/DC0%0/4
not defined $value or ref $value or not "$value" =~ /\A[0-9]+\z/u or $value < 1
not defined $value ref $value not "$value" =~ /\A[0-9]+\z/u $value < 1
Truth table20%1/5
not(A) or B or not(C) or D
A= not defined $valueB= ref $valueC= not "$value" =~ /\A[0-9]+\z/uD= $value < 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
54 if !defined($value) || ref($value)
55 || "$value" !~ /\A[0-9]+\z/ || $value < 1;
56 7 return 0 + $value;
57 }
58  
59 7 ▶sub _nonnegative_number ($where, $value) {
Subroutine _nonnegative_number: called CC=4 SCAR=17.9
60 7 ▶ croak "$where must be a non-negative number"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value or not "$value" =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/u 0 7 7
Condition33%2/6
not defined $value or ref $value or not "$value" =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/u
l!l&&r!l&&!r
007
not defined $value or ref $value
l!l&&r!l&&!r
007
MC/DC0%0/3
not defined $value or ref $value or not "$value" =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/u
not defined $value ref $value not "$value" =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/u
Truth table25%1/4
not(A) or B or not(C)
A= not defined $valueB= ref $valueC= not "$value" =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/u
ABCresult
1XX1
01X1
0011
0000
61 if !defined($value) || ref($value)
62 || "$value" !~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/;
63 7 return 0 + $value;
64 }
65  
66 7 ▶sub new ($class, %option) {
Subroutine new: called CC=11 SCAR=24.1
Pod undocumented
67 my $connection_class = _load_connection_class(
68 delete($option{connection_class})
69 7 ▶ // 'Linux::Event::WebSocket::Server::Connection',
Condition50%1/2
delete $option{"connection_class"} // "Linux::Event::WebSocket::Server::Connection"
l!l
07
MC/DC0%0/1
delete $option{"connection_class"} // "Linux::Event::WebSocket::Server::Connection"
delete $option{"connection_class"}
Truth table50%1/2
A
A= delete $option{"connection_class"}
Aresult
11
00
70 );
71  
72 7 my %callbacks;
73 7 for my $name (qw(on_open on_message on_close on_error on_drain)) {
74 35 my $callback = _take_callback($name, \%option);
75 35 (my $short = $name) =~ s/\Aon_//;
76 35 ▶ $callbacks{$short} = $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 21 14 35
77 }
78  
79 7 my $on_handshake = _take_callback('on_handshake', \%option);
80 7 my $on_listener_error = _take_callback('on_listener_error', \%option);
81  
82 my $subprotocols = _subprotocols(
83 7 ▶ exists($option{subprotocols}) ? delete($option{subprotocols}) : [],
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'subprotocols'} ? : 1 6 7
84 );
85 my $close_timeout = _nonnegative_number(
86 'new(): close_timeout',
87 7 ▶ exists($option{close_timeout}) ? delete($option{close_timeout}) : 5,
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'close_timeout'} ? : 0 7 7
88 );
89 my $max_message_size = _positive_integer(
90 'new(): max_message_size',
91 exists($option{max_message_size})
92 ? delete($option{max_message_size})
93 7 ▶ : $DEFAULT_MAX_MESSAGE_SIZE,
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'max_message_size'} ? : 0 7 7
94 );
95 7 my $data = delete $option{data};
96 7 ▶ my $secure = exists($option{tls}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'tls'} ? : 1 6 7
97  
98 7 my $config = bless {
99 connection_class => $connection_class,
100 callbacks => \%callbacks,
101 on_handshake => $on_handshake,
102 subprotocols => $subprotocols,
103 close_timeout => $close_timeout,
104 max_message_size => $max_message_size,
105 data => $data,
106 secure => $secure,
107 }, 'Linux::Event::WebSocket::Server::_Config';
108  
109 7 my %stream = (
110 class => 'Linux::Event::WebSocket::Server::_HTTPConnection',
111 data => $config,
112 );
113 7 for my $name (qw(tls tuning)) {
114 14 ▶ $stream{$name} = delete $option{$name} if exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{$name} 1 13 14
115 }
116 7 ▶ my $listener = Linux::Event::IO::Sock::Listener->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $on_listener_error ? : 0 7 7
117 %option, stream => \%stream,
118 (defined($on_listener_error)
119 ? (on_error => $on_listener_error) : ()),
120 );
121  
122 7 return bless {
123 listener => $listener,
124 config => $config,
125 data => $data,
126 connection_class => $connection_class,
127 }, $class;
128 }
129  
130 # Retain the old convenience accessor for source compatibility. With
131 # standalone Unblock::HTTP1 owning only the opening handshake there is no
132 # longer an HTTP Server object; listener() is the clear accessor.
133 0 ▶sub http_server ($self) { $self->{listener} }
Subroutine http_server: not called CC=1 SCAR=6.9
Pod undocumented
134 0 ▶sub listener ($self) { $self->{listener} }
Subroutine listener: not called CC=1 SCAR=6.9
Pod undocumented
135 0 ▶sub connection_class ($self) { $self->{connection_class} }
Subroutine connection_class: not called CC=1 SCAR=6.9
Pod undocumented
136 0 ▶sub data ($self) { $self->{data} }
Subroutine data: not called CC=1 SCAR=6.9
Pod undocumented
137 0 ▶sub loop ($self) { $self->{listener}->loop }
Subroutine loop: not called CC=1 SCAR=6.9
Pod undocumented
138 0 ▶sub host ($self) { $self->{listener}->host }
Subroutine host: not called CC=1 SCAR=6.9
Pod undocumented
139 8 ▶sub port ($self) { $self->{listener}->port }
Subroutine port: called CC=1 SCAR=0.0
Pod undocumented
140 0 ▶sub path ($self) { $self->{listener}->path }
Subroutine path: not called CC=1 SCAR=6.9
Pod undocumented
141 0 ▶sub state ($self) { $self->{listener}->state }
Subroutine state: not called CC=1 SCAR=6.9
Pod undocumented
142  
143 0 ▶sub pause ($self) { $self->{listener}->pause; return $self }
Subroutine pause: not called CC=1 SCAR=6.9
Pod undocumented
144 0 ▶sub resume ($self) { $self->{listener}->resume; return $self }
Subroutine resume: not called CC=1 SCAR=6.9
Pod undocumented
145 7 ▶sub close ($self) { $self->{listener}->close; return $self }
Subroutine close: called CC=1 SCAR=0.0
Pod undocumented
146  
147 6 1;
148  
149 __END__
« blib/lib/Linux/Event/WebSocket/Server/_HTTPConnection.pm ↑ Summary blib/lib/Linux/Event/WebSocket/Connection.pm »