From adf9297047e41ba0a40a1028dc4612d7e3874038 Mon Sep 17 00:00:00 2001 From: daniceexi Date: Fri, 19 Apr 2013 14:02:10 +0000 Subject: [PATCH] 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 --- xCAT-server/lib/xcat/plugins/rhevm.pm | 55 ++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/rhevm.pm b/xCAT-server/lib/xcat/plugins/rhevm.pm index d366ba25b..b63a602a3 100644 --- a/xCAT-server/lib/xcat/plugins/rhevm.pm +++ b/xCAT-server/lib/xcat/plugins/rhevm.pm @@ -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";