| 1 | package Net::WebSocket::Handshake::Server; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | =encoding utf-8 | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | Net::WebSocket::Handshake::Server | ||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | my $hsk = Net::WebSocket::Handshake::Server->new( | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | #optional | ||||||||||||||||||
| 14 | subprotocols => [ 'echo', 'haha' ], | ||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | #optional | ||||||||||||||||||
| 17 | extensions => \@extension_objects, | ||||||||||||||||||
| 18 | ); | ||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | $hsk->valid_method_or_die( $http_method ); #optional | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | $hsk->consume_headers(@headers_kv_pairs); | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | my $resp_hdr = $hsk->to_string(); | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | This class implements WebSocket handshake logic for a server. | ||||||||||||||||||
| 29 | It handles the basics of handshaking and, optionally, subprotocol | ||||||||||||||||||
| 30 | and extension negotiation. | ||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | =cut | ||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | 3 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 35 | 3 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | 3 | ▶ | use parent qw( Net::WebSocket::Handshake ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | 3 | ▶ | use Call::Context (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 40 | 3 | ▶ | use Digest::SHA (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | 3 | ▶ | use Net::WebSocket::Constants (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 43 | 3 | ▶ | use Net::WebSocket::X (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | #no-op | ||||||||||||||||||
| 46 | 3 | ▶ | use constant _handle_unrecognized_extension => (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | =head1 METHODS | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | =head2 I<CLASS>->new( %OPTS ) | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | Returns an instance of this class. %OPTS is as described in the base class; | ||||||||||||||||||
| 53 | there are no options specific to this class. | ||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | =head2 I<OBJ>->valid_method_or_die( METHOD ) | ||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | Throws an exception if the given METHOD isn’t the HTTP method (GET) that | ||||||||||||||||||
| 58 | WebSocket requires for all requests. | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | You only need this if if you’re not using a request-parsing interface | ||||||||||||||||||
| 61 | that’s compatible with L<HTTP::Request>; otherwise, | ||||||||||||||||||
| 62 | L<Net::WebSocket::HTTP_R>’s C<handshake_consume_request()> function | ||||||||||||||||||
| 63 | will do this (and other niceties) for you. | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | =cut | ||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | sub valid_method_or_die { | ||||||||||||||||||
| 68 | 1 | ▶ | my ($self, $method) = @_; | ||||||||||||||||
|
Subroutine
valid_method_or_die: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | 1 | ▶ | if ($method ne Net::WebSocket::Constants::REQUIRED_HTTP_METHOD()) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 71 | 0 | die Net::WebSocket::X->new('BadHTTPMethod', $method); | |||||||||||||||||
| 72 | } | ||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | 1 | return; | |||||||||||||||||
| 75 | } | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | sub _consume_peer_header { | ||||||||||||||||||
| 78 | 6 | ▶ | my ($self, $name => $value) = @_; | ||||||||||||||||
|
Subroutine
_consume_peer_header: called CC=7 SCAR=19.5
| |||||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | 6 | ▶ | if ($name eq 'Sec-WebSocket-Version') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 81 | 1 | ▶ | if ( $value ne Net::WebSocket::Constants::PROTOCOL_VERSION() ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 82 | 0 | die Net::WebSocket::X->new('BadHeader', 'Sec-WebSocket-Version', $value, 'Unsupported protocol version; must be ' . Net::WebSocket::Constants::PROTOCOL_VERSION()); | |||||||||||||||||
| 83 | } | ||||||||||||||||||
| 84 | |||||||||||||||||||
| 85 | 1 | $self->{'_version_ok'} = 1; | |||||||||||||||||
| 86 | } | ||||||||||||||||||
| 87 | elsif ($name eq 'Sec-WebSocket-Key') { | ||||||||||||||||||
| 88 | 1 | $self->{'key'} = $value; | |||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | elsif ($name eq 'Sec-WebSocket-Protocol') { | ||||||||||||||||||
| 91 | 1 | Module::Load::load('Net::WebSocket::HTTP'); | |||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | 1 | for my $token ( Net::WebSocket::HTTP::split_tokens($value) ) { | |||||||||||||||||
| 94 | 3 | ▶ | if (!defined $self->{'_subprotocol'}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 95 | 2 | ($self->{'_subprotocol'}) = grep { $_ eq $token } @{ $self->{'subprotocols'} }; | |||||||||||||||||
| 96 | } | ||||||||||||||||||
| 97 | } | ||||||||||||||||||
| 98 | } | ||||||||||||||||||
| 99 | else { | ||||||||||||||||||
| 100 | 3 | $self->_consume_generic_header($name => $value); | |||||||||||||||||
| 101 | } | ||||||||||||||||||
| 102 | |||||||||||||||||||
| 103 | 6 | return; | |||||||||||||||||
| 104 | } | ||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | #Send only those extensions that we’ve deduced the client can actually use. | ||||||||||||||||||
| 107 | sub _should_include_extension_in_headers { | ||||||||||||||||||
| 108 | 0 | ▶ | my ($self, $xtn) = @_; | ||||||||||||||||
|
Subroutine
_should_include_extension_in_headers: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | 0 | return $xtn->ok_to_use(); | |||||||||||||||||
| 111 | } | ||||||||||||||||||
| 112 | |||||||||||||||||||
| 113 | sub _encode_subprotocols { | ||||||||||||||||||
| 114 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
_encode_subprotocols: called CC=3 SCAR=11.7
| |||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 1 | ▶ | local $self->{'subprotocols'} = defined($self->{'_subprotocol'}) ? [ $self->{'_subprotocol'} ] : undef if $self->{'_no_use_legacy'}; | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 1 | return $self->SUPER::_encode_subprotocols(); | |||||||||||||||||
| 119 | } | ||||||||||||||||||
| 120 | |||||||||||||||||||
| 121 | sub _valid_headers_or_die { | ||||||||||||||||||
| 122 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
_valid_headers_or_die: called CC=4 SCAR=14.5
| |||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | 1 | my @needed = $self->_missing_generic_headers(); | |||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | 1 | ▶ | push @needed, 'Sec-WebSocket-Version' if !$self->{'_version_ok'}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 127 | 1 | ▶ | push @needed, 'Sec-WebSocket-Key' if !$self->{'key'}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | 1 | ▶ | die "Need: [@needed]" if @needed; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | 1 | return; | |||||||||||||||||
| 132 | } | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | sub _create_header_lines { | ||||||||||||||||||
| 135 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
_create_header_lines: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 136 | |||||||||||||||||||
| 137 | 1 | Call::Context::must_be_list(); | |||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | return ( | ||||||||||||||||||
| 140 | 1 | 'HTTP/1.1 101 Switching Protocols', | |||||||||||||||||
| 141 | |||||||||||||||||||
| 142 | #For now let’s assume no one wants any other Upgrade: | ||||||||||||||||||
| 143 | #or Connection: values than the ones WebSocket requires. | ||||||||||||||||||
| 144 | 'Upgrade: websocket', | ||||||||||||||||||
| 145 | 'Connection: Upgrade', | ||||||||||||||||||
| 146 | |||||||||||||||||||
| 147 | 'Sec-WebSocket-Accept: ' . $self->get_accept(), | ||||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | $self->_encode_subprotocols(), | ||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | $self->_encode_extensions(), | ||||||||||||||||||
| 152 | ); | ||||||||||||||||||
| 153 | } | ||||||||||||||||||
| 154 | |||||||||||||||||||
| 155 | #---------------------------------------------------------------------- | ||||||||||||||||||
| 156 | |||||||||||||||||||
| 157 | =head1 LEGACY INTERFACE: SYNOPSIS | ||||||||||||||||||
| 158 | |||||||||||||||||||
| 159 | #...Parse the request’s headers yourself... | ||||||||||||||||||
| 160 | |||||||||||||||||||
| 161 | my $hsk = Net::WebSocket::Handshake::Server->new( | ||||||||||||||||||
| 162 | |||||||||||||||||||
| 163 | #base 64, gotten from request | ||||||||||||||||||
| 164 | key => '..', | ||||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | #optional - same as in non-legacy interface | ||||||||||||||||||
| 167 | subprotocols => [ 'echo', 'haha' ], | ||||||||||||||||||
| 168 | |||||||||||||||||||
| 169 | #optional, instances of Net::WebSocket::Handshake::Extension | ||||||||||||||||||
| 170 | extensions => \@extension_objects, | ||||||||||||||||||
| 171 | ); | ||||||||||||||||||
| 172 | |||||||||||||||||||
| 173 | #Note the need to conclude the header text manually. | ||||||||||||||||||
| 174 | print $hsk->create_header_text() . "\x0d\x0a"; | ||||||||||||||||||
| 175 | |||||||||||||||||||
| 176 | =cut | ||||||||||||||||||
| 177 | |||||||||||||||||||
| 178 | 3 | *get_accept = __PACKAGE__->can('_get_accept'); | |||||||||||||||||
| 179 | |||||||||||||||||||
| 180 | 3 | 1; | |||||||||||||||||