| 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(); #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 | Returned as undef (for the code) and an empty string. This diverges | ||||||||||||||||||||||||||||
| 42 | from the RFC’s described behavior of returning code 1005. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =item * a code, and no reason | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Returned as the code number and an empty string. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =item * a code, and a reason that cannot exceed 123 bytes | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =back | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | The code (i.e., C<$code>) is subject to | ||||||||||||||||||||||||||||
| 53 | L<the limitations that RFC 6445 describes|https://tools.ietf.org/html/rfc6455#section-7.4>. | ||||||||||||||||||||||||||||
| 54 | You can also, in lieu of a numeric constant, use the following string | ||||||||||||||||||||||||||||
| 55 | constants that derive from L<Microsoft’s WebSocket API|https://msdn.microsoft.com/en-us/library/windows/desktop/hh449350(v=vs.85).aspx>: | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =over | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =item * C<SUCCESS> (1000) | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =item * C<ENDPOINT_UNAVAILABLE> (1001) | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =item * C<PROTOCOL_ERROR> (1002) | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =item * C<INVALID_DATA_TYPE> (1003) | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =item * C<INVALID_PAYLOAD> (1007) | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =item * C<POLICY_VIOLATION> (1008) | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =item * C<MESSAGE_TOO_BIG> (1009) | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =item * C<UNSUPPORTED_EXTENSIONS> (1010) | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =item * C<INTERNAL_ERROR>, aka C<SERVER_ERROR> (1011) | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | This appears as C<SERVER_ERROR> in Microsoft’s documentation; however, | ||||||||||||||||||||||||||||
| 78 | L<erratum 3227|https://www.rfc-editor.org/errata_search.php?rfc=6455> updates | ||||||||||||||||||||||||||||
| 79 | the RFC to have this status encompass client errors as well. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Net::WebSocket recognizes either string, but its parsing logic will return | ||||||||||||||||||||||||||||
| 82 | only C<INTERNAL_ERROR>. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =back | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | The following additional status constants derive from | ||||||||||||||||||||||||||||
| 87 | L<the official registry of status codes|http://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number> | ||||||||||||||||||||||||||||
| 88 | and are newer than either RFC 6455 or Microsoft’s API: | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =over | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =item * C<SERVICE_RESTART> (1012) | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =item * C<TRY_AGAIN_LATER> (1013) | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =item * C<BAD_GATEWAY> (1014) | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =back | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | It is hoped that a future update to the WebSocket specification | ||||||||||||||||||||||||||||
| 101 | can include these or similar constant names. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 106 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 15 | ▶ | use parent qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | Net::WebSocket::Base::ControlFrame | ||||||||||||||||||||||||||||
| 110 | 15 | ); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 15 | ▶ | use Call::Context (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 15 | ▶ | use Net::WebSocket::Constants (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 15 | ▶ | use Net::WebSocket::X (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 15 | ▶ | use constant get_opcode => 8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 47 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=26.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | 47 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 47 | ▶ | if (!$opts{'payload_sr'} && !defined $opts{'payload'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts{"payload_sr"} or defined $opts{"payload"}
MC/DC50%1/2
$opts{"payload_sr"} or defined $opts{"payload"}
$opts{"payload_sr"}
defined $opts{"payload"}
Truth table66%2/3
A or B
A= $opts{"payload_sr"}B= defined $opts{"payload"}
| |||||||||||||||||||||||||||||
| 123 | 46 | my $payload; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 46 | ▶ | if (my $code = delete $opts{'code'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 39 | my $num = Net::WebSocket::Constants::status_name_to_code($code); | |||||||||||||||||||||||||||
| 127 | 39 | ▶ | if (!$num) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 9 | $num = $code; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 9 | ▶ | if ($num !~ m<\A[1-4][0-9]{3}\z> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 3 | die Net::WebSocket::X->create('BadArg', 'code', $num, "Invalid WebSocket status code ($num) given"); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 6 | ▶ | if ( !Net::WebSocket::Constants::status_code_to_name($num) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 5 | ▶ | if ( $num < 4000 || $num > 4999 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$num < 4000 or $num > 4999
MC/DC0%0/2
$num < 4000 or $num > 4999
$num < 4000
$num > 4999
Truth table33%1/3
A or B
A= $num < 4000B= $num > 4999
| |||||||||||||||||||||||||||||
| 136 | 0 | die Net::WebSocket::X->create('BadArg', 'code', $num, "Disallowed WebSocket status code ($num) given"); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 36 | $payload = pack 'n', $num; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 36 | my $reason = delete $opts{'reason'}; | |||||||||||||||||||||||||||
| 144 | 36 | ▶ | if (defined $reason) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 20 | ▶ | if (length $reason > 123) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | die Net::WebSocket::X->create('BadArg', 'reason', $reason, 'Reason cannot exceed 123 bytes!'); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 20 | $payload .= $reason; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | else { | ||||||||||||||||||||||||||||
| 153 | 7 | my $reason = delete $opts{'reason'}; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 7 | ▶ | if (defined $reason && length $reason) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $reason and length $reason
MC/DC0%0/2
defined $reason and length $reason
defined $reason
length $reason
Truth table66%2/3
A and B
A= defined $reasonB= length $reason
| |||||||||||||||||||||||||||||
| 156 | 0 | warn "close frame constructor received “reason” ($opts{'reason'}) but no “code”!"; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 7 | $payload = q<>; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 43 | $opts{'payload'} = $payload; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 44 | return $class->SUPER::new( %opts ); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 51 | ▶ | sub get_code_and_reason { | ||||||||||||||||||||||||||
|
Subroutine
get_code_and_reason: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 169 | 51 | my ($self) = @_; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 51 | Call::Context::must_be_list(); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | #This shouldn’t happen … maybe leftover from previous architecture? | ||||||||||||||||||||||||||||
| 174 | 51 | ▶ | if ($self->get_type() ne 'close') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 0 | my $type = $self->get_type(); | |||||||||||||||||||||||||||
| 176 | 0 | die "Frame type is “$type”, not “close” as expected!"; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 51 | ▶ | if (!length ${ $self->[$self->PAYLOAD] }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 8 | return ( undef, q<> ); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 43 | return unpack 'na*', $self->get_payload(); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 15 | 1; | |||||||||||||||||||||||||||