WebSocket/Handshake.pm

filter: Statementstmt 79.6% 86 / 108 Branchbran 54.7% 23 / 42 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 18 / 20 Podpod 80.0% 4 / 5 totaltotal 73.3% 132 / 180 SCAR 38.6
CC
29
Cov
72%
_consume_sec_websocket_extensions_header
20.0
_consume_generic_header
19.6
_encode_extensions
6.6
CRAP 47.7
« WebSocket/Frame.pm ↑ Summary WebSocket/Frame/close.pm »
1 package Net::WebSocket::Handshake;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Net::WebSocket::Handshake - base class for handshake objects
11  
12 =head1 DESCRIPTION
13  
14 This base class’s L<Net::WebSocket::Handshake::Server> and
15 L<Net::WebSocket::Handshake::Client> subclasses implement
16 WebSocket’s handshake logic. They handle the basics of a WebSocket
17 handshake and, optionally, subprotocol and extension negotiation.
18  
19 This base class is NOT directly instantiable.
20  
21 =cut
22  
23 3 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use HTTP::Headers::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use Module::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 3 ▶use Net::WebSocket::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use Net::WebSocket::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 use constant {
31 3 _WS_MAGIC_CONSTANT => '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
32 CRLF => "\x0d\x0a",
33 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 #----------------------------------------------------------------------
36  
37 =head1 METHODS
38  
39 =head2 I<CLASS>->new( %OPTS )
40  
41 Returns an instance of the relevant subclass
42 (L<Net::WebSocket::Handshake::Client|::Client> or
43 L<Net::WebSocket::Handshake::Server|::Server>).
44 The following are common options for both:
45  
46 =over
47  
48 =item * C<subprotocols> - A list of HTTP tokens (e.g., C<wamp.2.json>)
49 that stand for subprotocols that this endpoint can use via the WebSocket
50 connection.
51  
52 =item * C<extensions> - A list of extension objects that the Handshake
53 object will interact with to determine extension support.
54  
55 =head1 COMMON EXTENSION INTERFACE
56  
57 Each object in the C<extensions> array must implement the following methods:
58  
59 =over
60  
61 =item * C<token()> The extension’s token. (e.g., C<permessage-deflate>)
62  
63 =item * C<get_handshake_object()> Returns an instance of
64 L<Net::WebSocket::Handshake::Extension> to represent the extension and
65 its parameters in the HTTP headers.
66  
67 =item * C<consume_parameters(..)> Receives the extension parameters
68 (in the format that C<Net::WebSocket::Handshake::Extension::parameters()>
69 returns). This operation should configure the object to return the proper
70 value from its C<ok_to_use()> method.
71  
72 =item * C<ok_to_use()> A boolean that indicates whether the peer indicates
73 proper support for the extension. This should not be called until after
74 C<consume_parameters().
75  
76 =back
77  
78 =cut
79  
80 sub new {
81 5 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
82  
83 5 ▶ if ($opts{'extensions'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"extensions"}) 1 4 5
84 1 $opts{'_extension_tokens'} = { map { $_->token() => $_ } @{ $opts{'extensions'} } };
85 }
86  
87 5 return bless \%opts, $class;
88 }
89  
90 =head2 $sp_token = I<OBJ>->get_subprotocol()
91  
92 Returns the negotiated subprotocol’s token (e.g., C<wamp.2.json>).
93  
94 =cut
95  
96 sub get_subprotocol {
97 2 ▶ my $self = shift;
Subroutine get_subprotocol: called CC=2 SCAR=7.6
Pod documented
98  
99 2 ▶ if (!$self->{'_no_use_legacy'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_no_use_legacy"}) 0 2 2
100 0 die 'Must call consume_headers() first!';
101 }
102  
103 2 return $self->{'_subprotocol'};
104 }
105  
106 #sub get_match_extensions {
107 # my $self = shift;
108 #
109 # Call::Context::must_be_list();
110 #
111 # return { %{ $self->{'_match_extensions'} } };
112 #}
113  
114 sub consume_headers {
115 2 ▶ my ($self, @kv_pairs) = @_;
Subroutine consume_headers: called CC=2 SCAR=6.9
Pod undocumented
116  
117 2 $self->{'_no_use_legacy'} = 1;
118  
119 2 ▶ while ( my ($k => $v) = splice( @kv_pairs, 0, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@kv_pairs, 0, 2)) 10 2 12
120 10 $self->_consume_peer_header($k => $v);
121 }
122  
123 2 $self->_valid_headers_or_die();
124  
125 2 return;
126 }
127  
128 =head2 my $hdrs_txt = I<OBJ>->to_string()
129  
130 The text of the HTTP headers to send, with the 2nd trailing CR/LF
131 that ends the headers portion of an HTTP message.
132  
133 If you use this object
134 to negotiate a subprotocol and/or extensions, those will be included
135 in the output from this method.
136  
137 To append custom headers, do the following with the result of this method:
138  
139 substr($hdrs_txt, -2, 0) = '..';
140  
141 =cut
142  
143 sub to_string {
144 3 ▶ my $self = shift;
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
145  
146 3 return join( CRLF(), $self->_create_header_lines(), q<>, q<> );
147 }
148  
149 =head1 LEGACY INTERFACE
150  
151 Prior to version 0.5 this module was a great deal less “helpful”:
152 it required callers to parse out and write WebSocket headers,
153 doing most of the validation manually. Version 0.5 added a generic
154 interface for entering in HTTP headers, which allows Net::WebSocket to
155 handle the parsing and creation of HTTP headers as well as subprotocol
156 and extension negotiation.
157  
158 For now the legacy functionality is being left in; however,
159 it is considered DEPRECATED and will be removed eventually.
160  
161 =head2 my $hdrs_txt = I<OBJ>->create_header_text()
162  
163 The same output as C<to_string()> but minus the 2nd trailing
164 CR/LF. (This was intended to facilitate adding other headers; however,
165 that’s done easily enough with the newer C<to_string()>.)
166  
167 =cut
168  
169 sub create_header_text {
170 0 ▶ my $self = shift;
Subroutine create_header_text: not called CC=1 SCAR=6.9
Pod documented
171  
172 0 return join( CRLF(), $self->_create_header_lines(), q<> );
173 }
174  
175 =head1 SEE ALSO
176  
177 =over
178  
179 =item * L<Net::WebSocket::Handshake::Client>
180  
181 =item * L<Net::WebSocket::Handshake::Server>
182  
183 =back
184  
185 =cut
186  
187 #----------------------------------------------------------------------
188  
189 sub _get_accept {
190 3 ▶ my ($self) = @_;
Subroutine _get_accept: called CC=2 SCAR=7.1
191  
192 3 ▶ my $key_b64 = $self->{'key'} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $key_b64 = $self->{"key"} or do { die "Net::WebSocket::X"->create("BadArg", "key", $self->{"key"}) } 0 3 3
193 0 die Net::WebSocket::X->create('BadArg', key => $self->{'key'});
194 };
195  
196 3 $key_b64 =~ s<\A\s+|\s+\z><>g;
197  
198 3 my $accept = Digest::SHA::sha1_base64( $key_b64 . _WS_MAGIC_CONSTANT() );
199  
200 #pad base64
201 3 $accept .= '=' x (4 - (length($accept) % 4));
202  
203 3 return $accept;
204 }
205  
206 #Post-legacy, move this to Client and have the Server use logic
207 #that allows only one.
208 sub _encode_subprotocols {
209 3 ▶ my ($self) = @_;
Subroutine _encode_subprotocols: called CC=3 SCAR=12.0
210  
211 3 return ( $self->{'subprotocols'} && @{ $self->{'subprotocols'} }
212 3 ▶ ? ( 'Sec-WebSocket-Protocol: ' . join(', ', @{ $self->{'subprotocols'} } ) )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'subprotocols'} && @{$self->{'subprotocols'};} ? : 3 0 3
Condition33%1/3
$self->{"subprotocols"} and @{$self->{"subprotocols"};}
!ll&&!rl&&r
003
MC/DC0%0/2
$self->{"subprotocols"} and @{$self->{"subprotocols"};}
$self->{"subprotocols"} @{$self->{"subprotocols"};}
Truth table33%1/3
A and B
A= $self->{"subprotocols"}B= @{$self->{"subprotocols"};}
ABresult
0X0
100
111
213 : ()
214 );
215 }
216  
217 sub _encode_extensions {
218 3 ▶ my ($self) = @_;
Subroutine _encode_extensions: called CC=6 SCAR=18.8
219  
220 3 ▶ return if !$self->{'extensions'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"extensions"} 2 1 3
221  
222 1 my @handshake_xtns;
223 1 for my $xtn ( @{ $self->{'extensions'} } ) {
224 2 ▶ if ( $xtn->isa('Net::WebSocket::Handshake::Extension') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($xtn->isa('Net::WebSocket::Handshake::Extension')) { } 2 0 2
elsif ($self->_should_include_extension_in_headers($xtn)) { } 0 0 0
225 2 $self->_warn_legacy();
226 2 push @handshake_xtns, $xtn;
227 }
228 elsif ( $self->_should_include_extension_in_headers($xtn) ) {
229 0 push @handshake_xtns, $xtn->get_handshake_object();
230 }
231 }
232  
233 1 ▶ return if !@handshake_xtns;
Branch: 1 branch
BranchTrueFalseTotal
unless @handshake_xtns 0 1 1
234  
235 1 my ($first, @others) = @handshake_xtns;
236  
237 1 return 'Sec-WebSocket-Extensions: ' . $first->to_string(@others);
238 }
239  
240 sub _warn_legacy {
241 2 ▶ my ($self) = @_;
Subroutine _warn_legacy: called CC=2 SCAR=6.9
242  
243 2 ▶ if (!$self->{'_warned_legacy'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_warned_legacy"}) 1 1 2
244 1 my $ref = ref $self;
245 1 warn "You are using $ref’s legacy interface. This interface will eventually be removed from $ref entirely, so please update your application to the newer interface. (The update should simplify your code.)";
246  
247 1 $self->{'_warned_legacy'}++;
248 }
249  
250 2 return;
251 }
252  
253 sub _missing_generic_headers {
254 2 ▶ my ($self) = @_;
Subroutine _missing_generic_headers: called CC=3 SCAR=11.3
255  
256 2 my @missing;
257 2 ▶ push @missing, 'Connection' if !$self->{'_connection_header_ok'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_connection_header_ok"} 0 2 2
258 2 ▶ push @missing, 'Upgrade' if !$self->{'_upgrade_header_ok'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_upgrade_header_ok"} 0 2 2
259  
260 2 return @missing;
261 }
262  
263 sub _consume_sec_websocket_extensions_header {
264 0 ▶ my ($self, $value) = @_;
Subroutine _consume_sec_websocket_extensions_header: not called CC=4 SCAR=30.0
265  
266 0 Module::Load::load('Net::WebSocket::Handshake::Extension');
267  
268 0 for my $xtn ( Net::WebSocket::Handshake::Extension->parse_string($value) ) {
269 0 my $xtn_token = $xtn->token();
270 0 my $xtn_handler = $self->{'_extension_tokens'}{ $xtn_token };
271 0 ▶ if ($xtn_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xtn_handler) { } 0 0 0
272 0 $xtn_handler->consume_parameters($xtn->parameters());
273  
274 0 ▶ if ($xtn_handler->ok_to_use()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xtn_handler->ok_to_use) 0 0 0
275 0 $self->{'_match_extensions'}{ $xtn_token } = $xtn_handler;
276 }
277 }
278 else {
279 0 $self->_handle_unrecognized_extension($xtn);
280 }
281 }
282  
283 0 return;
284 }
285  
286 sub _consume_generic_header {
287 5 ▶ my ($self, $hname, $value) = @_;
Subroutine _consume_generic_header: called CC=12 SCAR=29.8
288  
289 5 tr<A-Z><a-z> for ($hname);
290  
291 5 ▶ if ($hname eq 'connection') {
Branch: 4 branches
BranchTrueFalseTotal
if ($hname eq 'connection') { } 2 3 5
elsif ($hname eq 'upgrade') { } 2 1 3
elsif ($hname eq 'sec-websocket-protocol') { } 0 1 1
elsif ($hname eq 'sec-websocket-extensions') { } 0 1 1
292 2 $value =~ tr<A-Z><a-z>;
293 2 for my $t ( Net::WebSocket::HTTP::split_tokens($value) ) {
294 2 ▶ if ($t eq 'upgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "upgrade") 2 0 2
295 2 $self->{'_connection_header_ok'} = 1;
296 }
297 }
298 }
299 elsif ($hname eq 'upgrade') {
300 2 $value =~ tr<A-Z><a-z>;
301 2 for my $t ( Net::WebSocket::HTTP::split_tokens($value) ) {
302 2 ▶ if ($t eq 'websocket') {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "websocket") 2 0 2
303 2 $self->{'_upgrade_header_ok'} = 1;
304 }
305 }
306 }
307 elsif ($hname eq 'sec-websocket-protocol') {
308 0 for my $token ( Net::WebSocket::HTTP::split_tokens($value) ) {
309 0 ▶ if (!defined $self->{'_match_protocol'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_match_protocol"}) 0 0 0
310 0 ($self->{'_match_protocol'}) = grep { $_ eq $token } @{ $self->{'subprotocols'} };
311 }
312 }
313 }
314 elsif ($hname eq 'sec-websocket-extensions') {
315 0 $self->_consume_sec_websocket_extensions_header($value);
316 }
317  
318 5 return;
319 }
320  
321 3 1;
« WebSocket/Frame.pm ↑ Summary WebSocket/Frame/close.pm »