Permalink
Browse files

use class methods instead of constants

  • Loading branch information...
kraih committed Jan 15, 2017
1 parent 4bcbc03 commit e0d5bff75175d0fbf71bdda7505dbf490fa58760
View
@@ -1,7 +1,7 @@
7.19 2017-01-14
- Added module Mojo::IOLoop::TLS.
- Added HAS_NNR and HAS_SOCKS constants to Mojo::IOLoop::Client.
- Added can_nnr and can_socks methods to Mojo::IOLoop::Client.
7.18 2017-01-11
- Fixed support for relative %INC paths in Mojo::Home.
View
@@ -2,32 +2,32 @@ package Mojo::IOLoop::Client;
use Mojo::Base 'Mojo::EventEmitter';
use Errno 'EINPROGRESS';
use Exporter 'import';
use IO::Socket::IP;
use Mojo::IOLoop;
use Mojo::IOLoop::TLS;
use Scalar::Util 'weaken';
use Socket qw(IPPROTO_TCP SOCK_STREAM TCP_NODELAY);
# Non-blocking name resolution requires Net::DNS::Native
use constant HAS_NNR => $ENV{MOJO_NO_NNR}
use constant NNR => $ENV{MOJO_NO_NNR}
? 0
: eval 'use Net::DNS::Native 0.15 (); 1';
my $NDN = HAS_NNR ? Net::DNS::Native->new(pool => 5, extra_thread => 1) : undef;
my $NDN = NNR ? Net::DNS::Native->new(pool => 5, extra_thread => 1) : undef;
# SOCKS support requires IO::Socket::Socks
use constant HAS_SOCKS => $ENV{MOJO_NO_SOCKS}
use constant SOCKS => $ENV{MOJO_NO_SOCKS}
? 0
: eval 'use IO::Socket::Socks 0.64 (); 1';
use constant READ => HAS_SOCKS ? IO::Socket::Socks::SOCKS_WANT_READ() : 0;
use constant WRITE => HAS_SOCKS ? IO::Socket::Socks::SOCKS_WANT_WRITE() : 0;
use constant READ => SOCKS ? IO::Socket::Socks::SOCKS_WANT_READ() : 0;
use constant WRITE => SOCKS ? IO::Socket::Socks::SOCKS_WANT_WRITE() : 0;
has reactor => sub { Mojo::IOLoop->singleton->reactor };
our @EXPORT_OK = qw(HAS_NNR HAS_SOCKS);
sub DESTROY { shift->_cleanup }
sub can_nnr {NNR}
sub can_socks {SOCKS}
sub connect {
my ($self, $args) = (shift, ref $_[0] ? $_[0] : {@_});
@@ -41,7 +41,7 @@ sub connect {
$_ && s/[[\]]//g for @$args{qw(address socks_address)};
my $address = $args->{socks_address} || ($args->{address} ||= '127.0.0.1');
return $reactor->next_tick(sub { $self && $self->_connect($args) })
if !HAS_NNR || $args->{handle};
if !NNR || $args->{handle};
# Non-blocking name resolution
my $handle = $self->{dns} = $NDN->getaddrinfo($address, _port($args),
@@ -128,7 +128,7 @@ sub _try_socks {
return $self->_try_tls($args) unless $args->{socks_address};
return $self->emit(
error => 'IO::Socket::Socks 0.64+ required for SOCKS support')
unless HAS_SOCKS;
unless SOCKS;
my %options = (ConnectAddr => $args->{address}, ConnectPort => $args->{port});
@options{qw(AuthType Username Password)}
@@ -235,6 +235,19 @@ global L<Mojo::IOLoop> singleton.
L<Mojo::IOLoop::Client> inherits all methods from L<Mojo::EventEmitter> and
implements the following new ones.
=head2 can_nnr
my $bool = Mojo::IOLoop::Client->can_nnr;
True if L<Net::DNS::Native> 0.15+ is installed and non-blocking name resolution
support enabled.
=head2 can_socks
my $bool = Mojo::IOLoop::Client->can_socks;
True if L<IO::Socket::SOCKS> 0.64+ is installed and SOCKS5 support enabled.
=head2 connect
$client->connect(address => '127.0.0.1', port => 3000);
@@ -329,20 +342,6 @@ Path to the TLS key file.
=back
=head1 CONSTANTS
L<Mojo::IOLoop::Client> implements the following constants, which can be
imported individually.
=head2 HAS_NNR
True if L<Net::DNS::Native> 0.15+ is installed and non-blocking name resolution
support enabled.
=head2 HAS_SOCKS
True if L<IO::Socket::SOCKS> 0.64+ is installed and SOCKS5 support enabled.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
@@ -4,7 +4,7 @@ use Mojo::Base 'Mojo::EventEmitter';
use Carp 'croak';
use IO::Socket::IP;
use Mojo::IOLoop;
use Mojo::IOLoop::TLS 'HAS_TLS';
use Mojo::IOLoop::TLS;
use Scalar::Util 'weaken';
use Socket qw(IPPROTO_TCP TCP_NODELAY);
@@ -65,7 +65,7 @@ sub listen {
@$self{qw(args handle)} = ($args, $handle);
croak 'IO::Socket::SSL 1.94+ required for TLS support'
if !HAS_TLS && $args->{tls};
if !Mojo::IOLoop::TLS->can_tls && $args->{tls};
}
sub port { shift->{handle}->sockport }
@@ -166,7 +166,7 @@ implements the following new ones.
=head2 generate_port
my $port = $server->generate_port;
my $port = Mojo::IOLoop::Server->generate_port;
Find a free TCP port, primarily used for tests.
View
@@ -1,33 +1,33 @@
package Mojo::IOLoop::TLS;
use Mojo::Base 'Mojo::EventEmitter';
use Exporter 'import';
use Mojo::File 'path';
use Mojo::IOLoop;
use Scalar::Util 'weaken';
# TLS support requires IO::Socket::SSL
use constant HAS_TLS => $ENV{MOJO_NO_TLS}
use constant TLS => $ENV{MOJO_NO_TLS}
? 0
: eval 'use IO::Socket::SSL 1.94 (); 1';
use constant READ => HAS_TLS ? IO::Socket::SSL::SSL_WANT_READ() : 0;
use constant WRITE => HAS_TLS ? IO::Socket::SSL::SSL_WANT_WRITE() : 0;
use constant READ => TLS ? IO::Socket::SSL::SSL_WANT_READ() : 0;
use constant WRITE => TLS ? IO::Socket::SSL::SSL_WANT_WRITE() : 0;
has reactor => sub { Mojo::IOLoop->singleton->reactor };
our @EXPORT_OK = ('HAS_TLS');
# To regenerate the certificate run this command (18.04.2012)
# openssl req -new -x509 -keyout server.key -out server.crt -nodes -days 7300
my $CERT = path(__FILE__)->dirname->child('resources', 'server.crt')->to_string;
my $KEY = path(__FILE__)->dirname->child('resources', 'server.key')->to_string;
sub DESTROY { shift->_cleanup }
sub can_tls {TLS}
sub negotiate {
my ($self, $args) = (shift, ref $_[0] ? $_[0] : {@_});
return $self->emit(error => 'IO::Socket::SSL 1.94+ required for TLS support')
unless HAS_TLS;
unless TLS;
my $handle = $self->{handle};
return $self->emit(error => $IO::Socket::SSL::SSL_ERROR)
@@ -162,6 +162,12 @@ global L<Mojo::IOLoop> singleton.
L<Mojo::IOLoop::TLS> inherits all methods from L<Mojo::EventEmitter> and
implements the following new ones.
=head2 can_tls
my $bool = Mojo::IOLoop::TLS->can_tls;
True if L<IO::Socket::SSL> 1.94+ is installed and TLS support enabled.
=head2 negotiate
$tls->negotiate(server => 1, tls_version => 'TLSv1_2');
@@ -229,15 +235,6 @@ TLS protocol version.
Construct a new L<Mojo::IOLoop::Stream> object.
=head1 CONSTANTS
L<Mojo::IOLoop::TLS> implements the following constants, which can be
imported individually.
=head2 HAS_TLS
True if L<IO::Socket::SSL> 1.94+ is installed and TLS support enabled.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
@@ -1,8 +1,8 @@
package Mojolicious::Command::version;
use Mojo::Base 'Mojolicious::Command';
use Mojo::IOLoop::Client qw(HAS_NNR HAS_SOCKS);
use Mojo::IOLoop::TLS 'HAS_TLS';
use Mojo::IOLoop::Client;
use Mojo::IOLoop::TLS;
use Mojolicious;
has description => 'Show versions of available modules';
@@ -12,9 +12,10 @@ sub run {
my $self = shift;
my $ev = eval 'use Mojo::Reactor::EV; 1' ? $EV::VERSION : 'n/a';
my $socks = HAS_SOCKS ? $IO::Socket::Socks::VERSION : 'n/a';
my $tls = HAS_TLS ? $IO::Socket::SSL::VERSION : 'n/a';
my $ndn = HAS_NNR ? $Net::DNS::Native::VERSION : 'n/a';
my $socks
= Mojo::IOLoop::Client->can_socks ? $IO::Socket::Socks::VERSION : 'n/a';
my $tls = Mojo::IOLoop::TLS->can_tls ? $IO::Socket::SSL::VERSION : 'n/a';
my $nnr = Mojo::IOLoop::Client->can_nnr ? $Net::DNS::Native::VERSION : 'n/a';
print <<EOF;
CORE
@@ -25,7 +26,7 @@ OPTIONAL
EV 4.0+ ($ev)
IO::Socket::Socks 0.64+ ($socks)
IO::Socket::SSL 1.94+ ($tls)
Net::DNS::Native 0.15+ ($ndn)
Net::DNS::Native 0.15+ ($nnr)
EOF
View
@@ -1,6 +1,9 @@
use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
BEGIN {
$ENV{MOJO_NO_SOCKS} = $ENV{MOJO_NO_TLS} = 1;
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}
use Test::More;
use FindBin;
@@ -323,6 +326,13 @@ ok !$tx->keep_alive, 'will not be kept alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'Whatever!', 'right content';
# No TLS support
eval {
Mojo::Server::Daemon->new(listen => ['https://127.0.0.1'], silent => 1)
->start;
};
like $@, qr/IO::Socket::SSL/, 'right error';
# Abstract methods
eval { Mojo::Server->run };
like $@, qr/Method "run" not implemented by subclass/, 'right error';
View
@@ -3,13 +3,14 @@ use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::More;
use Mojo::IOLoop::TLS 'HAS_TLS';
use Mojo::IOLoop::TLS;
plan skip_all => 'set TEST_IPV6 to enable this test (developer only!)'
unless $ENV{TEST_IPV6};
plan skip_all => 'set TEST_TLS to enable this test (developer only!)'
unless $ENV{TEST_TLS};
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!' unless HAS_TLS;
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!'
unless Mojo::IOLoop::TLS->can_tls;
# To regenerate all required certificates run these commands (07.01.2016)
# openssl genrsa -out domain.key 1024
View
@@ -3,11 +3,12 @@ use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::More;
use Mojo::IOLoop::TLS 'HAS_TLS';
use Mojo::IOLoop::TLS;
plan skip_all => 'set TEST_TLS to enable this test (developer only!)'
unless $ENV{TEST_TLS};
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!' unless HAS_TLS;
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!'
unless Mojo::IOLoop::TLS->can_tls;
# To regenerate all required certificates run these commands (12.12.2014)
# openssl genrsa -out ca.key 1024
View
@@ -3,11 +3,12 @@ use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::More;
use Mojo::IOLoop::TLS 'HAS_TLS';
use Mojo::IOLoop::TLS;
plan skip_all => 'set TEST_TLS to enable this test (developer only!)'
unless $ENV{TEST_TLS};
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!' unless HAS_TLS;
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!'
unless Mojo::IOLoop::TLS->can_tls;
use Mojo::IOLoop;
use Socket;
@@ -6,11 +6,12 @@ BEGIN {
}
use Test::More;
use Mojo::IOLoop::TLS 'HAS_TLS';
use Mojo::IOLoop::TLS;
plan skip_all => 'set TEST_ONLINE to enable this test (developer only!)'
unless $ENV{TEST_ONLINE};
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!' unless HAS_TLS;
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!'
unless Mojo::IOLoop::TLS->can_tls;
use IO::Socket::INET;
use Mojo::IOLoop;
@@ -3,14 +3,15 @@ use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::More;
use Mojo::IOLoop::Client 'HAS_SOCKS';
use Mojo::IOLoop::TLS 'HAS_TLS';
use Mojo::IOLoop::Client;
use Mojo::IOLoop::TLS;
plan skip_all => 'set TEST_SOCKS to enable this test (developer only!)'
unless $ENV{TEST_SOCKS};
plan skip_all => 'IO::Socket::Socks 0.64+ required for this test!'
unless HAS_SOCKS;
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!' unless HAS_TLS;
unless Mojo::IOLoop::Client->can_socks;
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!'
unless Mojo::IOLoop::TLS->can_tls;
use Mojo::IOLoop;
use Mojo::IOLoop::Server;
View
@@ -3,11 +3,12 @@ use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::More;
use Mojo::IOLoop::TLS 'HAS_TLS';
use Mojo::IOLoop::TLS;
plan skip_all => 'set TEST_TLS to enable this test (developer only!)'
unless $ENV{TEST_TLS};
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!' unless HAS_TLS;
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!'
unless Mojo::IOLoop::TLS->can_tls;
use Mojo::IOLoop;
use Mojo::Server::Daemon;
@@ -3,11 +3,12 @@ use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::More;
use Mojo::IOLoop::TLS 'HAS_TLS';
use Mojo::IOLoop::TLS;
plan skip_all => 'set TEST_TLS to enable this test (developer only!)'
unless $ENV{TEST_TLS};
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!' unless HAS_TLS;
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!'
unless Mojo::IOLoop::TLS->can_tls;
use Mojo::IOLoop;
use Mojo::Server::Daemon;
@@ -3,11 +3,12 @@ use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::More;
use Mojo::IOLoop::TLS 'HAS_TLS';
use Mojo::IOLoop::TLS;
plan skip_all => 'set TEST_TLS to enable this test (developer only!)'
unless $ENV{TEST_TLS};
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!' unless HAS_TLS;
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!'
unless Mojo::IOLoop::TLS->can_tls;
use Mojo::IOLoop;
use Mojo::UserAgent;

0 comments on commit e0d5bff

Please sign in to comment.