# Http phase handler

**URL:** <https://www.ruby-forum.com/t/http-phase-handler/122225>\
**Category:** NGINX\
**Created:** [December 5, 2007, 6:42pm UTC](https://www.ruby-forum.com/t/http-phase-handler/122225 "2007-12-05T18:42:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manlio\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Manlio\_P](https://www.ruby-forum.com/u/Manlio_P)\
**Post date:** [December 5, 2007, 6:42pm UTC](https://www.ruby-forum.com/t/http-phase-handler/122225/1 "2007-12-05T18:42:14Z")

</div>

Hi.

I’m interested in how HTTP phases handlers works in nginx.

I would like to add support, in mod\_wsgi, to NGX\_HTTP\_PREACCESS\_PHASE,  
NGX\_HTTP\_ACCESS\_PHASE and NGX\_HTTP\_CONTENT\_PHASE.

I’m reading the sources but I’m not sure to understand how a phase  
handler terminates.

With a request handler, the handler can return NGX\_DONE and then call  
ngx\_http\_finalize\_request, but what about a phase handler?

Thanks Manlio P.

---

<div class="post-metadata">

**Author:** ![Manlio\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Manlio\_P](https://www.ruby-forum.com/u/Manlio_P)\
**Post date:** [December 5, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/http-phase-handler/122225/2 "2007-12-05T19:20:25Z")

</div>

On Wed, Dec 05, 2007 at 06:32:23PM +0100, Manlio P. wrote:

> I’m interested in how HTTP phases handlers works in nginx.
> 
> I would like to add support, in mod\_wsgi, to NGX\_HTTP\_PREACCESS\_PHASE,  
> NGX\_HTTP\_ACCESS\_PHASE and NGX\_HTTP\_CONTENT\_PHASE.
> 
> I’m reading the sources but I’m not sure to understand how a phase  
> handler terminates.
> 
> With a request handler, the handler can return NGX\_DONE and then call  
> ngx\_http\_finalize\_request, but what about a phase handler?

All phase handlers should simpy return value  
NGX\_OK/NGX\_DECLINED/NGX\_HTTP\_FORBIDDEN/etc.

In the context phase there may run a single defined content handler  
(perl,  
proxy\_pass, fastcgi\_pass, memcached\_pass, flv, empty\_gif) or stack  
of handlers: index, autoindex, dav, static.

If a defined handler calls finally  
ngx\_http\_finalize\_request(NGX\_DECLINED),  
then nginx goes to the stack of handlers. Now only perl can do this.

The preaccess phase is for generic handlers:  
on NGX\_OK it goes to a next phase,  
on NGX\_DECLINED it goes to a next handler in the same phase,  
on NGX\_AGAIN/NGX\_DONE it simply return,  
on NGX\_ERROR or NGX\_HTTP\_… it calls ngx\_http\_finalize\_request()

The access phase is special to handle NGX\_OK/NGX\_DECLINED depending on  
satisfy\_any. However, I’m going to change it, because now:

```
 location / {
     satisfy_any on;
     auth_basic ...
     # no allow/deny rules
 }

```

does not ask username/password.

---

<div class="post-metadata">

**Author:** ![Manlio\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Manlio\_P](https://www.ruby-forum.com/u/Manlio_P)\
**Post date:** [December 5, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/http-phase-handler/122225/3 "2007-12-05T20:02:06Z")

</div>

Igor S. ha scritto:

> > With a request handler, the handler can return NGX\_DONE and then call  
> > ngx\_http\_finalize\_request, but what about a phase handler?
> 
> All phase handlers should simpy return value  
> NGX\_OK/NGX\_DECLINED/NGX\_HTTP\_FORBIDDEN/etc.

Ok.

> In the context phase there may run a single defined content handler (perl,  
> proxy\_pass, fastcgi\_pass, memcached\_pass, flv, empty\_gif) or stack  
> of handlers: index, autoindex, dav, static.
> 
> If a defined handler calls finally ngx\_http\_finalize\_request(NGX\_DECLINED),  
> then nginx goes to the stack of handlers. Now only perl can do this.

Ok.  
This should be possible with Python, too.  
But I can’t see an useful application for this.

> ```
> satisfy_any on;
> auth_basic ...
> # no allow/deny rules
> }
> 
> ```
> 
> does not ask username/password.

Is it possible to return NGX\_DONE/NGX\_AGAIN from the access phase  
handler and then call ngx\_http\_finalize\_request(NGX\_HTTP\_…) when the  
handler is done (as an example it may setup the events when doing an  
asynchronous query to a database)?

The only module with a phase handler that does “interesting” things is  
the autoindex module, but it uses sub requests (and I have yet to  
understand how they works :)).

Thanks Manlio P.

---

<div class="post-metadata">

**Author:** ![Manlio\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Manlio\_P](https://www.ruby-forum.com/u/Manlio_P)\
**Post date:** [December 5, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/http-phase-handler/122225/4 "2007-12-05T20:38:26Z")

</div>

Igor S. ha scritto:

> > But I can’t see an useful application for this.
> 
> For example, perl may handle POSTs and decline other to default stack.

What happens if the perl handler sends the headers and writes some data?

> […]
> 
> > Is it possible to return NGX\_DONE/NGX\_AGAIN from the access phase  
> > handler and then call ngx\_http\_finalize\_request(NGX\_HTTP\_…) when the  
> > handler is done (as an example it may setup the events when doing an  
> > asynchronous query to a database)?
> 
> Yes. However, I could not say right how you should finalize processing  
> after second call: ngx\_http\_finalize\_request() or return.

I will try to do some tests.

> > The only module with a phase handler that does “interesting” things is  
> > the autoindex module, but it uses sub requests (and I have yet to  
> > understand how they works :)).
> 
> The stock autoindex module does not use subrequests.

Oh, right!

Thanks Manlio P.

---

<div class="post-metadata">

**Author:** ![Manlio\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Manlio\_P](https://www.ruby-forum.com/u/Manlio_P)\
**Post date:** [December 5, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/http-phase-handler/122225/5 "2007-12-05T20:13:14Z")

</div>

On Wed, Dec 05, 2007 at 07:54:54PM +0100, Manlio P. wrote:

> > > 
> > 
> > proxy\_pass, fastcgi\_pass, memcached\_pass, flv, empty\_gif) or stack  
> > of handlers: index, autoindex, dav, static.
> > 
> > If a defined handler calls finally ngx\_http\_finalize\_request(NGX\_DECLINED),  
> > then nginx goes to the stack of handlers. Now only perl can do this.
> 
> Ok.  
> This should be possible with Python, too.  
> But I can’t see an useful application for this.

For example, perl may handle POSTs and decline other to default stack.

> > ```
> > satisfy_any on;
> > 
> > ```
> 
> handler is done (as an example it may setup the events when doing an  
> asynchronous query to a database)?

Yes. However, I could not say right how you should finalize processing  
after second call: ngx\_http\_finalize\_request() or return.

> The only module with a phase handler that does “interesting” things is  
> the autoindex module, but it uses sub requests (and I have yet to  
> understand how they works :)).

The stock autoindex module does not use subrequests.

---

<div class="post-metadata">

**Author:** ![Manlio\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Manlio\_P](https://www.ruby-forum.com/u/Manlio_P)\
**Post date:** [December 5, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/http-phase-handler/122225/6 "2007-12-05T20:42:32Z")

</div>

On Wed, Dec 05, 2007 at 08:27:56PM +0100, Manlio P. wrote:

> > > > 
> > > 
> > > Ok.  
> > > This should be possible with Python, too.  
> > > But I can’t see an useful application for this.
> > 
> > For example, perl may handle POSTs and decline other to default stack.
> 
> What happens if the perl handler sends the headers and writes some data?

Two responses: perl’s one and default’s one.
