您的位置:首页 > 其它

ngx_conf_set_str_slot函数解析

2015-11-16 09:46 573 查看
文章来自:http://blog.chinaunix.net/uid-22400280-id-3218765.html

ngx_command_t

static ngx_command_t ngx_http_get_method_commands[]={
{
ngx_string("test"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
getMethodset,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL,
},
ngx_null_command
};

ngx_command_s
struct ngx_command_s {
ngx_str_t name;
ngx_uint_t type;
char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
ngx_uint_t conf;
ngx_uint_t offset;
void *post;
};

char *
ngx_conf_set_str_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *p = conf;  //获取当前配置结构体信息ngx_http__
_conf_t

ngx_str_t *field, *value;
ngx_conf_post_t *post;

field = (ngx_str_t *) (p + cmd->offset); //ngx_command_t的offset作为p的偏移量
/*ngx_command_t的 ngx_uint_t conf指明offset的位置是在哪个配置结构体信息中(main\server\loc)在ngx_http_config.h中定义了conf的三个取值,如下
#define NGX_HTTP_MAIN_CONF_OFFSET  offsetof(ngx_http_conf_ctx_t, main_conf)
#define NGX_HTTP_SRV_CONF_OFFSET   offsetof(ngx_http_conf_ctx_t, srv_conf)
#define NGX_HTTP_LOC_CONF_OFFSET   offsetof(ngx_http_conf_ctx_t, loc_conf)
typedef struct {
void        **main_conf;
void        **srv_conf;
void        **loc_conf;
} ngx_http_conf_ctx_t; 获取到保存指令参数的配置结构体conf中,offset指的是在结构体重偏移位置
ngx_conf_t *cf是获取当前环境中的读取配置文件的配置信息包含指令名称指令参数
*/

if (field->data) {
return "is duplicate";
}

value = cf->args->elts; //获取指令数组

*field = value[1]; //获取第一个参数

if (cmd->post) {
post = cmd->post;
return post->post_handler(cf, post, field);
}

return NGX_CONF_OK;
}


ngx_array_s
struct ngx_array_s {
void *elts; //数组数据区起始位置
ngx_uint_t nelts; //实际存放的元素个数
size_t size; //每个元素大小
ngx_uint_t nalloc; //数组所含空间个数,即实际分配的小空间的个数
ngx_pool_t *pool; //该数组在此内存池中分配
};


ngx_command_t中的set函数调用时间是,配置文件解析中ngx_conf_t获取指令和指令参数,解析函数查找模块与模块注册的指令,找到正确的指令,则调用该指令set函数执行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息