You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Arturo 'Buanzo' Busleiman <bu...@buanzo.com.ar> on 2007/04/12 19:05:59 UTC

read POST body

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Hi group!

For mod_auth_openpgp I need to read the POST body. During my research (googling, archives of this
list, apache.org, etc) I discovered three methods so far. I would like your opinions on the safest
one, fastest one, if should DECHUNK, how much to allow for post size allocation (probably a
configuration option, but i'd need a default value...).

This is what I got: anything you can think of would be of GREAT help:

Getting REQUEST BODY: (1)
============================

	ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK);
	
	char buffer[1024];

	if ( ap_should_client_block(r) == 1 ) {
		while ( ap_get_client_block(r, buffer, 1024) > 0 ) {
			ap_rputs("Reading in buffer...<br>",r);
			ap_rputs(buffer,r);
		}
	} else {
		ap_rputs("Nothing to read...<br>",r);
	}

Getting REQUEST BODY: (2)
============================

http://httpd.apache.org/apreq/

Getting REQUEST BODY: (3)
============================
static int util_read(request_rec *r, const char **rbuf)
{
   int rc;

    if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) {
       return rc;
   }

    if (ap_should_client_block(r)) {
       char argsbuffer[HUGE_STRING_LEN];
      int rsize, len_read, rpos=0;
      long length = r->remaining;
      *rbuf = ap_pcalloc(r->pool, length + 1);

       ap_hard_timeout("util_read", r);

       while ((len_read =
               ap_get_client_block(r, argsbuffer, sizeof(argsbuffer))) > 0) {
          ap_reset_timeout(r);
          if ((rpos + len_read) > length) {
              rsize = length - rpos;
          }
          else {
              rsize = len_read;
          }
          memcpy((char*)*rbuf + rpos, argsbuffer, rsize);
          rpos += rsize;
      }

       ap_kill_timeout(r);
    }
   return rc;
}

- --
Arturo "Buanzo" Busleiman - Consultor Independiente en Seguridad Informatica
Foros GNU/Buanzo: Respeto, Soluciones y Buena Onda: http://foros.buanzo.com.ar
Consulting and Secure Mail Hosting: http://www.buanzo.com.ar/pro/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGHmb3AlpOsGhXcE0RClVRAJ95UqidCJcSXqmdWMGupuNahdBk/QCfb5KO
R5y+G2NbAFGUIvHYytfHxb4=
=xxOM
-----END PGP SIGNATURE-----

Re: read POST body

Posted by Arturo 'Buanzo' Busleiman <bu...@buanzo.com.ar>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Graham Dumpleton wrote:
> Again, this is my understanding from studying code and reading
> different bits and pieces, so someone correct me if I am wrong.
> Confirmation from someone that this is correct would also be
> appreciated.

Graham:

	Wow.

Very interesting and insightful information! *THANKS* for your time! I will post my solution as soon
as I implement and test it.

Thanks!


- --
Arturo "Buanzo" Busleiman - Consultor Independiente en Seguridad Informatica
Foros GNU/Buanzo: Respeto, Soluciones y Buena Onda: http://foros.buanzo.com.ar
Consulting and Secure Mail Hosting: http://www.buanzo.com.ar/pro/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGHtgZAlpOsGhXcE0RCod8AJ41V9WLD8ryjFQr3eITGpQprAJjQQCfS6kN
XIdys39JD5XSFpZsy3Nii0w=
=eVQg
-----END PGP SIGNATURE-----

Re: read POST body

Posted by Graham Dumpleton <gr...@gmail.com>.
On 13/04/07, Graham Dumpleton <gr...@gmail.com> wrote:
> On 13/04/07, Arturo 'Buanzo' Busleiman <bu...@buanzo.com.ar> wrote:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA512
> >
> > Hi group!
> >
> > For mod_auth_openpgp I need to read the POST body. During my research (googling, archives of this
> > list, apache.org, etc) I discovered three methods so far. I would like your opinions on the safest
> > one, fastest one, if should DECHUNK, how much to allow for post size allocation (probably a
> > configuration option, but i'd need a default value...).
> >
> > This is what I got: anything you can think of would be of GREAT help:
> >
> > Getting REQUEST BODY: (1)
> > ============================
> >
> >         ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK);
> >
> >         char buffer[1024];
> >
> >         if ( ap_should_client_block(r) == 1 ) {
> >                 while ( ap_get_client_block(r, buffer, 1024) > 0 ) {
> >                         ap_rputs("Reading in buffer...<br>",r);
> >                         ap_rputs(buffer,r);
> >                 }
> >         } else {
> >                 ap_rputs("Nothing to read...<br>",r);
> >         }
>
> I can't find reference to point at so my memory could be wrong, but if
> using this approach one thing you must be mindful of is that the
> minimum size you use for the read buffer must be sufficient to hold
> any chunk size information and any trailers provided after the last
> null chunk when chunked transfer encoding is being used. This is
> because the HTTP filter code uses your buffer as working space for
> decoding those parts of the request stream.

Found the documentation about this. Was just looking at the wrong
place in the source code comment. What it says is:

 * 1. Call ap_setup_client_block() near the beginning of the request
 *    handler. This will set up all the necessary properties, and will
 *    return either OK, or an error code. If the latter, the module should
 *    return that error code. The second parameter selects the policy to
 *    apply if the request message indicates a body, and how a chunked
 *    transfer-coding should be interpreted. Choose one of
 *
 *    REQUEST_NO_BODY          Send 413 error if message has any body
 *    REQUEST_CHUNKED_ERROR    Send 411 error if body without Content-Length
 *    REQUEST_CHUNKED_DECHUNK  If chunked, remove the chunks for me.
 *    REQUEST_CHUNKED_PASS     If chunked, pass the chunk headers with body.
 *
 *    In order to use the last two options, the caller MUST provide a buffer
 *    large enough to hold a chunk-size line, including any extensions.

