| 1 | package Terse::WebSocket; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use base 'Terse'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Protocol::WebSocket::Handshake::Server; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 0 | my ($class, $t) = @_; | |||||||||||||||||||||||||||
| 12 | 0 | my $self = $class->SUPER::new(); | |||||||||||||||||||||||||||
| 13 | 0 | my $version = ''; | |||||||||||||||||||||||||||
| 14 | 0 | my $env = $t->request->env; | |||||||||||||||||||||||||||
| 15 | 0 | $self->psgix = $env->{'psgix.io'}; | |||||||||||||||||||||||||||
| 16 | 0 | $self->handshake = Protocol::WebSocket::Handshake::Server->new_from_psgi($t->request->env); | |||||||||||||||||||||||||||
| 17 | 0 | $self->handshake->parse(); | |||||||||||||||||||||||||||
| 18 | 0 | return $self; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub start { | ||||||||||||||||||||||||||
|
Subroutine
start: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 0 | my ($self, $t, $cbs, $responder) = @_; | |||||||||||||||||||||||||||
| 23 | 0 | my $writer = eval { $responder->([101, $self->handshake->res->headers]); }; | |||||||||||||||||||||||||||
| 24 | 0 | ▶ | $cbs->{($@ ? 'error' : 'connect')}->($self, $responder, $@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my $reset_rate = $t->websocket_reset_rate ||= 100000; | ||||||||||||||||||||||||||
|
Condition0%0/2
$t->websocket_reset_rate ||= 100000
MC/DC0%0/1
$t->websocket_reset_rate ||= 100000
$t->websocket_reset_rate
Truth table0%0/2
A
A= $t->websocket_reset_rate
| |||||||||||||||||||||||||||||
| 26 | 0 | eval { | |||||||||||||||||||||||||||
| 27 | 0 | my $ping_rate = $reset_rate; | |||||||||||||||||||||||||||
| 28 | 0 | while (1) { | |||||||||||||||||||||||||||
| 29 | 0 | $ping_rate--; | |||||||||||||||||||||||||||
| 30 | 0 | my $response; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ($ping_rate < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | $ping_rate = $reset_rate; | |||||||||||||||||||||||||||
| 33 | 0 | syswrite $self->psgix, $self->handshake->build_frame( type => 'ping' )->to_bytes; | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | $response = $self->recieve() while($ping_rate-- > 0 && !$response); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ping_rate-- > 0 and not $response
MC/DC0%0/2
$ping_rate-- > 0 and not $response
$ping_rate-- > 0
not $response
Truth table0%0/3
A and not(B)
A= $ping_rate-- > 0B= not $response
| |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if (!$response || $response ne 'pong') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $response or $response ne "pong"
MC/DC0%0/2
not $response or $response ne "pong"
not $response
$response ne "pong"
Truth table0%0/3
not(A) or B
A= not $responseB= $response ne "pong"
| |||||||||||||||||||||||||||||
| 36 | 0 | last; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 0 | $ping_rate = $reset_rate; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | $response = $self->recieve(); | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | if ($response) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ($response =~ m/^invalid_(length|version|host|required_key)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | $cbs->{error}->($self, $response, $responder); | |||||||||||||||||||||||||||
| 44 | 0 | last; | |||||||||||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||||||||||
| 46 | 0 | $ping_rate = $reset_rate; | |||||||||||||||||||||||||||
| 47 | 0 | $cbs->{recieve}->($self, $response, $responder); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | }; | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $cbs->{error}->($self, $responder, $@) if ($@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $cbs->{disconnect}->($self, $responder) if $cbs->{disconnect}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | delete $t->{_application}->websockets->{$t->sid->value} if $cbs->{close_delete}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | $responder->([200, []]); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub send { | ||||||||||||||||||||||||||
|
Subroutine
send: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 0 | my ($self, $message) = @_; | |||||||||||||||||||||||||||
| 60 | 0 | my $frame = $self->handshake->build_frame; | |||||||||||||||||||||||||||
| 61 | 0 | $frame->append($message); | |||||||||||||||||||||||||||
| 62 | 0 | my $pg = $self->psgix; | |||||||||||||||||||||||||||
| 63 | 0 | syswrite $pg, $frame->to_bytes; | |||||||||||||||||||||||||||
| 64 | 0 | return $self; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub recieve { | ||||||||||||||||||||||||||
|
Subroutine
recieve: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 0 | my ($self, @ENCODED) = @_; | |||||||||||||||||||||||||||
| 69 | 0 | my $length; | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | if (! scalar @ENCODED) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | return shift @{ $self->next_frame } if scalar @{ $self->next_frame ||= [] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->next_frame ||= []
MC/DC0%0/1
$self->next_frame ||= []
$self->next_frame
Truth table0%0/2
A
A= $self->next_frame
| |||||||||||||||||||||||||||||
| 72 | 0 | my $pg = $self->psgix; | |||||||||||||||||||||||||||
| 73 | 0 | my $content = ""; | |||||||||||||||||||||||||||
| 74 | 0 | $length = sysread($pg, $content, 8192); | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | return unless $length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | $length = sysread($pg, $content, 8192, length($content)) while $length >= 8192; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | @ENCODED = map { unpack "C", $_ } split //, $content; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 0 | my @bits = split //, sprintf("%b\n", $ENCODED[0]); | |||||||||||||||||||||||||||
| 80 | 0 | $self->fin = $bits[0]; | |||||||||||||||||||||||||||
| 81 | 0 | $self->rsv = [@bits[1 .. 3]]; | |||||||||||||||||||||||||||
| 82 | 0 | $self->op = shift @ENCODED; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($ENCODED[0] == 254) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | my @length = splice @ENCODED, 0, 3; | |||||||||||||||||||||||||||
| 85 | 0 | $length = ((($length[0] + 2) * $length[1]) + $length[2]); | |||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||
| 87 | 0 | $length = shift @ENCODED; | |||||||||||||||||||||||||||
| 88 | 0 | $length -= 128; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | return pack "C*", join("", @ENCODED) if (scalar @ENCODED == $length); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | my @MASK = splice @ENCODED, 0, 4; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | if (scalar @ENCODED > $length) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | my $next = $self->recieve(splice @ENCODED, $length, scalar @ENCODED); | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | return $next if ($next eq 'invalid_length'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | unshift @{ $self->next_frame }, $next; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | return 'invalid_length' if (scalar @ENCODED != $length); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | return join "", map { pack "C", ($ENCODED[$_] ^ $MASK[$_ % 4]) } 0 .. $#ENCODED; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 13 | 1; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | __END__; | ||||||||||||||||||||||||||||