Mojo/IOLoop/Client.pm

filter: Statementstmt 67.9% 91 / 134 Branchbran 42.8% 24 / 56 Conditioncond 38.1% 8 / 21 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 83.8% 26 / 31 Podpod 100% 3 / 3 totaltotal 59.6% 154 / 258 CC 39 SCAR 20.2
CC
39
Cov
58%
_socks
20.0
connect
16.0
_connect
10.9
CRAP 7.5
« Mojo/UserAgent.pm ↑ Summary Mojo/Transaction/HTTP.pm »
1 package Mojo::IOLoop::Client;
2 33 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 33 ▶use Errno qw(EINPROGRESS);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Mojo::IOLoop::TLS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶use Socket qw(IPPROTO_TCP SOCK_STREAM TCP_NODELAY);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Non-blocking name resolution requires Net::DNS::Native
13 0 ▶use constant NNR => $ENV{MOJO_NO_NNR} ? 0 : !!eval { require Net::DNS::Native; Net::DNS::Native->VERSION('0.15'); 1 };
Subroutine BEGIN: called CC=2 SCAR=7.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'MOJO_NO_NNR'} ? : 0 33 33
14 33 my $NDN;
15  
16 # SOCKS support requires IO::Socket::Socks
17 33 ▶use constant SOCKS => $ENV{MOJO_NO_SOCKS}
Subroutine BEGIN: called CC=2 SCAR=7.9
18 ? 0
19 0 ▶ : !!eval { require IO::Socket::Socks; IO::Socket::Socks->VERSION('0.64'); 1 };
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'MOJO_NO_SOCKS'} ? : 0 33 33
20 33 ▶use constant READ => SOCKS ? IO::Socket::Socks::SOCKS_WANT_READ() : 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 33 ▶use constant WRITE => SOCKS ? IO::Socket::Socks::SOCKS_WANT_WRITE() : 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶has reactor => sub { Mojo::IOLoop->singleton->reactor }, weak => 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 40 ▶sub DESTROY { shift->_cleanup }
Subroutine DESTROY: called CC=1 SCAR=0.0
26  
27 1 ▶sub can_nnr {NNR}
Subroutine can_nnr: called CC=1 SCAR=0.0
Pod documented
28 1 ▶sub can_socks {SOCKS}
Subroutine can_socks: called CC=1 SCAR=0.0
Pod documented
29  
30 39 ▶sub connect {
Subroutine connect: called CC=8 SCAR=27.7
Pod documented
31 39 ▶ my ($self, $args) = (shift, ref $_[0] ? $_[0] : {@_});
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 12 27 39
32  
33 # Timeout
34 39 weaken $self;
35 39 my $reactor = $self->reactor;
36 0 ▶ $self->{timer} = $reactor->timer($args->{timeout} || 10, sub { $self->emit(error => 'Connect timeout') });
Subroutine __ANON__: not called CC=1 SCAR=0.2
Condition100%2/2
$args->{'timeout'} || 10
l!l
2415
MC/DC100%1/1
$args->{'timeout'} || 10
$args->{'timeout'}
Truth table100%2/2
A
A= $args->{'timeout'}
Aresult
11
00
37  
38 # Blocking name resolution
39 39 ▶ $_ && s/[[\]]//g for @$args{qw(address socks_address)};
Branch: 1 branch
BranchTrueFalseTotal
$_ and s/[[\]]//gu 26 52 78
40 39 ▶ my $address = $args->{socks_address} || ($args->{address} ||= '127.0.0.1');
Condition60%3/5
$args->{'socks_address'} || ($args->{'address'} ||= '127.0.0.1')
l!l&&r!l&&!r
0390
$args->{"address"} ||= "127.0.0.1"
l!l
2613
MC/DC0%0/2
$args->{'socks_address'} || ($args->{'address'} ||= '127.0.0.1')
$args->{'socks_address'} ($args->{'address'} ||= '127.0.0.1')
MC/DC100%1/1
$args->{"address"} ||= "127.0.0.1"
$args->{"address"}
Truth table33%1/3
A || B
A= $args->{'socks_address'}B= ($args->{'address'} ||= '127.0.0.1')
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args->{"address"}
Aresult
11
00
41 39 ▶ return $reactor->next_tick(sub { $self && $self->_connect($args) }) if !NNR || $args->{handle} || $args->{path};
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_connect($args) 39 0 39
42  
43 # Non-blocking name resolution
44 0 ▶ $NDN //= Net::DNS::Native->new(pool => 5, extra_thread => 1);
Condition0%0/3
$NDN //= "Net::DNS::Native"->new("pool", 5, "extra_thread", 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$NDN //= "Net::DNS::Native"->new("pool", 5, "extra_thread", 1)
$NDN "Net::DNS::Native"->new("pool", 5, "extra_thread", 1)
Truth table0%0/3
A //= B
A= $NDNB= "Net::DNS::Native"->new("pool", 5, "extra_thread", 1)
ABresult
1X1
011
000
45 my $handle = $self->{dns}
46 0 = $NDN->getaddrinfo($address, _port($args), {protocol => IPPROTO_TCP, socktype => SOCK_STREAM});
47 $reactor->io(
48 0 ▶ $handle => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
49 0 my $reactor = shift;
50  
51 0 $reactor->remove($self->{dns});
52 0 my ($err, @res) = $NDN->get_result(delete $self->{dns});
53 0 ▶ return $self->emit(error => "Can't resolve: $err") if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
54  
55 0 $args->{addr_info} = \@res;
56 0 $self->_connect($args);
57 }
58 0 )->watch($handle, 1, 0);
59 }
60  
61 79 ▶sub _cleanup {
Subroutine _cleanup: called CC=6 SCAR=19.2
62 79 my $self = shift;
63 79 ▶ $NDN->timedout($self->{dns}) if $NDN && $self->{dns};
Branch: 1 branch
BranchTrueFalseTotal
if $NDN and $self->{"dns"} 0 79 79
Condition33%1/3
$NDN and $self->{"dns"}
!ll&&!rl&&r
7900
MC/DC0%0/2
$NDN and $self->{"dns"}
$NDN $self->{"dns"}
Truth table33%1/3
A and B
A= $NDNB= $self->{"dns"}
ABresult
0X0
100
111
64 79 ▶ return $self unless my $reactor = $self->reactor;
Branch: 1 branch
BranchTrueFalseTotal
unless my $reactor = $self->reactor 0 79 79
65 79 ▶ $self->{$_} && $reactor->remove(delete $self->{$_}) for qw(dns timer handle);
Branch: 1 branch
BranchTrueFalseTotal
$self->{$_} and $reactor->remove(delete $self->{$_}) 78 159 237
66 79 return $self;
67 }
68  
69 39 ▶sub _connect {
Subroutine _connect: called CC=9 SCAR=23.9
70 39 my ($self, $args) = @_;
71  
72 39 my $path = $args->{path};
73 39 my $handle = $self->{handle} = $args->{handle};
74  
75 39 ▶ unless ($handle) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle) 39 0 39
76 39 ▶ my $class = $path ? 'IO::Socket::UNIX' : 'IO::Socket::IP';
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 0 39 39
77 39 my %options = (Blocking => 0);
78  
79 # UNIX domain socket
80 0 ▶ if ($path) { $options{Peer} = $path }
Branch: 1 branch
BranchTrueFalseTotal
if ($path) { } 0 39 39
81  
82 # IP socket
83 else {
84 0 ▶ if (my $info = $args->{addr_info}) { $options{PeerAddrInfo} = $info }
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $args->{'addr_info'}) { } 0 39 39
85 else {
86 39 ▶ $options{PeerAddr} = $args->{socks_address} || $args->{address};
Condition33%1/3
$args->{'socks_address'} || $args->{'address'}
l!l&&r!l&&!r
0390
MC/DC0%0/2
$args->{'socks_address'} || $args->{'address'}
$args->{'socks_address'} $args->{'address'}
Truth table33%1/3
A || B
A= $args->{'socks_address'}B= $args->{'address'}
ABresult
1X1
011
000
87 39 $options{PeerPort} = _port($args);
88 }
89 39 ▶ @options{keys %{$args->{socket_options}}} = values %{$args->{socket_options}} if $args->{socket_options};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"socket_options"} 24 15 39
90 }
91  
92 39 ▶ return $self->emit(error => "Can't connect: $@") unless $self->{handle} = $handle = $class->new(%options);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handle"} = $handle = $class->new(%options) 0 39 39
93 }
94 39 $handle->blocking(0);
95  
96 39 ▶ $path ? $self->_try_socks($args) : $self->_wait('_ready', $handle, $args);
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 0 39 39
97 }
98  
99 39 ▶sub _port { $_[0]{socks_port} || $_[0]{port} || ($_[0]{tls} ? 443 : 80) }
Subroutine _port: called CC=4 SCAR=20.7
Branch: 2 branches
BranchTrueFalseTotal
$_[0]{"socks_port"} or $_[0]{"port"} or $_[0]{"tls"} ? 443 : 80 39 0 39
$_[0]{'tls'} ? : 0 0 0
Condition33%1/3
$_[0]{"socks_port"} or $_[0]{"port"}
l!l&&r!l&&!r
0390
MC/DC0%0/2
$_[0]{"socks_port"} or $_[0]{"port"}
$_[0]{"socks_port"} $_[0]{"port"}
Truth table33%1/3
A or B
A= $_[0]{"socks_port"}B= $_[0]{"port"}
ABresult
1X1
011
000
100  
101 39 ▶sub _ready {
Subroutine _ready: called CC=5 SCAR=21.6
102 39 my ($self, $args) = @_;
103  
104 # Socket changes in between attempts and needs to be re-added for epoll/kqueue
105 39 my $handle = $self->{handle};
106 39 ▶ unless ($handle->connect) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle->connect) 0 39 39
107 0 ▶ return $self->emit(error => $!) unless $! == EINPROGRESS;
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 115 0 0 0
108 0 $self->reactor->remove($handle);
109 0 return $self->_wait('_ready', $handle, $args);
110 }
111  
112 39 ▶ return $self->emit(error => $! || 'Not connected') unless $handle->connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle->connected 0 39 39
Condition0%0/2
$! || 'Not connected'
l!l
00
MC/DC0%0/1
$! || 'Not connected'
$!
Truth table0%0/2
A
A= $!
Aresult
11
00
113  
114 # Disable Nagle's algorithm
115 39 setsockopt $handle, IPPROTO_TCP, TCP_NODELAY, 1;
116  
117 39 $self->_try_socks($args);
118 }
119  
120 0 ▶sub _socks {
Subroutine _socks: not called CC=4 SCAR=30.0
121 0 my ($self, $args) = @_;
122  
123 # Connected
124 0 my $handle = $self->{handle};
125 0 ▶ return $self->_try_tls($args) if $handle->ready;
Branch: 1 branch
BranchTrueFalseTotal
if $handle->ready 0 0 0
126  
127 # Switch between reading and writing
128 0 my $err = $IO::Socket::Socks::SOCKS_ERROR;
129 0 ▶ if ($err == READ) { $self->reactor->watch($handle, 1, 0) }
Branch: 2 branches
BranchTrueFalseTotal
if ($err == 0) { } 0 0 0
elsif ($err == 0) { } 0 0 0
130 0 elsif ($err == WRITE) { $self->reactor->watch($handle, 1, 1) }
131 0 else { $self->emit(error => $err) }
132 }
133  
134 39 ▶sub _try_socks {
Subroutine _try_socks: called CC=4 SCAR=23.7
135 39 my ($self, $args) = @_;
136  
137 39 my $handle = $self->{handle};
138 39 ▶ return $self->_try_tls($args) unless $args->{socks_address};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"socks_address"} 39 0 39
139 0 return $self->emit(error => 'IO::Socket::Socks 0.64+ required for SOCKS support') unless SOCKS;
140  
141 0 my %options = (ConnectAddr => $args->{address}, ConnectPort => $args->{port});
142 0 ▶ @options{qw(AuthType Username Password)} = ('userpass', @$args{qw(socks_user socks_pass)}) if $args->{socks_user};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"socks_user"} 0 0 0
143 0 my $reactor = $self->reactor;
144 0 $reactor->remove($handle);
145 0 ▶ return $self->emit(error => 'SOCKS upgrade failed') unless IO::Socket::Socks->start_SOCKS($handle, %options);
Branch: 1 branch
BranchTrueFalseTotal
unless "IO::Socket::Socks"->start_SOCKS($handle, %options) 0 0 0
146  
147 0 $self->_wait('_socks', $handle, $args);
148 }
149  
150 39 ▶sub _try_tls {
Subroutine _try_tls: called CC=2 SCAR=12.4
151 39 my ($self, $args) = @_;
152  
153 39 my $handle = $self->{handle};
154 39 ▶ return $self->_cleanup->emit(connect => $handle) unless $args->{tls};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"tls"} 39 0 39
155 0 my $reactor = $self->reactor;
156 0 $reactor->remove($handle);
157  
158 # Start TLS handshake
159 0 weaken $self;
160 0 my $tls = Mojo::IOLoop::TLS->new($handle)->reactor($self->reactor);
161 0 ▶ $tls->on(upgrade => sub { $self->_cleanup->emit(connect => pop) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
162 0 ▶ $tls->on(error => sub { $self->emit(error => pop) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 $tls->negotiate(%$args);
164 }
165  
166 39 ▶sub _wait {
Subroutine _wait: called CC=1 SCAR=0.0
167 39 my ($self, $next, $handle, $args) = @_;
168 39 weaken $self;
169 39 ▶ $self->reactor->io($handle => sub { $self->$next($args) })->watch($handle, 0, 1);
Subroutine __ANON__: called CC=1 SCAR=0.0
170 }
171  
172 33 1;
173  
174 =encoding utf8
175  
176 =head1 NAME
177  
178 Mojo::IOLoop::Client - Non-blocking TCP/IP and UNIX domain socket client
179  
180 =head1 SYNOPSIS
181  
182 use Mojo::IOLoop::Client;
183  
184 # Create socket connection
185 my $client = Mojo::IOLoop::Client->new;
186 $client->on(connect => sub ($client, $handle) {...});
187 $client->on(error => sub ($client, $err) {...});
188 $client->connect(address => 'example.com', port => 80);
189  
190 # Start reactor if necessary
191 $client->reactor->start unless $client->reactor->is_running;
192  
193 =head1 DESCRIPTION
194  
195 L<Mojo::IOLoop::Client> opens TCP/IP and UNIX domain socket connections for L<Mojo::IOLoop>.
196  
197 =head1 EVENTS
198  
199 L<Mojo::IOLoop::Client> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones.
200  
201 =head2 connect
202  
203 $client->on(connect => sub ($client, $handle) {...});
204  
205 Emitted once the connection is established.
206  
207 =head2 error
208  
209 $client->on(error => sub ($client, $err) {...});
210  
211 Emitted if an error occurs on the connection, fatal if unhandled.
212  
213 =head1 ATTRIBUTES
214  
215 L<Mojo::IOLoop::Client> implements the following attributes.
216  
217 =head2 reactor
218  
219 my $reactor = $client->reactor;
220 $client = $client->reactor(Mojo::Reactor::Poll->new);
221  
222 Low-level event reactor, defaults to the C<reactor> attribute value of the global L<Mojo::IOLoop> singleton. Note that
223 this attribute is weakened.
224  
225 =head1 METHODS
226  
227 L<Mojo::IOLoop::Client> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones.
228  
229 =head2 can_nnr
230  
231 my $bool = Mojo::IOLoop::Client->can_nnr;
232  
233 True if L<Net::DNS::Native> 0.15+ is installed and non-blocking name resolution support enabled.
234  
235 =head2 can_socks
236  
237 my $bool = Mojo::IOLoop::Client->can_socks;
238  
239 True if L<IO::Socket::SOCKS> 0.64+ is installed and SOCKS5 support enabled.
240  
241 =head2 connect
242  
243 $client->connect(address => '127.0.0.1', port => 3000);
244 $client->connect({address => '127.0.0.1', port => 3000});
245  
246 Open a socket connection to a remote host. Note that non-blocking name resolution depends on L<Net::DNS::Native>
247 (0.15+), SOCKS5 support on L<IO::Socket::Socks> (0.64), and TLS support on L<IO::Socket::SSL> (2.009+).
248  
249 These options are currently available:
250  
251 =over 2
252  
253 =item address
254  
255 address => 'mojolicious.org'
256  
257 Address or host name of the peer to connect to, defaults to C<127.0.0.1>.
258  
259 =item handle
260  
261 handle => $handle
262  
263 Use an already prepared L<IO::Socket::IP> object.
264  
265 =item path
266  
267 path => '/tmp/myapp.sock'
268  
269 Path of UNIX domain socket to connect to.
270  
271 =item port
272  
273 port => 80
274  
275 Port to connect to, defaults to C<80> or C<443> with C<tls> option.
276  
277 =item socket_options
278  
279 socket_options => {LocalAddr => '127.0.0.1'}
280  
281 Additional options for L<IO::Socket::IP> when opening new connections.
282  
283 =item socks_address
284  
285 socks_address => '127.0.0.1'
286  
287 Address or host name of SOCKS5 proxy server to use for connection.
288  
289 =item socks_pass
290  
291 socks_pass => 'secr3t'
292  
293 Password to use for SOCKS5 authentication.
294  
295 =item socks_port
296  
297 socks_port => 9050
298  
299 Port of SOCKS5 proxy server to use for connection.
300  
301 =item socks_user
302  
303 socks_user => 'sri'
304  
305 Username to use for SOCKS5 authentication.
306  
307 =item timeout
308  
309 timeout => 15
310  
311 Maximum amount of time in seconds establishing connection may take before getting canceled, defaults to C<10>.
312  
313 =item tls
314  
315 tls => 1
316  
317 Enable TLS.
318  
319 =item tls_ca
320  
321 tls_ca => '/etc/tls/ca.crt'
322  
323 Path to TLS certificate authority file.
324  
325 =item tls_cert
326  
327 tls_cert => '/etc/tls/client.crt'
328  
329 Path to the TLS certificate file.
330  
331 =item tls_key
332  
333 tls_key => '/etc/tls/client.key'
334  
335 Path to the TLS key file.
336  
337 =item tls_options
338  
339 tls_options => {SSL_alpn_protocols => ['foo', 'bar'], SSL_verify_mode => 0x00}
340  
341 Additional options for L<IO::Socket::SSL>.
342  
343 =back
344  
345 =head1 SEE ALSO
346  
347 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
348  
349 =cut
« Mojo/UserAgent.pm ↑ Summary Mojo/Transaction/HTTP.pm »