Trouble getting the Request Body of a HTTP Post

Maxim Dounin mdounin at
Thu Dec 17 22:27:25 MSK 2009


On Thu, Dec 17, 2009 at 01:58:00PM -0500, Tronman wrote:

> Hi there, Thanks for your response!
> I'm still trying to get the hang of the I/O model going on here. 
> Bare with me to see if I understand:
> When a connection comes in, the "ngx_http_my_module_handler" is 
> called. This function has the request headers, but not the 
> request body. In "ngx_http_my_module_handler", we can use 
> "ngx_http_read_client_request_body(r, 
> ngx_http_my_module_body_handler);" to set a callback (handler) 
> to be run when the Request Body is read (yes, I did mean Request 
> Body, sorry for my typo). Since the response is dependant on the 
> body, "ngx_http_my_module_handler" really doesn't have to do 
> anything else after setting the call back but return NGX_DONE.


But please note that once ngx_http_read_client_request_body() 
returns something >= NGX_HTTP_SPECIAL_RESPONSE you should return 
it from handler instead of NGX_DONE.  Basically it means that your 
body handler was already called (since full body was already 
read from client) and you have to pass it's return code.  So code 
in handler should look like:

    rc = ngx_http_read_client_request_body(r, 

        return rc;

    return NGX_DONE;

See e.g. proxy module for an example.

> Thus, when the function "ngx_http_my_module_body_handler" gets 
> called, I've got both the original headers, and the request 
> body, and it's here that I should place all of the logic to 
> build my response and send it to the client.


> This implies that the client program connecting to the server 
> must use separate "send" calls to send the header first, then 
> the body, and cannot put both a header and a body into the same 
> buffer and send them at the same time. Is this the case? 

No.  As long as http request is correct it doesn't matter if it 
was send in one packet or not.

> > To see the difference between (2) and (3) you may consider 
> > using some big POST (e.g. 10M) slowly uploaded by client. This 
> > also should give you some idea about how to handle multiple 
> > request body buffers and disk-buffered data.
> In this case, what would be the order of function calls? For 
> example, would "ngx_http_my_module_body_handler" be called for 
> each piece of the body (which I would write my own logic to 
> handle) or called after the entire (say 10M) body has been read.

It's called once entire body has been read.

> Is "ngx_http_my_module_handler" still called only once when the header arises?


> In the meantime, I have moved my response generating logic into 
> the "ngx_http_my_module_body_handler" which is working much 
> better (for single chunk bodies at least). Also, when I was 
> telnet-ing, I was trying to send headers and body in one chunk, 
> which wasn't working. Instead I started sending them in separate 
> chunks and am getting closer to the behaviour I expected. 

Most likely you did something wrong, either while telneting or in 
you module.

Maxim Dounin

More information about the nginx mailing list