Fixed the method to receive http payload with chunk format

git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@16024 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd
This commit is contained in:
daniceexi 2013-04-19 14:02:10 +00:00
parent 370376f043
commit adf9297047

View File

@ -707,20 +707,55 @@ sub send_req {
print $connect $request;
$response = "";
while (1) {
my $retry;
my $ischunked;
my $firstnum;
while ($retry++ < 10) {
unless ($IOsel->can_read(2)) {
next;
}
my $readbytes;
$IOsel->can_read(0.5);
do { $readbytes=sysread($connect,$response,65535,length($response)); } while ($readbytes);
if ($response) {
#return $response;
last;
} else {
if (not defined $readbytes and $! == EAGAIN) { next; }
$rc = 2;
last;
my $res = "";
do { $readbytes=sysread($connect,$res,65535,length($res)); } while ($readbytes);
if ($res) {
my @part = split (/\r\n/, $res);
for my $data (@part) {
# for chunk formated data, check the last chunk to finish
if ($data =~ /Transfer-Encoding: (\S+)/) {
if ($1 eq "chunked") {
$ischunked = 1;
}
}
if ($ischunked && $data =~ /^([\dabcdefABCDEF]+)$/) {
if ($1 eq 0) {
# last chunk
goto FINISH;
}else {
# continue to get the rest chunks
$retry = 0;
next;
}
} else {
# put all data together
$response .= $data;
}
}
}
unless ($ischunked) {
# for non chunk data, just read once
if ($response) {
last;
} else {
if (not defined $readbytes and $! == EAGAIN) { next; }
$rc = 2;
last;
}
}
}
FINISH:
if ($retry >= 10 ) {$rc = 3;}
if ($verbose) {
my $rsp;
push @{$rsp->{data}}, "$response===================================================\n";