How to merge subrequest header

Hi,all.

I use ngx_http_subrequest to send a subrequest with the parent’s
response
body,after the sub request processed, I got the subrequest’s body, but
It
seems has no way to merge the subrequest header to parent request.

Anybody else has met this same problem, and how to do this?

Thanks.


The time you enjoy wasting is not wasted time!

Anybody else has met this same problem, and how to do this?

On Wed, Nov 24, 2010 at 9:00 PM, Roast [email protected] wrote:

Hi,all.

I use ngx_http_subrequest to send a subrequest with the parent’s response
body,after the sub request processed, I got the subrequest’s body, but It
seems has no way to merge the subrequest header to parent request.

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

Anybody else has met this same problem, and how to do this?

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):

location /proxy {
    proxy_pass http://foo.bar.com/$query_string;
}

location /main {
    content_by_lua '
        var res = ngx.location.capture("/proxy",
            { args = "/foo/bar/baz" }
        )
        if res.status == ngx.HTTP_OK then
            for k,v in pairs(res.header) do
                ngx.header[k] = v
            end
            ngx.print(res.body)
        else
            ngx.exit(res.status)
        end
    ';
}

Cheers,
-agentzh

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

    part = &sr->headers_out.headers.part;

            part = part->next;
  1.          hh->lowcase_key = ngx_pnalloc(r->pool, h[i].key.len);
    
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.

On Thu, Nov 25, 2010 at 6:18 PM, agentzh [email protected] wrote:

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):

Sorry, the previous sample has bugs. The following example is tested
and working:

location /proxy {
proxy_pass http://www.baidu.com/s;
}

location /main {
content_by_lua ’
local res = ngx.location.capture("/proxy",
{ args = { wd = “Perl” } }
)

       if res.status == ngx.HTTP_OK then
           for k,v in pairs(res.header) do
               ngx.header[k] = v
           end
           ngx.print(res.body)
       else
           ngx.exit(res.status)
       end
   ';

}

I used the following command line to test the /main location to check
the response body:

curl localhost/main|grep Perl|iconv -f gbk -t utf8

and the following command to check the response headers:

$ curl -I localhost/main
HTTP/1.1 200 OK
Server: nginx/0.8.41
Date: Thu, 25 Nov 2010 10:30:12 GMT
Content-Type: text/html;charset=gbk
Connection: keep-alive
Content-Length: 32274
Cache-Control: private
Set-Cookie: BAIDUID=F8A74DD5F378F2EFFEA0EC6B8BD10E1B:FG=1;

expires=Thu, 25-Nov-40 10:30:11 GMT; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "

Cheers,
-agentzh

On Thu, Nov 25, 2010 at 6:36 PM, Roast [email protected] wrote:

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.

The key point is that not to send out the parent headers when you need
to change it later (according to its subrequests or other things).

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

You can surely buffer the parent requests’ headers and any outputs
before your subrequest actually finishes in your output filter
functions :wink:

Cheers,
-agentzh

On Sun, Feb 26, 2012 at 8:02 AM, edo888 [email protected] wrote:

Can you please share some piece of code. I want to make a post
subrequest to send the response body to fastcgi and filter it there.

See the detailed discussions here:
https://github.com/agentzh/echo-nginx-module/issues/8

Best regards,
-agentzh

Hi,

Can you please share some piece of code. I want to make a post
subrequest to send the response body to fastcgi and filter it there.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,152939,222982#msg-222982

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs