2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-16 23:06:05 +00:00

HttpUtils.pm, 98_update.pm: add Connection:keep-alive (Forum #39923)

git-svn-id: https://svn.fhem.de/fhem/trunk@9101 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2015-08-20 17:11:28 +00:00
parent 1c536fb096
commit 2c058b0019
2 changed files with 61 additions and 5 deletions

View File

@ -21,6 +21,7 @@ my $updRet;
my %updDirs; my %updDirs;
my $updArg; my $updArg;
my $mainPgm = "/fhem.pl\$"; my $mainPgm = "/fhem.pl\$";
my %upd_connecthash;
######################################## ########################################
@ -59,6 +60,7 @@ CommandUpdate($$)
} else { } else {
doUpdate($src, $arg); doUpdate($src, $arg);
HttpUtils_Close(\%upd_connecthash);
my $ret = $updRet; $updRet = ""; my $ret = $updRet; $updRet = "";
return $ret; return $ret;
@ -101,6 +103,7 @@ doUpdateInBackground($)
*Log = \&update_Log2Event; *Log = \&update_Log2Event;
sleep(2); # Give time for ActivateInform / FHEMWEB / JavaScript sleep(2); # Give time for ActivateInform / FHEMWEB / JavaScript
doUpdate($h->{src}, $h->{arg}); doUpdate($h->{src}, $h->{arg});
HttpUtils_Close(\%upd_connecthash);
} }
@ -372,7 +375,10 @@ upd_getUrl($)
{ {
my ($url) = @_; my ($url) = @_;
$url =~ s/%/%25/g; $url =~ s/%/%25/g;
my ($err, $data) = HttpUtils_BlockingGet({ url=>$url }); $upd_connecthash{url} = $url;
$upd_connecthash{keepalive} = 1;
# $upd_connecthash{compress} = 1; # fhem.de does not support compression
my ($err, $data) = HttpUtils_BlockingGet(\%upd_connecthash);
if($err) { if($err) {
uLog 1, $err; uLog 1, $err;
return ""; return "";

View File

@ -22,6 +22,8 @@ my %ext2MIMEType= qw{
}; };
my $HU_use_zlib;
sub sub
ext2MIMEType($) { ext2MIMEType($) {
my ($ext)= @_; my ($ext)= @_;
@ -54,6 +56,19 @@ urlDecode($) {
return $_; return $_;
} }
sub
HttpUtils_Close($)
{
my ($hash) = @_;
delete($hash->{FD});
delete($selectlist{$hash});
$hash->{conn}->close();
delete($hash->{conn});
delete($hash->{hu_sslAdded});
delete($hash->{hu_filecount});
delete($hash->{hu_blocking});
}
sub sub
HttpUtils_ConnErr($) HttpUtils_ConnErr($)
{ {
@ -126,6 +141,8 @@ HttpUtils_Connect($)
$hash->{addr} = "$hash->{protocol}://$host:$port"; $hash->{addr} = "$hash->{protocol}://$host:$port";
$hash->{auth} = encode_base64("$user:$pwd","") if($authstring); $hash->{auth} = encode_base64("$user:$pwd","") if($authstring);
return HttpUtils_Connect2($hash) if($hash->{conn} && $hash->{keepalive});
if($hash->{callback}) { # Nonblocking staff if($hash->{callback}) { # Nonblocking staff
$hash->{conn} = IO::Socket::INET->new(Proto=>'tcp', Blocking=>0); $hash->{conn} = IO::Socket::INET->new(Proto=>'tcp', Blocking=>0);
if($hash->{conn}) { if($hash->{conn}) {
@ -171,6 +188,16 @@ HttpUtils_Connect($)
return "$hash->{displayurl}: Can't connect(1) to $hash->{addr}: $@" return "$hash->{displayurl}: Can't connect(1) to $hash->{addr}: $@"
if(!$hash->{conn}); if(!$hash->{conn});
} }
if($hash->{compress}) {
if(!defined($HU_use_zlib)) {
$HU_use_zlib = 1;
eval { require Compress::Zlib; };
$HU_use_zlib = 0 if($@);
}
$hash->{compress} = $HU_use_zlib;
}
return HttpUtils_Connect2($hash); return HttpUtils_Connect2($hash);
} }
@ -179,7 +206,7 @@ HttpUtils_Connect2($)
{ {
my ($hash) = @_; my ($hash) = @_;
if($hash->{protocol} eq "https" && $hash->{conn}) { if($hash->{protocol} eq "https" && $hash->{conn} && !$hash->{hu_sslAdded}) {
eval "use IO::Socket::SSL"; eval "use IO::Socket::SSL";
if($@) { if($@) {
Log3 $hash, $hash->{loglevel}, $@; Log3 $hash, $hash->{loglevel}, $@;
@ -191,6 +218,7 @@ HttpUtils_Connect2($)
Timeout => $hash->{timeout}, Timeout => $hash->{timeout},
SSL_version => $sslVersion SSL_version => $sslVersion
}) || undef $hash->{conn}; }) || undef $hash->{conn};
$hash->{hu_sslAdded} = 1 if($hash->{keepalive});
} }
} }
@ -223,7 +251,11 @@ HttpUtils_Connect2($)
my $httpVersion = $hash->{httpversion} ? $hash->{httpversion} : "1.0"; my $httpVersion = $hash->{httpversion} ? $hash->{httpversion} : "1.0";
my $hdr = "$method $hash->{path} HTTP/$httpVersion\r\n"; my $hdr = "$method $hash->{path} HTTP/$httpVersion\r\n";
$hdr .= "Host: $hash->{host}\r\n"; $hdr .= "Host: $hash->{host}\r\n";
$hdr .= "Connection: Close\r\n" if($httpVersion ne "1.0"); $hdr .= "User-Agent: fhem\r\n";
$hdr .= "Accept-Encoding: gzip,deflate\r\n" if($hash->{compress});
$hdr .= "Connection: keep-alive\r\n" if($hash->{keepalive});
$hdr .= "Connection: Close\r\n"
if($httpVersion ne "1.0" && !$hash->{keepalive});
$hdr .= "Authorization: Basic $hash->{auth}\r\n" if(defined($hash->{auth})); $hdr .= "Authorization: Basic $hash->{auth}\r\n" if(defined($hash->{auth}));
$hdr .= $hash->{header}."\r\n" if(defined($hash->{header})); $hdr .= $hash->{header}."\r\n" if(defined($hash->{header}));
if(defined($data)) { if(defined($data)) {
@ -285,13 +317,29 @@ HttpUtils_ParseAnswer($$)
{ {
my ($hash, $ret) = @_; my ($hash, $ret) = @_;
$hash->{conn}->close(); if(!$hash->{keepalive}) {
undef $hash->{conn}; $hash->{conn}->close();
undef $hash->{conn};
}
if(!$ret) { if(!$ret) {
# Server answer: Keep-Alive: timeout=2, max=200
if($hash->{keepalive} && $hash->{hu_filecount}) {
my $bc = $hash->{hu_blocking};
HttpUtils_Close($hash);
if($bc) {
return HttpUtils_BlockingGet($hash);
} else {
return HttpUtils_NonblockingGet($hash);
}
}
return ("$hash->{displayurl}: empty answer received", ""); return ("$hash->{displayurl}: empty answer received", "");
} }
$hash->{hu_filecount} = 0 if(!$hash->{hu_filecount});
$hash->{hu_filecount}++;
$ret=~ s/(.*?)\r\n\r\n//s; # Not greedy: switch off the header. $ret=~ s/(.*?)\r\n\r\n//s; # Not greedy: switch off the header.
return ("", $ret) if(!defined($1)); return ("", $ret) if(!defined($1));
@ -355,6 +403,7 @@ sub
HttpUtils_NonblockingGet($) HttpUtils_NonblockingGet($)
{ {
my ($hash) = @_; my ($hash) = @_;
$hash->{hu_blocking} = 0;
my ($isFile, $fErr, $fContent) = HttpUtils_File($hash); my ($isFile, $fErr, $fContent) = HttpUtils_File($hash);
return $hash->{callback}($hash, $fErr, $fContent) if($isFile); return $hash->{callback}($hash, $fErr, $fContent) if($isFile);
my $err = HttpUtils_Connect($hash); my $err = HttpUtils_Connect($hash);
@ -368,6 +417,7 @@ sub
HttpUtils_BlockingGet($) HttpUtils_BlockingGet($)
{ {
my ($hash) = @_; my ($hash) = @_;
$hash->{hu_blocking} = 1;
my ($isFile, $fErr, $fContent) = HttpUtils_File($hash); my ($isFile, $fErr, $fContent) = HttpUtils_File($hash);
return ($fErr, $fContent) if($isFile); return ($fErr, $fContent) if($isFile);
my $err = HttpUtils_Connect($hash); my $err = HttpUtils_Connect($hash);