WebSocket/Handshake/Client.pm

filter: Statementstmt 88.2% 75 / 85 Branchbran 63.3% 19 / 30 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 94.1% 16 / 17 Podpod 50.0% 2 / 4 totaltotal 74.3% 116 / 156 CC 21 SCAR 15.6
CC
21
Cov
77%
new
8.2
_create_header_lines
7.8
_consume_peer_header
7.1
CRAP 4.8
« WebSocket/PMCE/deflate/Client.pm ↑ Summary WebSocket/Handshake/Server.pm »
1 package Net::WebSocket::Handshake::Client;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Net::WebSocket::Handshake::Client
8  
9 =head1 SYNOPSIS
10  
11 my $hsk = Net::WebSocket::Handshake::Client->new(
12  
13 #required
14 uri => 'ws://haha.test',
15  
16 #optional, to imitate a web client
17 origin => ..,
18  
19 #optional, base 64 .. auto-created if not given
20 key => '..',
21  
22 #optional
23 subprotocols => [ 'echo', 'haha' ],
24  
25 #optional
26 extensions => \@extension_objects,
27 );
28  
29 print $hsk->to_string();
30  
31 $hsk->consume_headers( NAME1 => VALUE1, .. );
32  
33 =head1 DESCRIPTION
34  
35 This class implements WebSocket handshake logic for a client.
36 It handles the basics of handshaking and, optionally, subprotocol
37 and extension negotiation.
38  
39 It is a subclass of L<Net::WebSocket::Handshake>.
40  
41 =cut
42  
43 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 4 ▶use parent qw( Net::WebSocket::Handshake );
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 4 ▶use URI::Split ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 4 ▶use Net::WebSocket::Constants ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51 4 ▶use Net::WebSocket::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 4 ▶use constant SCHEMAS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
54 'ws', 'wss',
55 'http', 'https',
56 4 );
57  
58 =head1 METHODS
59  
60 =head2 I<OBJ>->new( %OPTS )
61  
62 Returns an instance of the class; %OPTS includes the options from
63 L<Net::WebSocket::Handshake> as well as:
64  
65 =over
66  
67 =item * C<uri> - (required) The full URI you’re connecting to.
68  
69 =item * C<origin> - (optional) The HTTP Origin header’s value. Useful
70 for imitating a web browser.
71  
72 =back
73  
74 =cut
75  
76 4 ▶sub new {
Subroutine new: called CC=6 SCAR=21.0
Pod documented
77 4 my ($class, %opts) = @_;
78  
79 4 ▶ if (length $opts{'uri'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $opts{"uri"}) 4 0 4
80 4 @opts{ 'uri_schema', 'uri_auth', 'uri_path', 'uri_query' } = URI::Split::uri_split($opts{'uri'});
81 }
82  
83 4 ▶ if (!$opts{'uri_schema'} || !grep { $_ eq $opts{'uri_schema'} } SCHEMAS()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"uri_schema"} and grep {$_ eq $opts{"uri_schema"};} ("ws", "wss", "http", "https")) 0 4 4
Condition33%1/3
$opts{"uri_schema"} and grep {$_ eq $opts{"uri_schema"};} ("ws", "wss", "http", "https")
!ll&&!rl&&r
004
MC/DC0%0/2
$opts{"uri_schema"} and grep {$_ eq $opts{"uri_schema"};} ("ws", "wss", "http", "https")
$opts{"uri_schema"} grep {$_ eq $opts{"uri_schema"};} ("ws", "wss", "http", "https")
Truth table33%1/3
A and B
A= $opts{"uri_schema"}B= grep {$_ eq $opts{"uri_schema"};} ("ws", "wss", "http", "https")
ABresult
0X0
100
111
84 0 die Net::WebSocket::X->create('BadArg', uri => $opts{'uri'});
85 }
86  
87 4 ▶ if (!length $opts{'uri_auth'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $opts{"uri_auth"}) 0 4 4
88 0 die Net::WebSocket::X->create('BadArg', uri => $opts{'uri'});
89 }
90  
91 4 @opts{ 'uri_host', 'uri_port' } = split m<:>, $opts{'uri_auth'};
92  
93 4 ▶ $opts{'key'} ||= _create_key();
Condition33%1/3
$opts{"key"} ||= _create_key()
l!l&&r!l&&!r
040
MC/DC0%0/2
$opts{"key"} ||= _create_key()
$opts{"key"} _create_key()
Truth table33%1/3
A ||= B
A= $opts{"key"}B= _create_key()
ABresult
1X1
011
000
94  
95 4 return $class->SUPER::new(%opts);
96 }
97  
98 =head2 I<OBJ>->valid_status_or_die( CODE, REASON )
99  
100 Throws an exception if the given CODE isn’t the HTTP status code (101)
101 that WebSocket requires in response to all requests. (REASON is included
102 with the exception on error; otherwise it’s unused.)
103  
104 You only need this if if you’re not using a request-parsing interface
105 that’s compatible with L<HTTP::Response>; otherwise,
106 L<Net::WebSocket::HTTP_R>’s C<handshake_consume_response()> function
107 will do this (and other niceties) for you.
108  
109 =cut
110  
111 1 ▶sub valid_status_or_die {
Subroutine valid_status_or_die: called CC=2 SCAR=7.6
Pod documented
112 1 my ($self, $code, $reason) = @_;
113  
114 1 ▶ if ($code ne Net::WebSocket::Constants::REQUIRED_HTTP_STATUS()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne 101) 0 1 1
115 0 die Net::WebSocket::X->create('BadHTTPStatus', $code, $reason);
116 }
117  
118 1 return;
119 }
120  
121 #Shouldn’t be needed?
122 1 ▶sub get_key {
Subroutine get_key: called CC=1 SCAR=0.0
Pod undocumented
123 1 my ($self) = @_;
124  
125 1 return $self->{'key'};
126 }
127  
128 #----------------------------------------------------------------------
129 #Legacy:
130  
131 =head1 LEGACY INTERFACE: SYNOPSIS
132  
133 my $hsk = Net::WebSocket::Handshake::Client->new(
134  
135 #..same as the newer interface, except:
136  
137 #optional
138 extensions => \@extension_objects,
139 );
140  
141 print $hsk->create_header_text() . "\x0d\x0a";
142  
143 #...Parse the response’s headers yourself...
144  
145 #Validates the value of the “Sec-WebSocket-Accept” header;
146 #throws Net::WebSocket::X::BadAccept if not.
147 $hsk->validate_accept_or_die($accept_value);
148  
149 =cut
150  
151 2 ▶sub validate_accept_or_die {
Subroutine validate_accept_or_die: called CC=2 SCAR=7.6
Pod undocumented
152 2 my ($self, $received) = @_;
153  
154 2 my $should_be = $self->_get_accept();
155  
156 2 ▶ return if $received eq $should_be;
Branch: 1 branch
BranchTrueFalseTotal
if $received eq $should_be 2 0 2
157  
158 0 die Net::WebSocket::X->create('BadAccept', $should_be, $received );
159 }
160  
161 #----------------------------------------------------------------------
162  
163 3 ▶sub _create_header_lines {
Subroutine _create_header_lines: called CC=6 SCAR=20.5
164 3 my ($self) = @_;
165  
166 3 my $path = $self->{'uri_path'};
167  
168 3 ▶ if (!defined $path || !length $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path and length $path) 3 0 3
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
030
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
169 3 $path = '/';
170 }
171  
172 3 ▶ if (defined $self->{'uri_query'} && length $self->{'uri_query'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"uri_query"} and length $self->{"uri_query"}) 0 3 3
Condition33%1/3
defined $self->{"uri_query"} and length $self->{"uri_query"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $self->{"uri_query"} and length $self->{"uri_query"}
defined $self->{"uri_query"} length $self->{"uri_query"}
Truth table33%1/3
A and B
A= defined $self->{"uri_query"}B= length $self->{"uri_query"}
ABresult
0X0
100
111
173 0 $path .= "?$self->{'uri_query'}";
174 }
175  
176 return (
177 "GET $path HTTP/1.1",
178 "Host: $self->{'uri_host'}",
179  
180 #For now let’s assume no one wants any other Upgrade:
181 #or Connection: values than the ones WebSocket requires.
182 'Upgrade: websocket',
183 'Connection: Upgrade',
184  
185 "Sec-WebSocket-Key: $self->{'key'}",
186 'Sec-WebSocket-Version: ' . Net::WebSocket::Constants::PROTOCOL_VERSION(),
187  
188 $self->_encode_extensions(),
189  
190 $self->_encode_subprotocols(),
191  
192 3 ▶ ( $self->{'origin'} ? "Origin: $self->{'origin'}" : () ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'origin'} ? : 1 2 3
193 );
194 }
195  
196 2 ▶sub _die_if_missing_headers {
Subroutine _die_if_missing_headers: called CC=3 SCAR=11.8
197 2 my ($self) = @_;
198  
199 2 my @needed = $self->_missing_generic_headers();
200 2 ▶ push @needed, 'Sec-WebSocket-Accept' if !$self->{'_accept_header_ok'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_accept_header_ok"} 0 2 2
201  
202 2 ▶ if (@needed) {
Branch: 1 branch
BranchTrueFalseTotal
if (@needed) 0 2 2
203 0 die Net::WebSocket::X->create('MissingHeaders', @needed);
204 }
205  
206 2 return;
207 }
208  
209 7 ▶sub _consume_peer_header {
Subroutine _consume_peer_header: called CC=7 SCAR=19.7
210 7 my ($self, $name => $value) = @_;
211  
212 7 my $orig_name = $name;
213  
214 7 $name =~ tr<A-Z><a-z>; #case insensitivity
215  
216 7 for my $hdr_part ( qw( accept protocol extensions ) ) {
217 21 ▶ if ($name eq "sec-websocket-$hdr_part") {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "sec-websocket-$hdr_part") 3 18 21
218 3 ▶ if ( exists $self->{"_got_$name"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$self{"_got_$name"}) 0 3 3
219 0 die Net::WebSocket::X->create('DuplicateHeader', $orig_name, $self->{"_got_$name"}, $value);
220 }
221  
222 3 $self->{"_got_$name"} = $value;
223 }
224 }
225  
226 7 ▶ if ($name eq 'sec-websocket-accept') {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'sec-websocket-accept') { } 2 5 7
elsif ($name eq 'sec-websocket-protocol') { } 1 4 5
227 2 $self->validate_accept_or_die($value);
228 2 $self->{'_accept_header_ok'} = 1;
229 }
230 elsif ($name eq 'sec-websocket-protocol') {
231 1 ▶ if (!grep { $_ eq $value } @{ $self->{'subprotocols'} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $value;} @{$$self{"subprotocols"};}) 0 1 1
232 0 die Net::WebSocket::X->create('UnknownSubprotocol', $value);
233 }
234  
235 1 $self->{'_subprotocol'} = $value;
236 }
237 else {
238 4 $self->_consume_generic_header($name => $value);
239 }
240  
241 7 return;
242 }
243  
244 0 ▶sub _handle_unrecognized_extension {
Subroutine _handle_unrecognized_extension: not called CC=1 SCAR=6.9
245 0 my ($self, $xtn_obj) = @_;
246  
247 0 die Net::WebSocket::X->create('UnknownExtension', $xtn_obj->to_string());
248 }
249  
250  
251 4 ▶sub _create_key {
Subroutine _create_key: called CC=1 SCAR=0.0
252 4 require MIME::Base64;
253  
254 #NB: Not cryptographically secure, but it should be good enough
255 #for the purpose of a nonce. Most implementations use TLS anyway,
256 #so this is kind of pointless except that the RFC mandates it. :-/
257 4 my $sixteen_bytes = pack 'S8', map { rand 65536 } 1 .. 8;
258  
259 4 my $b64 = MIME::Base64::encode_base64($sixteen_bytes);
260 4 chomp $b64;
261  
262 4 return $b64;
263 }
264  
265 #Send all extensions to the server in the request.
266 4 ▶use constant _should_include_extension_in_headers => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
267  
268 4 1;
« WebSocket/PMCE/deflate/Client.pm ↑ Summary WebSocket/Handshake/Server.pm »