webserver - check last blank line of HTTP request using perl chomp -
i'm pretty new perl. perl program getting http request message browser, want detect last blank line.
i trying use $_ =~ /\s/
, doesn't work:
while (<connection>) { print $_; if ($_ =~ /\s/) {print "blank line detected\n"; } }
the output
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=(organic)|utmcmd=organic|utmctr=leung%20carson blank line detected
i trying use chomp(), not work 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 nothing whitespace,
while (<connection>) { print $_; if ($_ =~ /\s/) {print "blank line detected\n"; } }
should be
while (<connection>) { print $_; if ($_ !~ /\s/) {print "blank line detected\n"; } }
or 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 because http header lines end \r\n. you'd need
while (<connection>) { s/\r?\n//; print "$_\n"; if ($_ eq "") {print "blank line detected\n"; } }
Comments
Post a Comment