Newer Older
268 lines | 6.521kb
copy gitweblite soruce code
root authored on 2012-11-23
1
package Mojo::IOLoop::Client;
2
use Mojo::Base 'Mojo::EventEmitter';
3

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
4
use Errno 'EINPROGRESS';
copy gitweblite soruce code
root authored on 2012-11-23
5
use IO::Socket::INET;
6
use Scalar::Util 'weaken';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
7
use Socket qw(IPPROTO_TCP SO_ERROR TCP_NODELAY);
copy gitweblite soruce code
root authored on 2012-11-23
8

            
9
# IPv6 support requires IO::Socket::IP
10
use constant IPV6 => $ENV{MOJO_NO_IPV6}
11
  ? 0
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
12
  : eval 'use IO::Socket::IP 0.16 (); 1';
copy gitweblite soruce code
root authored on 2012-11-23
13

            
14
# TLS support requires IO::Socket::SSL
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
15
use constant TLS => $ENV{MOJO_NO_TLS} ? 0
16
  : eval(IPV6 ? 'use IO::Socket::SSL 1.75 (); 1'
17
  : 'use IO::Socket::SSL 1.75 "inet4"; 1');
copy gitweblite soruce code
root authored on 2012-11-23
18
use constant TLS_READ  => TLS ? IO::Socket::SSL::SSL_WANT_READ()  : 0;
19
use constant TLS_WRITE => TLS ? IO::Socket::SSL::SSL_WANT_WRITE() : 0;
20

            
21
has reactor => sub {
22
  require Mojo::IOLoop;
23
  Mojo::IOLoop->singleton->reactor;
24
};
25

            
26
sub DESTROY { shift->_cleanup }
27

            
28
sub connect {
29
  my $self = shift;
30
  my $args = ref $_[0] ? $_[0] : {@_};
31
  weaken $self;
32
  $self->{delay} = $self->reactor->timer(0 => sub { $self->_connect($args) });
33
}
34

            
35
sub _cleanup {
36
  my $self = shift;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
37
  return $self unless my $reactor = $self->reactor;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
38
  $self->{$_} && $reactor->remove(delete $self->{$_})
39
    for qw(delay timer handle);
40
  return $self;
copy gitweblite soruce code
root authored on 2012-11-23
41
}
42

            
43
sub _connect {
44
  my ($self, $args) = @_;
45

            
46
  my $handle;
47
  my $reactor = $self->reactor;
48
  my $address = $args->{address} ||= 'localhost';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
49
  unless ($handle = $self->{handle} = $args->{handle}) {
copy gitweblite soruce code
root authored on 2012-11-23
50
    my %options = (
51
      Blocking => 0,
52
      PeerAddr => $address eq 'localhost' ? '127.0.0.1' : $address,
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
53
      PeerPort => $args->{port} || ($args->{tls} ? 443 : 80)
copy gitweblite soruce code
root authored on 2012-11-23
54
    );
55
    $options{LocalAddr} = $args->{local_address} if $args->{local_address};
56
    $options{PeerAddr} =~ s/[\[\]]//g if $options{PeerAddr};
57
    my $class = IPV6 ? 'IO::Socket::IP' : 'IO::Socket::INET';
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
58
    return $self->emit(error => "Couldn't connect: $@")
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
59
      unless $self->{handle} = $handle = $class->new(%options);
copy gitweblite soruce code
root authored on 2012-11-23
60

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
61
    # Timeout
copy gitweblite soruce code
root authored on 2012-11-23
62
    $self->{timer} = $reactor->timer($args->{timeout} || 10,
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
63
      sub { $self->emit(error => 'Connect timeout') });
copy gitweblite soruce code
root authored on 2012-11-23
64
  }
65
  $handle->blocking(0);
66

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
67
  # Wait for handle to become writable
68
  weaken $self;
69
  $reactor->io($handle => sub { $self->_try($args) })->watch($handle, 0, 1);
70
}
71

            
72
sub _tls {
73
  my $self = shift;
74

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
75
  # Connected
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
76
  my $handle = $self->{handle};
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
77
  return $self->_cleanup->emit_safe(connect => $handle)
78
    if $handle->connect_SSL;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
79

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
80
  # Switch between reading and writing
81
  my $err = $IO::Socket::SSL::SSL_ERROR;
82
  if    ($err == TLS_READ)  { $self->reactor->watch($handle, 1, 0) }
83
  elsif ($err == TLS_WRITE) { $self->reactor->watch($handle, 1, 1) }
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
84
}
85

            
86
sub _try {
87
  my ($self, $args) = @_;
88

            
89
  # Retry or handle exceptions
90
  my $handle = $self->{handle};
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
91
  return $! == EINPROGRESS ? undef : $self->emit(error => $!)
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
92
    if IPV6 && !$handle->connect;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
93
  return $self->emit(error => $! = $handle->sockopt(SO_ERROR))
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
94
    if !IPV6 && !$handle->connected;
95

            
copy gitweblite soruce code
root authored on 2012-11-23
96
  # Disable Nagle's algorithm
97
  setsockopt $handle, IPPROTO_TCP, TCP_NODELAY, 1;
98

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
99
  return $self->_cleanup->emit_safe(connect => $handle)
100
    if !$args->{tls} || $handle->isa('IO::Socket::SSL');
101
  return $self->emit(error => 'IO::Socket::SSL 1.75 required for TLS support')
102
    unless TLS;
copy gitweblite soruce code
root authored on 2012-11-23
103

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
104
  # Upgrade
105
  weaken $self;
106
  my %options = (
107
    SSL_ca_file => $args->{tls_ca}
108
      && -T $args->{tls_ca} ? $args->{tls_ca} : undef,
109
    SSL_cert_file       => $args->{tls_cert},
110
    SSL_error_trap      => sub { $self->_cleanup->emit(error => $_[1]) },
111
    SSL_hostname        => $args->{address},
112
    SSL_key_file        => $args->{tls_key},
113
    SSL_startHandshake  => 0,
114
    SSL_verify_mode     => $args->{tls_ca} ? 0x01 : 0x00,
115
    SSL_verifycn_name   => $args->{address},
116
    SSL_verifycn_scheme => $args->{tls_ca} ? 'http' : undef
117
  );
118
  my $reactor = $self->reactor;
119
  $reactor->remove($handle);
120
  return $self->emit(error => 'TLS upgrade failed')
121
    unless $handle = IO::Socket::SSL->start_SSL($handle, %options);
122
  $reactor->io($handle => sub { $self->_tls })->watch($handle, 0, 1);
copy gitweblite soruce code
root authored on 2012-11-23
123
}
124

            
125
1;
126

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
127
=encoding utf8
128

            
copy gitweblite soruce code
root authored on 2012-11-23
129
=head1 NAME
130

            
131
Mojo::IOLoop::Client - Non-blocking TCP client
132

            
133
=head1 SYNOPSIS
134

            
135
  use Mojo::IOLoop::Client;
