文章来自: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
};
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;
}
struct ngx_array_s {
void *elts; //数组数据区起始位置
ngx_uint_t nelts; //实际存放的元素个数
size_t size; //每个元素大小
ngx_uint_t nalloc; //数组所含空间个数,即实际分配的小空间的个数
ngx_pool_t *pool; //该数组在此内存池中分配
};