How to merge subrequest header.

Roast zhang.libing at gmail.com
Thu Nov 25 13:36:36 MSK 2010


Thanks agentzh.

You can register a post_subrequest callback function to your
> ngx_http_subrequest call and do subrequest header processing there.
>

It seems post_subrequest callback function can't change the parent's
header,because the parent's header filter has been process finished before
the function call, and the output buf has created.

And I have fixed this problem, at the header filter function. Just like
following steps,maybe that's not a good way, agentzh pls give me some more
advanced suggestion.

static ngx_int_t
> ngx_http_social_header_filter(ngx_http_request_t *r)
> {
>     ...... //some other works
>
>     ctx = r->connection->log->data;
>     r = ctx->request;
>
>     if (r !=  ctx->current_request)
>     {
>         sr = ctx->current_request;
>         part = &sr->headers_out.headers.part;
>         h = part->elts;
>
>         /* set main status with subrequest */
>         if (r->headers_out.status != sr->headers_out.status)
>         {
>             r->headers_out.status = sr->headers_out.status;
>
>             r->headers_out.status_line.data = NULL;
>             r->headers_out.status_line.len = 0;
>         }
>
>         /* clear out buffer */
>         r->out = NULL;
>
>         for (i = 0; /* void */ ; i++)
>         {
>             if (i >= part->nelts)
>             {
>                 if (part->next == NULL)
>                     break;
>
>                 part = part->next;
>                 h = part->elts;
>                 i = 0;
>             }
>
>             if (h->hash == 0)
>                 continue;
>
>             if (ngx_strncasecmp(h[i].key.data, (u_char *) "Set-Cookie", 10)
> == 0 ||
>                 ngx_strncasecmp(h[i].key.data, (u_char *) "Location", 8) ==
> 0)
>             {
>                 /* merge header: Set-Cookie*/
>                 hh = ngx_list_push(&r->headers_out.headers);
>                 if (h == NULL) {
>                     return NGX_HTTP_INTERNAL_SERVER_ERROR;
>                 }
>
>                 hh->hash = sr->header_hash;
>                 hh->key = h[i].key;
>                 hh->value = h[i].value;
>                 hh->lowcase_key = ngx_pnalloc(r->pool, h[i].key.len);
>                 if (hh->lowcase_key == NULL) {
>                     return NGX_HTTP_INTERNAL_SERVER_ERROR;
>                 }
>
>                 ngx_strlow(hh->lowcase_key, hh->key.data, hh->key.len);
>             }
>         }
>     }
>
>     ...... //some other works
>

>
    return ngx_http_next_header_filter(r);
> }
>


You can check out the corresponding code in our ngx_echo or ngx_lua
> module. There's a live example for merging subrequest headers and body
> into the main request in ngx_lua (untested though, but *should* work):
>

That's two great module,but we need more work within our module.


-- 
The time you enjoy wasting is not wasted time!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://nginx.org/pipermail/nginx/attachments/20101125/5dab8cd4/attachment.html>


More information about the nginx mailing list