目录
1、模块介绍
2、配置项和上下文
3、定义http_footer_filter模块
4、初始化ngx_http_footer_filter_module过滤模块
5、处理请求中的HTTP头部
6、处理请求中的HTTP包体
1、模块介绍
模块功能:This module implements a body filter that adds a given string to thepage footer.
配置示例:
location / {
## Using the $date_gmt variable from the SSI module (prints a
## UNIX timestamp).
footer "<!-- $date_gmt -->";
index index.html;
}
2、配置项和上下文
建立ngx_http_footer_loc_conf_t结构体存储配置项,成员vairiable来存储配置需要在页面底部添加的变量,如下所示:
typedef struct {
ngx_hash_t types;
ngx_array_t *types_keys;
ngx_http_complex_value_t *variable;
} ngx_http_footer_loc_conf_t;
再建立一个上下文结构体ngx_http_footer_ctx_t,其中包括footer成员,在处理HTTP头部时将ngx_http_footer_loc_conf_t结构体的variable的值转变为ngx_str_t类型,写到footer变量中
typedef struct {
ngx_str_t footer;
} ngx_http_footer_ctx_t;
ngx_http_footer_create_loc_conf用于分配存储配置项的结构体ngx_http_footer_loc_conf_t:
static void * ngx_http_footer_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_footer_loc_conf_t *conf;
// 创建存储配置项的结构体
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_footer_loc_conf_t));
if (conf == NULL) {
return NULL;
}
return conf;
}
3、定义http_footer_filter模块
定义http_footer_filter模块之前,需要先定义ngx_command_t类型的commands数组和ngx_http_module_t类型的ctx成员。ngx_http_footer_filter_commands处理footer和footer_types的配置项,将配置项参数解析到对应的ngx_http_footer_loc_conf_t的variable成员和types成员中,
static ngx_command_t ngx_http_footer_filter_commands[] = {
{ ngx_string("footer"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_footer_filter,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("footer_types"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_http_types_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_footer_loc_conf_t, types_keys),
&ngx_http_html_default_types[0] },
ngx_null_command
};
ngx_http_footer_filter的功能是将解析的配置项写到variable中
static char *
ngx_http_footer_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_footer_loc_conf_t *flcf = conf;
ngx_str_t *value;
ngx_http_complex_value_t **cv;
cv = &flcf->variable;
if (*cv != NULL) {
return "is duplicate";
}
value = cf->args->elts;
if (value[1].len) {
// 获得variable成员相对于结构体ngx_http_footer_loc_conf_t的偏移量
cmd->offset = offsetof(ngx_http_footer_loc_conf_t, variable);
// 该函数将解析的配置项写到variable中
return ngx_http_set_complex_value_slot(cf, cmd, conf);
}
*cv = (ngx_http_complex_value_t *) -1;
return NGX_OK;
}
ngx_http_footer_merge_loc_conf用于合并出现在多个配置块中配置项,代码如下:
static char * ngx_http_footer_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_footer_loc_conf_t *prev = parent;
ngx_http_footer_loc_conf_t *conf = child;
if (ngx_http_merge_types(cf, &conf->types_keys, &conf->types,
&prev->types_keys,&prev->types,
ngx_http_html_default_types)
!= NGX_OK)
{
return NGX_CONF_ERROR;
}
if (conf->variable == NULL) {
conf->variable = prev->variable;
}
// 若variable变量没有配置
if (conf->variable == NULL) {
conf->variable = (ngx_http_complex_value_t *) -1;
}
return NGX_CONF_OK;
}
ngx_http_footer_filter_init放在postconfiguration成员中,表示读取完所有配置项后就会回调ngx_http_footer_filter_init。
static ngx_http_module_t ngx_http_footer_filter_module_ctx = {
NULL, /* proconfiguration */
ngx_http_footer_filter_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_footer_create_loc_conf, /* create location configuration */
ngx_http_footer_merge_loc_conf /* merge location configuration */
};
下面定义了ngx_http_footer_filter_module过滤模块:
ngx_module_t ngx_http_access_module = {
NGX_MODULE_V1,
&ngx_http_access_module_ctx, /* module context */
ngx_http_access_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
};
4、初始化ngx_http_footer_filter_module过滤模块
定义静态指针ngx_http_next_header_filter,用于指向下一个过滤模块的HTTP头部处理方法,定义静态指针ngx_http_next_body_filter,用于指向下一个过滤模块的HTTP包体处理方法
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_int_t
ngx_http_footer_filter_init(ngx_conf_t *cf)
{
// 将该模块的包体处理方法插入到包体处理方法链表首部
ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_footer_body_filter;
//将该模块的头部处理方法插入到包体处理方法链表首部
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_footer_header_filter;
return NGX_OK;
}
5、处理请求中的HTTP头部
ngx_http_footer_header_filter函数将存储配置项的结构体ngx_http_footer_loc_conf_t中的variable成员写到上下文结构体ngx_http_footer_ctx_t的footer成员中
static ngx_int_t
ngx_http_footer_header_filter(ngx_http_request_t *r)
{
ngx_http_footer_ctx_t *ctx;
ngx_http_footer_loc_conf_t *lcf;
lcf = ngx_http_get_module_loc_conf(r, ngx_http_footer_filter_module);
if (lcf->variable == (ngx_http_complex_value_t *) -1 /* 是否配置了ngx_http_footer_loc_conf_结构体的variable成员,location配置里没有“footer”就是关闭 */
|| r->header_only
|| (r->method & NGX_HTTP_HEAD)
|| r != r->main //是子请求
// 回应没有正文
|| r->headers_out.status == NGX_HTTP_NO_CONTENT
// HTTP Content type未匹配
|| ngx_http_test_content_type(r, &lcf->types) == NULL)
{
return ngx_http_next_header_filter(r);
}
// 分配上下文的内存
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_footer_ctx_t));
if (ctx == NULL) {
return NGX_ERROR;
}
/* 将存储配置信息的结构体的variable成员写到上下文结构体ngx_http_footer_ctx_t的footer成员中 */
if (ngx_http_complex_value(r, lcf->variable, &ctx->footer) != NGX_OK) {
return NGX_ERROR;
}
/* #define ngx_http_set_ctx(r, c, module) r->ctx[module.ctx_index] = c,设置HTTP上下文结构体 */
ngx_http_set_ctx(r, ctx, ngx_http_footer_filter_module);
if (r->headers_out.content_length_n != -1) {
r->headers_out.content_length_n += ctx->footer.len;
}
if (r->headers_out.content_length) {
r->headers_out.content_length->hash = 0;
r->headers_out.content_length = NULL;
}
ngx_http_clear_accept_ranges(r);
return ngx_http_next_header_filter(r);
}
6、处理请求中的HTTP包体
static ngx_int_t
ngx_http_footer_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
ngx_buf_t *buf;
ngx_uint_t last;
ngx_chain_t *cl, *nl;
ngx_http_footer_ctx_t *ctx;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http footer body filter");
// 获得上下文结构体
ctx = ngx_http_get_module_ctx(r, ngx_http_footer_filter_module);
if (ctx == NULL) {
return ngx_http_next_body_filter(r, in);
}
last = 0;
找到HTTP包体链表的最后一个ngx_chain_t
for (cl = in; cl; cl = cl->next) {
if (cl->buf->last_buf) {
last = 1;
break;
}
}
if (!last) {
return ngx_http_next_body_filter(r, in);
}
buf = ngx_calloc_buf(r->pool);
if (buf == NULL) {
return NGX_ERROR;
}
// buf中写入配置的footer
buf->pos = ctx->footer.data;
buf->last = buf->pos + ctx->footer.len;
buf->start = buf->pos;
buf->end = buf->last;
buf->last_buf = 1;
buf->memory = 1;
if (ngx_buf_size(cl->buf) == 0) {
cl->buf = buf;
} else {
/* 从请求的内存池中生成nl链表,将前面的ngx_buf_t设置到nl的buf成员中,并将nl添加到HTTP包体最后 */
nl = ngx_alloc_chain_link(r->pool);
if (nl == NULL) {
return NGX_ERROR;
}
// 在之前找到的链表最后一个成员cl后面插入nl,nl中写入
nl->buf = buf;
nl->next = NULL;
cl->next = nl;
cl->buf->last_buf = 0;
}
// 调用下一个模块的HTTP包体处理方法
return ngx_http_next_body_filter(r, in);
}
参考文章
陶辉《深入理解Nginx-模块开发与架构设计》
https://github.com/alibaba/nginx-http-footer-filter