Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nginx模块开发之set函数 #25

Open
chunpu opened this issue Jan 10, 2015 · 0 comments
Open

Nginx模块开发之set函数 #25

chunpu opened this issue Jan 10, 2015 · 0 comments
Labels

Comments

@chunpu
Copy link
Owner

chunpu commented Jan 10, 2015

set函数就是上一篇提到的很重要的函数之一, 是command命令结构体中的第三个参数.在读取命令时候触发的回调函数.

set是一个函数指针, 原型是这样的.

char * (*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

返回值

char *, 返回值可以是NGX_CONF_OK, 或者NGX_CONF_ERROR. 也就是0或者-1的指针.

参数

ngx_conf_t *cf含义未知

ngx_command_t *cmd 表示自己所在的那个command结构体

void *conf 当然是空

如何在set函数中挂上handler

ngx_http_core_loc_conf_t *corecf;
corecf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
corecf->handler = handler;

corecf为何物?, 其实就是读到test命令时的配置状态, corecf是一个巨大的结构体, name属性是"/test", 也就是我们定义的那个路径.后面还有regex等属性, 应该也是用来匹配路径的.

应该可以猜测, 给corecf的handler属性赋值后, 如果请求的路径匹配corecf的name或者正则等, 就会触发我们挂载的handler函数.

挂完后返回NGX_CONF_OK即可.

源代码中如何调用set函数

set在ngx_conf_handler.c文件在ngx_int_t ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last)函数中被调用.

显然set函数的第一个参数cf就是这个大函数中的第一个参数.

第二个参数cmd, 是每一个模块的cmd, 在gdb中print每个模块的cmd, 可以发现daemon, error_log都是command.

ngx_conf_handler函数的作用就是遍历所有模块, 然后带着cf和cmd参数触发cmd的set函数,仅此而已.

@chunpu chunpu added the nginx label Jan 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant