| 1 | package Terse::WebSocket; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use base 'Terse'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | sub new { | ||||||||||||||||||||||||||||||||
| 7 | 0 | ▶ | my ($class, $t) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 8 | 0 | my $self = $class->SUPER::new(); | |||||||||||||||||||||||||||||||
| 9 | 0 | my $version = ''; | |||||||||||||||||||||||||||||||
| 10 | 0 | my $env = $t->request->env; | |||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | if (!$env->{$t->{_sock}} || !$env->{$t->{_stream_check}}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$$env{$t->{"_sock"}} and $$env{$t->{"_stream_check"}}
MC/DC0%0/2
$$env{$t->{"_sock"}} and $$env{$t->{"_stream_check"}}
$$env{$t->{"_sock"}}
$$env{$t->{"_stream_check"}}
Truth table0%0/3
A and B
A= $$env{$t->{"_sock"}}B= $$env{$t->{"_stream_check"}}
| |||||||||||||||||||||||||||||||||
| 12 | 0 | return 'Invalid environment no _sock or _stream_check in env'; | |||||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||||||
| 14 | $self->options = { | ||||||||||||||||||||||||||||||||
| 15 | secret => 'ABABCABC-ABC-ABC-ABCD-ABCABCABCABC', | ||||||||||||||||||||||||||||||||
| 16 | upgrade => $env->{HTTP_UPGRADE}, | ||||||||||||||||||||||||||||||||
| 17 | connection => $env->{HTTP_CONNECTION}, | ||||||||||||||||||||||||||||||||
| 18 | host => $env->{HTTP_HOST}, | ||||||||||||||||||||||||||||||||
| 19 | origin => $env->{HTTP_ORIGIN}, | ||||||||||||||||||||||||||||||||
| 20 | ($env->{HTTP_SEC_WEBSOCKET_KEY} | ||||||||||||||||||||||||||||||||
| 21 | ? (sec_websocket_key => $env->{HTTP_SEC_WEBSOCKET_KEY}) | ||||||||||||||||||||||||||||||||
| 22 | : () | ||||||||||||||||||||||||||||||||
| 23 | ), | ||||||||||||||||||||||||||||||||
| 24 | ($env->{HTTP_SEC_WEBSOCKET_KEY1} | ||||||||||||||||||||||||||||||||
| 25 | ? (sec_websocket_key1 => $env->{HTTP_SEC_WEBSOCKET_KEY1}) | ||||||||||||||||||||||||||||||||
| 26 | : (sec_websocket_key2 => $env->{HTTP_SEC_WEBSOCKET_KEY2}) | ||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | ), | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 28 | subprotocol => 'chat', | ||||||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if (exists $env->{HTTP_SEC_WEBSOCKET_VERSION}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | 0 | $fields->{'sec_websocket_version'} = $env->{HTTP_SEC_WEBSOCKET_VERSION}; | |||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | if ($env->{HTTP_SEC_WEBSOCKET_VERSION} eq '13') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 33 | 0 | $self->version = 'draft-ietf-hybi-17'; | |||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | else { | ||||||||||||||||||||||||||||||||
| 36 | 0 | $self->version = 'draft-ietf-hybi-10'; | |||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||
| 39 | $self->resource_name = "$env->{SCRIPT_NAME}$env->{PATH_INFO}" | ||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | . ($env->{QUERY_STRING} ? "?$env->{QUERY_STRING}" : ""); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | 0 | $self->psgix = $env->{'psgix.io'}; | |||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ($env->{HTTP_X_FORWARDED_PROTO} && $env->{HTTP_X_FORWARDED_PROTO} eq 'https') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$env->{"HTTP_X_FORWARDED_PROTO"} and $env->{"HTTP_X_FORWARDED_PROTO"} eq "https"
MC/DC0%0/2
$env->{"HTTP_X_FORWARDED_PROTO"} and $env->{"HTTP_X_FORWARDED_PROTO"} eq "https"
$env->{"HTTP_X_FORWARDED_PROTO"}
$env->{"HTTP_X_FORWARDED_PROTO"} eq "https"
Truth table0%0/3
A and B
A= $env->{"HTTP_X_FORWARDED_PROTO"}B= $env->{"HTTP_X_FORWARDED_PROTO"} eq "https"
| |||||||||||||||||||||||||||||||||
| 43 | 0 | $self->secure(1); | |||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | unless ($self->parse($_[0])) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 0 | $self->error($req->error); | |||||||||||||||||||||||||||||||
| 47 | 0 | return; | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | sub start { | ||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my ($self, $t, $cbs, $responder) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
start: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 54 | 0 | my $writer = eval { $responder->([101, [$self->headers]]); }; | |||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $cbs->{($@ ? 'error' : 'connect')}->($self, $responder, $@); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | 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
| |||||||||||||||||||||||||||||||||
| 57 | 0 | eval { | |||||||||||||||||||||||||||||||
| 58 | 0 | my $ping_rate = $reset_rate; | |||||||||||||||||||||||||||||||
| 59 | 0 | while (1) { | |||||||||||||||||||||||||||||||
| 60 | 0 | $ping_rate--; | |||||||||||||||||||||||||||||||
| 61 | 0 | my $response; | |||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ($ping_rate < 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | 0 | $ping_rate = $reset_rate; | |||||||||||||||||||||||||||||||
| 64 | 0 | $self->send($ping); | |||||||||||||||||||||||||||||||
| 65 | 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
| |||||||||||||||||||||||||||||||||
| 66 | 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"
| |||||||||||||||||||||||||||||||||
| 67 | 0 | last; | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | 0 | $ping_rate = $reset_rate; | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | 0 | $response = $self->recieve(); | |||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($response) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ($response =~ m/^invalid_(length|version|host|required_key)$/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | 0 | $cbs->{error}->($self, $response, $responder); | |||||||||||||||||||||||||||||||
| 75 | 0 | last; | |||||||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||||||
| 77 | 0 | $ping_rate = $reset_rate; | |||||||||||||||||||||||||||||||
| 78 | 0 | $cbs->{recieve}->($self, $response, $responder); | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $cbs->{error}->($self, $responder, $@) if ($@); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $cbs->{disconnect}->($self, $responder) if $cbs->{disconnect}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | delete $t->{_application}->websockets->{$t->sid->value} if $cbs->{close_delete}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 0 | $responder->([200, []]); | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | sub headers { | ||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
headers: not called CC=19 SCAR=59.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | my $version = $self->version || 'draft-ietf-hybi-10'; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->version || 'draft-ietf-hybi-10'
MC/DC0%0/1
$self->version || 'draft-ietf-hybi-10'
$self->version
Truth table0%0/2
A
A= $self->version
| |||||||||||||||||||||||||||||||||
| 92 | 0 | my @headers = (); | |||||||||||||||||||||||||||||||
| 93 | 0 | push @headers, Upgrade => 'WebSocket'; | |||||||||||||||||||||||||||||||
| 94 | 0 | push @headers, Connection => 'Upgrade'; | |||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($version eq 'draft-hixie-75' || $version eq 'draft-ietf-hybi-00') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$version eq "draft-hixie-75" or $version eq "draft-ietf-hybi-00"
MC/DC0%0/2
$version eq "draft-hixie-75" or $version eq "draft-ietf-hybi-00"
$version eq "draft-hixie-75"
$version eq "draft-ietf-hybi-00"
Truth table0%0/3
A or B
A= $version eq "draft-hixie-75"B= $version eq "draft-ietf-hybi-00"
| |||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | return 'invalid_host' unless defined $self->options->host; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 97 | 0 | my $location = 'ws'; | |||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | $location .= 's' if $self->options->secure; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | 0 | $location .= '://'; | |||||||||||||||||||||||||||||||
| 100 | 0 | $location .= $self->options->host; | |||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | $location .= ':' . $self->options->port if defined $self->options->port; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $location .= $self->resource_name || '/'; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->resource_name || '/'
MC/DC0%0/1
$self->resource_name || '/'
$self->resource_name
Truth table0%0/2
A
A= $self->resource_name
| |||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | my $origin = $self->options->origin ? $self->options->origin : 'http://' . $self->options->host; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $origin =~ s{^http:}{https:} if !$self->options->origin && $self->options->secure; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $self->options->origin and $self->options->secure
MC/DC0%0/2
not $self->options->origin and $self->options->secure
not $self->options->origin
$self->options->secure
Truth table0%0/3
not(A) and B
A= not $self->options->originB= $self->options->secure
| |||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ($version eq 'draft-hixie-75') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | push @headers, 'WebSocket-Protocol' => $self->subprotocol | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | if defined $self->options->subprotocol; | ||||||||||||||||||||||||||||||||
| 108 | 0 | push @headers, 'WebSocket-Origin' => $origin; | |||||||||||||||||||||||||||||||
| 109 | 0 | push @headers, 'WebSocket-Location' => $location; | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | elsif ($version eq 'draft-ietf-hybi-00') { | ||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | push @headers, 'Sec-WebSocket-Protocol' => $self->options->subprotocol | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 113 | if defined $self->options->subprotocol; | ||||||||||||||||||||||||||||||||
| 114 | 0 | push @headers, 'Sec-WebSocket-Origin' => $origin; | |||||||||||||||||||||||||||||||
| 115 | 0 | push @headers, 'Sec-WebSocket-Location' => $location; | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | ▶ | elsif ($version eq 'draft-ietf-hybi-10' || $version eq 'draft-ietf-hybi-17') { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
$version eq "draft-ietf-hybi-10" or $version eq "draft-ietf-hybi-17"
MC/DC0%0/2
$version eq "draft-ietf-hybi-10" or $version eq "draft-ietf-hybi-17"
$version eq "draft-ietf-hybi-10"
$version eq "draft-ietf-hybi-17"
Truth table0%0/3
A or B
A= $version eq "draft-ietf-hybi-10"B= $version eq "draft-ietf-hybi-17"
| |||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return 'invalid_required_key' unless defined $self->options->key; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | 0 | my $key = $self->options->key; | |||||||||||||||||||||||||||||||
| 121 | 0 | $key .= $self->options->secret; | |||||||||||||||||||||||||||||||
| 122 | 0 | $key = Digest::SHA::sha1($key); | |||||||||||||||||||||||||||||||
| 123 | 0 | $key = MIME::Base64::encode_base64($key); | |||||||||||||||||||||||||||||||
| 124 | 0 | $key =~ s{\s+}{}g; | |||||||||||||||||||||||||||||||
| 125 | 0 | push @headers, 'Sec-WebSocket-Accept' => $key; | |||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | push @headers, 'Sec-WebSocket-Protocol' => $self->options->subprotocol | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | if defined $self->options->subprotocol; | ||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||||||||||||||
| 130 | 0 | return 'invalid_version'; | |||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||
| 132 | 0 | return @headers; | |||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | sub send { | ||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | my ($self, $message) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
send: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 137 | 0 | my $pg = $self->psgix; | |||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | my $mask = $self->mask ||= 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->mask ||= 0
MC/DC0%0/1
$self->mask ||= 0
$self->mask
Truth table0%0/2
A
A= $self->mask
| |||||||||||||||||||||||||||||||||
| 139 | 0 | my (@ENCODED) = map { ord($_) } split //, $message; | |||||||||||||||||||||||||||||||
| 140 | 0 | my $length = scalar @ENCODED + 128; | |||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ($length > 254 || $mask) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$length > 254 or $mask
MC/DC0%0/2
$length > 254 or $mask
$length > 254
$mask
Truth table0%0/3
A or B
A= $length > 254B= $mask
| |||||||||||||||||||||||||||||||||
| 142 | 0 | @MASK = map { int(rand(256)) } 0 .. 3; | |||||||||||||||||||||||||||||||
| 143 | 0 | my $i; | |||||||||||||||||||||||||||||||
| 144 | 0 | $ENCODED[$i++] = $_ ^ $MASK[$i % 4] for (@ENCODED); | |||||||||||||||||||||||||||||||
| 145 | 0 | unshift @ENCODED, @MASK; | |||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ($length > 256) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | 0 | my $times = int(($length + 2) / 254) - 1; | |||||||||||||||||||||||||||||||
| 148 | 0 | my $excess = $length - (($times * 256) + 128); | |||||||||||||||||||||||||||||||
| 149 | 0 | unshift @ENCODED, (254, $times, $excess); | |||||||||||||||||||||||||||||||
| 150 | } else { | ||||||||||||||||||||||||||||||||
| 151 | 0 | unshift @ENCODED, $length; | |||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||||||
| 154 | 0 | unshift @ENCODED, $length - 128; | |||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | 0 | syswrite $pg, join("", map {chr($_)} (129, @ENCODED)); | |||||||||||||||||||||||||||||||
| 157 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | sub recieve { | ||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | my ($self, @ENCODED) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
recieve: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 162 | 0 | my $length; | |||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if (! scalar @ENCODED ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 164 | 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
| |||||||||||||||||||||||||||||||||
| 165 | 0 | my $pg = $self->psgix; | |||||||||||||||||||||||||||||||
| 166 | 0 | my $content = ""; | |||||||||||||||||||||||||||||||
| 167 | 0 | $length = sysread($pg, $content, 8192); | |||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | return unless $length; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | $length = sysread($pg, $content, 8192, length($content)) while $length >= 8192; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | 0 | @ENCODED = map { unpack "C", $_ } split //, $content; | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | 0 | my @bits = split //, sprintf("%b\n", $ENCODED[0]); | |||||||||||||||||||||||||||||||
| 173 | 0 | $self->fin = $bits[0]; | |||||||||||||||||||||||||||||||
| 174 | 0 | $self->rsv = [@bits[1 .. 3]]; | |||||||||||||||||||||||||||||||
| 175 | 0 | $self->op = shift @ENCODED; | |||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | if ($ENCODED[0] == 254) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 177 | 0 | my @length = splice @ENCODED, 0, 3; | |||||||||||||||||||||||||||||||
| 178 | 0 | $length = ((($length[0] + 2) * $length[1]) + $length[2]); | |||||||||||||||||||||||||||||||
| 179 | } else { | ||||||||||||||||||||||||||||||||
| 180 | 0 | $length = shift @ENCODED; | |||||||||||||||||||||||||||||||
| 181 | 0 | $length -= 128; | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | return pack "C*", join("", @ENCODED) if (scalar @ENCODED == $length); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 184 | 0 | my @MASK = splice @ENCODED, 0, 4; | |||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if (scalar @ENCODED > $length) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 186 | 0 | my $next = $self->recieve(splice @ENCODED, $length, scalar @ENCODED); | |||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | return $next if ($next eq 'invalid_length'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 188 | 0 | unshift @{ $self->next_frame }, $next; | |||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | return 'invalid_length' if (scalar @ENCODED != $length); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 191 | 0 | return join "", map { pack "C", ($ENCODED[$_] ^ $MASK[$_ % 4]) } 0 .. $#ENCODED; | |||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | 3 | 1; | |||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | __END__; | ||||||||||||||||||||||||||||||||