定义了一个ngx_http_hello_init的方法,将ngx_http_hello_log_handler函数挂载到NGX_HTTP_LOG_PHASE日志处理阶段

对Nginx源码分析 - 实战篇 - 编写一个阶段化的模块(25)原文https://blog.csdn.net/initphp/article/details/72912723的理解。如何定义了一个ngx_http_hello_init的方法,将ngx_http_hello_log_handler函数挂载到NGX_HTTP_LOG_PHASE日志处理阶段

首先看conf的解析指令集,处理“http” block的回调函数是ngx_http_block。
static ngx_command_t ngx_http_commands[] = {

{ ngx_string("http"),
  NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
  ngx_http_block,
  0,
  0,
  NULL },

  ngx_null_command

};

ngx_http_block函数:
static char *
ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *rv;
ngx_uint_t mi, m, s;
ngx_conf_t pcf;
ngx_http_module_t *module;
ngx_http_conf_ctx_t *ctx;
ngx_http_core_loc_conf_t *clcf;
ngx_http_core_srv_conf_t **cscfp;
ngx_http_core_main_conf_t *cmcf;

if (*(ngx_http_conf_ctx_t **) conf) {
    return "is duplicate";
}

/* the main http context */

ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
if (ctx == NULL) {
    return NGX_CONF_ERROR;
}

*(ngx_http_conf_ctx_t **) conf = ctx;


/* count the number of the http modules and set up their indices */

ngx_http_max_module = ngx_count_modules(cf->cycle, NGX_HTTP_MODULE);


/* the http main_conf context, it is the same in the all http contexts */

ctx->main_conf = ngx_pcalloc(cf->pool,
                             sizeof(void *) * ngx_http_max_module);
if (ctx->main_conf == NULL) {
    return NGX_CONF_ERROR;
}


/*
 * the http null srv_conf context, it is used to merge
 * the server{}s' srv_conf's
 */

ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
if (ctx->srv_conf == NULL) {
    return NGX_CONF_ERROR;
}


/*
 * the http null loc_conf context, it is used to merge
 * the server{}s' loc_conf's
 */

ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
if (ctx->loc_conf == NULL) {
    return NGX_CONF_ERROR;
}


/*
 * create the main_conf's, the null srv_conf's, and the null loc_conf's
 * of the all http modules
 */

for (m = 0; cf->cycle->modules[m]; m++) {
    if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
        continue;
    }

    module = cf->cycle->modules[m]->ctx;
    mi = cf->cycle->modules[m]->ctx_index;

    if (module->create_main_conf) {
        ctx->main_conf[mi] = module->create_main_conf(cf);
        if (ctx->main_conf[mi] == NULL) {
            return NGX_CONF_ERROR;
        }
    }

    if (module->create_srv_conf) {
        ctx->srv_conf[mi] = module->create_srv_conf(cf);
        if (ctx->srv_conf[mi] == NULL) {
            return NGX_CONF_ERROR;
        }
    }

    if (module->create_loc_conf) {
        ctx->loc_conf[mi] = module->create_loc_conf(cf);
        if (ctx->loc_conf[mi] == NULL) {
            return NGX_CONF_ERROR;
        }
    }
}

pcf = *cf;
cf->ctx = ctx;

for (m = 0; cf->cycle->modules[m]; m++) {
    if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
        continue;
    }

    module = cf->cycle->modules[m]->ctx;

    if (module->preconfiguration) {
        if (module->preconfiguration(cf) != NGX_OK) {
            return NGX_CONF_ERROR;
        }
    }
}

/* parse inside the http{} block */

cf->module_type = NGX_HTTP_MODULE;
cf->cmd_type = NGX_HTTP_MAIN_CONF;
rv = ngx_conf_parse(cf, NULL);

if (rv != NGX_CONF_OK) {
    goto failed;
}

/*
 * init http{} main_conf's, merge the server{}s' srv_conf's
 * and its location{}s' loc_conf's
 */

cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
cscfp = cmcf->servers.elts;

for (m = 0; cf->cycle->modules[m]; m++) {
    if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
        continue;
    }

    module = cf->cycle->modules[m]->ctx;
    mi = cf->cycle->modules[m]->ctx_index;

    /* init http{} main_conf's */

    if (module->init_main_conf) {
        rv = module->init_main_conf(cf, ctx->main_conf[mi]);
        if (rv != NGX_CONF_OK) {
            goto failed;
        }
    }

    rv = ngx_http_merge_servers(cf, cmcf, module, mi);
    if (rv != NGX_CONF_OK) {
        goto failed;
    }
}


/* create location trees */

for (s = 0; s < cmcf->servers.nelts; s++) {

    clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];

    if (ngx_http_init_locations(cf, cscfp[s], clcf) != NGX_OK) {
        return NGX_CONF_ERROR;
    }

    if (ngx_http_init_static_location_trees(cf, clcf) != NGX_OK) {
        return NGX_CONF_ERROR;
    }
}


if (ngx_http_init_phases(cf, cmcf) != NGX_OK) {
    return NGX_CONF_ERROR;
}

