Welcome! Log In Create A New Profile

Advanced

Is this the right way to do synchronous subrequests?

Brian Pane
January 11, 2010 02:28PM
In one of my modules, I needed to run several subrequests serially and
send their output back to the original client. After looking at
mod_ssi_filter and mod_echo, I was able to write a module that worked.
However, because the subrequest code is complicated, I'd like a
second opinion. Can someone who's familiar with the subrequest
mechanism please take a look at the code example below (which is a
simplified version of my actual code, to make it easier to read) and
let me know if I'm using subrequests in the proper manner?

One specific question I have is: What does the
NGX_HTTP_SUBREQUEST_IN_MEMORY flag do?

I'm using nginx-0.7, if that makes a difference.

Thanks,
-Brian

typedef struct {
ngx_http_request_t *wait;
} my_ctx_t;

static ngx_int_t
my_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
my_ctx_t *ctx;

ctx = ngx_http_get_module_ctx(r, my_module);
if (ctx == NULL) {
ctx = ngx_pcalloc(r->pool, sizeof(*ctx));
ngx_http_set_ctx(r, ctx, my_module);
}

if (r != r->main) {
return ngx_http_next_body_filter(r, in);
}

if (ctx->wait != NULL) {
if (r != r->connection->data) {
return NGX_AGAIN;
}
if (ctx->wait->done) {
ctx->wait = NULL;
}
else {
return ngx_http_next_body_filter(r, NULL);
}
}

if (/* more subrequests remain to be issued */) {
ngx_http_request_t *sr;
int rc;
ngx_str_t *uri = /* URI of next subrequest */
rc = ngx_http_subrequest(r, uri, NULL, &sr, NULL,
NGX_HTTP_SUBREQUEST_WAITED);
if (rc != NGX_OK) {
/* error */
return rc;
}
ctx->wait = sr;
}

return ngx_http_next_body_filter(r, NULL);
}

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

Is this the right way to do synchronous subrequests?

Brian Pane 2580 January 11, 2010 02:28PM

Re: Is this the right way to do synchronous subrequests?

agentzh 1213 January 11, 2010 09:02PM



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

Online Users

Guests: 75
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready