Модуль фильтра Nginx - не получать директивные значения
Я только начинаю писать свой модуль.
Ниже приведен простой модуль фильтра:
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
typedef struct {
ngx_str_t status;
ngx_str_t content;
} ngx_http_ds_filter_conf_t;
static ngx_command_t ngx_http_ds_filter_commands[] = {
{
ngx_string("dsFilterStatus"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_ds_filter,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_ds_filter_conf_t,status),
NULL
},
{
ngx_string("dsFilterContent"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_ds_filter,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_ds_filter_conf_t,content),
NULL
},
ngx_null_command
};
static ngx_int_t ngx_http_ds_header_filter(ngx_http_request_t *r)
{
ngx_http_ds_filter_conf_t *conf;
conf = (ngx_http_ds_filter_conf_t*)ngx_http_get_module_loc_conf(r,ngx_http_ds_filter_module);
if(conf->status.data == (u_char*)"off")
{
return ngx_http_next_header_filter(r);
}
else
{
if(r->headers_out.content_length_n != -1)
{
r->headers_out.content_length_n += conf->content.len;
}
}
return ngx_http_next_header_filter(r);
}
static ngx_int_t ngx_http_ds_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
ngx_buf_t *b;
ngx_chain_t *cl, *nl;
bool last = false;
for(cl = in; cl; cl = cl->next)
{
if(cl->buf->last_buf)
{
last = true;
break;
}
}
if(!last)
{
return ngx_http_next_body_filter(r,in);
}
ngx_http_ds_filter_conf_t *conf;
conf = (ngx_http_ds_filter_conf_t*)ngx_http_get_module_loc_conf(r, ngx_http_ds_filter_module);
b = (ngx_buf_t*)ngx_calloc_buf(r->pool);
if(b == NULL)
{
return NGX_ERROR;
}
b->pos = conf->content.data;
b->last = b->pos + conf->content.len;
b->start = b->pos;
b->end = b->last;
b->last_buf = 1;
b->memory = 1;
if(ngx_buf_size(cl->buf) == 0)
{
cl->buf = b;
}
else
{
nl = ngx_alloc_chain_link(r->pool);
if(nl == NULL)
{
return NGX_ERROR;
}
nl->buf = b;
nl->next = NULL;
cl->next = nl;
cl->buf->last_buf = 0;
}
return ngx_http_next_body_filter(r, in);
}
static ngx_int_t ngx_http_ds_filter_init(ngx_conf_t *cf)
{
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_ds_header_filter;
ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_ds_body_filter;
return NGX_OK;
}
Мои функции "создать и слить":
static void *ngx_http_ds_filter_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_ds_filter_conf_t *conf;
conf = (ngx_http_ds_filter_conf_t*)ngx_pcalloc(cf->pool, sizeof(ngx_http_ds_filter_conf_t));
if(conf == NULL)
{
return NULL;
}
conf->content.len = 0;
conf->content.data = NULL;
conf->status.len = 0;
conf->status.data = NULL;
return conf;
}
static char *ngx_http_ds_filter_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_ds_filter_conf_t *prev = (ngx_http_ds_filter_conf_t*)parent;
ngx_http_ds_filter_conf_t *conf = (ngx_http_ds_filter_conf_t*)child;
ngx_conf_merge_str_value(conf->status, prev->status, "NADA");
ngx_conf_merge_str_value(conf->content,prev->content,"NJET");
return NGX_CONF_OK;
}
Как вы можете видеть в приведенном выше коде, я просто добавляю "conf->content.data" в буфер как значение.
С такой конфигурацией, как:
dsFilterStatus on;
dsFilterContent Hello;
показывающий результат - "NJET".
Я не знаю почему?
Было бы здорово, если бы кто-нибудь сказал мне, что я делаю не так.
Спасибо