Welcome! Log In Create A New Profile

Advanced

Subrequests from body filters

Marat Dakota
March 24, 2013 01:14PM
Hi,

I'm writing a handler module. It makes subrequests and uses body
filter for subrequests.

Here is simplified handler code:

static ngx_int_t
ngx_http_my_handler(ngx_http_request_t *r) {

// ... Sending headers.

ngx_chain_t *out;
ngx_buf_t *b;

out = ngx_alloc_chain_link(r->pool);
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));

if (out == NULL || b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}

b->pos = "aaa";
b->last = "aaa" + 3;
b->memory = 1;
b->last_buf = 1;

out->buf = b;
out->next = NULL;

// ngx_http_my_subrequest() creates ngx_str_t from second argument
and calls ngx_http_subrequest().
if (ngx_http_my_subrequest(r, "/uri1") != NGX_OK) {
return NGX_ERROR;
}

if (ngx_http_my_subrequest(r, "/uri2") != NGX_OK) {
return NGX_ERROR;
}

return ngx_http_output_filter(r, out);
}


Here is subrequest body filter:

static ngx_int_t
ngx_http_my_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
// ... Do something with subrequest's body in "in" argument.

ngx_chain_t *out;
ngx_buf_t *b;
ngx_chain_t *cl;

out = ngx_alloc_chain_link(r->pool);
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));

if (out == NULL || b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}

// HERE BE DRAGONS.

b->pos = "bbb";
b->last = "bbb" + 3;
b->memory = 1;
b->last_buf = 1;

out->buf = b;
out->next = NULL;

// Discard subrequest's body.
for (cl = in; cl; cl = cl->next) {
cl->buf->pos = cl->buf->last;
cl->buf->file_pos = cl->buf->file_last;
}

// We just put "bbb" to main response for each call of this body filter.
return ngx_http_output_filter(r->main, out);
}

And everything works just fine. For example, if
ngx_http_my_body_filter is called twice for each (/uri1 and /uri2)
subrequest started from ngx_http_my_handler, the result will be
"aaabbbbbbbbbbbb".

But if I replace "HERE BE DRAGONS" with a call to
ngx_http_my_subrequest(r->main, "/uri3"), let's suppose this
subrequest is being started only in second call of
ngx_http_my_body_filter for /uri1 subrequest started from
ngx_http_my_handler. The result will be "aaabbb", not
"aaabbbbbbbbbbbbbbbbbb" as I expect (and logs say all three
subrequests are processed ok).

I guess my problem is about some magic with buffers and chains, but I
can't figure out what to fix and where.

I would be thankful for any help.

--
Marat

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

Subrequests from body filters

Marat Dakota 980 March 24, 2013 01:14PM

Re: Subrequests from body filters

Marat Dakota 448 March 24, 2013 05:16PM

Re: Subrequests from body filters

Marat Dakota 553 March 24, 2013 05:40PM

Re: Subrequests from body filters

Maxim Dounin 503 March 25, 2013 07:58AM

Re: Subrequests from body filters

Marat Dakota 470 March 25, 2013 09:42AM

Re: Subrequests from body filters

Maxim Dounin 441 March 25, 2013 09:48AM

Re: Subrequests from body filters

Marat Dakota 457 March 25, 2013 09:54AM

Re: Subrequests from body filters

Maxim Dounin 458 March 25, 2013 10:22AM

Re: Subrequests from body filters

Marat Dakota 407 March 25, 2013 10:48AM

Re: Subrequests from body filters

Maxim Dounin 482 March 25, 2013 10:56AM

Re: Subrequests from body filters

Marat Dakota 465 March 25, 2013 11:42AM

Re: Subrequests from body filters

Maxim Dounin 408 March 25, 2013 12:02PM

Re: Subrequests from body filters

Marat Dakota 448 March 25, 2013 12:24PM

Re: Subrequests from body filters

Maxim Dounin 650 March 25, 2013 02:12PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 196
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready