Hi all, I'm having another problem with a script I'm writing. I have a standard function that reads the response from a server after being sent some request. However, I have a problem in the loop that reads the message body. If there is a message body, it'll read everything in, but then loop indefinitely when it reaches the end of the response. I've tried several things, but I have no idea what to do anymore. (Please keep in mind that I taught myself Perl last weekend, so I'm still a noob in this respect.)
Code:sub readHTTPResponseHeader { my $hfile = $_[0]; my $thisline = ''; my %header = (); my $has_body = 0; my $trash = ""; #process first line - status line $thisline = <$hfile>; ($trash,$header{'.Http-Version'},$header{'.Status-Code'},$header{'.Status-Message'}) = split(/^HTTP\/(\d\.\d) (\d{3}) (.*)$/,$thisline); #process header lines while(defined($thisline = <$hfile>)) { chomp($thisline); if($thisline =~ /^\s*?$/){ $has_body = 1; last; } my($trash,$key,$value) = split(/^\s*([\w\-\d]+)\s*:\s*(.*)\s*$/,$thisline); $header{$key} = $value; } #if there is a message body, read it in all at once. if($has_body == 1) { if(exists $header{'Content-Length'}){ read $hfile,$header{'.Message-Body'},$header{'Content-Length'}; } else { my $message_body = ''; #PROBLEM HERE - NOT RECOGNIZING END OF TRANSMISSION while(defined ($thisline = <$hfile>)){ $message_body .= $thisline; } $header{'.Message-Body'} = $message_body; } } else { $header{'.Message-Body'} = ''; } return %header; }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks