Welcome! Log In Create A New Profile

Advanced

Re: Trouble getting the Request Body of a HTTP Post

Maxim Dounin
December 17, 2009 02:32PM
Hello!

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.


Yes.

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,
ngx_http_my_module_body_handler);

if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
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.

Yes.

> 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?

Yes.

> 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

_______________________________________________
nginx mailing list
nginx@nginx.org
http://nginx.org/mailman/listinfo/nginx
Subject Author Posted

Trouble getting the Request Body of a HTTP Post

Tronman December 17, 2009 11:35AM

Re: Trouble getting the Request Body of a HTTP Post

Maxim Dounin December 17, 2009 12:44PM

Re: Trouble getting the Request Body of a HTTP Post

Tronman December 17, 2009 01:58PM

Re: Trouble getting the Request Body of a HTTP Post

Tronman December 17, 2009 02:28PM

Re: Trouble getting the Request Body of a HTTP Post

Maxim Dounin December 17, 2009 02:32PM

Re: Trouble getting the Request Body of a HTTP Post

Tronman December 17, 2009 03:33PM

Re: Trouble getting the Request Body of a HTTP Post

barretto_chris February 08, 2011 06:38PM

Re: Trouble getting the Request Body of a HTTP Post

Nick Pearson December 17, 2009 03:50PM

Re: Trouble getting the Request Body of a HTTP Post

Maxim Dounin December 17, 2009 05:02PM

Re: Trouble getting the Request Body of a HTTP Post

Tronman December 18, 2009 08:32AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 273
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready