ngx_conf_set_str_slot函数解析

文章来自: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函数执行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值