一直容易忘记的httphandle处理过程
ngx_http_handler 起步
void
ngx_http_core_run_phases(ngx_http_request_t *r) //循环处理函数
{
ngx_int_t rc;
ngx_http_phase_handler_t *ph;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
ph = cmcf->phase_engine.handlers;
while (ph[r->phase_handler].checker) {
rc = ph[r->phase_handler].checker(r, &ph[r->phase_handler]);
if (rc == NGX_OK) {
return;
}
}
}
上面代码涉及到cmcf->phase_engine.handlers ,先看一下ngx_http_phase_handler_t 的结构
struct ngx_http_phase_handler_s {
ngx_http_phase_handler_pt checker; //阶段性 处理函数
ngx_http_handler_pt handler; //真实处理函数
ngx_uint_t next; //链表结构
};
接下来是模块的处理函数,push到cmcf->phases[各个阶段] 的数组中(以gzip为例子):
static ngx_int_t
ngx_http_gzip_static_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_gzip_static_handler;
return NGX_OK;
}
接下来就是在每个handler函数中去处理,结束返回ngx_OK,想执行下一个返回ngx_again
if (gzcf->enable == NGX_HTTP_GZIP_STATIC_OFF) { //config 是否配置
return NGX_DECLINED;
}
if (gzcf->enable == NGX_HTTP_GZIP_STATIC_ON) {
rc = ngx_http_gzip_ok(r);
} else {
/* always */
rc = NGX_OK;
}
关于NGX_HTTP_CONTENT_PHASE 阶段还有一个单独的优先级非常高的处理函数
ngx_int_t
ngx_http_core_content_phase(ngx_http_request_t *r,
ngx_http_phase_handler_t *ph){
....
if (r->content_handler) {
r->write_event_handler = ngx_http_request_empty_handler;
ngx_http_finalize_request(r, r->content_handler(r));//clcf->handler postconfiguration中初始化的优先级高
return NGX_OK;
}
rc = ph->handler(r); // array 中的处理函数