WebSocket/Message.pm

filter: Statementstmt 98.1% 108 / 110 Branchbran 87.5% 63 / 72 Conditioncond 71.4% 15 / 21 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 24 / 24 Podpod 100% 14 / 14 totaltotal 90.9% 231 / 254 CC 45 SCAR 18.0
CC
45
Cov
92%
parse
15.0
checksum
7.3
new
6.1
CRAP 6.0
« WebSocket/Cookie/Response.pm ↑ Summary WebSocket/Handshake/Server.pm »
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
BranchTrueFalseTotal
if ref $class 0 112 112
16  
17 112 my $self = {@_};
18 112 bless $self, $class;
19  
20 112 ▶ $self->{version} ||= '';
Condition100%2/2
$self->{"version"} ||= ""
l!l
1993
MC/DC100%1/1
$self->{"version"} ||= ""
$self->{"version"}
Truth table100%2/2
A
A= $self->{"version"}
Aresult
11
00
21  
22 112 $self->{buffer} = '';
23  
24 112 ▶ $self->{fields} ||= {};
Condition100%2/2
$self->{"fields"} ||= {}
l!l
7105
MC/DC100%1/1
$self->{"fields"} ||= {}
$self->{"fields"}
Truth table100%2/2
A
A= $self->{"fields"}
Aresult
11
00
25  
26 112 ▶ $self->{max_message_size} ||= $MAX_MESSAGE_SIZE;
Condition33%1/3
$self->{"max_message_size"} ||= $Protocol::WebSocket::Message::MAX_MESSAGE_SIZE
l!l&&r!l&&!r
01120
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
ABresult
1X1
011
000
27  
28 112 ▶ $self->{cookies} ||= [];
Condition100%2/2
$self->{"cookies"} ||= []
l!l
7105
MC/DC100%1/1
$self->{"cookies"} ||= []
$self->{"cookies"}
Truth table100%2/2
A
A= $self->{"cookies"}
Aresult
11
00
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
BranchTrueFalseTotal
@_ > 1 ? : 23 254 277
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
BranchTrueFalseTotal
unless @_ 486 218 704
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
BranchTrueFalseTotal
unless @_ 518 11 529
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
BranchTrueFalseTotal
@_ > 1 ? : 27 341 368
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
BranchTrueFalseTotal
@_ > 1 ? : 64 432 496
67 637 ▶sub origin { @_ > 1 ? $_[0]->{origin} = $_[1] : $_[0]->{origin} }
Subroutine origin: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 45 592 637
68  
69 505 ▶sub version { @_ > 1 ? $_[0]->{version} = $_[1] : $_[0]->{version} }
Subroutine version: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 67 438 505
70  
71 19 ▶sub number1 { @_ > 1 ? $_[0]->{number1} = $_[1] : $_[0]->{number1} }
Subroutine number1: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 3 16 19
72 19 ▶sub number2 { @_ > 1 ? $_[0]->{number2} = $_[1] : $_[0]->{number2} }
Subroutine number2: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 3 16 19
73 70 ▶sub challenge { @_ > 1 ? $_[0]->{challenge} = $_[1] : $_[0]->{challenge} }
Subroutine challenge: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 14 56 70
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
BranchTrueFalseTotal
if (@_) 9 19 28
79 9 $self->{checksum} = $_[0];
80 9 return $self;
81 }
82  
83 19 ▶ return $self->{checksum} if defined $self->{checksum};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"checksum"} 4 15 19
84  
85 15 ▶ Carp::croak(qq/number1 is required/) unless defined $self->number1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->number1 0 15 15
86 15 ▶ Carp::croak(qq/number2 is required/) unless defined $self->number2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->number2 0 15 15
87 15 ▶ Carp::croak(qq/challenge is required/) unless defined $self->challenge;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->challenge 0 15 15
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
l!l&&r!l&&!r
0150
MC/DC0%0/2
$self->{"checksum"} ||= $checksum
$self->{"checksum"} $checksum
Truth table33%1/3
A ||= B
A= $self->{"checksum"}B= $checksum
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless defined $_[0] 11 254 265
102  
103 254 ▶ return if $self->error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->error 0 254 254
104  
105 254 ▶ return unless $self->_append(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_append(@_) 2 252 254
106  
107 252 ▶ while (!$self->is_state('body') && defined(my $line = $self->_get_line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_state("body") and defined(my $line = $self->_get_line)) 284 207 491
Condition100%3/3
not $self->is_state("body") and defined(my $line = $self->_get_line)
!ll&&!rl&&r
10197284
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)
ABresult
0X0
100
111
108 284 ▶ if ($self->state eq 'first_line') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->state eq 'first_line') { } 48 236 284
elsif ($line ne '') { } 197 39 236
109 48 ▶ return unless defined $self->_parse_first_line($line);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_parse_first_line($line) 6 42 48
110  
111 42 $self->state('fields');
112 }
113 elsif ($line ne '') {
114 197 ▶ return unless defined $self->_parse_field($line);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_parse_field($line) 0 197 197
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
BranchTrueFalseTotal
unless $self->is_state("body") 197 49 246
123  
124 49 my $rv = $self->_parse_body;
125 49 ▶ return unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 1 48 49
126  
127 # Need more data
128 48 ▶ return $rv unless ref $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rv 4 44 48
129  
130 44 ▶ $_[0] = $self->{buffer} unless readonly $_[0] || ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless &readonly($_[0]) or ref $_[0] 9 35 44
Condition100%3/3
&readonly($_[0]) or ref $_[0]
l!l&&r!l&&!r
2789
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]
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if $spaces == 0 0 22 22
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
BranchTrueFalseTotal
if $self->error 0 254 254
150  
151 254 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 8 246 254
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
BranchTrueFalseTotal
unless &readonly($_[0]) 17 229 246
158 }
159  
160 254 ▶ if (length $self->{buffer} > $self->{max_message_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"buffer"} > $self->{"max_message_size"}) 2 252 254
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
BranchTrueFalseTotal
if ($self->{"buffer"} =~ s/^(.*?)\x0d?\x0a//) 284 197 481
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
BranchTrueFalseTotal
unless (defined $name and defined $value) 0 197 197
Condition33%1/3
defined $name and defined $value
!ll&&!rl&&r
00197
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($name =~ /^x-forwarded-proto$/i) 1 196 197
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__
« WebSocket/Cookie/Response.pm ↑ Summary WebSocket/Handshake/Server.pm »