| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2010-2017 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Net::Async::WebSocket::Client; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use base qw( Net::Async::WebSocket::Protocol ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use 5.010; # // | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | IO::Async::Notifier->VERSION( '0.63' ); # ->adopt_future | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | BEGIN { | ||||||||||||||||||||||||||||
| 22 | eval { | ||||||||||||||||||||||||||||
| 23 | 3 | require URI::wss; | |||||||||||||||||||||||||||
| 24 | 3 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=16.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | # In case URI doesn't know that ws:// and wss:// URIs use host/port | ||||||||||||||||||||||||||||
| 26 | 0 | require URI::_server; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | @URI::ws::ISA = qw( URI::_server ); | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | *URI::ws::default_port = sub { 80 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | @URI::wss::ISA = qw( URI::_server ); | |||||||||||||||||||||||||||
| 32 | 0 | ▶ | *URI::wss::default_port = sub { 443 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | *URI::wss::secure = sub { 1 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 34 | }; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # We also need to support ->resource_name, which the CPAN module does not | ||||||||||||||||||||||||||||
| 37 | # understand as of 2017-01-01 | ||||||||||||||||||||||||||||
| 38 | 3 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | *URI::wss::resource_name = sub { | ||||||||||||||||||||||||||||
| 40 | shift->path_query | ||||||||||||||||||||||||||||
| 41 | 3 | ▶ | } unless URI::wss->can( "resource_name" ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | our $VERSION = '0.13'; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 3 | ▶ | use Protocol::WebSocket::Handshake::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 NAME | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | C<Net::Async::WebSocket::Client> - connect to a WebSocket server using | ||||||||||||||||||||||||||||
| 51 | C<IO::Async> | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | use IO::Async::Loop; | ||||||||||||||||||||||||||||
| 56 | use Net::Async::WebSocket::Client; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | my $client = Net::Async::WebSocket::Client->new( | ||||||||||||||||||||||||||||
| 59 | on_text_frame => sub { | ||||||||||||||||||||||||||||
| 60 | my ( $self, $frame ) = @_; | ||||||||||||||||||||||||||||
| 61 | print $frame; | ||||||||||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||||||||||
| 66 | $loop->add( $client ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | $client->connect( | ||||||||||||||||||||||||||||
| 69 | url => "ws://$HOST:$PORT/", | ||||||||||||||||||||||||||||
| 70 | )->then( sub { | ||||||||||||||||||||||||||||
| 71 | $client->send_text_frame( "Hello, world!\n" ); | ||||||||||||||||||||||||||||
| 72 | })->get; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | $loop->run; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | This subclass of L<Net::Async::WebSocket::Protocol> connects to a WebSocket | ||||||||||||||||||||||||||||
| 79 | server to establish a WebSocket connection for passing frames. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub new | ||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||
| 85 | 3 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 3 | return $class->SUPER::new( | |||||||||||||||||||||||||||
| 87 | masked => 1, | ||||||||||||||||||||||||||||
| 88 | @_, | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 METHODS | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | The following methods documented with a trailing call to C<< ->get >> return | ||||||||||||||||||||||||||||
| 95 | L<Future> instances. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub _do_handshake | ||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_do_handshake: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | 3 | my %params = @_; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | my $hs = Protocol::WebSocket::Handshake::Client->new( | ||||||||||||||||||||||||||||
| 105 | url => $params{url}, | ||||||||||||||||||||||||||||
| 106 | req => $params{req}, | ||||||||||||||||||||||||||||
| 107 | 3 | ); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 3 | $self->debug_printf( "HANDSHAKE start" ); | |||||||||||||||||||||||||||
| 110 | 3 | $self->write( $hs->to_string ); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 3 | my $f = $self->loop->new_future; | |||||||||||||||||||||||||||
| 113 | $self->SUPER::configure( on_read => sub { | ||||||||||||||||||||||||||||
| 114 | 2 | ▶ | my ( undef, $buffref, $closed ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | $hs->parse( $$buffref ); # modifies $$buffref | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 2 | ▶ | if( $hs->is_done ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 2 | $self->debug_printf( "HANDSHAKE done" ); | |||||||||||||||||||||||||||
| 120 | 2 | $self->SUPER::configure( on_read => undef ); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 2 | $f->done( $self ); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 2 | return 0; | |||||||||||||||||||||||||||
| 126 | 3 | } ); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 3 | return $f; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head2 connect | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | $self->connect( %params )->get | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | Connect to a WebSocket server. Takes the following named parameters: | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =over 8 | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =item url => STRING | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | URL to provide to WebSocket handshake. This is also used to infer the host and | ||||||||||||||||||||||||||||
| 142 | service name (port number) if not otherwise supplied. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =item req => Protocol::WebSocket::Request | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Optional. If provided, gives the L<Protocol::WebSocket::Request> instance used | ||||||||||||||||||||||||||||
| 147 | for performing the handshake. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =back | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | The returned L<Future> returns the client instance itself, making it useful | ||||||||||||||||||||||||||||
| 152 | in chaining constructors. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head2 connect (void) | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | $self->connect( %params ) | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | When not returning a C<Future>, the following additional parameters provide | ||||||||||||||||||||||||||||
| 159 | continuations: | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =over 8 | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =item on_connected => CODE | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | CODE reference to invoke when the handshaking is complete. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =back | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | sub connect | ||||||||||||||||||||||||||||
| 172 | { | ||||||||||||||||||||||||||||
| 173 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
connect: called CC=10 SCAR=30.3
Pod
documented
| |||||||||||||||||||||||||||||
| 174 | 1 | my %params = @_; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 1 | ▶ | if( my $url = $params{url} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 1 | ▶ | $url = URI->new( $url ) unless blessed $url and $url->isa( "URI" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($url) and $url->isa("URI")
MC/DC0%0/2
builtin::blessed($url) and $url->isa("URI")
builtin::blessed($url)
$url->isa("URI")
Truth table33%1/3
A and B
A= builtin::blessed($url)B= $url->isa("URI")
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 1 | ▶ | $params{host} //= $url->host; | ||||||||||||||||||||||||||
|
Condition33%1/3
$params{"host"} //= $url->host
MC/DC0%0/2
$params{"host"} //= $url->host
$params{"host"}
$url->host
Truth table33%1/3
A //= B
A= $params{"host"}B= $url->host
| |||||||||||||||||||||||||||||
| 180 | 1 | ▶ | $params{service} //= $url->port; | ||||||||||||||||||||||||||
|
Condition33%1/3
$params{"service"} //= $url->port
MC/DC0%0/2
$params{"service"} //= $url->port
$params{"service"}
$url->port
Truth table33%1/3
A //= B
A= $params{"service"}B= $url->port
| |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | ▶ | if( $url->secure ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | require IO::Async::SSL; | |||||||||||||||||||||||||||
| 184 | 0 | push @{ $params{extensions} }, qw( SSL ); | |||||||||||||||||||||||||||
| 185 | 0 | ▶ | $params{SSL_hostname} //= $url->host; | ||||||||||||||||||||||||||
|
Condition0%0/3
$params{"SSL_hostname"} //= $url->host
MC/DC0%0/2
$params{"SSL_hostname"} //= $url->host
$params{"SSL_hostname"}
$url->host
Truth table0%0/3
A //= B
A= $params{"SSL_hostname"}B= $url->host
| |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 1 | my $on_connected = delete $params{on_connected}; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | my $f = $self->SUPER::connect( %params )->then( sub { | ||||||||||||||||||||||||||||
| 192 | 1 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 1 | $self->_do_handshake( %params ); | |||||||||||||||||||||||||||
| 195 | 1 | }); | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 1 | ▶ | $f->on_done( $on_connected ) if $on_connected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 1 | ▶ | return $f if defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | $self->adopt_future( $f ); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head2 connect_handle | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | $client->connect_handle( $handle, %params )->get | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Sets the read and write handles to the IO reference given, then performs the | ||||||||||||||||||||||||||||
| 209 | initial handshake using the parameters given. These are as for C<connect>. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | sub connect_handle | ||||||||||||||||||||||||||||
| 214 | { | ||||||||||||||||||||||||||||
| 215 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
connect_handle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | 2 | my ( $handle, %params ) = @_; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 2 | $self->set_handle( $handle ); | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 2 | $self->_do_handshake( %params ); | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =cut | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 3 | 0x55AA; | |||||||||||||||||||||||||||