136

            
137
  # Create socket connection
138
  my $client = Mojo::IOLoop::Client->new;
139
  $client->on(connect => sub {
140
    my ($client, $handle) = @_;
141
    ...
142
  });
143
  $client->on(error => sub {
144
    my ($client, $err) = @_;
145
    ...
146
  });
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
147
  $client->connect(address => 'example.com', port => 80);
copy gitweblite soruce code
root authored on 2012-11-23
148

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
149
  # Start reactor if necessary
150
  $client->reactor->start unless $client->reactor->is_running;
151

            
copy gitweblite soruce code
root authored on 2012-11-23
152
=head1 DESCRIPTION
153

            
154
L<Mojo::IOLoop::Client> opens TCP connections for L<Mojo::IOLoop>.
155

            
156
=head1 EVENTS
157

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
158
L<Mojo::IOLoop::Client> inherits all events from L<Mojo::EventEmitter> and can
159
emit the following new ones.
copy gitweblite soruce code
root authored on 2012-11-23
160

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
161
=head2 connect
copy gitweblite soruce code
root authored on 2012-11-23
162

            
163
  $client->on(connect => sub {
164
    my ($client, $handle) = @_;
165
    ...
166
  });
167

            
168
Emitted safely once the connection is established.
169

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
170
=head2 error
copy gitweblite soruce code
root authored on 2012-11-23
171

            
172
  $client->on(error => sub {
173
    my ($client, $err) = @_;
174
    ...
175
  });
