Multiple subrequest in handler

Adrian Perez de Castro adrianperez at
Mon Sep 10 16:54:15 MSD 2007

Hello again,

As I mentioned some days ago, I am writing an extended autoindex module
which should be able of including custom header and footer to listings.
Both Evan and Igor suggested using subrequests for them. After some
tinkering, I implemented sending the header, and I used a similar piece
of code to send the footer, snippet follows:

    if (alcf->header.len > 0) {
        /* header is configured -> do a subrequest. */
        ngx_http_request_t *sr;
        ngx_str_t *sr_uri = &alcf->header;
        ngx_str_t rel_uri;
        ngx_int_t rc;

        if (*sr_uri->data != '/') {
            /* Relative path */
            rel_uri.len  = r->uri.len + alcf->header.len;
   = ngx_palloc(r->pool, rel_uri.len);
            if ( == NULL)
                return NGX_HTTP_INTERNAL_SERVER_ERROR;

                    ngx_cpymem(, r->, r->uri.len),
                    alcf->, alcf->header.len);
            sr_uri = &rel_uri;

        rc = ngx_http_subrequest(r, sr_uri, NULL, &sr, NULL, 0);
        if (rc == NGX_ERROR || rc == NGX_DONE)
            return rc;

But as it is the file listing appears at the *end* of the generated
response. So, instead of:

 - header (from subrequest)
 - listing (generated in handler)
 - footer (from subrequest)

I am currently getting this:

 - header (from subrequest)
 - footer (from subrequest)
 - listing (generated in handler)

...which is not a proper order at all!

I took another look at the addition module, and it seems to be called
*twice* per request and it controls whether to add before or after
original content depending on the context (checking the boolean of
ctx->before_body_sent), but about line ~160 it seems to reorder buffers
in the output chain with this piece of code:

    last = 0;

    for (cl = in; cl; cl = cl->next) {
        if (cl->buf->last_buf) {
            cl->buf->last_buf = 0;
            cl->buf->sync = 1;
            last = 1;

I am using a content handler (as autoindex does) and the addition
module uses a filter handler... Is it enough for me if I reorder
buffers or should I rewrite my module using a filter too?

Also, the addition module calls ngx_http_next_body_filter() in some
places, but I think I should not call it: if I understood the code
correctly, it does so to filter the original content to which pieces
are added. As I am generated my own content, I think it would not be
necessary to filter it...

Once again, thank you in advance for your support and your time.
Comments are welcome ;-)

P.S. Once I fix this issue, I will start publishing the code of the
module. I wonder which format is preferred by the Nginx users for
work-in-progress code: repository or snapshot tarballs or whatever.

Adrian Perez de Castro               <adrianperez at>
Oficina de Software Libre
Universidade da Coruña        
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <>

More information about the nginx mailing list