Chinaunix首页 | 论坛 | 博客
  • 博客访问: 419927
  • 博文数量: 83
  • 博客积分: 2622
  • 博客等级: 少校
  • 技术积分: 1345
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 08:59
个人简介

一直在努力

文章分类

全部博文(83)

文章存档

2014年(3)

2013年(9)

2012年(46)

2010年(25)

分类: 系统运维

2012-05-22 23:54:23

ngx_conf_set_str_slot函数目的获取指令参数并把参数存储在配置文件结构体中,通过这个函数了解如何将配置文件中的命令参数传递到handler中。

ngx_command_t

  1. static ngx_command_t ngx_http_get_method_commands[]={
  2.     {
  3.         ngx_string("test"),
  4.         NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  5.         getMethodset,
  6.         NGX_HTTP_LOC_CONF_OFFSET,
  7.         0,
  8.         NULL,        
  9.     },
  10.     ngx_null_command
  11. };

ngx_command_s结构体构成

  1. struct ngx_command_s {
  2.     ngx_str_t name;
  3.     ngx_uint_t type;
  4.     char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  5.     ngx_uint_t conf;
  6.     ngx_uint_t offset;
  7.     void *post;
  8. };


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

  5.     ngx_str_t *field, *value;
  6.     ngx_conf_post_t *post;

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

  13.     if (field->data) {
  14.         return "is duplicate";
  15.     }

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

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

  18.     if (cmd->post) {
  19.         post = cmd->post;
  20.         return post->post_handler(cf, post, field);
  21.     }

  22.     return NGX_CONF_OK;
  23. }

ngx_array_t数据结构

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

ngx_command_t中的set函数调用时间是,配置文件解析中ngx_conf_t获取指令和指令参数,解析函数查找模块与模块注册的指令,找到正确的指令,则调用该指令set函数执行。
阅读(5666) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~