AnyEvent-Connector
view release on metacpan or search on metacpan
lib/AnyEvent/Connector.pm view on Meta::CPAN
if(defined($p)) {
$self->_set_proxy($p);
return;
}
}
}
sub _env_no_proxy {
my ($self) = @_;
foreach my $key (qw(no_proxy NO_PROXY)) {
my $no_proxy = $ENV{$key};
if(defined($no_proxy)) {
$self->_set_no_proxy([split /\s*,\s*/, $no_proxy]);
return;
}
}
}
sub _proxy_uri_for {
my ($self, $host, $port) = @_;
foreach my $no_domain (@{$self->{no_proxy}}) {
if($host =~ /\Q$no_domain\E$/) {
return undef;
}
}
return $self->{proxy_obj};
}
sub proxy_for {
my ($self, $host, $port) = @_;
my $p = $self->_proxy_uri_for($host, $port);
return defined($p) ? $p->uri_string : undef;
}
sub tcp_connect {
my ($self, $host, $port, $connect_cb, $prepare_cb) = @_;
my $proxy = $self->_proxy_uri_for($host, $port);
if(!defined($proxy)) {
return AnyEvent::Socket::tcp_connect $host, $port, $connect_cb, $prepare_cb;
}
return AnyEvent::Socket::tcp_connect $proxy->host, $proxy->port, sub {
my ($fh, $conn_host, $conn_port, $retry) = @_;
if(!defined($fh)) {
$connect_cb->();
return;
}
$proxy->establish_proxy($fh, $host, $port, sub {
my ($success) = @_;
$connect_cb->($success ? ($fh, $conn_host, $conn_port, $retry) : ());
});
}, $prepare_cb;
}
1;
__END__
=pod
=head1 NAME
AnyEvent::Connector - tcp_connect with transparent proxy handling
=head1 SYNOPSIS
use AnyEvent::Connector;
## Specify the proxy setting explicitly.
my $c = AnyEvent::Connector->new(
proxy => 'http://proxy.example.com:8080',
no_proxy => ['localhost', 'your-internal-domain.net']
);
## Proxy setting from "http_proxy" and "no_proxy" environment variables.
my $cenv = AnyEvent::Connector->new(
env_proxy => "http",
);
## Same API as AnyEvent::Socket::tcp_connect
my $guard = $c->tcp_connect(
"target.hogehoge.org", 80,
sub {
## connect callback
my ($fh ,$host, $port, $retry) = @_;
...;
},
sub {
## prepare calback
my ($fh) = @_;
...;
}
);
=head1 DESCRIPTION
L<AnyEvent::Connector> object has C<tcp_connect> method compatible
with that from L<AnyEvent::Socket>, and it handles proxy settings
transparently.
=head1 CLASS METHODS
=head2 $conn = AnyEvent::Connector->new(%args)
The constructor.
Fields in C<%args> are:
=over
=item C<proxy> => STR (optional)
String of proxy URL. Currently only C<http> proxy is supported.
If both C<proxy> and C<env_proxy> are not specified, the C<$conn> will directly connect to the destination host.
If both C<proxy> and C<env_proxy> are specified, setting by C<proxy> is used.
Setting empty string to C<proxy> disables the proxy setting done by C<env_proxy> option.
=item C<no_proxy> => STR or ARRAYREF of STR (optional)
String or array-ref of strings of domain names, to which the C<$conn> will directly connect.
If both C<no_proxy> and C<env_proxy> are specified, setting by C<no_proxy> is used.
Setting empty string or empty array-ref to C<no_proxy> disables the no_proxy setting done by C<env_proxy> option.
=item C<env_proxy> => STR (optional)
String of protocol specifier. If specified, proxy settings for that
protocol are loaded from environment variables, and C<$conn> is
created.
For example, if C<"http"> is specified, C<http_proxy> (or
C<HTTP_PROXY>) and C<no_proxy> (or C<NO_PROXY>) environment variables
are used to set C<proxy> and C<no_proxy> options, respectively.
C<proxy> and C<no_proxy> options have precedence over C<env_proxy>
option.
=back
=head1 OBJECT METHOD
=head2 $guard = $conn->tcp_connect($host, $port, $connect_cb, $prepare_cb)
Make a (possibly proxied) TCP connection to the given C<$host> and
C<$port>.
If C<< $conn->proxy_for($host, $port) >> returns C<undef>, the
behavior of this method is exactly the same as C<tcp_connect> function
from L<AnyEvent::Socket>.
If C<< $conn->proxy_for($host, $port) >> returns a proxy URL, it
behaves in the following way.
=over
( run in 0.541 second using v1.01-cache-2.11-cpan-39bf76dae61 )