Question about create an asynchronous http handler

Hey, I’m making a custom http handler. The idea is, the user connects to
this handler, and this handler connect to another server to fetch some
information. Therefore the handler itself can’t return the http
response. The code framework is as below:

ngx_http_my_custom_handler (ngx_http_request * r) {
//create a peer connection to another peer

peer.connection->write->handler = ngx_http_my_send;
peer.connection->read->handler = ngx_http_my_recv;

return ???;

ngx_http_my_send {
//send the request to another peer

ngx_http_my_recv {
//read the response to buffer
//create response to client
ngx_http_finalize_request(r, NGX_HTTP_OK);

The question is what should I return in “ngx_http_my_custom_handler” to
avoid the request and the connection being disposed? And how to send
back the final response in "ngx_http_my_recv? (is directly using
ngx_http_finalize_request correct?) I check the code of
ngx_http_proxy_module, and it returns NGX_DONE. But it seems not work in
my project. The request is always disposed when NGX_DONE is returned.


Posted at Nginx Forum:

Is this correct? r->count++; then return NGX_DONE?

Posted at Nginx Forum:


On Mon, Jun 20, 2011 at 09:21:12AM -0400, speedfirst wrote:

Is this correct? r->count++; then return NGX_DONE?

Yes. In proxy module r->count++ happens in
ngx_http_read_client_request_body() call.

Maxim D.