Marat Dakota
March 24, 2013 05:16PM
It looks like I've figured out. When I set b->last_buf to 0 for all
the buffers and to 1 for just actual last buffer, everything is ok.

But then another question happens. Even if all the buffers have
b->flush set to 1, I see my response only in one single chunk, after
all the subrequests finished. How to actually flush the response
buffer by buffer?

--
Marat

On Sun, Mar 24, 2013 at 9:12 PM, Marat Dakota <dakota@brokenpipe.ru> wrote:
> 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 982 March 24, 2013 01:14PM

Re: Subrequests from body filters

Marat Dakota 449 March 24, 2013 05:16PM

Re: Subrequests from body filters

Marat Dakota 555 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 472 March 25, 2013 09:42AM

Re: Subrequests from body filters

Maxim Dounin 443 March 25, 2013 09:48AM

Re: Subrequests from body filters

Marat Dakota 459 March 25, 2013 09:54AM

Re: Subrequests from body filters

Maxim Dounin 460 March 25, 2013 10:22AM

Re: Subrequests from body filters

Marat Dakota 409 March 25, 2013 10:48AM

Re: Subrequests from body filters

Maxim Dounin 484 March 25, 2013 10:56AM

Re: Subrequests from body filters

Marat Dakota 467 March 25, 2013 11:42AM

Re: Subrequests from body filters

Maxim Dounin 410 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 652 March 25, 2013 02:12PM



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

Online Users

Guests: 160
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