Re: proper use of $r->read?

Torsten Förtsch <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
On Wednesday, April 27, 2011 17:40:11 E R wrote:
> What's the proper way to read in the posted content from a request?
> 
> Using this Google code search:
> 
> http://www.google.com/codesearch?q=lang%3Aperl+%5C%24r-%3Eread.*length
> &hl=en
> 
> I see instances of:
> 
>   $r->read($line, $r->headers_in->get('Content-length'));
> 
> and also loops like:
> 
>     while ( $r->read( $buffer, $content_length ) ) {
>         $content .= $buffer;
>     }
> 
> What's the best/proper way to slurp in all of the POST content?

At first, there is a bug in $r->read in 2.0.4 that prevents loops like

  my $buf='';
  1 while $r->read($buf, CHUNK_SIZE, length $buf);

In general I'd recommend to read up to the end of the stream. Apache 
knows when it is reached. Relying on the Content-Length input header only 
is bad because the request may not contain that field.

Also, think twice before implementing something similar unless you have a 
really controlled environment. It is quite simple for an attacker to send 
an infinite stream of data.

Torsten Förtsch

-- 
Need professional modperl support? Hire me! (http://foertsch.name)

Like fantasy? http://kabatinte.net
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.