I've been coding an NGINX filter module that can read/write cookies for incoming requests. If a particular cookie isn't set correctly (i.e. authentication cookie), it will set the outgoing header status to the appropriate error code. This works fine per the directions of Evan Miller's tutorial. The next part I'm trying to get working (and haven't thus far) is having the body filter be invoked so I can insert/replace body response text when error responses are encountered. I again followed Evan Miller's tutorial on body filters, and I cannot for the life of me get this working. Here's my setup:
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
...
...
static ngx_http_module_t ngx_http_source_cookie_module_ctx = {
NULL, /* preconfiguration */
ngx_http_source_cookie_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_source_cookie_create_loc_conf, /* create location configuration */
ngx_http_source_cookie_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_source_cookie_module = {
NGX_MODULE_V1,
&ngx_http_source_cookie_module_ctx, /* module context */
ngx_http_source_cookie_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
static ngx_int_t
ngx_http_source_cookie_header_filter(ngx_http_request_t *r)
{
// this gets invoked
...
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
static ngx_int_t
ngx_http_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
// this never get invoked
...
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
static ngx_int_t
ngx_http_source_cookie_init(ngx_conf_t *cf)
{
// registering of my filters
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_source_cookie_header_filter;
ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_body_filter;
return NGX_OK;
}
This is my basic setup, and as far as I can tell, it's spot on all the examples/tutorials I've come across. I'm wondering if there's something different altogether I need to enable... like a NGINX config option, NGINX ./configure compile option, etc.
Any help is greatly appreciated.
I note that Evan doesnt fix http content length in
ngx_http_<module_name>_header_filter()
.If I dont add http content length(
r->headers_out.content_length_n
), the inserted text to the end of request will not be output from nginx-1.2.7 stable.Also you can see footer filter module.