开发者

check last blank line of HTTP request using perl chomp

I'm pretty new to Perl. My Perl program is getting the HTTP request message from browser, I want to detect the last blank line.

I was trying to use $_ =~ /\S/, but which doesn't work:

while (<CONNECTION>) {
  print $_;
  if ($_ =~ /\S/) {print "blank line detected\n"; }
}

the output is

GET / HTTP/1.1
blank line detected
Host: xxx.ca:15000
blank line detected
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20100101 Firefox/5.0
blank line detected
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
blank line detected
Accept-Language: en-us,en;q=0.7,zh-cn;q=0.3
blank line detected
Accept-Encoding: gzip, deflate
blank line detected
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
blank line detected
Connection: keep-alive
blank line detected
Cookie: __utma=32770362.1159201788.1291912625.1308033463.1309142872.11; __utmz=32770362.1307124126.7.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=Manitoba%20Locum%20Tenens%20Program; __utma=70597634.1054437369.1308785920.1308785920.1308785920.1; __utmz=70597634.1308785920.1.1.utmcsr=google|utmccn=(organi开发者_如何学运维c)|utmcmd=organic|utmctr=leung%20carson
blank line detected



I was trying to use chomp(), which does not work for me too:

  while (<CONNECTION>) {
    chomp(); 
    print "$_\n";
    if ($_ eq "") {print "blank line detected\n"; }
  }

the output:

GET / HTTP/1.1
Host: xxx.ca:15000
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.7,zh-cn;q=0.3
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Cookie: __utma=32770362.1159201788.1291912625.1308033463.1309142872.11; __utmz=32770362.1307124126.7.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=Manitoba%20Locum%20Tenens%20Program; __utma=70597634.1054437369.1308785920.1308785920.1308785920.1; __utmz=70597634.1308785920.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=leung%20carson

Thanks in advance~


To detect lines with nothing but whitespace,

while (<CONNECTION>) {
  print $_;
  if ($_ =~ /\S/) {print "blank line detected\n"; }
}

should be

while (<CONNECTION>) {
   print $_;
   if ($_ !~ /\S/) {print "blank line detected\n"; }
}

Or for short,

while (<CONNECTION>) {
   print;
   if (!/\S/) {print "blank line detected\n"; }
}

The reason

while (<CONNECTION>) {
   chomp(); 
   print "$_\n";
   if ($_ eq "") {print "blank line detected\n"; }
 }

might not work is because HTTP header lines end with \r\n. You'd need

while (<CONNECTION>) {
   s/\r?\n//; 
   print "$_\n";
   if ($_ eq "") {print "blank line detected\n"; }
 }


I think you want:

/^\s+$/

for blank line detection.

Using /\S/ will detect NON-blank lines.

Better yet, use something like Net::HTTP or LWP to do the heavy lifting for you. Some of the HTTP encoding issues are subtle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