WebSocket/Defragmenter.pm

filter: Statementstmt 97.2% 35 / 36 Branchbran 85.0% 17 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 93.6% 59 / 63 CC 11 SCAR 19.7
CC
11
Cov
93%
get_next_message
10.0
_on_protocol_error
2.0
BEGIN
1.0
CRAP 7.2
« WebSocket/Frame.pm ↑ Summary WebSocket/PMCE/deflate/Server.pm »
1 package Net::WebSocket::Defragmenter;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Net::WebSocket::Defragmenter
11  
12 =head1 SYNOPSIS
13  
14 my $defragger = Net::WebSocket::Defragmenter->new(
15 parser => $parser_obj, #i.e., isa Net::WebSocket::Parser
16  
17 #Optional; these two receive the Net::WebSocket::Frame object.
18 on_control_frame => sub { ... },
19 on_data_frame => sub { ... },
20  
21 #Optional; receives a type string and a human-readable message.
22 #An exception is thrown after this callback runs.
23 on_protocol_error => sub { ... },
24 );
25  
26 my $msg_or_undef = $defragger->get_next_message();
27  
28 =head1 DESCRIPTION
29  
30 You ordinarily shouldn’t instantiate this class because
31 L<Net::WebSocket::Endpoint> already uses it.
32  
33 This class implements WebSocket’s defragmentation logic.
34 It’s mostly meant for internal use but is documented for cases
35 where L<Net::WebSocket::Endpoint> may not be usable or desirable.
36  
37 =cut
38  
39 =head1 METHODS
40  
41 =head2 I<CLASS>->new( %OPTS )
42  
43 See SYNOPSIS above.
44  
45 =cut
46  
47 10 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
48 10 my ($class, %opts) = @_;
49  
50 my %self = (
51 _fragments => [],
52  
53 10 ( map { ( "_$_" => $opts{$_} ) } (
54 'parser',
55 'on_control_frame',
56 'on_data_frame',
57 'on_protocol_error',
58 ) ),
59 );
60  
61 10 return bless \%self, $class;
62 }
63  
64 =head2 I<OBJ>->get_next_message()
65  
66 Reads a frame from C<parser>.
67  
68 Returns a L<Net::WebSocket::Message> object if there is a message
69 ready to return; otherwise returns undef.
70  
71 An exception (L<Net::WebSocket::X>) is thrown on fragmentation errors.
72  
73 =cut
74  
75 23 ▶sub get_next_message {
Subroutine get_next_message: called CC=10 SCAR=23.1
Pod documented
76 23 my ($self) = @_;
77  
78 23 my $_msg_frame;
79  
80 23 ▶ if ( $_msg_frame = $self->{'_parser'}->get_next_frame() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_msg_frame = $self->{"_parser"}->get_next_frame) 22 1 23
81 22 ▶ if ($_msg_frame->is_control()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_msg_frame->is_control) { } 7 15 22
82 7 ▶ if ($self->{'_on_control_frame'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_on_control_frame"}) 7 0 7
83 7 $self->{'_on_control_frame'}->($_msg_frame);
84 }
85 }
86 else {
87 15 ▶ if ($self->{'_on_data_frame'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_on_data_frame"}) 0 15 15
88 0 $self->{'_on_data_frame'}->($_msg_frame);
89 }
90  
91 #Failure cases:
92 # - continuation without prior fragment
93 # - non-continuation within fragment
94  
95 15 ▶ if ( $_msg_frame->get_type() eq 'continuation' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_msg_frame->get_type eq 'continuation') { } 6 9 15
elsif (@{$self->{'_fragments'};}) { } 1 8 9
96 6 ▶ if ( !@{ $self->{'_fragments'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"_fragments"};}) 1 5 6
97 1 $self->_on_protocol_error(
98 'ReceivedBadControlFrame',
99 sprintf('Received continuation outside of fragment!'),
100 );
101 }
102 }
103 9 elsif ( @{ $self->{'_fragments'} } ) {
104 1 $self->_on_protocol_error(
105 'ReceivedBadDataFrame',
106 sprintf('Received %s; expected continuation!', $_msg_frame->get_type())
107 );
108 }
109  
110 13 ▶ if ($_msg_frame->get_fin()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_msg_frame->get_fin) { } 7 6 13
111 return Net::WebSocket::Message->new(
112 7 splice( @{ $self->{'_fragments'} } ),
113 $_msg_frame,
114 );
115 }
116 else {
117 6 push @{ $self->{'_fragments'} }, $_msg_frame;
118 }
119 }
120  
121 13 $_msg_frame = undef;
122 }
123  
124 14 ▶ return defined($_msg_frame) ? q<> : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $_msg_frame ? : 1 13 14
125 }
126  
127 2 ▶sub _on_protocol_error {
Subroutine _on_protocol_error: called CC=2 SCAR=7.0
128 2 my ($self, $type, $msg) = @_;
129  
130 2 ▶ if ( $self->{'_on_protocol_error'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_on_protocol_error"}) 2 0 2
131 2 $self->{'_on_protocol_error'}->( $type, $msg );
132 }
133  
134 2 die Net::WebSocket::X->create( $type, $msg );
135 }
136  
137 11 1;
« WebSocket/Frame.pm ↑ Summary WebSocket/PMCE/deflate/Server.pm »