Trouble getting the Request Body of a HTTP Post

Maxim Dounin mdounin at mdounin.ru
Fri Dec 18 00:58:11 MSK 2009


Hello!

On Thu, Dec 17, 2009 at 03:33:04PM -0500, Tronman wrote:

[...]

> Great! Your response cleared up nearly everything for me, the 
> only other thing I'm not completely clear on is the difference 
> between:
> 
> ngx_chain_t                      *bufs;
> 
> and
> 
> ngx_buf_t                        *buf;
> 
> in ngx_http_request_body_t. What is the difference between them? 
> When should I use either to access the body?

You should use ->bufs.  Here is the comment from 
ngx_http_request_body.c which should be helpful:

/*
 * on completion ngx_http_read_client_request_body() adds to
 * r->request_body->bufs one or two bufs:
 *    *) one memory buf that was preread in r->header_in;
 *    *) one memory or file buf that contains the rest of the body
 */

Simple example of using request_body may be found in 
ngx_http_variables.c, in function 
ngx_http_variable_request_body().

[...]

> So if I specify "client_body_in_single_buffer" as "true", does 
> it store the entire client request in r->request_body->buf?

No.  It will be in first buffer of r->request_body->bufs, either 
in memory if it fits into client_body_buffer_size or in file.

> But if not, does it separate the client request body into the 
> chunks of whatever client_body_buffer_size is set to, and then 
> you use the chain to gain access to the entire thing?

Not exactly.  Currently r->request_body->bufs will contain up to two 
buffers: one for data preread with headers, and one for data got 
later.  If there are more data than client_body_buffer_size - the 
last buffer will be in temporary file.

It's probably a good idea to spend some time reading 
ngx_http_request_body.c for better clue what happens in various 
cases.

Maxim Dounin



More information about the nginx mailing list