WebSocket/Frame.pm

filter: Statementstmt 50.7% 64 / 126 Branchbran 50.0% 15 / 30 Conditioncond 57.1% 4 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 50.0% 14 / 28 Podpod 0.0% 0 / 17 totaltotal 46.7% 99 / 212 SCAR 41.2
CC
19
Cov
51%
AUTOLOAD
30.9
new
8.0
set_payload_sr
6.0
CRAP 61.7
« WebSocket/Endpoint.pm ↑ Summary WebSocket/Handshake.pm »
1 package Net::WebSocket::Frame;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Net::WebSocket::Frame
8  
9 =head1 SYNOPSIS
10  
11 #Never instantiate Net::WebSocket::Frame directly;
12 #always call new() on a subclass:
13 my $frame = Net::WebSocket::Frame::text->new(
14 fin => 0, #to start a fragmented message
15 rsv => 0b11, #RSV2 and RSV3 are on
16 mask => "\x01\x02\x03\x04 #clients MUST include; servers MUST NOT
17 payload_sr => \'Woot!',
18 );
19  
20 $frame->get_fin();
21 $frame->get_mask_bytes();
22 $frame->get_payload();
23  
24 $frame->set_rsv();
25 $frame->get_rsv();
26  
27 $frame->to_bytes(); #for sending over the wire
28  
29 =head1 DESCRIPTION
30  
31 This is the base class for all frame objects. The interface as described
32 above should be fairly straightforward.
33  
34 =head1 EXPERIMENTAL: CUSTOM FRAME CLASSES
35  
36 You can have custom frame classes, e.g., to support WebSocket extensions that
37 use custom frame opcodes. RFC 6455 allocates opcodes 3-7 for data frames and
38 11-15 (0xb - 0xf) for control frames.
39  
40 The best way to do this is to subclass either
41 L<Net::WebSocket::Base::DataFrame> or L<Net::WebSocket::Base::ControlFrame>,
42 depending on what kind of frame you’re dealing with.
43  
44 An example of such a class is below:
45  
46 package My::Custom::Frame::booya;
47  
48 use strict;
49 use warnings;
50  
51 use parent qw( Net::WebSocket::Base::DataFrame );
52  
53 use constant get_opcode => 3;
54  
55 use constant get_type => 'booya';
56  
57 Note that L<Net::WebSocket::Parser> still won’t know how to handle such a
58 custom frame, so if you intend to receive custom frames as part of messages,
59 you’ll also need to create a custom base class of this class, then also
60 subclass L<Net::WebSocket::Parser>. You may additionally want to subclass
61 L<Net::WebSocket::Streamer::Server> (or -C<::Client>) if you do streaming.
62  
63 B<NOTE: THIS IS LARGELY UNTESTED.> I’m not familiar with any application that
64 actually requires this feature. The C<permessage-deflate> extension seems to
65 be the only one that has much widespread web browser support.
66  
67 =cut
68  
69 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 9 ▶use Net::WebSocket::Constants ();
Subroutine BEGIN: called CC=1 SCAR=0.0
73 9 ▶use Net::WebSocket::Mask ();
Subroutine BEGIN: called CC=1 SCAR=0.0
74 9 ▶use Net::WebSocket::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 use constant {
77 9 FIRST2 => 0,
78 LEN_LEN => 1,
79 MASK => 2,
80 PAYLOAD => 3,
81  
82 _RSV1 => chr(4 << 4),
83 _RSV2 => chr(2 << 4),
84 _RSV3 => chr(1 << 4),
85 9 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 #fin, rsv, mask, payload_sr
88 #rsv is a bitmask of the three values, with RSV1 as MOST significant bit.
89 #So, represent RSV1 and RSV2 being on via 0b110 (= 4 + 2 = 6)
90 sub new {
91 65 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=8 SCAR=20.8
Pod undocumented
92  
93 65 my ( $fin, $rsv, $mask, $payload_sr ) = @opts{ qw( fin rsv mask payload_sr ) };
94  
95 65 my $type = $class->get_type();
96  
97 65 my $opcode = $class->get_opcode($type);
98  
99 65 ▶ if (!defined $fin) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fin) 6 59 65
100 6 $fin = 1;
101 }
102  
103 65 ▶ $payload_sr ||= \do { my $v = q<> };
Condition100%2/2
$payload_sr ||= \do { my $v = "" }
l!l
632
MC/DC100%1/1
$payload_sr ||= \do { my $v = "" }
$payload_sr
Truth table100%2/2
A
A= $payload_sr
Aresult
11
00
104  
105 65 my ($byte2, $len_len) = $class->_assemble_length($payload_sr);
106  
107 65 ▶ if (defined $mask) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mask) { } 53 12 65
108 53 _validate_mask($mask);
109  
110 53 ▶ if (length $mask) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $mask) 53 0 53
111 53 $byte2 |= "\x80";
112 53 Net::WebSocket::Mask::apply($payload_sr, $mask);
113 }
114 }
115 else {
116 12 $mask = q<>;
117 }
118  
119 65 my $first2 = chr $opcode;
120 65 ▶ $first2 |= "\x80" if $fin;
Branch: 1 branch
BranchTrueFalseTotal
if $fin 30 35 65
121  
122 65 ▶ if ($rsv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rsv) 5 60 65
123 5 ▶ die "“rsv” must be < 0-7!" if $rsv > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $rsv > 7 0 5 5
124 5 $first2 |= chr( $rsv << 4 );
125 }
126  
127 65 substr( $first2, 1, 0, $byte2 );
128  
129 65 return bless [ \$first2, \$len_len, \$mask, $payload_sr ], $class;
130 }
131  
132 sub set_payload_sr {
133 0 ▶ my ($self, $new_payload_sr) = @_;
Subroutine set_payload_sr: not called CC=2 SCAR=17.9
Pod undocumented
134  
135 0 my ($byte2, $len_len) = $self->_assemble_length($new_payload_sr);
136  
137 0 ▶ if (length ${ $self->[2] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (length ${$self->[2];}) 0 0 0
138 0 $byte2 |= "\x80";
139 0 Net::WebSocket::Mask::apply($new_payload_sr, ${ $self->[2] });
140 }
141  
142 0 substr( ${ $self->[0] }, 1, 1, $byte2 );
143 0 @{$self}[1, 3] = (\$len_len, $new_payload_sr);
144  
145 0 return $self;
146 }
147  
148 # All string refs: first2, length octets, mask octets, payload
149 sub create_from_parse {
150 83 ▶ return bless \@_, shift;
Subroutine create_from_parse: called CC=1 SCAR=0.0
Pod undocumented
151 }
152  
153 sub get_mask_bytes {
154 93 ▶ my ($self) = @_;
Subroutine get_mask_bytes: called CC=1 SCAR=0.0
Pod undocumented
155  
156 93 return ${ $self->[MASK] };
157 }
158  
159 #To collect the goods
160 sub get_payload {
161 92 ▶ my ($self) = @_;
Subroutine get_payload: called CC=2 SCAR=6.9
Pod undocumented
162  
163 92 my $pl = "" . ${ $self->[PAYLOAD] };
164  
165 92 ▶ if (my $mask = $self->get_mask_bytes()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mask = $self->get_mask_bytes) 58 34 92
166 58 Net::WebSocket::Mask::apply( \$pl, $mask );
167 }
168  
169 92 return $pl;
170 }
171  
172 #For sending over the wire
173 sub to_bytes {
174 59 ▶ my ($self) = @_;
Subroutine to_bytes: called CC=1 SCAR=0.0
Pod undocumented
175  
176 59 return join( q<>, map { $$_ } @$self );
177 }
178  
179 sub get_rsv {
180 0 ▶ my ($self) = @_;
Subroutine get_rsv: not called CC=1 SCAR=6.9
Pod undocumented
181  
182 #0b01110000 = 0x70
183 0 return( ord( substr( ${ $self->[FIRST2] }, 0, 1 ) & "\x70" ) >> 4 );
184 }
185  
186 sub set_rsv {
187 0 ▶ my ($self, $rsv) = @_;
Subroutine set_rsv: not called CC=1 SCAR=6.9
Pod undocumented
188  
189 0 ${ $self->[FIRST2] } |= chr( $rsv << 4 );
190  
191 0 return $self;
192 }
193  
194 sub set_rsv1 {
195 0 ▶ ${ $_[0][FIRST2] } |= _RSV1();
Subroutine set_rsv1: not called CC=1 SCAR=6.9
Pod undocumented
196  
197 0 return $_[0];
198 }
199  
200 sub set_rsv2 {
201 0 ▶ ${ $_[0][FIRST2] } |= _RSV2();
Subroutine set_rsv2: not called CC=1 SCAR=6.9
Pod undocumented
202  
203 0 return $_[0];
204 }
205  
206 sub set_rsv3 {
207 0 ▶ ${ $_[0][FIRST2] } |= _RSV3();
Subroutine set_rsv3: not called CC=1 SCAR=6.9
Pod undocumented
208  
209 0 return $_[0];
210 }
211  
212 sub has_rsv1 {
213 0 ▶ return ("\0\0" ne (${ $_[0][FIRST2] } & _RSV1()));
Subroutine has_rsv1: not called CC=1 SCAR=6.9
Pod undocumented
214 }
215  
216 sub has_rsv2 {
217 0 ▶ return ("\0\0" ne (${ $_[0][FIRST2] } & _RSV2()));
Subroutine has_rsv2: not called CC=1 SCAR=6.9
Pod undocumented
218 }
219  
220 sub has_rsv3 {
221 0 ▶ return ("\0\0" ne (${ $_[0][FIRST2] } & _RSV3()));
Subroutine has_rsv3: not called CC=1 SCAR=6.9
Pod undocumented
222 }
223  
224 #----------------------------------------------------------------------
225 #Redundancies with methods in DataFrame.pm and ControlFrame.pm.
226 #These are here so that we don’t have to re-bless in order to get this
227 #information.
228  
229 sub is_control_frame {
230 0 ▶ my ($self) = @_;
Subroutine is_control_frame: not called CC=2 SCAR=17.9
Pod undocumented
231  
232 #8 == 0b1000 == 010
233 0 ▶ return( ($self->_extract_opcode() & 8) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$self->_extract_opcode & 8 ? : 0 0 0
234 }
235  
236 sub get_fin {
237 7 ▶ my ($self) = @_;
Subroutine get_fin: called CC=2 SCAR=6.9
Pod undocumented
238  
239 7 ▶ return( ord ("\x80" & ${$self->[$self->FIRST2]}) && 1 );
Condition100%2/2
ord("\200" & ${$$self[$self->FIRST2];}) && 1
!ll
25
MC/DC100%1/1
ord("\200" & ${$$self[$self->FIRST2];}) && 1
ord("\200" & ${$$self[$self->FIRST2];})
Truth table100%2/2
A
A= ord("\200" & ${$$self[$self->FIRST2];})
Aresult
00
11
240 }
241  
242 #----------------------------------------------------------------------
243  
244 #sub get_opcode {
245 # my ($class) = @_;
246 #
247 # die "$class (type “$type”) must define a custom get_opcode() method!";
248 #}
249  
250 #----------------------------------------------------------------------
251  
252 #Unneeded?
253 #sub set_mask_bytes {
254 # my ($self, $bytes) = @_;
255 #
256 # if (!defined $bytes) {
257 # die "Set either a 4-byte mask, or empty string!";
258 # }
259 #
260 # if (length $bytes) {
261 # _validate_mask($bytes);
262 #
263 # $self->_activate_highest_bit( $self->[FIRST2], 1 );
264 # }
265 # else {
266 # $self->_deactivate_highest_bit( $self->[FIRST2], 1 );
267 # }
268 #
269 # if (${ $self->[MASK] }) {
270 # Net::WebSocket::Mask::apply( $self->[PAYLOAD], ${ $self->[MASK] } );
271 # }
272 #
273 # $self->[MASK] = \$bytes;
274 #
275 # if ($bytes) {
276 # Net::WebSocket::Mask::apply( $self->[PAYLOAD], $bytes );
277 # }
278 #
279 # return $self;
280 #}
281  
282 #----------------------------------------------------------------------
283  
284 sub opcode_to_type {
285 0 ▶ my ($class, $opcode) = @_;
Subroutine opcode_to_type: not called CC=1 SCAR=6.9
Pod undocumented
286 0 return Net::WebSocket::Constants::opcode_to_type($opcode);
287 }
288  
289 9 our $AUTOLOAD;
290 sub AUTOLOAD {
291 148 ▶ my ($self) = shift;
Subroutine AUTOLOAD: called CC=6 SCAR=34.3
292  
293 148 ▶ return if substr( $AUTOLOAD, -8 ) eq ':DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if substr($Net::WebSocket::Frame::AUTOLOAD, -8) eq ":DESTROY" 148 0 148
294  
295 0 my $last_colon_idx = rindex( $AUTOLOAD, ':' );
296 0 my $method = substr( $AUTOLOAD, 1 + $last_colon_idx );
297  
298 #Figure out what type this is, and re-bless.
299 0 ▶ if (ref($self) eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq __PACKAGE__) 0 0 0
300 0 my $opcode = $self->_extract_opcode();
301 0 my $type = $self->opcode_to_type($opcode);
302  
303 0 my $class = __PACKAGE__ . "::$type";
304 0 ▶ if (!$class->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("new")) 0 0 0
305 0 Module::Load::load($class);
306 }
307  
308 0 bless $self, $class;
309  
310 0 ▶ if ($self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) 0 0 0
311 0 return $self->$method(@_);
312 }
313 }
314  
315 0 ▶ my $class = (ref $self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
316  
317 0 die( "$class has no method “$method”!" );
318 }
319  
320 #----------------------------------------------------------------------
321  
322 sub _extract_opcode {
323 0 ▶ my ($self) = @_;
Subroutine _extract_opcode: not called CC=1 SCAR=6.9
324  
325 0 return 0xf & ord substr( ${ $self->[FIRST2] }, 0, 1 );
326 }
327  
328 sub _validate_mask {
329 53 ▶ my ($bytes) = @_;
Subroutine _validate_mask: called CC=3 SCAR=12.7
330  
331 53 ▶ if (length $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bytes) 53 0 53
332 53 ▶ if (4 != length $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (4 != length $bytes) 0 53 53
333 0 my $len = length $bytes;
334 0 die "Mask must be 4 bytes long, not $len ($bytes)!";
335 }
336 }
337  
338 53 return;
339 }
340  
341 sub _activate_highest_bit {
342 0 ▶ my ($self, $sr, $offset) = @_;
Subroutine _activate_highest_bit: not called CC=1 SCAR=6.9
343  
344 0 substr( $$sr, $offset, 1 ) = chr( 0x80 | ord substr( $$sr, $offset, 1 ) );
345  
346 0 return;
347 }
348  
349 sub _deactivate_highest_bit {
350 0 ▶ my ($sr, $offset) = @_;
Subroutine _deactivate_highest_bit: not called CC=1 SCAR=6.9
351  
352 0 substr( $$sr, $offset, 1 ) = chr( 0x7f & ord substr( $$sr, $offset, 1 ) );
353  
354 0 return;
355 }
356  
357 9 1;
« WebSocket/Endpoint.pm ↑ Summary WebSocket/Handshake.pm »