176

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
177
Emitted if an error occurs on the connection, fatal if unhandled.
copy gitweblite soruce code
root authored on 2012-11-23
178

            
179
=head1 ATTRIBUTES
180

            
181
L<Mojo::IOLoop::Client> implements the following attributes.
182

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
183
=head2 reactor
copy gitweblite soruce code
root authored on 2012-11-23
184

            
185
  my $reactor = $client->reactor;
186
  $client     = $client->reactor(Mojo::Reactor::Poll->new);
187

            
188
Low level event reactor, defaults to the C<reactor> attribute value of the
189
global L<Mojo::IOLoop> singleton.
190

            
191
=head1 METHODS
192

            
193
L<Mojo::IOLoop::Client> inherits all methods from L<Mojo::EventEmitter> and
194
implements the following new ones.
195

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
196
=head2 connect
copy gitweblite soruce code
root authored on 2012-11-23
197

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
198
  $client->connect(address => '127.0.0.1', port => 3000);
copy gitweblite soruce code
root authored on 2012-11-23
199

            
200
Open a socket connection to a remote host. Note that TLS support depends on
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
201
L<IO::Socket::SSL> (1.75+) and IPv6 support on L<IO::Socket::IP> (0.16+).
copy gitweblite soruce code
root authored on 2012-11-23
202

            
203
These options are currently available:
204

            
205
=over 2
206

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
207
=item address
copy gitweblite soruce code
root authored on 2012-11-23
208

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
209
  address => 'mojolicio.us'
210

            
copy gitweblite soruce code
root authored on 2012-11-23
211
Address or host name of the peer to connect to, defaults to C<localhost>.
212

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
213
=item handle
copy gitweblite soruce code
root authored on 2012-11-23
214

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
215
  handle => $handle
216

            
copy gitweblite soruce code
root authored on 2012-11-23
217
Use an already prepared handle.
218

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
219
=item local_address
copy gitweblite soruce code
root authored on 2012-11-23
220

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
221
  local_address => '127.0.0.1'
222

            
copy gitweblite soruce code
root authored on 2012-11-23
223
Local address to bind to.
224

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
225
=item port
copy gitweblite soruce code
root authored on 2012-11-23
226

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
227
  port => 80
228

            
229
Port to connect to, defaults to C<80> or C<443> with C<tls> option.
copy gitweblite soruce code
root authored on 2012-11-23
230

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
231
=item timeout
copy gitweblite soruce code
root authored on 2012-11-23
232

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
233
  timeout => 15
234

            
copy gitweblite soruce code
root authored on 2012-11-23
235
Maximum amount of time in seconds establishing connection may take before
236
getting canceled, defaults to C<10>.
237

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
238
=item tls
copy gitweblite soruce code
root authored on 2012-11-23
239

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
240
  tls => 1
241

            
copy gitweblite soruce code
root authored on 2012-11-23
242
Enable TLS.
243

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
244
=item tls_ca
copy gitweblite soruce code
root authored on 2012-11-23
245

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
246
  tls_ca => '/etc/tls/ca.crt'
247

            
copy gitweblite soruce code
root authored on 2012-11-23
248
Path to TLS certificate authority file. Also activates hostname verification.
249

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
250
=item tls_cert
copy gitweblite soruce code
root authored on 2012-11-23
251

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
252
  tls_cert => '/etc/tls/client.crt'
253

            
copy gitweblite soruce code
root authored on 2012-11-23
254
Path to the TLS certificate file.
255

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
256
=item tls_key
copy gitweblite soruce code
root authored on 2012-11-23
257

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
258
  tls_key => '/etc/tls/client.key'
259

            
copy gitweblite soruce code
root authored on 2012-11-23
260
Path to the TLS key file.
261

            
262
=back
263

            
264
=head1 SEE ALSO
265

            
266
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
267

            
268
=cut