mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-04-08 01:14:19 +00:00
HttpUtils.pm: add try6 hash parameter (Forum #127464)
git-svn-id: https://svn.fhem.de/fhem/trunk@26006 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
6b04c426ee
commit
985b7dfd61
@ -147,10 +147,10 @@ HttpUtils_dumpDnsCache()
|
|||||||
my @ret;
|
my @ret;
|
||||||
my $max = 0;
|
my $max = 0;
|
||||||
map { my $l=length($_); $max=$l if($l>$max) } keys %HU_dnsCache;
|
map { my $l=length($_); $max=$l if($l>$max) } keys %HU_dnsCache;
|
||||||
for my $hn (sort keys %HU_dnsCache) {
|
for my $key (sort keys %HU_dnsCache) {
|
||||||
push @ret, sprintf("%*s TS: %s TTL: %5s ADDR: %s", -$max, $hn,
|
push @ret, sprintf("%*s TS: %s TTL: %5s ADDR: %s", -$max, $key,
|
||||||
FmtDateTime($HU_dnsCache{$hn}{TS}), $HU_dnsCache{$hn}{TTL},
|
FmtDateTime($HU_dnsCache{$key}{TS}), $HU_dnsCache{$key}{TTL},
|
||||||
join(".", unpack("C*", $HU_dnsCache{$hn}{addr})));
|
join(".", unpack("C*", $HU_dnsCache{$key}{addr})));
|
||||||
}
|
}
|
||||||
return join("\n", @ret);
|
return join("\n", @ret);
|
||||||
}
|
}
|
||||||
@ -189,6 +189,14 @@ HttpUtils_dnsParse($$$)
|
|||||||
return "No A record found";
|
return "No A record found";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
sub
|
||||||
|
HttpUtils_wantInet6($)
|
||||||
|
{
|
||||||
|
my ($hash) = @_;
|
||||||
|
return $haveInet6 && (!defined($hash->{try6}) || $hash->{try6} == 1);
|
||||||
|
}
|
||||||
|
|
||||||
# { HttpUtils_gethostbyname({timeout=>4}, "fhem.de", 1,
|
# { HttpUtils_gethostbyname({timeout=>4}, "fhem.de", 1,
|
||||||
# sub(){my($h,$e,$a)=@_;; Log 1, $e ? "ERR:$e": ("IP:".ip2str($a)) }) }
|
# sub(){my($h,$e,$a)=@_;; Log 1, $e ? "ERR:$e": ("IP:".ip2str($a)) }) }
|
||||||
sub
|
sub
|
||||||
@ -205,7 +213,7 @@ HttpUtils_gethostbyname($$$$)
|
|||||||
my $dnsServer = AttrVal("global", "dnsServer", undef);
|
my $dnsServer = AttrVal("global", "dnsServer", undef);
|
||||||
|
|
||||||
if(!$dnsServer) { # use the blocking libc to get the IP
|
if(!$dnsServer) { # use the blocking libc to get the IP
|
||||||
if($haveInet6) {
|
if(HttpUtils_wantInet6($hash)) {
|
||||||
$host = $1 if($host =~ m/^\[([a-f0-9:]+)\]+$/); # remove [] from IPV6
|
$host = $1 if($host =~ m/^\[([a-f0-9:]+)\]+$/); # remove [] from IPV6
|
||||||
my $iaddr = Socket6::inet_pton(AF_INET6, $host); # Try it as IPV6
|
my $iaddr = Socket6::inet_pton(AF_INET6, $host); # Try it as IPV6
|
||||||
return $fn->($hash, undef, $iaddr) if($iaddr);
|
return $fn->($hash, undef, $iaddr) if($iaddr);
|
||||||
@ -243,10 +251,10 @@ HttpUtils_gethostbyname($$$$)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $fn->($hash, undef, $HU_dnsCache{$host}{addr}) # check the cache
|
my $dnsKey = "$host/".($try6 ? "IPv6" : "IPv4");
|
||||||
if($HU_dnsCache{$host} &&
|
return $fn->($hash, undef, $HU_dnsCache{$dnsKey}{addr}) # check the cache
|
||||||
$HU_dnsCache{$host}{TS}+$HU_dnsCache{$host}{TTL} > gettimeofday());
|
if($HU_dnsCache{$dnsKey} &&
|
||||||
|
$HU_dnsCache{$dnsKey}{TS}+$HU_dnsCache{$dnsKey}{TTL}>gettimeofday());
|
||||||
my $dh = AttrVal("global", "dnsHostsFile", "undef");
|
my $dh = AttrVal("global", "dnsHostsFile", "undef");
|
||||||
if($dh) {
|
if($dh) {
|
||||||
my $fh;
|
my $fh;
|
||||||
@ -293,9 +301,10 @@ HttpUtils_gethostbyname($$$$)
|
|||||||
if($err && $dh->{try6});
|
if($err && $dh->{try6});
|
||||||
return $dh->{callback}->($dh->{origHash}, "DNS: $err", undef) if($err);
|
return $dh->{callback}->($dh->{origHash}, "DNS: $err", undef) if($err);
|
||||||
Log 4, "DNS result for $dh->{host}: ".ip2str($addr).", ttl:$ttl";
|
Log 4, "DNS result for $dh->{host}: ".ip2str($addr).", ttl:$ttl";
|
||||||
$HU_dnsCache{$dh->{host}}{TS} = gettimeofday();
|
my $dnsKey = $dh->{host}."/".($dh->{try6} ? "IPv6" : "IPv4");
|
||||||
$HU_dnsCache{$dh->{host}}{TTL} = $ttl;
|
$HU_dnsCache{$dnsKey}{TS} = gettimeofday();
|
||||||
$HU_dnsCache{$dh->{host}}{addr} = $addr;
|
$HU_dnsCache{$dnsKey}{TTL} = $ttl;
|
||||||
|
$HU_dnsCache{$dnsKey}{addr} = $addr;
|
||||||
return $dh->{callback}->($dh->{origHash}, undef, $addr);
|
return $dh->{callback}->($dh->{origHash}, undef, $addr);
|
||||||
}
|
}
|
||||||
$dh{directReadFn} = \&directReadFn;
|
$dh{directReadFn} = \&directReadFn;
|
||||||
@ -398,7 +407,8 @@ HttpUtils_Connect($)
|
|||||||
return HttpUtils_Connect2($hash) if($hash->{conn} && $hash->{keepalive});
|
return HttpUtils_Connect2($hash) if($hash->{conn} && $hash->{keepalive});
|
||||||
|
|
||||||
if($hash->{callback}) { # Nonblocking staff
|
if($hash->{callback}) { # Nonblocking staff
|
||||||
HttpUtils_gethostbyname($hash, $host, $haveInet6, sub($$$) {
|
HttpUtils_gethostbyname($hash, $host, HttpUtils_wantInet6($hash),
|
||||||
|
sub($$$) {
|
||||||
my ($hash, $err, $iaddr) = @_;
|
my ($hash, $err, $iaddr) = @_;
|
||||||
$hash = $hash->{origHash} if($hash->{origHash});
|
$hash = $hash->{origHash} if($hash->{origHash});
|
||||||
if($err) {
|
if($err) {
|
||||||
@ -473,7 +483,7 @@ HttpUtils_Connect($)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$hash->{conn} = $haveInet6 ?
|
$hash->{conn} = HttpUtils_wantInet6($hash) ?
|
||||||
IO::Socket::INET6->new(PeerAddr=>"$host:$port",Timeout=>$hash->{timeout}):
|
IO::Socket::INET6->new(PeerAddr=>"$host:$port",Timeout=>$hash->{timeout}):
|
||||||
IO::Socket::INET ->new(PeerAddr=>"$host:$port",Timeout=>$hash->{timeout});
|
IO::Socket::INET ->new(PeerAddr=>"$host:$port",Timeout=>$hash->{timeout});
|
||||||
|
|
||||||
@ -999,12 +1009,27 @@ HttpUtils_ParseAnswer($)
|
|||||||
|
|
||||||
# Parameters in the hash:
|
# Parameters in the hash:
|
||||||
# mandatory:
|
# mandatory:
|
||||||
# url, callback
|
# url,
|
||||||
# optional(default):
|
# callback
|
||||||
# digest(0),hideurl(0),timeout(4),data(""),loglevel(4),header("" or HASH),
|
# optional(default-value):
|
||||||
# noshutdown(1),shutdown(0),httpversion("1.0"),ignoreredirects(0)
|
# compress(1)
|
||||||
# method($data?"POST":"GET"),keepalive(0),sslargs({}),user(),pwd()
|
# data("") # sending data via POST
|
||||||
# compress(1), incrementalTimeout(0), forceEncoding(undef)
|
# forceEncoding(undef) # Encode received data with this charset
|
||||||
|
# header("" or {})
|
||||||
|
# hideurl(0) # hide the url in the logs
|
||||||
|
# httpversion("1.0")
|
||||||
|
# ignoreredirects(0)
|
||||||
|
# incrementalTimeout(0)
|
||||||
|
# keepalive(0) # leave connection open for repeated queries
|
||||||
|
# loglevel(4)
|
||||||
|
# method($data ? "POST" : "GET")
|
||||||
|
# noshutdown(1) # 0: do not close write channel (see below: shutdown)
|
||||||
|
# pwd() # basic auth password
|
||||||
|
# shutdown(0) # close write channel after sending data / HTTP only
|
||||||
|
# sslargs({})
|
||||||
|
# timeout(4)
|
||||||
|
# try6() # 0: prohibit IPV6, even if useInet6 is set
|
||||||
|
# user() # basic auth uset
|
||||||
# Example:
|
# Example:
|
||||||
# { HttpUtils_NonblockingGet({ url=>"http://fhem.de/MAINTAINER.txt",
|
# { HttpUtils_NonblockingGet({ url=>"http://fhem.de/MAINTAINER.txt",
|
||||||
# callback=>sub($$$){ Log 1,"ERR:$_[1] DATA:".length($_[2]) } }) }
|
# callback=>sub($$$){ Log 1,"ERR:$_[1] DATA:".length($_[2]) } }) }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user