if (ngx_http_init_headers_in_hash(cf, cmcf) != NGX_OK) {
    return NGX_CONF_ERROR;
}


for (m = 0; cf->cycle->modules[m]; m++) {
    if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
        continue;
    }

    module = cf->cycle->modules[m]->ctx;

    if (module->postconfiguration) {
        if (module->postconfiguration(cf) != NGX_OK) {
            return NGX_CONF_ERROR;
        }
    }
}

if (ngx_http_variables_init_vars(cf) != NGX_OK) {
    return NGX_CONF_ERROR;
}

/*
 * http{}'s cf->ctx was needed while the configuration merging
 * and in postconfiguration process
 */

*cf = pcf;


if (ngx_http_init_phase_handlers(cf, cmcf) != NGX_OK) {
    return NGX_CONF_ERROR;
}


/* optimize the lists of ports, addresses and server names */

if (ngx_http_optimize_servers(cf, cmcf, cmcf->ports) != NGX_OK) {
    return NGX_CONF_ERROR;
}

return NGX_CONF_OK;

failed:

*cf = pcf;

return rv;

}
ngx_http_block函数会调用ngx_http_init_phase_handlers:

static ngx_int_t
ngx_http_init_phase_handlers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
{
ngx_int_t j;
ngx_uint_t i, n;
ngx_uint_t find_config_index, use_rewrite, use_access;
ngx_http_handler_pt *h;
ngx_http_phase_handler_t *ph;
ngx_http_phase_handler_pt checker;

cmcf->phase_engine.server_rewrite_index = (ngx_uint_t) -1;
cmcf->phase_engine.location_rewrite_index = (ngx_uint_t) -1;
find_config_index = 0;
use_rewrite = cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers.nelts ? 1 : 0;
use_access = cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers.nelts ? 1 : 0;

n = use_rewrite + use_access + cmcf->try_files + 1 /* find config phase */;

for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
    n += cmcf->phases[i].handlers.nelts;
}

ph = ngx_pcalloc(cf->pool,
                 n * sizeof(ngx_http_phase_handler_t) + sizeof(void *));
if (ph == NULL) {
    return NGX_ERROR;
}

cmcf->phase_engine.handlers = ph;
n = 0;

for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
    h = cmcf->phases[i].handlers.elts;

    switch (i) {

    case NGX_HTTP_SERVER_REWRITE_PHASE:
        if (cmcf->phase_engine.server_rewrite_index == (ngx_uint_t) -1) {
            cmcf->phase_engine.server_rewrite_index = n;
        }
        checker = ngx_http_core_rewrite_phase;

        break;

    case NGX_HTTP_FIND_CONFIG_PHASE:
        find_config_index = n;

        ph->checker = ngx_http_core_find_config_phase;
        n++;
        ph++;

        continue;

    case NGX_HTTP_REWRITE_PHASE:
        if (cmcf->phase_engine.location_rewrite_index == (ngx_uint_t) -1) {
            cmcf->phase_engine.location_rewrite_index = n;
        }
        checker = ngx_http_core_rewrite_phase;

        break;

    case NGX_HTTP_POST_REWRITE_PHASE:
        if (use_rewrite) {
            ph->checker = ngx_http_core_post_rewrite_phase;
            ph->next = find_config_index;
            n++;
            ph++;
        }

        continue;

    case NGX_HTTP_ACCESS_PHASE:
        checker = ngx_http_core_access_phase;
        n++;
        break;

    case NGX_HTTP_POST_ACCESS_PHASE:
        if (use_access) {
            ph->checker = ngx_http_core_post_access_phase;
            ph->next = n;
            ph++;
        }

        continue;

    case NGX_HTTP_TRY_FILES_PHASE:
        if (cmcf->try_files) {
            ph->checker = ngx_http_core_try_files_phase;
            n++;
            ph++;
        }

        continue;

    case NGX_HTTP_CONTENT_PHASE:
        checker = ngx_http_core_content_phase;
        break;

    default:
        checker = ngx_http_core_generic_phase;
    }

    n += cmcf->phases[i].handlers.nelts;

    for (j = cmcf->phases[i].handlers.nelts - 1; j >=0; j--) {
        ph->checker = checker;
        ph->handler = h[j];
        ph->next = n;
        ph++;
    }
}

return NGX_OK;

}

在ngx_http_init_phase_handlers函数中会依次调用cmcf->phases[i]数组中的函数:
for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
n += cmcf->phases[i].handlers.nelts;
}

在我们自己定义的模块中,ngx_http_hello_init方法将ngx_http_hello_log_handler函数挂载到NGX_HTTP_LOG_PHASE日志处理阶段
/**

  • 初始化

  • 将ngx_http_hello_log_handler挂载到NGX_HTTP_LOG_PHASE日志处理阶段
    */
    static ngx_int_t ngx_http_hello_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_LOG_PHASE].handlers);
    if (h == NULL) {
    return NGX_ERROR;
    }

    *h = ngx_http_hello_log_handler;

    return NGX_OK;
    }

你可能感兴趣的:(http,网络协议,网络)