Server/Connection.pm

filter: Statementstmt 75.7% 100 / 132 Branchbran 50.0% 38 / 76 Conditioncond 40.0% 6 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 54.8% 17 / 31 Podpod 100% 15 / 15 totaltotal 63.3% 176 / 278 CC 46 SCAR 24.7
CC
46
Cov
65%
recv
29.1
nodelay
20.0
max_recv_size
12.0
CRAP 11.9
« Server.pm ↑ Summary
1 package Net::WebSocket::Server::Connection;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Protocol::WebSocket::Handshake::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Protocol::WebSocket::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Socket qw(IPPROTO_TCP TCP_NODELAY);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶sub new {
Subroutine new: called CC=10 SCAR=24.6
Pod documented
14 1 my $class = shift;
15  
16 1 my %params = @_;
17  
18 my $self = {
19 socket => undef,
20 server => undef,
21 nodelay => 1,
22 1 ▶ max_send_size => eval { Protocol::WebSocket::Frame->new->{max_payload_size} } || 65536,
Condition50%1/2
eval { do { 'Protocol::WebSocket::Frame'->new->{'max_payload_size'} } } || 65536
l!l
10
MC/DC0%0/1
eval { do { 'Protocol::WebSocket::Frame'->new->{'max_payload_size'} } } || 65536
eval { do { 'Protocol::WebSocket::Frame'->new->{'max_payload_size'} } }
Truth table50%1/2
A
A= eval { do { 'Protocol::WebSocket::Frame'->new->{'max_payload_size'} } }
Aresult
11
00
23 1 max_recv_size => eval { Protocol::WebSocket::Frame->new->{max_payload_size} } || 65536,
24 0 ▶ on_handshake => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
25 0 ▶ on_ready => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
26 0 ▶ on_disconnect => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
27 0 ▶ on_utf8 => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
28 0 ▶ on_pong => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
29 0 ▶ on_binary => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
30 1 ▶ };
Condition50%1/2
eval { do { 'Protocol::WebSocket::Frame'->new->{'max_payload_size'} } } || 65536
l!l
10
MC/DC0%0/1
eval { do { 'Protocol::WebSocket::Frame'->new->{'max_payload_size'} } } || 65536
eval { do { 'Protocol::WebSocket::Frame'->new->{'max_payload_size'} } }
Truth table50%1/2
A
A= eval { do { 'Protocol::WebSocket::Frame'->new->{'max_payload_size'} } }
Aresult
11
00
31  
32 1 ▶ while (my ($key, $value) = each %params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 2 1 3
33 2 ▶ croak "Invalid $class parameter '$key'" unless exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$key} 0 2 2
34 2 ▶ croak "$class parameter '$key' expects a coderef" if ref $self->{$key} eq 'CODE' && ref $value ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{$key} eq "CODE" and ref $value ne "CODE" 0 2 2
Condition33%1/3
ref $self->{$key} eq "CODE" and ref $value ne "CODE"
!ll&&!rl&&r
200
MC/DC0%0/2
ref $self->{$key} eq "CODE" and ref $value ne "CODE"
ref $self->{$key} eq "CODE" ref $value ne "CODE"
Truth table33%1/3
A and B
A= ref $self->{$key} eq "CODE"B= ref $value ne "CODE"
ABresult
0X0
100
111
35 2 $self->{$key} = $value;
36 }
37  
38 1 croak "$class construction requires '$_'" for grep { !defined $self->{$_} } qw(socket server);
39  
40 1 $self->{handshake} = new Protocol::WebSocket::Handshake::Server();
41 1 $self->{disconnecting} = 0;
42 1 $self->{ip} = $self->{socket}->peerhost;
43 1 $self->{port} = $self->{socket}->peerport;
44  
45 # only attempt to start SSL if this is an IO::Socket::SSL-like socket that also has not completed its SSL handshake (SSL_startHandshake => 0)
46 1 ▶ $self->{needs_ssl} = 1 if $self->{socket}->can("accept_SSL") && !$self->{socket}->opened;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"socket"}->can("accept_SSL") and not $self->{"socket"}->opened 0 1 1
Condition33%1/3
$self->{"socket"}->can("accept_SSL") and not $self->{"socket"}->opened
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"socket"}->can("accept_SSL") and not $self->{"socket"}->opened
$self->{"socket"}->can("accept_SSL") not $self->{"socket"}->opened
Truth table33%1/3
A and not(B)
A= $self->{"socket"}->can("accept_SSL")B= not $self->{"socket"}->opened
ABresult
0X0
100
111
47  
48 1 bless $self, $class;
49 }
50  
51 1 ▶sub on {
Subroutine on: called CC=4 SCAR=14.0
Pod documented
52 1 my $self = shift;
53 1 my %params = @_;
54  
55 1 ▶ while (my ($key, $value) = each %params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 6 1 7
56 6 ▶ croak "Invalid event '$key'" unless exists $self->{"on_$key"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$self{"on_$key"} 0 6 6
57 6 ▶ croak "Expected a coderef for event '$key'" unless ref $value eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "CODE" 0 6 6
58 6 $self->{"on_$key"} = $value;
59 }
60 }
61  
62  
63 ### accessors
64  
65 0 ▶sub server { $_[0]->{server} }
Subroutine server: not called CC=1 SCAR=6.9
Pod documented
66  
67 0 ▶sub socket { $_[0]->{socket} }
Subroutine socket: not called CC=1 SCAR=6.9
Pod documented
68  
69 2 ▶sub is_ready { !$_[0]->{handshake} }
Subroutine is_ready: called CC=1 SCAR=0.0
Pod documented
70  
71 0 ▶sub ip { $_[0]{ip} }
Subroutine ip: not called CC=1 SCAR=6.9
Pod documented
72  
73 0 ▶sub port { $_[0]{port} }
Subroutine port: not called CC=1 SCAR=6.9
Pod documented
74  
75 0 ▶sub nodelay {
Subroutine nodelay: not called CC=4 SCAR=30.0
Pod documented
76 0 my $self = shift;
77 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
78 0 $self->{nodelay} = $_[0];
79 0 ▶ setsockopt($self->{socket}, IPPROTO_TCP, TCP_NODELAY, $self->{nodelay} ? 1 : 0) unless $self->{handshake};
Branch: 2 branches
BranchTrueFalseTotal
unless $self->{"handshake"} 0 0 0
$self->{'nodelay'} ? : 0 0 0
80 }
81 0 return $self->{nodelay};
82 }
83  
84 0 ▶sub max_send_size {
Subroutine max_send_size: not called CC=2 SCAR=17.9
Pod documented
85 0 my $self = shift;
86 0 ▶ $self->{max_send_size} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
87 0 return $self->{max_send_size};
88 }
89  
90 0 ▶sub max_recv_size {
Subroutine max_recv_size: not called CC=3 SCAR=24.8
Pod documented
91 0 my $self = shift;
92 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
93 0 ▶ croak "Cannot change max_recv_size; handshake is already complete" if $self->{parser};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parser"} 0 0 0
94 0 $self->{max_recv_size} = $_[0];
95 }
96 0 return $self->{max_recv_size};
97 }
98  
99  
100 ### methods
101  
102 2 ▶sub disconnect {
Subroutine disconnect: called CC=7 SCAR=20.5
Pod documented
103 2 my ($self, $code, $reason) = @_;
104 2 ▶ return if $self->{disconnecting};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"disconnecting"} 1 1 2
105 1 $self->{disconnecting} = 1;
106  
107 1 $self->_event('on_disconnect', $code, $reason);
108  
109 1 my $data = '';
110 1 ▶ if (defined $code || defined $reason) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code or defined $reason) 1 0 1
Condition33%1/3
defined $code or defined $reason
l!l&&r!l&&!r
100
MC/DC0%0/2
defined $code or defined $reason
defined $code defined $reason
Truth table33%1/3
A or B
A= defined $codeB= defined $reason
ABresult
1X1
011
000
111 1 ▶ $code ||= 1000;
Condition50%1/2
$code ||= 1000
l!l
10
MC/DC0%0/1
$code ||= 1000
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
112 1 ▶ $reason = '' unless defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason 0 1 1
113 1 $data = pack("na*", $code, $reason);
114 }
115 1 ▶ $self->send(close => $data) unless $self->{handshake};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handshake"} 1 0 1
116  
117 1 $self->{server}->disconnect($self->{socket});
118 }
119  
120 14 ▶sub send_binary {
Subroutine send_binary: called CC=1 SCAR=0.0
Pod documented
121 14 $_[0]->send(binary => $_[1]);
122 }
123  
124 5 ▶sub send_utf8 {
Subroutine send_utf8: called CC=1 SCAR=0.0
Pod documented
125 5 $_[0]->send(text => Encode::encode('UTF-8', $_[1]));
126 }
127  
128 20 ▶sub send {
Subroutine send: called CC=5 SCAR=19.8
Pod documented
129 20 my ($self, $type, $data) = @_;
130  
131 20 ▶ if ($self->{handshake}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"handshake"}) 0 20 20
132 0 carp "tried to send data before finishing handshake";
133 0 return 0;
134 }
135  
136 20 my $frame = new Protocol::WebSocket::Frame(type => $type, max_payload_size => $self->{max_send_size});
137 20 ▶ $frame->append($data) if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 20 0 20
138  
139 20 my $bytes = eval { $frame->to_bytes };
140 20 ▶ if (!defined $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bytes) 0 20 20
141 0 ▶ carp "error while building message: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
142 0 return;
143 }
144  
145 20 syswrite($self->{socket}, $bytes);
146 }
147  
148 19 ▶sub recv {
Subroutine recv: called CC=18 SCAR=33.7
Pod documented
149 19 my ($self) = @_;
150  
151 19 ▶ if ($self->{needs_ssl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needs_ssl"}) 0 19 19
152 0 my $ssl_done = $self->{socket}->accept_SSL;
153 0 ▶ if ($self->{socket}->errstr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"socket"}->errstr) 0 0 0
154 0 $self->disconnect;
155 0 return;
156 }
157 0 ▶ return unless $ssl_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $ssl_done 0 0 0
158 0 $self->{needs_ssl} = 0;
159 }
160  
161 19 my ($len, $data) = (0, "");
162 19 ▶ if (!($len = sysread($self->{socket}, $data, 8192))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($len = sysread($self->{"socket"}, $data, 8192)) 0 19 19
163 0 $self->disconnect();
164 0 return;
165 }
166  
167 # read remaining data
168 19 ▶ $len = sysread($self->{socket}, $data, 8192, length($data)) while $len >= 8192;
Branch: 1 branch
BranchTrueFalseTotal
$len >= 8192 and do { $len = sysread($self->{"socket"}, $data, 8192, length $data) } 12 19 31
169  
170 19 ▶ if ($self->{handshake}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"handshake"}) 1 18 19
171 1 $self->{handshake}->parse($data);
172 1 ▶ if ($self->{handshake}->error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'handshake'}->error) { } 0 1 1
elsif ($self->{'handshake'}->is_done) { } 1 0 1
173 0 $self->disconnect(1002);
174 } elsif ($self->{handshake}->is_done) {
175 1 $self->_event(on_handshake => $self->{handshake});
176 1 ▶ return unless do { local $SIG{__WARN__} = sub{}; $self->{socket}->connected };
Subroutine __ANON__: not called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
unless do { local $SIG{"__WARN__"} = sub { } ; $self->{"socket"}->connected } 0 1 1
177  
178 1 syswrite($self->{socket}, $self->{handshake}->to_string);
179 1 delete $self->{handshake};
180  
181 1 $self->{parser} = new Protocol::WebSocket::Frame(max_payload_size => $self->{max_recv_size});
182 1 ▶ setsockopt($self->{socket}, IPPROTO_TCP, TCP_NODELAY, 1) if $self->{nodelay};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nodelay"} 1 0 1
183 1 $self->_event('on_ready');
184 }
185 1 return;
186 }
187  
188 18 $self->{parser}->append($data);
189  
190 18 my $bytes;
191 18 ▶ while (defined ($bytes = eval { $self->{parser}->next_bytes })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($bytes = eval { do { $self->{"parser"}->next_bytes } })) 18 17 35
192 18 ▶ if ($self->{parser}->is_binary) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'parser'}->is_binary) { } 6 12 18
elsif ($self->{'parser'}->is_text) { } 5 7 12
elsif ($self->{'parser'}->is_pong) { } 6 1 7
elsif ($self->{'parser'}->is_close) { } 1 0 1
193 6 $self->_event(on_binary => $bytes);
194 } elsif ($self->{parser}->is_text) {
195 5 $self->_event(on_utf8 => Encode::decode('UTF-8', $bytes));
196 } elsif ($self->{parser}->is_pong) {
197 6 $self->_event(on_pong => $bytes);
198 } elsif ($self->{parser}->is_close) {
199 1 ▶ $self->disconnect(length $bytes ? unpack("na*",$bytes) : ());
Branch: 1 branch
BranchTrueFalseTotal
length $bytes ? : 1 0 1
200 1 return;
201 }
202 }
203  
204 17 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 17 17
205 0 $self->disconnect(1002);
206 0 return;
207 }
208 }
209  
210 ### internal methods
211  
212 20 ▶sub _event {
Subroutine _event: called CC=1 SCAR=0.0
213 20 my ($self, $event, @args) = @_;
214 20 $self->{$event}($self, @args);
215 }
216  
217 3 1; # End of Net::WebSocket::Server
218  
219 __END__
« Server.pm ↑ Summary