WebSocket/Endpoint.pm

filter: Statementstmt 93.0% 107 / 115 Branchbran 69.2% 18 / 26 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 96.5% 28 / 29 Podpod 0.0% 0 / 12 totaltotal 83.6% 159 / 190 CC 16 SCAR 10.8
CC
16
Cov
88%
_handle_control_frame
8.8
new
4.0
close
2.0
CRAP 3.0
« WebSocket/X/BadHTTPStatus.pm ↑ Summary WebSocket/Base/DataFrame.pm »
1 package Net::WebSocket::Endpoint;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Net::WebSocket::Endpoint
8  
9 =head1 DESCRIPTION
10  
11 See L<Net::WebSocket::Endpoint::Server>.
12  
13 =cut
14  
15 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 11 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 11 ▶use Net::WebSocket::Defragmenter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶use Net::WebSocket::Frame::close ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶use Net::WebSocket::Frame::ping ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 11 ▶use Net::WebSocket::Frame::pong ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 11 ▶use Net::WebSocket::Message ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 11 ▶use Net::WebSocket::PingStore ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶use Net::WebSocket::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 11 ▶use constant DEFAULT_MAX_PINGS => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 10 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod undocumented
31 10 my ($class, %opts) = @_;
32  
33 10 my @missing = grep { !length $opts{$_} } qw( out parser );
34  
35 10 ▶ die "Missing: [@missing]" if @missing;
Branch: 1 branch
BranchTrueFalseTotal
if @missing 0 10 10
36  
37 10 ▶ if ( !(ref $opts{'out'})->can('write') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($opts{"out"})->can("write")) 0 10 10
38 0 die "“out” ($opts{'out'}) needs a write() method!";
39 }
40  
41 10 my $self;
42  
43 my $defragger = Net::WebSocket::Defragmenter->new(
44 parser => $opts{'parser'},
45  
46 on_data_frame => $opts{'on_data_frame'},
47  
48 7 ▶ on_control_frame => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
49 7 $self->_handle_control_frame(shift);
50 },
51  
52 2 ▶ on_protocol_error => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
53 2 ( undef, my $msg ) = @_;
54  
55 #For now … there may be some multiplexing extension
56 #that allows some other behavior down the line,
57 #but let’s enforce standard protocol for now.
58 2 my $err_frame = Net::WebSocket::Frame::close->new(
59 code => 'PROTOCOL_ERROR',
60 reason => $msg,
61 $self->FRAME_MASK_ARGS(),
62 );
63  
64 2 $self->_write_frame($err_frame);
65 },
66 10 );
67  
68 $self = {
69 _max_pings => $class->DEFAULT_MAX_PINGS(),
70  
71 _ping_store => Net::WebSocket::PingStore->new(),
72  
73 _defragger => $defragger,
74  
75 10 ▶ (map { defined($opts{$_}) ? ( "_$_" => $opts{$_} ) : () } qw(
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{$_} ? : 10 20 30
76 max_pings
77  
78 on_data_frame
79  
80 out
81 )),
82 };
83  
84 10 return bless $self, $class;
85 }
86  
87 23 ▶sub get_next_message {
Subroutine get_next_message: called CC=1 SCAR=0.0
Pod undocumented
88 23 my ($self) = @_;
89  
90 23 $self->_verify_not_closed();
91  
92 23 return $self->{'_defragger'}->get_next_message();
93 }
94  
95 2 ▶sub create_message {
Subroutine create_message: called CC=2 SCAR=7.0
Pod undocumented
96 2 my ($self, $frame_type, $payload) = @_;
97  
98 2 require Net::WebSocket::FrameTypeName;
99  
100 2 my $frame_class = Net::WebSocket::FrameTypeName::get_module($frame_type);
101 2 ▶ Module::Runtime::require_module($frame_class) if !$frame_class->can('new');
Branch: 1 branch
BranchTrueFalseTotal
unless $frame_class->can("new") 2 0 2
102  
103 2 return Net::WebSocket::Message->new(
104 $frame_class->new(
105 payload => $payload,
106 $self->FRAME_MASK_ARGS(),
107 ),
108 );
109 }
110  
111 10 ▶sub check_heartbeat {
Subroutine check_heartbeat: called CC=2 SCAR=6.9
Pod undocumented
112 10 my ($self) = @_;
113  
114 10 my $ping_counter = $self->{'_ping_store'}->get_count();
115  
116 10 ▶ if ($ping_counter == $self->{'_max_pings'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ping_counter == $self->{'_max_pings'}) { } 1 9 10
117 1 $self->close(
118 code => 'POLICY_VIOLATION',
119 reason => "Unanswered ping(s): $ping_counter",
120 );
121 }
122 else {
123 9 my $ping_message = $self->{'_ping_store'}->add();
124  
125 9 my $ping = Net::WebSocket::Frame::ping->new(
126 payload => $ping_message,
127 $self->FRAME_MASK_ARGS(),
128 );
129  
130 9 $self->_write_frame($ping);
131 }
132  
133 10 return;
134 }
135  
136 1 ▶sub close {
Subroutine close: called CC=2 SCAR=7.1
Pod undocumented
137 1 my ($self, %opts) = @_;
138  
139 my $close = Net::WebSocket::Frame::close->new(
140 $self->FRAME_MASK_ARGS(),
141 code => $opts{'code'} || 'ENDPOINT_UNAVAILABLE',
142 1 ▶ reason => $opts{'reason'},
Condition50%1/2
$opts{'code'} || 'ENDPOINT_UNAVAILABLE'
l!l
10
MC/DC0%0/1
$opts{'code'} || 'ENDPOINT_UNAVAILABLE'
$opts{'code'}
Truth table50%1/2
A
A= $opts{'code'}
Aresult
11
00
143 );
144  
145 1 return $self->_close_with_frame($close);
146 }
147  
148 4 ▶sub _close_with_frame {
Subroutine _close_with_frame: called CC=1 SCAR=0.0
149 4 my ($self, $close_frame) = @_;
150  
151 4 $self->_write_frame($close_frame);
152  
153 4 $self->{'_sent_close_frame'} = $close_frame;
154  
155 4 return $self;
156 }
157  
158 11 *shutdown = *close;
159  
160 2 ▶sub is_closed {
Subroutine is_closed: called CC=2 SCAR=6.9
Pod undocumented
161 2 my ($self) = @_;
162 2 ▶ return $self->{'_sent_close_frame'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_sent_close_frame'} ? : 1 1 2
163 }
164  
165 1 ▶sub received_close_frame {
Subroutine received_close_frame: called CC=1 SCAR=0.0
Pod undocumented
166 1 my ($self) = @_;
167 1 return $self->{'_received_close_frame'};
168 }
169  
170 5 ▶sub sent_close_frame {
Subroutine sent_close_frame: called CC=1 SCAR=0.0
Pod undocumented
171 5 my ($self) = @_;
172 5 return $self->{'_sent_close_frame'};
173 }
174  
175 0 ▶sub die_on_close {
Subroutine die_on_close: not called CC=1 SCAR=6.9
Pod undocumented
176 0 my ($self) = @_;
177  
178 0 $self->{'_no_die_on_close'} = 0;
179  
180 0 return;
181 }
182  
183 3 ▶sub do_not_die_on_close {
Subroutine do_not_die_on_close: called CC=1 SCAR=0.0
Pod undocumented
184 3 my ($self) = @_;
185  
186 3 $self->{'_no_die_on_close'} = 1;
187  
188 3 return;
189 }
190  
191 #----------------------------------------------------------------------
192  
193 3 ▶sub on_ping {
Subroutine on_ping: called CC=1 SCAR=0.0
Pod undocumented
194 3 my ($self, $frame) = @_;
195  
196 3 $self->_write_frame(
197 Net::WebSocket::Frame::pong->new(
198 payload => $frame->get_payload(),
199 $self->FRAME_MASK_ARGS(),
200 ),
201 );
202  
203 3 return;
204 }
205  
206 1 ▶sub on_pong {
Subroutine on_pong: called CC=1 SCAR=0.0
Pod undocumented
207 1 my ($self, $frame) = @_;
208  
209 1 $self->{'_ping_store'}->remove( $frame->get_payload() );
210  
211 1 return;
212 }
213  
214 #----------------------------------------------------------------------
215  
216 23 ▶sub _verify_not_closed {
Subroutine _verify_not_closed: called CC=2 SCAR=7.1
217 23 my ($self) = @_;
218  
219 23 ▶ die Net::WebSocket::X->create('EndpointAlreadyClosed') if $self->{'_closed'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} 0 23 23
220  
221 23 return;
222 }
223  
224 7 ▶sub _handle_control_frame {
Subroutine _handle_control_frame: called CC=8 SCAR=21.8
225 7 my ($self, $frame) = @_;
226  
227 7 my $type = $frame->get_type();
228  
229 7 ▶ if ($type eq 'close') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'close') { } 3 4 7
elsif (my $handler_cr = $self->can("on_$type")) { } 4 0 4
230 3 ▶ if (!$self->{'_sent_close_frame'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_sent_close_frame"}) 3 0 3
231 3 my ($code, $reason) = $frame->get_code_and_reason();
232  
233 3 ▶ if ($code && $code > 4999) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code and $code > 4999) 1 2 3
Condition100%3/3
$code and $code > 4999
!ll&&!rl&&r
111
MC/DC100%2/2
$code and $code > 4999
$code $code > 4999
Truth table100%3/3
A and B
A= $codeB= $code > 4999
ABresult
0X0
100
111
234 1 my $reason_chunk = substr( $reason, 0, 50 );
235 1 $reason = "Invalid close code ($code, reason: $reason_chunk) received.";
236 1 $code = 'PROTOCOL_ERROR';
237 }
238  
239 $self->_close_with_frame(
240 3 Net::WebSocket::Frame::close->new(
241 code => $code,
242 reason => $reason,
243 $self->FRAME_MASK_ARGS(),
244 ),
245 );
246 }
247  
248 3 ▶ if ($self->{'_received_close_frame'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_received_close_frame'}) { } 0 3 3
249 0 warn sprintf('Extra close frame received! (%v.02x)', $frame->to_bytes());
250 }
251 else {
252 3 $self->{'_received_close_frame'} = $frame;
253 }
254  
255 3 ▶ if (!$self->{'_no_die_on_close'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_no_die_on_close"}) 0 3 3
256 0 die Net::WebSocket::X->create('ReceivedClose', $frame);
257 }
258 }
259 elsif ( my $handler_cr = $self->can("on_$type") ) {
260 4 $handler_cr->( $self, $frame );
261 }
262 else {
263 0 my $ref = ref $self;
264 0 die Net::WebSocket::X->create(
265 'ReceivedBadControlFrame',
266 "“$ref” cannot handle a control frame of type “$type”",
267 );
268 }
269  
270 7 return;
271 }
272  
273 18 ▶sub _write_frame {
Subroutine _write_frame: called CC=1 SCAR=0.0
274 18 my ($self, $frame) = @_;
275  
276 18 return $self->{'_out'}->write($frame->to_bytes());
277 }
278  
279 11 1;
« WebSocket/X/BadHTTPStatus.pm ↑ Summary WebSocket/Base/DataFrame.pm »