From e3830fc30f7bca0b433636d333e43ec91ba896f3 Mon Sep 17 00:00:00 2001 From: rudolfkoenig <> Date: Thu, 6 Feb 2014 09:07:25 +0000 Subject: [PATCH] HttpUtils: code for checking HTML content before the connection is closed. Needed for FHEMWEB when noShutdown is set. git-svn-id: https://svn.fhem.de/fhem/trunk@4817 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/HttpUtils.pm | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/fhem/FHEM/HttpUtils.pm b/fhem/FHEM/HttpUtils.pm index 18f8d164d..6eb415185 100644 --- a/fhem/FHEM/HttpUtils.pm +++ b/fhem/FHEM/HttpUtils.pm @@ -192,7 +192,8 @@ HttpUtils_Connect2($) $hash->{directReadFn} = sub() { my $buf; my $len = sysread($hash->{conn},$buf,65536); - if(!defined($len) || $len <= 0) { # EOF + $hash->{buf} .= $buf if(defined($len) && $len > 0); + if(!defined($len) || $len <= 0 || HttpUtils_DataComplete($hash->{buf})) { delete($hash->{FD}); delete($hash->{directReadFn}); delete($selectlist{$hash}); @@ -200,7 +201,6 @@ HttpUtils_Connect2($) $hash->{callback}($hash, $err, $ret) if(!$redirect); return; } - $hash->{buf} .= $buf; }; $selectlist{$hash} = $hash; InternalTimer(gettimeofday()+$hash->{timeout}, @@ -211,6 +211,18 @@ HttpUtils_Connect2($) return undef; } +sub +HttpUtils_DataComplete($) +{ + my ($ret) = @_; + return 0 if($ret !~ m/^(.*?)\r\n\r\n(.*)$/s); + my $hdr = $1; + my $data = $2; + return 0 if($hdr !~ m/Content-Length:\s*(\d+)/s); + return 0 if(length($data) < $1); + return 1; +} + sub HttpUtils_ParseAnswer($$) { @@ -310,6 +322,7 @@ HttpUtils_BlockingGet($) my $len = sysread($hash->{conn},$buf,65536); last if(!defined($len) || $len <= 0); $ret .= $buf; + last if(HttpUtils_DataComplete($ret)); } return HttpUtils_ParseAnswer($hash, $ret); }