config文件:
ngx_addon_name=ngx_http_req_filter_module
HTTP_FILTER_MODULES="$HTTP_FILTER_MODULES ngx_http_req_filter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_req_filter_module.c"
注意对于filter模块需要指定其为 HTTP_FILTER_MODULES
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <gd.h>
typedef struct {
ngx_flag_t filter_switch;
} ngx_http_req_filter_conf_t;
static void *ngx_http_req_filter_create_conf(ngx_conf_t *cf);
static ngx_int_t ngx_http_req_header_filter(ngx_http_request_t *r);
static ngx_int_t ngx_http_req_body_filter(ngx_http_request_t *r, ngx_chain_t *chain);
static ngx_int_t ngx_http_req_filter_init(ngx_conf_t *cf);
static ngx_str_t filter_prefix = ngx_string("~~~~~~~This is a prefix~~~~~~~\n");
static ngx_command_t ngx_http_req_filter_commands[] = {
{ ngx_string("req_filter"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_req_filter_conf_t, filter_switch),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_req_filter_module_ctx = {
NULL, /* preconfiguration */
ngx_http_req_filter_init, /* postconfiguration */ //过滤模块要在这个阶段进行添加
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_req_filter_create_conf, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_req_filter_module = {
NGX_MODULE_V1,
&ngx_http_req_filter_module_ctx, /* module context */
ngx_http_req_filter_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_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
static void *ngx_http_req_filter_create_conf(ngx_conf_t *cf)
{
ngx_http_req_filter_conf_t* conf;
conf = ngx_palloc(cf->pool,sizeof(ngx_http_req_filter_conf_t));
if(!conf)
return NULL;
conf->filter_switch = NGX_CONF_UNSET_UINT;
return conf;
}
static ngx_int_t ngx_http_req_header_filter(ngx_http_request_t *r)
{
ngx_http_req_filter_conf_t* conf;
conf = ngx_http_get_module_loc_conf(r,ngx_http_req_filter_module);
if(!conf->filter_switch)
return NGX_ERROR;
if(r->headers_out.status != NGX_HTTP_OK)
return ngx_http_next_header_filter(r);
if(r->headers_out.content_type.len >= sizeof("text/plain") -1
&& ngx_strncasecmp(r->headers_out.content_type.data,(u_char*)"text/plain",sizeof("text/plain")-1) ==0)
{
if(r->headers_out.content_length_n > 0)
r->headers_out.content_length_n += filter_prefix.len;
}
return ngx_http_next_header_filter(r);
}
static ngx_int_t ngx_http_req_body_filter(ngx_http_request_t *r, ngx_chain_t *chain)
{
ngx_buf_t* b = ngx_create_temp_buf(r->pool, filter_prefix.len);
b->start = b->pos = filter_prefix.data;
b->last = b->pos + filter_prefix.len;
ngx_chain_t *cl = ngx_alloc_chain_link(r->pool);
cl->buf = b;
cl->next = chain;
return ngx_http_next_body_filter(r, cl);
}
static ngx_int_t
ngx_http_req_filter_init(ngx_conf_t *cf)
{
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_req_header_filter;
ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_req_body_filter;
return NGX_OK;
}