Note the last line where it says:

"""caller MUST provide a buffer large enough to hold a chunk-size
line, including any extensions."""

I think I am slightly wrong on one point though when I said 'trailers'
as that is something different to 'extensions'. RFC says:

       Chunked-Body   = *chunk
                        last-chunk
                        trailer
                        CRLF

       chunk          = chunk-size [ chunk-extension ] CRLF
                        chunk-data CRLF
       chunk-size     = 1*HEX
       last-chunk     = 1*("0") [ chunk-extension ] CRLF

       chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
       chunk-ext-name = token
       chunk-ext-val  = token | quoted-string
       chunk-data     = chunk-size(OCTET)
       trailer        = *(entity-header CRLF)

Even so, how long is an extension going to be and therefore what is
the minimum buffer size one should use. This is something I have never
found a good answer to as examples of 'extensions' are hard to find.
The only thing I have found so far that defines an 'extension' is ICAP
protocol (RFC 3507). It defines:

      \r\n
      0; ieof\r\n\r\n

Thus it is short.

Overall I really don't know how big a deal this is. If you are always
controlling the buffer size used it probably isn't, but if specified
by higher up code in some scripting language wrapper then you have to
be mindful of it and always use a bigger buffer but still only return
what higher up code asks for and cache any extra for next read.

Am I just being overly cautious? Anyone else have any comments to make
about this issue with chunked data?

Graham

Re: read POST body

Posted by Graham Dumpleton <gr...@gmail.com>.
On 13/04/07, Arturo 'Buanzo' Busleiman <bu...@buanzo.com.ar> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA512
>
> Hi group!
>
> For mod_auth_openpgp I need to read the POST body. During my research (googling, archives of this
> list, apache.org, etc) I discovered three methods so far. I would like your opinions on the safest
> one, fastest one, if should DECHUNK, how much to allow for post size allocation (probably a
> configuration option, but i'd need a default value...).
>
> This is what I got: anything you can think of would be of GREAT help:
>
> Getting REQUEST BODY: (1)
> ============================
>
>         ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK);
>
>         char buffer[1024];
>
>         if ( ap_should_client_block(r) == 1 ) {
>                 while ( ap_get_client_block(r, buffer, 1024) > 0 ) {
>                         ap_rputs("Reading in buffer...<br>",r);
>                         ap_rputs(buffer,r);
>                 }
>         } else {
>                 ap_rputs("Nothing to read...<br>",r);
>         }

I can't find reference to point at so my memory could be wrong, but if
using this approach one thing you must be mindful of is that the
minimum size you use for the read buffer must be sufficient to hold
any chunk size information and any trailers provided after the last
null chunk when chunked transfer encoding is being used. This is
because the HTTP filter code uses your buffer as working space for
decoding those parts of the request stream.

Someone correct me if I am wrong on this, but if correct and you know
where I read this, other than by looking at code, please point me to
where it is as would like to find it again.

In terms of a reasonable size to use, Apache defines HUGE_STRING_LEN
(8192) which some code uses  as its default block size. Other option
if appropriate is to interrogate the socket to determine the buffer
size used by the OS for sockets. Don't know how valid it is, but
performing reads using the same size may or may not be more optimal.

> Getting REQUEST BODY: (2)
> ============================
>
> http://httpd.apache.org/apreq/

Can't comment on this one.

> Getting REQUEST BODY: (3)
> ============================
> static int util_read(request_rec *r, const char **rbuf)
> {
>    int rc;
>
>     if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) {
>        return rc;
>    }
>
>     if (ap_should_client_block(r)) {
>        char argsbuffer[HUGE_STRING_LEN];
>       int rsize, len_read, rpos=0;
>       long length = r->remaining;
>       *rbuf = ap_pcalloc(r->pool, length + 1);
>
>        ap_hard_timeout("util_read", r);
>
>        while ((len_read =
>                ap_get_client_block(r, argsbuffer, sizeof(argsbuffer))) > 0) {
>           ap_reset_timeout(r);
>           if ((rpos + len_read) > length) {
>               rsize = length - rpos;
>           }
>           else {
>               rsize = len_read;
>           }
>           memcpy((char*)*rbuf + rpos, argsbuffer, rsize);
>           rpos += rsize;
>       }
>
>        ap_kill_timeout(r);
>     }
>    return rc;
> }

Even if you use REQUEST_CHUNKED_ERROR, disallowing chunked transfer
encoding and requiring a content length to be specified, I am not sure
you can rely on r->remaining being always correct.

This is because a mutating input filter could actually change the
amount of data which is available but it will not update any content
length value as it may not know in advance what the final content
length may be. An example of such an input filter is one that does
decompression on request content.

In Apache 2.2 the filter protocol directive:

  http://httpd.apache.org/docs/2.2/mod/mod_filter.html#filterprotocol

provides a way of saying that a particular filter changes the content,
including possibly the content length, but I think it is still up to
the user to set up these directives in the configuration to mark
filters as such and then all it does from memory is remove the content
length header to try and block handlers from believing it is correct.

Thus, although one can use r->remaining as a guide as to how much
buffer space may be required, a robust handler would have to be
prepared to cope with more than that thus potentially resizing its
input buffer, or ensure that it does actually stop reading when that
amount of data has been read in, rather than reading until no more
data, although that may result in truncation of data if such a
mutating input filter is present and it yields more data than the
content length specified.

Again, this is my understanding from studying code and reading
different bits and pieces, so someone correct me if I am wrong.
Confirmation from someone that this is correct would also be
appreciated.

Graham