| 1 | package Unblock::WebSocket::_Frame; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 19 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use Config (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 19 | ▶ | use Unblock::WebSocket::_Random (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 19 | my %TYPE_TO_OPCODE = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | continuation => 0x0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | text => 0x1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | binary => 0x2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | close => 0x8, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | ping => 0x9, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | pong => 0xA, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 19 | my %OPCODE_TO_TYPE = reverse %TYPE_TO_OPCODE; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub opcode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
opcode: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | my ($class, $type) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | return $TYPE_TO_OPCODE{$type}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub type { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
type: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | my ($class, $opcode) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | return $OPCODE_TO_TYPE{$opcode}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 665 | ▶ | sub _byte_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_byte_string: called CC=4 SCAR=15.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 665 | my ($where, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 665 | ▶ | croak "$where requires a defined scalar" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $value or ref $value
MC/DC0%0/2
not defined $value or ref $value
not defined $value
ref $value
Truth table33%1/3
not(A) or B
A= not defined $valueB= ref $value
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | if !defined($value) || ref($value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 665 | my $bytes = "$value"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 665 | ▶ | croak "$where requires a byte string" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | unless utf8::downgrade($bytes, 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 665 | return $bytes; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 239 | ▶ | sub mask { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mask: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 239 | my ($class, $payload, $key, $offset) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 239 | $payload = _byte_string('mask()', $payload); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 239 | $key = _byte_string('mask()', $key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 239 | ▶ | $offset ||= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$offset ||= 0
MC/DC0%0/1
$offset ||= 0
$offset
Truth table50%1/2
A
A= $offset
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 239 | ▶ | croak 'mask(): key must contain exactly four bytes' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | unless length($key) == 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 239 | my $out = $payload; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 239 | for my $i (0 .. length($out) - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 17694 | substr($out, $i, 1) = chr( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | ord(substr($out, $i, 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | ^ ord(substr($key, ($offset + $i) & 3, 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 239 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 108 | ▶ | sub encode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=27 SCAR=36.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 108 | my ($class, %option) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 108 | my $opcode = delete $option{opcode}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 108 | ▶ | my $payload = exists($option{payload}) ? delete($option{payload}) : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 108 | ▶ | my $fin = exists($option{fin}) ? delete($option{fin}) : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 108 | ▶ | my $masked = delete($option{masked}) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 108 | my $mask_key = delete $option{mask_key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 108 | ▶ | my $rsv1 = delete($option{rsv1}) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 108 | ▶ | my $rsv2 = delete($option{rsv2}) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 108 | ▶ | my $rsv3 = delete($option{rsv3}) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 108 | ▶ | croak 'encode(): unknown option(s): ' . join(', ', sort keys %option) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | if %option; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 108 | ▶ | croak 'encode(): opcode must be an integer between 0 and 15' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
defined $opcode and not ref $opcode and $opcode =~ /\A[0-9]+\z/ and $opcode <= 15
defined $opcode and not ref $opcode and $opcode =~ /\A[0-9]+\z/
defined $opcode and not ref $opcode
MC/DC0%0/4
defined $opcode and not ref $opcode and $opcode =~ /\A[0-9]+\z/ and $opcode <= 15
defined $opcode
not ref $opcode
$opcode =~ /\A[0-9]+\z/
$opcode <= 15
Truth table20%1/5
A and not(B) and C and D
A= defined $opcodeB= not ref $opcodeC= $opcode =~ /\A[0-9]+\z/D= $opcode <= 15
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | unless defined($opcode) && !ref($opcode) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | && $opcode =~ /\A[0-9]+\z/ && $opcode <= 15; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 108 | ▶ | croak 'encode(): fin must be zero or one' unless "$fin" =~ /\A[01]\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 108 | $payload = _byte_string('encode(): payload', $payload); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 108 | my $length = length($payload); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 108 | ▶ | if ($opcode >= 8) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 45 | ▶ | croak 'encode(): control frames must not be fragmented' unless $fin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 45 | ▶ | croak 'encode(): control frame payload exceeds 125 bytes' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | if $length > 125; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 107 | ▶ | my $first = ($fin ? 0x80 : 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | | ($rsv1 ? 0x40 : 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | | ($rsv2 ? 0x20 : 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | | ($rsv3 ? 0x10 : 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | | $opcode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 107 | ▶ | my $mask_bit = $masked ? 0x80 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 107 | my $header; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 107 | ▶ | if ($length < 126) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 105 | $header = pack('CC', $first, $mask_bit | $length); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | elsif ($length <= 0xffff) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | $header = pack('CCn', $first, $mask_bit | 126, $length); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my $high = int($length / 4_294_967_296); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $low = $length % 4_294_967_296; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | croak 'encode(): payload length exceeds RFC 6455 63-bit limit' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | if $high >= 0x80000000; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $header = pack('CCNN', $first, $mask_bit | 127, $high, $low); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 107 | ▶ | return $header . $payload unless $masked; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 79 | ▶ | $mask_key = Unblock::WebSocket::_Random->mask_key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | unless defined $mask_key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 79 | $mask_key = _byte_string('encode(): mask_key', $mask_key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 79 | ▶ | croak 'encode(): mask_key must contain exactly four bytes' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | unless length($mask_key) == 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 79 | return $header . $mask_key . $class->mask($payload, $mask_key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 210 | ▶ | sub new_parser { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_parser: called CC=12 SCAR=30.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 210 | my ($class, %option) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 210 | my $expect_masked = delete $option{expect_masked}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | my $max_frame_size = exists($option{max_frame_size}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 210 | ▶ | ? delete($option{max_frame_size}) : 16 * 1024 * 1024; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 210 | ▶ | my $allow_rsv1 = delete($option{allow_rsv1}) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 210 | ▶ | croak 'new_parser(): expect_masked must be zero or one' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $expect_masked and not ref $expect_masked and "$expect_masked" =~ /\A[01]\z/
defined $expect_masked and not ref $expect_masked
MC/DC0%0/3
defined $expect_masked and not ref $expect_masked and "$expect_masked" =~ /\A[01]\z/
defined $expect_masked
not ref $expect_masked
"$expect_masked" =~ /\A[01]\z/
Truth table25%1/4
A and not(B) and C
A= defined $expect_maskedB= not ref $expect_maskedC= "$expect_masked" =~ /\A[01]\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | unless defined($expect_masked) && !ref($expect_masked) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | && "$expect_masked" =~ /\A[01]\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 210 | ▶ | croak 'new_parser(): max_frame_size must be a positive integer' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
defined $max_frame_size and not ref $max_frame_size and $max_frame_size =~ /\A[0-9]+\z/ and $max_frame_size > 0
defined $max_frame_size and not ref $max_frame_size and $max_frame_size =~ /\A[0-9]+\z/
defined $max_frame_size and not ref $max_frame_size
MC/DC0%0/4
defined $max_frame_size and not ref $max_frame_size and $max_frame_size =~ /\A[0-9]+\z/ and $max_frame_size > 0
defined $max_frame_size
not ref $max_frame_size
$max_frame_size =~ /\A[0-9]+\z/
$max_frame_size > 0
Truth table20%1/5
A and not(B) and C and D
A= defined $max_frame_sizeB= not ref $max_frame_sizeC= $max_frame_size =~ /\A[0-9]+\z/D= $max_frame_size > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | unless defined($max_frame_size) && !ref($max_frame_size) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | && $max_frame_size =~ /\A[0-9]+\z/ && $max_frame_size > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 210 | ▶ | croak 'new_parser(): unknown option(s): ' . join(', ', sort keys %option) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | if %option; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 210 | ▶ | return bless { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | expect_masked => $expect_masked ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | max_frame_size => 0 + $max_frame_size, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | allow_rsv1 => $allow_rsv1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | buffer => '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | }, 'Unblock::WebSocket::_Frame::Parser'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | package Unblock::WebSocket::_Frame::Parser; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 19 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | sub buffered_bytes { length($_[0]{buffer}) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
buffered_bytes: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 347 | ▶ | sub input { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
input: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 347 | my ($self, $bytes) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 347 | ▶ | croak 'input(): bytes must be a scalar' if ref($bytes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 347 | ▶ | $bytes = '' unless defined $bytes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 347 | my $copy = "$bytes"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 347 | ▶ | croak 'input(): bytes must be a byte string' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | unless utf8::downgrade($copy, 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 347 | $self->{buffer} .= $copy; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 347 | return length($copy); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 396 | ▶ | sub next_frame { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next_frame: called CC=35 SCAR=36.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 396 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 396 | my $buffer = $self->{buffer}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 396 | ▶ | return unless length($buffer) >= 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 357 | my ($first, $second) = unpack('CC', substr($buffer, 0, 2)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 357 | ▶ | my $fin = ($first & 0x80) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 357 | ▶ | my $rsv1 = ($first & 0x40) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 357 | ▶ | my $rsv2 = ($first & 0x20) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 357 | ▶ | my $rsv3 = ($first & 0x10) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 357 | my $opcode = $first & 0x0f; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 357 | ▶ | my $masked = ($second & 0x80) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 357 | my $length7 = $second & 0x7f; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 357 | ▶ | croak 'WebSocket frame uses reserved RSV bits' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$rsv2 or $rsv3
MC/DC50%1/2
$rsv2 or $rsv3
$rsv2
$rsv3
Truth table66%2/3
A or B
A= $rsv2B= $rsv3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | if $rsv2 || $rsv3; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | croak 'WebSocket frame uses reserved RSV bits (RSV1 not negotiated)' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 356 | ▶ | if $rsv1 && !$self->{allow_rsv1}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rsv1 and not $self->{"allow_rsv1"}
MC/DC100%2/2
$rsv1 and not $self->{"allow_rsv1"}
$rsv1
not $self->{"allow_rsv1"}
Truth table100%3/3
A and not(B)
A= $rsv1B= not $self->{"allow_rsv1"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | croak 'WebSocket frame has reserved opcode' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 354 | ▶ | unless exists $OPCODE_TO_TYPE{$opcode}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 353 | ▶ | croak 'WebSocket continuation frame must not set RSV1' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$rsv1 and $opcode == 0
MC/DC0%0/2
$rsv1 and $opcode == 0
$rsv1
$opcode == 0
Truth table66%2/3
A and B
A= $rsv1B= $opcode == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | if $rsv1 && $opcode == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 353 | ▶ | croak 'WebSocket control frame must not set RSV1' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$rsv1 and $opcode >= 8
MC/DC0%0/2
$rsv1 and $opcode >= 8
$rsv1
$opcode >= 8
Truth table66%2/3
A and B
A= $rsv1B= $opcode >= 8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | if $rsv1 && $opcode >= 8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | croak 'WebSocket peer used incorrect masking direction' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 353 | ▶ | if $masked != $self->{expect_masked}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 351 | my $offset = 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 351 | my $length; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 351 | ▶ | if ($length7 < 126) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 80 | $length = $length7; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | elsif ($length7 == 126) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 269 | ▶ | return unless length($buffer) >= $offset + 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 267 | $length = unpack('n', substr($buffer, $offset, 2)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 267 | $offset += 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 267 | ▶ | croak 'WebSocket frame uses non-minimal 16-bit length' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | if $length < 126; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 2 | ▶ | return unless length($buffer) >= $offset + 8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 2 | my ($high, $low) = unpack('NN', substr($buffer, $offset, 8)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 2 | ▶ | croak 'WebSocket frame length has reserved high bit set' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | if $high & 0x80000000; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | croak 'WebSocket frame length is too large for this Perl build' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 1 | ▶ | if $high && ($Config::Config{uvsize} || 4) < 8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
$high and ($Config::Config{"uvsize"} || 4) < 8
$Config::Config{'uvsize'} || 4
MC/DC0%0/2
$high and ($Config::Config{"uvsize"} || 4) < 8
$high
$Config::Config{'uvsize'}
Truth table33%1/3
A and B
A= $highB= $Config::Config{'uvsize'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 1 | $length = $high * 4_294_967_296 + $low; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | $offset += 8; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 1 | ▶ | croak 'WebSocket frame uses non-minimal 64-bit length' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | if $length <= 0xffff; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 346 | ▶ | if ($opcode >= 8) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 51 | ▶ | croak 'WebSocket control frame is fragmented' unless $fin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 50 | ▶ | croak 'WebSocket control frame payload exceeds 125 bytes' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | if $length > 125; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | croak 'WebSocket frame exceeds configured limit' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 344 | ▶ | if $length > $self->{max_frame_size}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 343 | my $mask_key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 343 | ▶ | if ($masked) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 291 | ▶ | return unless length($buffer) >= $offset + 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 286 | $mask_key = substr($buffer, $offset, 4); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 286 | $offset += 4; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 338 | ▶ | return unless length($buffer) >= $offset + $length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 212 | my $payload = substr($buffer, $offset, $length); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 212 | ▶ | if ($masked) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 160 | $payload = Unblock::WebSocket::_Frame->mask($payload, $mask_key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 212 | substr($self->{buffer}, 0, $offset + $length, ''); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | fin => $fin, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | rsv1 => $rsv1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | rsv2 => $rsv2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | rsv3 => $rsv3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | opcode => $opcode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 212 | type => $OPCODE_TO_TYPE{$opcode}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | masked => $masked, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | payload => $payload, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 19 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||