Skip to content

Commit

Permalink
improved start_tls method in Mojo::IOLoop by allowing it to accept mo…
Browse files Browse the repository at this point in the history
…re options
  • Loading branch information
kraih committed Oct 21, 2011
1 parent cdbc51b commit d238b56
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.05 2011-10-22 00:00:00
- Improved start_tls method in Mojo::IOLoop by allowing it to accept
more options.
- Improved documentation.
- Fixed HTTPS proxy bug in Mojo::UserAgent.

Expand Down
33 changes: 29 additions & 4 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -50,10 +50,10 @@ sub connect {
weaken $client->{resolver};

# Events
$c->{close} ||= delete $args->{on_close};
$c->{connect} ||= delete $args->{on_connect};
$c->{error} ||= delete $args->{on_error};
$c->{read} ||= delete $args->{on_read};
$c->{close} = delete $args->{on_close} if $args->{on_close};
$c->{connect} = delete $args->{on_connect} if $args->{on_connect};
$c->{error} = delete $args->{on_error} if $args->{on_error};
$c->{read} = delete $args->{on_read} if $args->{on_read};
weaken $self;
$client->on(
connect => sub {
Expand Down Expand Up @@ -606,6 +606,10 @@ following new ones.
address => '127.0.0.1',
port => 3000
);
my $id = $loop->connect({
address => '127.0.0.1',
port => 3000
});
Open a TCP connection to a remote host.
Note that TLS support depends on L<IO::Socket::SSL> and IPv6 support on
Expand Down Expand Up @@ -711,6 +715,7 @@ Check if loop is running.
my $id = Mojo::IOLoop->listen(port => 3000);
my $id = $loop->listen(port => 3000);
my $id = $loop->listen({port => 3000});
Create a new listen socket.
Note that TLS support depends on L<IO::Socket::SSL> and IPv6 support on
Expand Down Expand Up @@ -883,6 +888,10 @@ Start the loop, this will block until C<stop> is called.
tls_cert => '/foo/client.cert',
tls_key => '/foo/client.key'
));
$loop->start_tls($id => {
tls_cert => '/foo/client.cert',
tls_key => '/foo/client.key'
});
Start new TLS connection inside old connection.
Note that TLS support depends on L<IO::Socket::SSL>.
Expand All @@ -891,6 +900,22 @@ These options are currently available:
=over 2
=item C<on_connect>
Callback to be invoked once the connection is established.
=item C<on_close>
Callback to be invoked if the connection gets closed.
=item C<on_error>
Callback to be invoked if an error happens on the connection.
=item C<on_read>
Callback to be invoked if new data arrives on the connection.
=item C<tls_cert>
Path to the TLS certificate file.
Expand Down
5 changes: 3 additions & 2 deletions t/mojo/ioloop.t
Expand Up @@ -123,10 +123,10 @@ isa_ok $handle, 'IO::Socket', 'right reference';

# Dropped listen socket
$port = Mojo::IOLoop->generate_port;
$id = $loop->listen(port => $port);
$id = $loop->listen({port => $port});
$error = undef;
my $connected;
$loop->connect(
my %args = (
address => 'localhost',
port => $port,
on_connect => sub {
Expand All @@ -140,6 +140,7 @@ $loop->connect(
$error = pop;
}
);
$loop->connect(\%args);
$loop->start;
ok $connected, 'connected';
ok !$error, 'no error';
Expand Down
56 changes: 55 additions & 1 deletion t/mojo/ioloop_tls.t
Expand Up @@ -34,7 +34,7 @@ plan skip_all => 'set TEST_TLS to enable this test (developer only!)'
unless $ENV{TEST_TLS};
plan skip_all => 'IO::Socket::SSL 1.37 required for this test!'
unless Mojo::IOLoop::Server::TLS;
plan tests => 20;
plan tests => 28;

use Mojo::IOLoop;

Expand Down Expand Up @@ -111,6 +111,38 @@ ok $running, 'loop was running';
ok !$drop, 'event dropped successfully';
ok !$error, 'no error';

# Delayed TLS handshake with valid client certificate
$server = $client = '';
$server_close = $client_close = 0;
$id = $loop->connect(
address => 'localhost',
port => $port,
on_connect => sub {
my ($loop, $id) = @_;
$loop->start_tls(
$id => {
on_close => sub { $client_close++ },
on_connect => sub {
shift->write(shift, 'tset', sub { shift->write(shift, '123') });
},
on_read => sub { $client .= pop },
tls_cert => 't/mojo/certs/client.crt',
tls_key => 't/mojo/certs/client.key'
}
);
}
);
$loop->connection_timeout($id => '0.5');
$loop->timer(1 => sub { shift->stop });
$loop->start;
is $server, 'tset123', 'right content';
is $client, 'test321', 'right content';
ok $server_close, 'close event has been emitted';
ok $client_close, 'close event has been emitted';
ok $running, 'loop was running';
ok !$drop, 'event dropped successfully';
ok !$error, 'no error';

# Invalid client certificate
$error = '';
$id = $loop->connect(
Expand All @@ -126,6 +158,28 @@ $loop->timer(1 => sub { shift->stop });
$loop->start;
ok $error, 'has error';

# Delayed TLS handshake with invalid client certificate
$error = '';
$id = $loop->connect(
address => 'localhost',
port => $port,
on_connect => sub {
my ($loop, $id) = @_;
$loop->start_tls(
$id => {
tls_cert => 't/mojo/certs/badcert.key',
tls_key => 't/mojo/certs/badcert.crt',
on_error => sub { $error = pop },
}
);
},
on_error => sub { }
);
$loop->connection_timeout($id => '0.5');
$loop->timer(1 => sub { shift->stop });
$loop->start;
ok $error, 'has error';

# Valid client certificate but rejected by callback
$loop = Mojo::IOLoop->new;
$port = Mojo::IOLoop->generate_port;
Expand Down

0 comments on commit d238b56

Please sign in to comment.