WebSocket/Frame/close.pm

filter: Statementstmt 53.1% 25 / 47 Branchbran 15.0% 3 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 2 totaltotal 42.6% 35 / 82 CC 12 SCAR 40.7
CC
12
Cov
40%
new
110.0
get_code_and_reason
3.1
BEGIN
1.0
CRAP 58.7
↑ Summary WebSocket/Handshake/Server.pm »
1 package Net::WebSocket::Frame::close;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Net::WebSocket::Frame::close
8  
9 =head1 SYNOPSIS
10  
11 my $frm = Net::WebSocket::Frame::close->new(
12  
13 #Optional, can be either empty (default) or four random bytes
14 mask => q<>,
15  
16 code => 'SUCCESS', #See below
17  
18 reason => 'yeah, baby', #See below
19 );
20  
21 $frm->get_type(); #"close"
22  
23 $frm->is_control_frame(); #1
24  
25 my $mask = $frm->get_mask_bytes();
26  
27 my ($code, $reason) = $frm->get_code_and_reason();
28  
29 #If, for some reason, you need the raw payload:
30 my $payload = $frm->get_payload();
31  
32 my $serialized = $frm->to_bytes();
33  
34 Note that, L<as per RFC 6455|https://tools.ietf.org/html/rfc6455#section-5.5>,
35 close messages can have any of:
36  
37 =over
38  
39 =item * no code, and no reason
40  
41 =item * a code, and no reason
42  
43 =item * a code, and a reason that cannot exceed 123 bytes
44  
45 =back
46  
47 The code (i.e., C<$code>) is subject to
48 L<the limitations that RFC 6445 describes|https://tools.ietf.org/html/rfc6455#section-7.4>.
49 You can also, in lieu of a numeric constant, use the following string
50 constants that L<Microsoft defines|https://msdn.microsoft.com/en-us/library/windows/desktop/hh449350(v=vs.85).aspx>:
51  
52 =over
53  
54 =item * C<SUCCESS> (1000)
55  
56 =item * C<ENDPOINT_UNAVAILABLE> (1001)
57  
58 =item * C<PROTOCOL_ERROR> (1002)
59  
60 =item * C<INVALID_DATA_TYPE> (1003)
61  
62 =item * C<INVALID_PAYLOAD> (1007)
63  
64 =item * C<POLICY_VIOLATION> (1008)
65  
66 =item * C<MESSAGE_TOO_BIG> (1009)
67  
68 =item * C<UNSUPPORTED_EXTENSIONS> (1010)
69  
70 =item * C<SERVER_ERROR> (1011)
71  
72 NOTE: As per L<erratum|https://www.rfc-editor.org/errata_search.php?rfc=6455>
73 3227, this status is meant to encompass client errors as well. Since these
74 constants are meant to match Microsoft’s (in default of such in the actual
75 WebSocket standard), however, Net::WebSocket only recognizes C<SERVER_ERROR>
76 as an alias of 1011. Hopefully a future update to the WebSocket standard will
77 include useful string aliases for the status codes.
78  
79 Also note that L<the official list of status codes|http://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number> contains some that
80 don’t have string constants.
81  
82 =back
83  
84 =cut
85  
86 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 3 use parent qw(
90 Net::WebSocket::Base::ControlFrame
91 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 3 ▶use Call::Context ();
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 3 ▶use Net::WebSocket::Constants ();
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 3 ▶use constant get_opcode => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 sub new {
100 0 ▶ my ($class, %opts) = @_;
Subroutine new: not called CC=10 SCAR=47.0
Pod undocumented
101  
102 0 ▶ if (!$opts{'payload_sr'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"payload_sr"}) 0 0 0
103 0 my $payload;
104  
105 0 ▶ if ($opts{'code'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'code'}) { } 0 0 0
106 0 my $num = Net::WebSocket::Constants::status_name_to_code($opts{'code'});
107 0 ▶ if (!$num) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num) 0 0 0
108 0 $num = $opts{'code'};
109  
110 0 ▶ if ($num !~ m<\A[0-9]{4}\z> ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num =~ /\A[0-9]{4}\z/) 0 0 0
111 0 die Net::WebSocket::X->create('BadArg', 'code', $num, 'Invalid WebSocket status code');
112 }
113  
114 0 ▶ if ( !Net::WebSocket::Constants::status_code_to_name($num) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Net::WebSocket::Constants::status_code_to_name($num)) 0 0 0
115 0 ▶ if ( $num < 4000 || $num > 4999 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num < 4000 or $num > 4999) 0 0 0
Condition0%0/3
$num < 4000 or $num > 4999
l!l&&r!l&&!r
000
MC/DC0%0/2
$num < 4000 or $num > 4999
$num < 4000 $num > 4999
Truth table0%0/3
A or B
A= $num < 4000B= $num > 4999
ABresult
1X1
011
000
116 0 die Net::WebSocket::X->create('BadArg', 'code', $num, 'Disallowed WebSocket status code');
117 }
118 }
119 }
120  
121 0 $payload = pack 'n', $num;
122  
123 0 ▶ if (defined $opts{'reason'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"reason"}) 0 0 0
124 0 ▶ if (length $opts{'reason'} > 123) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $opts{"reason"} > 123) 0 0 0
125 0 die Net::WebSocket::X->create('BadArg', 'reason', $opts{'reason'}, 'Reason cannot exceed 123 bytes!');
126 }
127  
128 0 $payload .= $opts{'reason'};
129 }
130 }
131 else {
132 0 $payload = q<>;
133 }
134  
135 0 $opts{'payload_sr'} = \$payload;
136 }
137  
138 0 return $class->SUPER::new( %opts, type => 'close' );
139 }
140  
141 sub get_code_and_reason {
142 15 ▶ my ($self) = @_;
Subroutine get_code_and_reason: called CC=3 SCAR=11.4
Pod undocumented
143  
144 15 Call::Context::must_be_list();
145  
146 #This shouldn’t happen … maybe leftover from previous architecture?
147 15 ▶ if ($self->get_type() ne 'close') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_type ne "close") 0 15 15
148 0 my $type = $self->get_type();
149 0 die "Frame type is “$type”, not “close” as expected!";
150 }
151  
152 15 ▶ return if !length ${ $self->[$self->PAYLOAD] };
Branch: 1 branch
BranchTrueFalseTotal
unless length ${$$self[$self->PAYLOAD];} 5 10 15
153  
154 10 return unpack 'na*', $self->get_payload();
155 }
156  
157 3 1;
↑ Summary WebSocket/Handshake/Server.pm »