| 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 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 1 | ▶ | use Call::Context (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | |||||||||||||||
| 20 | 1 | ▶ | use Net::WebSocket::Frame::close (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 1 | ▶ | use Net::WebSocket::Frame::ping (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 1 | ▶ | use Net::WebSocket::Frame::pong (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 1 | ▶ | use Net::WebSocket::Message (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 1 | ▶ | use Net::WebSocket::PingStore (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | 1 | ▶ | use Net::WebSocket::X (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | |||||||||||||||
| 27 | 1 | ▶ | use constant DEFAULT_MAX_PINGS => 3; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | |||||||||||||||
| 29 | sub new { | ||||||||||||||
| 30 | 1 | ▶ | my ($class, %opts) = @_; | ||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||
| 31 | |||||||||||||||
| 32 | 1 | my @missing = grep { !length $opts{$_} } qw( out parser ); | |||||||||||||
| 33 | |||||||||||||||
| 34 | 1 | ▶ | die "Missing: [@missing]" if @missing; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | |||||||||||||||
| 36 | 1 | ▶ | if ( !(ref $opts{'out'})->can('write') ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | 0 | die "“out” ($opts{'out'}) needs a write() method!"; | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | my $self = { | ||||||||||||||
| 41 | _fragments => [], | ||||||||||||||
| 42 | |||||||||||||||
| 43 | _max_pings => $class->DEFAULT_MAX_PINGS(), | ||||||||||||||
| 44 | |||||||||||||||
| 45 | _ping_store => Net::WebSocket::PingStore->new(), | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 1 | ▶ | (map { defined($opts{$_}) ? ( "_$_" => $opts{$_} ) : () } qw( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | parser | ||||||||||||||
| 49 | max_pings | ||||||||||||||
| 50 | |||||||||||||||
| 51 | on_data_frame | ||||||||||||||
| 52 | |||||||||||||||
| 53 | out | ||||||||||||||
| 54 | )), | ||||||||||||||
| 55 | }; | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 1 | return bless $self, $class; | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | sub get_next_message { | ||||||||||||||
| 61 | 9 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
get_next_message: called CC=9 SCAR=22.9
Pod
undocumented
| |||||||||||||||
| 62 | |||||||||||||||
| 63 | 9 | $self->_verify_not_closed(); | |||||||||||||
| 64 | |||||||||||||||
| 65 | 9 | my $_msg_frame; | |||||||||||||
| 66 | |||||||||||||||
| 67 | 9 | ▶ | if ( $_msg_frame = $self->{'_parser'}->get_next_frame() ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 9 | ▶ | if ($_msg_frame->is_control_frame()) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 2 | $self->_handle_control_frame($_msg_frame); | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | else { | ||||||||||||||
| 72 | 7 | ▶ | if ($self->{'_on_data_frame'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 0 | $self->{'_on_data_frame'}->($_msg_frame); | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | #Failure cases: | ||||||||||||||
| 77 | # - continuation without prior fragment | ||||||||||||||
| 78 | # - non-continuation within fragment | ||||||||||||||
| 79 | |||||||||||||||
| 80 | 7 | ▶ | if ( $_msg_frame->get_type() eq 'continuation' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 81 | 2 | ▶ | if ( !@{ $self->{'_fragments'} } ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 0 | $self->_got_continuation_during_non_fragment($_msg_frame); | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | } | ||||||||||||||
| 85 | 5 | elsif ( @{ $self->{'_fragments'} } ) { | |||||||||||||
| 86 | 0 | $self->_got_non_continuation_during_fragment($_msg_frame); | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 7 | ▶ | if ($_msg_frame->get_fin()) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | return Net::WebSocket::Message::create_from_frames( | ||||||||||||||
| 91 | 5 | splice( @{ $self->{'_fragments'} } ), | |||||||||||||
| 92 | $_msg_frame, | ||||||||||||||
| 93 | ); | ||||||||||||||
| 94 | } | ||||||||||||||
| 95 | else { | ||||||||||||||
| 96 | 2 | push @{ $self->{'_fragments'} }, $_msg_frame; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | } | ||||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 4 | ▶ | return defined($_msg_frame) ? q<> : undef; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | sub check_heartbeat { | ||||||||||||||
| 105 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
check_heartbeat: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 106 | |||||||||||||||
| 107 | 0 | my $ping_counter = $self->{'_ping_store'}->get_count(); | |||||||||||||
| 108 | |||||||||||||||
| 109 | 0 | ▶ | if ($ping_counter == $self->{'_max_pings'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | 0 | $self->close( | |||||||||||||
| 111 | code => 'POLICY_VIOLATION', | ||||||||||||||
| 112 | reason => "Unanswered ping(s): $ping_counter", | ||||||||||||||
| 113 | ); | ||||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 0 | my $ping_message = $self->{'_ping_store'}->add(); | |||||||||||||
| 117 | |||||||||||||||
| 118 | 0 | my $ping = Net::WebSocket::Frame::ping->new( | |||||||||||||
| 119 | payload_sr => \$ping_message, | ||||||||||||||
| 120 | $self->FRAME_MASK_ARGS(), | ||||||||||||||
| 121 | ); | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 0 | $self->_write_frame($ping); | |||||||||||||
| 124 | |||||||||||||||
| 125 | 0 | return; | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | sub close { | ||||||||||||||
| 129 | 0 | ▶ | my ($self, %opts) = @_; | ||||||||||||
|
Subroutine
close: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 130 | |||||||||||||||
| 131 | my $close = Net::WebSocket::Frame::close->new( | ||||||||||||||
| 132 | $self->FRAME_MASK_ARGS(), | ||||||||||||||
| 133 | code => $opts{'code'} || 'ENDPOINT_UNAVAILABLE', | ||||||||||||||
| 134 | 0 | ▶ | reason => $opts{'reason'}, | ||||||||||||
|
Condition0%0/2
$opts{'code'} || 'ENDPOINT_UNAVAILABLE'
MC/DC0%0/1
$opts{'code'} || 'ENDPOINT_UNAVAILABLE'
$opts{'code'}
Truth table0%0/2
A
A= $opts{'code'}
| |||||||||||||||
| 135 | ); | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 0 | return $self->_close_with_frame($close); | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | |||||||||||||||
| 140 | sub _close_with_frame { | ||||||||||||||
| 141 | 0 | ▶ | my ($self, $close_frame) = @_; | ||||||||||||
|
Subroutine
_close_with_frame: not called CC=1 SCAR=6.9
| |||||||||||||||
| 142 | |||||||||||||||
| 143 | 0 | $self->_write_frame($close_frame); | |||||||||||||
| 144 | |||||||||||||||
| 145 | 0 | $self->{'_sent_close_frame'} = $close_frame; | |||||||||||||
| 146 | |||||||||||||||
| 147 | 0 | return $self; | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | 1 | *shutdown = *close; | |||||||||||||
| 151 | |||||||||||||||
| 152 | sub is_closed { | ||||||||||||||
| 153 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
is_closed: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 154 | 0 | ▶ | return $self->{'_sent_close_frame'} ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 155 | } | ||||||||||||||
| 156 | |||||||||||||||
| 157 | sub received_close_frame { | ||||||||||||||
| 158 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
received_close_frame: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 159 | 0 | return $self->{'_received_close_frame'}; | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | sub sent_close_frame { | ||||||||||||||
| 163 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
sent_close_frame: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 164 | 0 | return $self->{'_sent_close_frame'}; | |||||||||||||
| 165 | } | ||||||||||||||
| 166 | |||||||||||||||
| 167 | sub die_on_close { | ||||||||||||||
| 168 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
die_on_close: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 169 | |||||||||||||||
| 170 | 0 | $self->{'_no_die_on_close'} = 0; | |||||||||||||
| 171 | |||||||||||||||
| 172 | 0 | return; | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | sub do_not_die_on_close { | ||||||||||||||
| 176 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
do_not_die_on_close: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 177 | |||||||||||||||
| 178 | 0 | $self->{'_no_die_on_close'} = 1; | |||||||||||||
| 179 | |||||||||||||||
| 180 | 0 | return; | |||||||||||||
| 181 | } | ||||||||||||||
| 182 | |||||||||||||||
| 183 | #---------------------------------------------------------------------- | ||||||||||||||
| 184 | |||||||||||||||
| 185 | sub on_ping { | ||||||||||||||
| 186 | 2 | ▶ | my ($self, $frame) = @_; | ||||||||||||
|
Subroutine
on_ping: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 187 | |||||||||||||||
| 188 | 2 | $self->_write_frame( | |||||||||||||
| 189 | Net::WebSocket::Frame::pong->new( | ||||||||||||||
| 190 | payload_sr => \$frame->get_payload(), | ||||||||||||||
| 191 | $self->FRAME_MASK_ARGS(), | ||||||||||||||
| 192 | ), | ||||||||||||||
| 193 | ); | ||||||||||||||
| 194 | |||||||||||||||
| 195 | 2 | return; | |||||||||||||
| 196 | } | ||||||||||||||
| 197 | |||||||||||||||
| 198 | sub on_pong { | ||||||||||||||
| 199 | 0 | ▶ | my ($self, $frame) = @_; | ||||||||||||
|
Subroutine
on_pong: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 200 | |||||||||||||||
| 201 | 0 | $self->{'_ping_store'}->remove( $frame->get_payload() ); | |||||||||||||
| 202 | |||||||||||||||
| 203 | 0 | return; | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | |||||||||||||||
| 206 | #---------------------------------------------------------------------- | ||||||||||||||
| 207 | |||||||||||||||
| 208 | sub _got_continuation_during_non_fragment { | ||||||||||||||
| 209 | 0 | ▶ | my ($self, $frame) = @_; | ||||||||||||
|
Subroutine
_got_continuation_during_non_fragment: not called CC=1 SCAR=6.9
| |||||||||||||||
| 210 | |||||||||||||||
| 211 | 0 | my $msg = sprintf('Received continuation outside of fragment!'); | |||||||||||||
| 212 | |||||||||||||||
| 213 | #For now … there may be some multiplexing extension | ||||||||||||||
| 214 | #that allows some other behavior down the line, | ||||||||||||||
| 215 | #but let’s enforce standard protocol for now. | ||||||||||||||
| 216 | 0 | my $err_frame = Net::WebSocket::Frame::close->new( | |||||||||||||
| 217 | code => 'PROTOCOL_ERROR', | ||||||||||||||
| 218 | reason => $msg, | ||||||||||||||
| 219 | $self->FRAME_MASK_ARGS(), | ||||||||||||||
| 220 | ); | ||||||||||||||
| 221 | |||||||||||||||
| 222 | 0 | $self->_write_frame($err_frame); | |||||||||||||
| 223 | |||||||||||||||
| 224 | 0 | die Net::WebSocket::X->create( 'ReceivedBadControlFrame', $msg ); | |||||||||||||
| 225 | } | ||||||||||||||
| 226 | |||||||||||||||
| 227 | sub _got_non_continuation_during_fragment { | ||||||||||||||
| 228 | 0 | ▶ | my ($self, $frame) = @_; | ||||||||||||
|
Subroutine
_got_non_continuation_during_fragment: not called CC=1 SCAR=6.9
| |||||||||||||||
| 229 | |||||||||||||||
| 230 | 0 | my $msg = sprintf('Received %s; expected continuation!', $frame->get_type()); | |||||||||||||
| 231 | |||||||||||||||
| 232 | #For now … there may be some multiplexing extension | ||||||||||||||
| 233 | #that allows some other behavior down the line, | ||||||||||||||
| 234 | #but let’s enforce standard protocol for now. | ||||||||||||||
| 235 | 0 | my $err_frame = Net::WebSocket::Frame::close->new( | |||||||||||||
| 236 | code => 'PROTOCOL_ERROR', | ||||||||||||||
| 237 | reason => $msg, | ||||||||||||||
| 238 | $self->FRAME_MASK_ARGS(), | ||||||||||||||
| 239 | ); | ||||||||||||||
| 240 | |||||||||||||||
| 241 | 0 | $self->_write_frame($err_frame); | |||||||||||||
| 242 | |||||||||||||||
| 243 | 0 | die Net::WebSocket::X->create( 'ReceivedBadControlFrame', $msg ); | |||||||||||||
| 244 | } | ||||||||||||||
| 245 | |||||||||||||||
| 246 | sub _verify_not_closed { | ||||||||||||||
| 247 | 9 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_verify_not_closed: called CC=2 SCAR=7.1
| |||||||||||||||
| 248 | |||||||||||||||
| 249 | 9 | ▶ | die Net::WebSocket::X->create('EndpointAlreadyClosed') if $self->{'_closed'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 250 | |||||||||||||||
| 251 | 9 | return; | |||||||||||||
| 252 | } | ||||||||||||||
| 253 | |||||||||||||||
| 254 | sub _handle_control_frame { | ||||||||||||||
| 255 | 2 | ▶ | my ($self, $frame) = @_; | ||||||||||||
|
Subroutine
_handle_control_frame: called CC=6 SCAR=29.3
| |||||||||||||||
| 256 | |||||||||||||||
| 257 | 2 | my $type = $frame->get_type(); | |||||||||||||
| 258 | |||||||||||||||
| 259 | 2 | ▶ | if ($type eq 'close') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 260 | 0 | ▶ | if (!$self->{'_sent_close_frame'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 261 | 0 | $self->_close_with_frame($frame); | |||||||||||||
| 262 | } | ||||||||||||||
| 263 | |||||||||||||||
| 264 | 0 | ▶ | if ($self->{'_received_close_frame'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 265 | 0 | warn sprintf('Extra close frame received! (%v.02x)', $frame->to_bytes()); | |||||||||||||
| 266 | } | ||||||||||||||
| 267 | else { | ||||||||||||||
| 268 | 0 | $self->{'_received_close_frame'} = $frame; | |||||||||||||
| 269 | } | ||||||||||||||
| 270 | |||||||||||||||
| 271 | 0 | ▶ | if (!$self->{'_no_die_on_close'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 272 | 0 | die Net::WebSocket::X->create('ReceivedClose', $frame); | |||||||||||||
| 273 | } | ||||||||||||||
| 274 | } | ||||||||||||||
| 275 | elsif ( my $handler_cr = $self->can("on_$type") ) { | ||||||||||||||
| 276 | 2 | $handler_cr->( $self, $frame ); | |||||||||||||
| 277 | } | ||||||||||||||
| 278 | else { | ||||||||||||||
| 279 | 0 | my $ref = ref $self; | |||||||||||||
| 280 | 0 | die Net::WebSocket::X->create( | |||||||||||||
| 281 | 'ReceivedBadControlFrame', | ||||||||||||||
| 282 | "“$ref” cannot handle a control frame of type “$type”", | ||||||||||||||
| 283 | ); | ||||||||||||||
| 284 | } | ||||||||||||||
| 285 | |||||||||||||||
| 286 | 2 | return; | |||||||||||||
| 287 | } | ||||||||||||||
| 288 | |||||||||||||||
| 289 | sub _write_frame { | ||||||||||||||
| 290 | 2 | ▶ | my ($self, $frame) = @_; | ||||||||||||
|
Subroutine
_write_frame: called CC=1 SCAR=0.0
| |||||||||||||||
| 291 | |||||||||||||||
| 292 | 2 | return $self->{'_out'}->write($frame->to_bytes()); | |||||||||||||
| 293 | } | ||||||||||||||
| 294 | |||||||||||||||
| 295 | 1 | 1; | |||||||||||||