2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-22 08:11:44 +00:00

HttpUtils.pm: Async write for POST Requests (Forum #41583)

git-svn-id: https://svn.fhem.de/fhem/trunk@9576 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2015-10-21 14:28:19 +00:00
parent 06ac962867
commit 61ad1ce5d4

View File

@ -67,32 +67,23 @@ HttpUtils_Close($)
delete($hash->{hu_sslAdded}); delete($hash->{hu_sslAdded});
delete($hash->{hu_filecount}); delete($hash->{hu_filecount});
delete($hash->{hu_blocking}); delete($hash->{hu_blocking});
delete($hash->{directReadFn});
delete($hash->{directWriteFn});
} }
sub sub
HttpUtils_ConnErr($) HttpUtils_Err($$)
{ {
my ($hash) = @_; my ($hash, $errtxt) = @_;
$hash = $hash->{hash}; $hash = $hash->{hash};
if(defined($hash->{FD})) { return if(!defined($hash->{FD})); # Already closed
delete($hash->{FD}); HttpUtils_Close($hash);
delete($selectlist{$hash}); $hash->{callback}($hash, "$errtxt to $hash->{addr} timed out", "");
$hash->{conn}->close() if($hash->{conn});
$hash->{callback}($hash, "connect to $hash->{addr} timed out", "");
}
} }
sub sub HttpUtils_ConnErr($) { my ($hash) = @_; HttpUtils_Err($hash, "connect to");}
HttpUtils_ReadErr($) sub HttpUtils_ReadErr($) { my ($hash) = @_; HttpUtils_Err($hash, "read from"); }
{ sub HttpUtils_WriteErr($){ my ($hash) = @_; HttpUtils_Err($hash, "write to"); }
my ($hash) = @_;
$hash = $hash->{hash};
if(defined($hash->{FD})) {
delete($hash->{FD});
delete($selectlist{$hash});
$hash->{callback}($hash, "read from $hash->{addr} timed out", "");
}
}
sub sub
HttpUtils_File($) HttpUtils_File($)
@ -268,13 +259,10 @@ HttpUtils_Connect2($)
if ($hdr !~ "Content-Type:"); if ($hdr !~ "Content-Type:");
} }
$hdr .= "\r\n"; $hdr .= "\r\n";
syswrite $hash->{conn}, $hdr;
syswrite $hash->{conn}, $data if(defined($data));
my $s = $hash->{shutdown}; my $s = $hash->{shutdown};
$s =(defined($hash->{noshutdown}) && $hash->{noshutdown}==0) if(!defined($s)); $s =(defined($hash->{noshutdown}) && $hash->{noshutdown}==0) if(!defined($s));
$s = 0 if($hash->{protocol} eq "https"); $s = 0 if($hash->{protocol} eq "https");
shutdown($hash->{conn}, 1) if($s);
if($hash->{callback}) { # Nonblocking read if($hash->{callback}) { # Nonblocking read
$hash->{FD} = $hash->{conn}->fileno(); $hash->{FD} = $hash->{conn}->fileno();
@ -292,15 +280,40 @@ HttpUtils_Connect2($)
RemoveInternalTimer(\%timerHash); RemoveInternalTimer(\%timerHash);
my ($err, $ret, $redirect) = HttpUtils_ParseAnswer($hash, $hash->{buf}); my ($err, $ret, $redirect) = HttpUtils_ParseAnswer($hash, $hash->{buf});
$hash->{callback}($hash, $err, $ret) if(!$redirect); $hash->{callback}($hash, $err, $ret) if(!$redirect);
return; }
};
$data = $hdr.(defined($data) ? $data:"");
$hash->{directWriteFn} = sub($) { # Nonblocking write
my $ret = syswrite $hash->{conn}, $data;
if($ret <= 0) {
my $err = $!;
RemoveInternalTimer(\%timerHash);
HttpUtils_Close($hash);
return $hash->{callback}($hash, "write error: $err", undef)
}
$data = substr($data,$ret);
if(length($data) == 0) {
shutdown($hash->{conn}, 1) if($s);
delete($hash->{directWriteFn});
RemoveInternalTimer(\%timerHash);
InternalTimer(gettimeofday()+$hash->{timeout},
"HttpUtils_ReadErr", \%timerHash, 0);
} }
}; };
$selectlist{$hash} = $hash; $selectlist{$hash} = $hash;
InternalTimer(gettimeofday()+$hash->{timeout}, InternalTimer(gettimeofday()+$hash->{timeout},
"HttpUtils_ReadErr", \%timerHash, 0); "HttpUtils_WriteErr", \%timerHash, 0);
return undef; return undef;
} else {
syswrite $hash->{conn}, $hdr;
syswrite $hash->{conn}, $data if(defined($data));
shutdown($hash->{conn}, 1) if($s);
} }
return undef; return undef;
} }