# Question about create an asynchronous http handler

**URL:** <https://www.ruby-forum.com/t/question-about-create-an-asynchronous-http-handler/208517>\
**Category:** NGINX\
**Created:** [June 20, 2011, 2:50pm UTC](https://www.ruby-forum.com/t/question-about-create-an-asynchronous-http-handler/208517 "2011-06-20T14:50:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [June 20, 2011, 2:50pm UTC](https://www.ruby-forum.com/t/question-about-create-an-asynchronous-http-handler/208517/1 "2011-06-20T14:50:02Z")

</div>

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  
…  
ngx\_event\_peer\_connect(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.

Thanks.

Posted at Nginx Forum:

> **[Question about create an asynchronous http handler](https://forum.nginx.org/read.php?2,208453,208453#msg-208453)**
>
> 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:...

---

<div class="post-metadata">

**Author:** ![speedfirst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19adc/32.png) [@speedfirst](https://www.ruby-forum.com/u/speedfirst)\
**Post date:** [June 20, 2011, 3:21pm UTC](https://www.ruby-forum.com/t/question-about-create-an-asynchronous-http-handler/208517/2 "2011-06-20T15:21:36Z")

</div>

Is this correct? r-\>count++; then return NGX\_DONE?

Posted at Nginx Forum:

> **[Re: Question about create an asynchronous http handler](https://forum.nginx.org/read.php?2,208453,208462#msg-208462)**
>
> 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:...

---

<div class="post-metadata">

**Author:** ![speedfirst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19adc/32.png) [@speedfirst](https://www.ruby-forum.com/u/speedfirst)\
**Post date:** [June 20, 2011, 4:05pm UTC](https://www.ruby-forum.com/t/question-about-create-an-asynchronous-http-handler/208517/3 "2011-06-20T16:05:31Z")

</div>

Hello!

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.
