mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-04-23 20:52:13 +00:00
git-svn-id: https://svn.fhem.de/fhem/trunk@12734 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
5f7e4ea62f
commit
57c15fbb07
@ -405,7 +405,6 @@ HttpUtils_Connect2($)
|
|||||||
if($hash->{callback}) { # Nonblocking read
|
if($hash->{callback}) { # Nonblocking read
|
||||||
$hash->{FD} = $hash->{conn}->fileno();
|
$hash->{FD} = $hash->{conn}->fileno();
|
||||||
$hash->{buf} = "";
|
$hash->{buf} = "";
|
||||||
delete($hash->{httpdatalen});
|
|
||||||
$hash->{NAME} = "" if(!defined($hash->{NAME}));
|
$hash->{NAME} = "" if(!defined($hash->{NAME}));
|
||||||
my %timerHash = (hash=>$hash, checkSTS=>$selectTimestamp, msg=>"write to");
|
my %timerHash = (hash=>$hash, checkSTS=>$selectTimestamp, msg=>"write to");
|
||||||
$hash->{directReadFn} = sub() {
|
$hash->{directReadFn} = sub() {
|
||||||
@ -413,12 +412,12 @@ HttpUtils_Connect2($)
|
|||||||
my $len = sysread($hash->{conn},$buf,65536);
|
my $len = sysread($hash->{conn},$buf,65536);
|
||||||
$hash->{buf} .= $buf if(defined($len) && $len > 0);
|
$hash->{buf} .= $buf if(defined($len) && $len > 0);
|
||||||
if(!defined($len) || $len <= 0 ||
|
if(!defined($len) || $len <= 0 ||
|
||||||
HttpUtils_DataComplete($hash)) {
|
HttpUtils_DataComplete($hash->{buf})) {
|
||||||
delete($hash->{FD});
|
delete($hash->{FD});
|
||||||
delete($hash->{directReadFn});
|
delete($hash->{directReadFn});
|
||||||
delete($selectlist{$hash});
|
delete($selectlist{$hash});
|
||||||
RemoveInternalTimer(\%timerHash);
|
RemoveInternalTimer(\%timerHash);
|
||||||
my ($err, $ret, $redirect) = HttpUtils_ParseAnswer($hash);
|
my ($err, $ret, $redirect) = HttpUtils_ParseAnswer($hash, $hash->{buf});
|
||||||
$hash->{callback}($hash, $err, $ret) if(!$redirect);
|
$hash->{callback}($hash, $err, $ret) if(!$redirect);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -460,48 +459,13 @@ HttpUtils_Connect2($)
|
|||||||
sub
|
sub
|
||||||
HttpUtils_DataComplete($)
|
HttpUtils_DataComplete($)
|
||||||
{
|
{
|
||||||
my ($hash) = @_;
|
my ($ret) = @_;
|
||||||
my ($hdr, $data) = ($1, $2);
|
return 0 if($ret !~ m/^(.*?)\r?\n\r?\n(.*)$/s);
|
||||||
my $hl = $hash->{httpdatalen};
|
my $hdr = $1;
|
||||||
if(!defined($hl)) {
|
my $data = $2;
|
||||||
return 0 if($hash->{buf} !~ m/^(.*?)\r?\n\r?\n(.*)$/s);
|
return 0 if($hdr !~ m/Content-Length:\s*(\d+)/si);
|
||||||
my ($hdr, $data) = ($1, $2);
|
return 0 if(length($data) < $1);
|
||||||
if($hdr =~ m/Transfer-Encoding:\s*chunked/si) {
|
return 1;
|
||||||
$hash->{httpheader} = $hdr;
|
|
||||||
$hash->{httpdata} = "";
|
|
||||||
$hash->{buf} = $data;
|
|
||||||
$hash->{httpdatalen} = -1;
|
|
||||||
|
|
||||||
} elsif($hdr =~ m/Content-Length:\s*(\d+)/si) {
|
|
||||||
$hash->{httpdatalen} = $1;
|
|
||||||
$hash->{httpheader} = $hdr;
|
|
||||||
$hash->{httpdata} = $data;
|
|
||||||
$hash->{buf} = "";
|
|
||||||
|
|
||||||
} else {
|
|
||||||
$hash->{httpdatalen} = -2;
|
|
||||||
|
|
||||||
}
|
|
||||||
$hl = $hash->{httpdatalen};
|
|
||||||
}
|
|
||||||
return 0 if($hl == -2);
|
|
||||||
|
|
||||||
if($hl == -1) { # chunked
|
|
||||||
while($hash->{buf} =~ m/^[\r\n]*([0-9A-F]+)\r?\n(.*)$/si) {
|
|
||||||
my ($l, $r) = (hex($1), $2);
|
|
||||||
return 0 if(length($r) < $l);
|
|
||||||
$hash->{httpdata} .= substr($r, 0, $l);
|
|
||||||
$hash->{buf} = substr($r, $l);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
$hash->{httpdata} .= $hash->{buf};
|
|
||||||
$hash->{buf} = "";
|
|
||||||
return 0 if(length($hash->{httpdata}) < $hash->{httpdatalen});
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub
|
sub
|
||||||
@ -555,16 +519,16 @@ HttpUtils_DigestHeader($$)
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub
|
sub
|
||||||
HttpUtils_ParseAnswer($)
|
HttpUtils_ParseAnswer($$)
|
||||||
{
|
{
|
||||||
my ($hash) = @_;
|
my ($hash, $ret) = @_;
|
||||||
|
|
||||||
if(!$hash->{keepalive}) {
|
if(!$hash->{keepalive}) {
|
||||||
$hash->{conn}->close();
|
$hash->{conn}->close();
|
||||||
undef $hash->{conn};
|
undef $hash->{conn};
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$hash->{buf} && !$hash->{httpheader}) {
|
if(!$ret) {
|
||||||
# Server answer: Keep-Alive: timeout=2, max=200
|
# Server answer: Keep-Alive: timeout=2, max=200
|
||||||
if($hash->{keepalive} && $hash->{hu_filecount}) {
|
if($hash->{keepalive} && $hash->{hu_filecount}) {
|
||||||
my $bc = $hash->{hu_blocking};
|
my $bc = $hash->{hu_blocking};
|
||||||
@ -582,12 +546,11 @@ HttpUtils_ParseAnswer($)
|
|||||||
$hash->{hu_filecount} = 0 if(!$hash->{hu_filecount});
|
$hash->{hu_filecount} = 0 if(!$hash->{hu_filecount});
|
||||||
$hash->{hu_filecount}++;
|
$hash->{hu_filecount}++;
|
||||||
|
|
||||||
return ("", $hash->{buf}) if(!defined($hash->{httpheader}));
|
$ret=~ s/(.*?)\r?\n\r?\n//s; # Not greedy: separate the header (F:#43482)
|
||||||
my $ret = $hash->{httpdata};
|
return ("", $ret) if(!defined($1));
|
||||||
delete $hash->{httpdata};
|
|
||||||
delete $hash->{httpdatalen};
|
|
||||||
|
|
||||||
my @header= split("\r\n", $hash->{httpheader});
|
$hash->{httpheader} = $1;
|
||||||
|
my @header= split("\r\n", $1);
|
||||||
my @header0= split(" ", shift @header);
|
my @header0= split(" ", shift @header);
|
||||||
my $code= $header0[1];
|
my $code= $header0[1];
|
||||||
|
|
||||||
@ -620,7 +583,7 @@ HttpUtils_ParseAnswer($)
|
|||||||
}
|
}
|
||||||
|
|
||||||
} elsif($code==401 && defined($hash->{auth})) {
|
} elsif($code==401 && defined($hash->{auth})) {
|
||||||
return ("$hash->{displayurl}: wrong authentication", "")
|
return ("$hash->{displayurl}: wrong authentication", "")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -645,9 +608,31 @@ HttpUtils_ParseAnswer($)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( $hash->{httpheader} =~ m/^Transfer-Encoding: Chunked/mi ) {
|
||||||
|
my $data;
|
||||||
|
my $header;
|
||||||
|
my ($size, $offset) = (length($ret), 0);
|
||||||
|
while( $offset < $size ) {
|
||||||
|
my $next = index($ret, "\r\n", $offset);
|
||||||
|
last if( $next == -1 );
|
||||||
|
if( substr($ret,$offset,$next-$offset) =~ m/([\da-f]+)/i ) {
|
||||||
|
my $len = hex($1);
|
||||||
|
$offset = $next + 2;
|
||||||
|
$data .= substr($ret,$offset,$len);
|
||||||
|
$offset += $len + 2;
|
||||||
|
next if( $len > 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
$hash->{httpheader} .= substr($ret,$offset);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$ret = $data;
|
||||||
|
}
|
||||||
|
|
||||||
# Debug
|
# Debug
|
||||||
Log3 $hash, $hash->{loglevel},
|
Log3 $hash, $hash->{loglevel},
|
||||||
"HttpUtils $hash->{displayurl}: Got data, length: ". length($ret);
|
"HttpUtils $hash->{displayurl}: Got data, length: ". length($ret);
|
||||||
if(!length($ret)) {
|
if(!length($ret)) {
|
||||||
Log3 $hash, $hash->{loglevel}, "HttpUtils $hash->{displayurl}: ".
|
Log3 $hash, $hash->{loglevel}, "HttpUtils $hash->{displayurl}: ".
|
||||||
"Zero length data, header follows:";
|
"Zero length data, header follows:";
|
||||||
@ -693,10 +678,8 @@ HttpUtils_BlockingGet($)
|
|||||||
my $err = HttpUtils_Connect($hash);
|
my $err = HttpUtils_Connect($hash);
|
||||||
return ($err, undef) if($err);
|
return ($err, undef) if($err);
|
||||||
|
|
||||||
my $buf = "";
|
my ($buf, $ret) = ("", "");
|
||||||
$hash->{conn}->timeout($hash->{timeout});
|
$hash->{conn}->timeout($hash->{timeout});
|
||||||
$hash->{buf} = "";
|
|
||||||
delete($hash->{httpdatalen});
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
my ($rout, $rin) = ('', '');
|
my ($rout, $rin) = ('', '');
|
||||||
vec($rin, $hash->{conn}->fileno(), 1) = 1;
|
vec($rin, $hash->{conn}->fileno(), 1) = 1;
|
||||||
@ -708,10 +691,10 @@ HttpUtils_BlockingGet($)
|
|||||||
|
|
||||||
my $len = sysread($hash->{conn},$buf,65536);
|
my $len = sysread($hash->{conn},$buf,65536);
|
||||||
last if(!defined($len) || $len <= 0);
|
last if(!defined($len) || $len <= 0);
|
||||||
$hash->{buf} .= $buf;
|
$ret .= $buf;
|
||||||
last if(HttpUtils_DataComplete($hash));
|
last if(HttpUtils_DataComplete($ret));
|
||||||
}
|
}
|
||||||
return HttpUtils_ParseAnswer($hash);
|
return HttpUtils_ParseAnswer($hash, $ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
# Deprecated, use GetFileFromURL/GetFileFromURLQuiet
|
# Deprecated, use GetFileFromURL/GetFileFromURLQuiet
|
||||||
|
Loading…
x
Reference in New Issue
Block a user