AnyEvent-Whois-Raw
view release on metacpan or search on metacpan
lib/AnyEvent/Whois/Raw.pm view on Meta::CPAN
}
}
sub get_whois {
local $stash = {
caller => \&_get_whois,
params => _extract_known_params(\@_),
args => [@_],
};
&_get_whois;
}
sub _get_whois {
my $cb = pop;
my ($res_text, $res_srv);
eval {
($res_text, $res_srv) = Net::Whois::Raw::get_whois(@_);
};
if (!$@) {
$cb->($res_text, $res_srv);
}
elsif ($@ !~ /^Call me later/) {
$cb->('', $@);
}
}
sub Net::Whois::Raw::whois_query {
my $call = $stash->{calls}{whois_query}++;
if ($call <= $#{$stash->{results}{whois_query}}) {
return $stash->{results}{whois_query}[$call] || die $stash->{errors}{whois_query}[$call], "\n";
}
whois_query_ae(@_);
die "Call me later";
}
sub whois_query_ae {
my ($dom, $srv_and_port, $is_ns) = @_;
my $whoisquery = Net::Whois::Raw::Common::get_real_whois_query($dom, $srv_and_port, $is_ns);
my $stash_ref = $stash;
my ($srv, $port) = split /:/, $srv_and_port;
tcp_connect $srv, $port || 43, sub {
my $fh = shift;
unless ($fh) {
local $stash = $stash_ref;
$stash->{calls}{whois_query} = 0;
my $i = push @{$stash->{results}{whois_query}}, undef;
$stash->{errors}{whois_query}[$i-1] = "Connection to $srv failed: $!";
$stash->{caller}->(@{$stash->{args}});
return;
}
my @lines;
my $handle;
my $timer = AnyEvent->timer(
after => exists $stash_ref->{params}{timeout} ?
$stash_ref->{params}{timeout} :
$Net::Whois::Raw::TIMEOUT||30,
cb => sub {
if ($handle && !$handle->destroyed) {
$handle->destroy();
local $stash = $stash_ref;
$stash->{calls}{whois_query} = 0;
my $i = push @{$stash->{results}{whois_query}}, undef;
$stash->{errors}{whois_query}[$i-1] = "Connection to $srv timed out";
$stash->{caller}->(@{$stash->{args}});
}
}
);
$handle = AnyEvent::Handle->new(
fh => $fh,
on_read => sub {
my @l = split /(?<=\n)/, $_[0]->{rbuf};
if (@lines && substr($lines[-1], -1) ne "\n") {
$lines[-1] .= shift(@l);
}
push @lines, @l;
$_[0]->{rbuf} = '';
},
on_error => sub {
undef $timer;
$handle->destroy();
local $stash = $stash_ref;
$stash->{calls}{whois_query} = 0;
my $i = push @{$stash->{results}{whois_query}}, undef;
$stash->{errors}{whois_query}[$i-1] = "Read error from $srv: $!";
$stash->{caller}->(@{$stash->{args}});
},
on_eof => sub {
undef $timer;
local $stash = $stash_ref;
$handle->destroy();
$stash->{calls}{whois_query} = 0;
push @{$stash->{results}{whois_query}}, \@lines;
$stash->{caller}->(@{$stash->{args}});
}
);
$handle->push_write($whoisquery."\015\012");
}, sub {
my $fh = shift;
local $stash = $stash_ref;
_sock_prepare_cb($fh, $srv);
};
}
sub _sock_prepare_cb {
my ($fh, $srv) = @_;
my $sockname = getsockname($fh);
my $timeout = $Net::Whois::Raw::TIMEOUT||30;
if (exists $stash->{params}{on_prepare}) {
$timeout = $stash->{params}{on_prepare}->($fh);
}
my $rotate_reference = eval { Net::Whois::Raw::get_ips_for_query($srv) };
if (!$rotate_reference && @Net::Whois::Raw::SRC_IPS && $sockname eq getsockname($fh)) {
# we have ip and there was no bind request in on_prepare callback
$rotate_reference = \@Net::Whois::Raw::SRC_IPS;
}
if ($rotate_reference) {
my $ip = shift @$rotate_reference;
bind $fh, AnyEvent::Socket::pack_sockaddr(0, parse_address($ip));
push @$rotate_reference, $ip; # rotate ips
}
return exists $stash->{params}{timeout} ?
$stash->{params}{timeout} :
$timeout;
}
sub Net::Whois::Raw::www_whois_query {
my $call = $stash->{calls}{www_whois_query}++;
if ($call <= $#{$stash->{results}{www_whois_query}}) {
return $stash->{results}{www_whois_query}[$call];
}
www_whois_query_ae(@_);
die "Call me later";
}
sub www_whois_query_ae {
my ($dom) = (lc shift);
my ($resp, $url);
my ($name, $tld) = Net::Whois::Raw::Common::split_domain( $dom );
my @http_query_urls = @{Net::Whois::Raw::Common::get_http_query_url($dom)};
( run in 2.412 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )