| 1 | package Protocol::WebSocket::Client; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | require Carp; | |||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Protocol::WebSocket::URL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Protocol::WebSocket::Handshake::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Protocol::WebSocket::Frame; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 9 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 9 | my $class = shift; | |||||||||||||||||||||||||||
| 13 | 9 | ▶ | $class = ref $class if ref $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 9 | my (%params) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 9 | my $self = {}; | |||||||||||||||||||||||||||
| 17 | 9 | bless $self, $class; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 9 | ▶ | Carp::croak('url is required') unless $params{url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | $self->{url} = Protocol::WebSocket::URL->new->parse($params{url}) | ||||||||||||||||||||||||||||
| 21 | 9 | ▶ | or Carp::croak("Can't parse url"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 9 | $self->{version} = $params{version}; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 9 | $self->{on_connect} = $params{on_connect}; | |||||||||||||||||||||||||||
| 26 | 9 | $self->{on_write} = $params{on_write}; | |||||||||||||||||||||||||||
| 27 | 9 | $self->{on_frame} = $params{on_frame}; | |||||||||||||||||||||||||||
| 28 | 9 | $self->{on_eof} = $params{on_eof}; | |||||||||||||||||||||||||||
| 29 | 9 | $self->{on_error} = $params{on_error}; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | $self->{hs} = | ||||||||||||||||||||||||||||
| 32 | 9 | Protocol::WebSocket::Handshake::Client->new(url => $self->{url}); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | my %frame_buffer_params = ( | ||||||||||||||||||||||||||||
| 35 | max_fragments_amount => $params{max_fragments_amount} | ||||||||||||||||||||||||||||
| 36 | 9 | ); | |||||||||||||||||||||||||||
| 37 | 9 | ▶ | $frame_buffer_params{max_payload_size} = $params{max_payload_size} if exists $params{max_payload_size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 9 | $self->{frame_buffer} = $self->_build_frame(%frame_buffer_params); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 9 | return $self; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub url { shift->{url} } | ||||||||||||||||||||||||||
|
Subroutine
url: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | sub version { shift->{version} } | ||||||||||||||||||||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 7 | ▶ | sub on { | ||||||||||||||||||||||||||
|
Subroutine
on: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 49 | 7 | my ($event, $cb) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 7 | $self->{"on_$event"} = $cb; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 7 | return $self; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 3 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=7 SCAR=19.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | 3 | my ($buffer) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 3 | my $hs = $self->{hs}; | |||||||||||||||||||||||||||
| 61 | 3 | my $frame_buffer = $self->{frame_buffer}; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 3 | ▶ | unless ($hs->is_done) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 2 | ▶ | if (!$hs->parse($buffer)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | $self->{on_error}->($self, $hs->error); | |||||||||||||||||||||||||||
| 66 | 0 | return $self; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 2 | ▶ | $self->{on_connect}->($self) if $self->{on_connect} && $hs->is_done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"on_connect"} and $hs->is_done
MC/DC50%1/2
$self->{"on_connect"} and $hs->is_done
$self->{"on_connect"}
$hs->is_done
Truth table66%2/3
A and B
A= $self->{"on_connect"}B= $hs->is_done
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 3 | ▶ | if ($hs->is_done) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 3 | $frame_buffer->append($buffer); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 3 | ▶ | while (my $bytes = $frame_buffer->next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | $self->{on_read}->($self, $bytes); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | #$self->{on_frame}->($self, $bytes); | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | return $self; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | 1 | my ($buffer) = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | my $frame = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | ref $buffer | ||||||||||||||||||||||||||||
| 91 | ? $buffer | ||||||||||||||||||||||||||||
| 92 | : $self->_build_frame(masked => 1, buffer => $buffer); | ||||||||||||||||||||||||||||
| 93 | 1 | $self->{on_write}->($self, $frame->to_bytes); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | return $self; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 3 | ▶ | sub connect { | ||||||||||||||||||||||||||
|
Subroutine
connect: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 3 | my $hs = $self->{hs}; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 3 | $self->{on_write}->($self, $hs->to_string); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | return $self; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | ▶ | sub disconnect { | ||||||||||||||||||||||||||
|
Subroutine
disconnect: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | my $frame = $self->_build_frame(type => 'close'); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | $self->{on_write}->($self, $frame->to_bytes); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 1 | return $self; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 11 | ▶ | sub _build_frame { | ||||||||||||||||||||||||||
|
Subroutine
_build_frame: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 119 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 11 | return Protocol::WebSocket::Frame->new(version => $self->{version}, @_); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | 1; | |||||||||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||||||||||