I have this piece of code to capture HTTP request from browser:

sub recv_req { my $browser = shift; my $content_length = 0; my $req = ""; while (1) { my $chunk; $browser->recv($chunk, 10000); if ($chunk =~ m/Content-Length: (\d*)/) { $content_length = $1; print "content_length = $content_length\n"; } $req .= $chunk; last if ($chunk =~ "\r\n\r\n"); } $req =~ /(.*?)\r\n\r\n(.*)/s; if (length($2) > 0) { $content_length -= length($2); print "after -, content_length = $content_length\n"; print "[$2]\n";# I added this line minutes ago to capture $2 } while ($content_length > 0) { my $chunk; $browser->recv($chunk, $content_length); $req .= $chunk; $content_length -= length($chunk); } return $req; }

For this line,

print "after -, content_length = $content_length\n";

I expect it to print 0 for the last time it goes into the loop (if ever). However it prints -2 occasionaly (I cannot remember whether this happens to other web site, but for sure some particular request for this site)

This has been going on for a while, but today I decided to figure out what's going on, so I added:

print "[$2]\n";

to capture the content.

In one instance, I captured this, when I tried to delete a message:

after -, content_length = -2 [node_id=3628&deletemsg_540904478=yup&op=message&message=&message_send +=talk&.cgi #this line break here is caused by screen width fields=deletemsg_540904478 ]

It seems to me that there is a "\r\n" at the very end of the content, which is not counted in Content-Length, and that's where that -2 comes from.

I think there is a standard comformance issue somewhere, but I would like others to double check my script.


In reply to wrong Content-Length by pg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.