| 1 | package Protocol::WebSocket::Message; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 21 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 21 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 21 | ▶ | use base 'Protocol::WebSocket::Stateful'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 21 | ▶ | use Scalar::Util qw(readonly); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 21 | require Digest::MD5; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 21 | our $MAX_MESSAGE_SIZE = 10 * 2048; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 112 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 112 | my $class = shift; | |||||||||||||||||||||||||||
| 15 | 112 | ▶ | $class = ref $class if ref $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 112 | my $self = {@_}; | |||||||||||||||||||||||||||
| 18 | 112 | bless $self, $class; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 112 | ▶ | $self->{version} ||= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"version"} ||= ""
MC/DC100%1/1
$self->{"version"} ||= ""
$self->{"version"}
Truth table100%2/2
A
A= $self->{"version"}
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 112 | $self->{buffer} = ''; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 112 | ▶ | $self->{fields} ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"fields"} ||= {}
MC/DC100%1/1
$self->{"fields"} ||= {}
$self->{"fields"}
Truth table100%2/2
A
A= $self->{"fields"}
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 112 | ▶ | $self->{max_message_size} ||= $MAX_MESSAGE_SIZE; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"max_message_size"} ||= $Protocol::WebSocket::Message::MAX_MESSAGE_SIZE
MC/DC0%0/2
$self->{"max_message_size"} ||= $Protocol::WebSocket::Message::MAX_MESSAGE_SIZE
$self->{"max_message_size"}
$Protocol::WebSocket::Message::MAX_MESSAGE_SIZE
Truth table33%1/3
A ||= B
A= $self->{"max_message_size"}B= $Protocol::WebSocket::Message::MAX_MESSAGE_SIZE
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 112 | ▶ | $self->{cookies} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"cookies"} ||= []
MC/DC100%1/1
$self->{"cookies"} ||= []
$self->{"cookies"}
Truth table100%2/2
A
A= $self->{"cookies"}
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 112 | $self->state('first_line'); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 112 | return $self; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 277 | ▶ | sub secure { @_ > 1 ? $_[0]->{secure} = $_[1] : $_[0]->{secure} } | ||||||||||||||||||||||||||
|
Subroutine
secure: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 704 | ▶ | sub fields { shift->{fields} } | ||||||||||||||||||||||||||
|
Subroutine
fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 704 | ▶ | sub field { | ||||||||||||||||||||||||||
|
Subroutine
field: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 704 | my $self = shift; | |||||||||||||||||||||||||||
| 41 | 704 | my $name = lc shift; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 704 | ▶ | return $self->fields->{$name} unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 218 | $self->fields->{$name} = $_[0]; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 218 | return $self; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 529 | ▶ | sub error { | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 529 | my $self = shift; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 529 | ▶ | return $self->{error} unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 11 | my $error = shift; | |||||||||||||||||||||||||||
| 56 | 11 | $self->{error} = $error; | |||||||||||||||||||||||||||
| 57 | 11 | $self->state('error'); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 11 | return $self; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 368 | ▶ | sub subprotocol { | ||||||||||||||||||||||||||
|
Subroutine
subprotocol: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 368 | ▶ | @_ > 1 ? $_[0]->{subprotocol} = $_[1] : $_[0]->{subprotocol}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 496 | ▶ | sub host { @_ > 1 ? $_[0]->{host} = $_[1] : $_[0]->{host} } | ||||||||||||||||||||||||||
|
Subroutine
host: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 637 | ▶ | sub origin { @_ > 1 ? $_[0]->{origin} = $_[1] : $_[0]->{origin} } | ||||||||||||||||||||||||||
|
Subroutine
origin: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 505 | ▶ | sub version { @_ > 1 ? $_[0]->{version} = $_[1] : $_[0]->{version} } | ||||||||||||||||||||||||||
|
Subroutine
version: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 19 | ▶ | sub number1 { @_ > 1 ? $_[0]->{number1} = $_[1] : $_[0]->{number1} } | ||||||||||||||||||||||||||
|
Subroutine
number1: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 19 | ▶ | sub number2 { @_ > 1 ? $_[0]->{number2} = $_[1] : $_[0]->{number2} } | ||||||||||||||||||||||||||
|
Subroutine
number2: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 70 | ▶ | sub challenge { @_ > 1 ? $_[0]->{challenge} = $_[1] : $_[0]->{challenge} } | ||||||||||||||||||||||||||
|
Subroutine
challenge: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 28 | ▶ | sub checksum { | ||||||||||||||||||||||||||
|
Subroutine
checksum: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 28 | my $self = shift; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 28 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 9 | $self->{checksum} = $_[0]; | |||||||||||||||||||||||||||
| 80 | 9 | return $self; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 19 | ▶ | return $self->{checksum} if defined $self->{checksum}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 15 | ▶ | Carp::croak(qq/number1 is required/) unless defined $self->number1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 15 | ▶ | Carp::croak(qq/number2 is required/) unless defined $self->number2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 15 | ▶ | Carp::croak(qq/challenge is required/) unless defined $self->challenge; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 15 | my $checksum = ''; | |||||||||||||||||||||||||||
| 90 | 15 | $checksum .= pack 'N' => $self->number1; | |||||||||||||||||||||||||||
| 91 | 15 | $checksum .= pack 'N' => $self->number2; | |||||||||||||||||||||||||||
| 92 | 15 | $checksum .= $self->challenge; | |||||||||||||||||||||||||||
| 93 | 15 | $checksum = Digest::MD5::md5($checksum); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 15 | ▶ | return $self->{checksum} ||= $checksum; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"checksum"} ||= $checksum
MC/DC0%0/2
$self->{"checksum"} ||= $checksum
$self->{"checksum"}
$checksum
Truth table33%1/3
A ||= B
A= $self->{"checksum"}B= $checksum
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 265 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 265 | my $self = shift; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 265 | ▶ | return 1 unless defined $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 254 | ▶ | return if $self->error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 254 | ▶ | return unless $self->_append(@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 252 | ▶ | while (!$self->is_state('body') && defined(my $line = $self->_get_line)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->is_state("body") and defined(my $line = $self->_get_line)
MC/DC100%2/2
not $self->is_state("body") and defined(my $line = $self->_get_line)
not $self->is_state("body")
defined(my $line = $self->_get_line)
Truth table100%3/3
not(A) and B
A= not $self->is_state("body")B= defined(my $line = $self->_get_line)
| |||||||||||||||||||||||||||||
| 108 | 284 | ▶ | if ($self->state eq 'first_line') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 109 | 48 | ▶ | return unless defined $self->_parse_first_line($line); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 42 | $self->state('fields'); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | elsif ($line ne '') { | ||||||||||||||||||||||||||||
| 114 | 197 | ▶ | return unless defined $self->_parse_field($line); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | else { | ||||||||||||||||||||||||||||
| 117 | 39 | $self->state('body'); | |||||||||||||||||||||||||||
| 118 | 39 | last; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 246 | ▶ | return 1 unless $self->is_state('body'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 49 | my $rv = $self->_parse_body; | |||||||||||||||||||||||||||
| 125 | 49 | ▶ | return unless defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Need more data | ||||||||||||||||||||||||||||
| 128 | 48 | ▶ | return $rv unless ref $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 44 | ▶ | $_[0] = $self->{buffer} unless readonly $_[0] || ref $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
&readonly($_[0]) or ref $_[0]
MC/DC100%2/2
&readonly($_[0]) or ref $_[0]
&readonly($_[0])
ref $_[0]
Truth table100%3/3
A or B
A= &readonly($_[0])B= ref $_[0]
| |||||||||||||||||||||||||||||
| 131 | 44 | return $self->done; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 22 | ▶ | sub _extract_number { | ||||||||||||||||||||||||||
|
Subroutine
_extract_number: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 135 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | 22 | my $key = shift; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 22 | my $number = join '' => $key =~ m/\d+/g; | |||||||||||||||||||||||||||
| 139 | 22 | my $spaces = $key =~ s/ / /g; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 22 | ▶ | return if $spaces == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 22 | return int($number / $spaces); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 254 | ▶ | sub _append { | ||||||||||||||||||||||||||
|
Subroutine
_append: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 147 | 254 | my $self = shift; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 254 | ▶ | return if $self->error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 254 | ▶ | if (ref $_[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 8 | $_[0]->read(my $buf, $self->{max_message_size}); | |||||||||||||||||||||||||||
| 153 | 8 | $self->{buffer} .= $buf; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | else { | ||||||||||||||||||||||||||||
| 156 | 246 | $self->{buffer} .= $_[0]; | |||||||||||||||||||||||||||
| 157 | 246 | ▶ | $_[0] = '' unless readonly $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 254 | ▶ | if (length $self->{buffer} > $self->{max_message_size}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 2 | $self->error('Message is too long'); | |||||||||||||||||||||||||||
| 162 | 2 | return; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 252 | return $self; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 481 | ▶ | sub _get_line { | ||||||||||||||||||||||||||
|
Subroutine
_get_line: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 169 | 481 | my $self = shift; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 481 | ▶ | if ($self->{buffer} =~ s/^(.*?)\x0d?\x0a//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 284 | return $1; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 197 | return; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 2 | ▶ | sub _parse_first_line {shift} | ||||||||||||||||||||||||||
|
Subroutine
_parse_first_line: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 197 | ▶ | sub _parse_field { | ||||||||||||||||||||||||||
|
Subroutine
_parse_field: called CC=4 SCAR=14.8
| |||||||||||||||||||||||||||||
| 181 | 197 | my $self = shift; | |||||||||||||||||||||||||||
| 182 | 197 | my $line = shift; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 197 | my ($name, $value) = split /:\s*/ => $line => 2; | |||||||||||||||||||||||||||
| 185 | 197 | ▶ | unless (defined $name && defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $name and defined $value
MC/DC0%0/2
defined $name and defined $value
defined $name
defined $value
Truth table33%1/3
A and B
A= defined $nameB= defined $value
| |||||||||||||||||||||||||||||
| 186 | 0 | $self->error('Invalid field'); | |||||||||||||||||||||||||||
| 187 | 0 | return; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | #$name =~ s/^Sec-WebSocket-Origin$/Origin/i; # FIXME | ||||||||||||||||||||||||||||
| 191 | 197 | $self->field($name => $value); | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 197 | ▶ | if ($name =~ m/^x-forwarded-proto$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 1 | $self->secure(1); | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 197 | return $self; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 2 | ▶ | sub _parse_body {shift} | ||||||||||||||||||||||||||
|
Subroutine
_parse_body: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 21 | 1; | |||||||||||||||||||||||||||
| 203 | __END__ | ||||||||||||||||||||||||||||