📄 ngx_http_core_module.c
字号:
ngx_http_cleanup_t *ngx_http_cleanup_add(ngx_http_request_t *r, size_t size){ ngx_http_cleanup_t *cln; r = r->main; cln = ngx_palloc(r->pool, sizeof(ngx_http_cleanup_t)); if (cln == NULL) { return NULL; } if (size) { cln->data = ngx_palloc(r->pool, size); if (cln->data == NULL) { return NULL; } } else { cln->data = NULL; } cln->handler = NULL; cln->next = r->cleanup; r->cleanup = cln; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http cleanup add: %p", cln); return cln;}static char *ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy){ char *rv; void *mconf; ngx_uint_t i; ngx_conf_t pcf; ngx_http_module_t *module; ngx_http_conf_ctx_t *ctx, *http_ctx; ngx_http_core_srv_conf_t *cscf, **cscfp; ngx_http_core_loc_conf_t **clcfp; ngx_http_core_main_conf_t *cmcf; ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); if (ctx == NULL) { return NGX_CONF_ERROR; } http_ctx = cf->ctx; ctx->main_conf = http_ctx->main_conf; /* the server{}'s srv_conf */ ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); if (ctx->srv_conf == NULL) { return NGX_CONF_ERROR; } /* the server{}'s loc_conf */ ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); if (ctx->loc_conf == NULL) { return NGX_CONF_ERROR; } for (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->type != NGX_HTTP_MODULE) { continue; } module = ngx_modules[i]->ctx; if (module->create_srv_conf) { mconf = module->create_srv_conf(cf); if (mconf == NULL) { return NGX_CONF_ERROR; } ctx->srv_conf[ngx_modules[i]->ctx_index] = mconf; } if (module->create_loc_conf) { mconf = module->create_loc_conf(cf); if (mconf == NULL) { return NGX_CONF_ERROR; } ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf; } } /* the server configuration context */ cscf = ctx->srv_conf[ngx_http_core_module.ctx_index]; cscf->ctx = ctx; cmcf = ctx->main_conf[ngx_http_core_module.ctx_index]; cscfp = ngx_array_push(&cmcf->servers); if (cscfp == NULL) { return NGX_CONF_ERROR; } *cscfp = cscf; /* parse inside server{} */ pcf = *cf; cf->ctx = ctx; cf->cmd_type = NGX_HTTP_SRV_CONF; rv = ngx_conf_parse(cf, NULL); *cf = pcf; if (rv != NGX_CONF_OK) { return rv; } ngx_sort(cscf->locations.elts, (size_t) cscf->locations.nelts, sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations); clcfp = cscf->locations.elts;#if (NGX_PCRE) cscf->regex_start = cscf->locations.nelts; for (i = 0; i < cscf->locations.nelts; i++) { if (clcfp[i]->regex) { cscf->regex_start = i; break; } }#endif cscf->named_start = cscf->locations.nelts; for (i = 0; i < cscf->locations.nelts; i++) { if (clcfp[i]->named) { cscf->named_start = i; break; } } return rv;}static char *ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy){ char *rv; ngx_uint_t i; ngx_str_t *value; ngx_conf_t save; ngx_http_module_t *module; ngx_http_conf_ctx_t *ctx, *pctx; ngx_http_core_srv_conf_t *cscf; ngx_http_core_loc_conf_t *clcf, *pclcf, **clcfp; ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); if (ctx == NULL) { return NGX_CONF_ERROR; } pctx = cf->ctx; ctx->main_conf = pctx->main_conf; ctx->srv_conf = pctx->srv_conf; ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); if (ctx->loc_conf == NULL) { return NGX_CONF_ERROR; } for (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->type != NGX_HTTP_MODULE) { continue; } module = ngx_modules[i]->ctx; if (module->create_loc_conf) { ctx->loc_conf[ngx_modules[i]->ctx_index] = module->create_loc_conf(cf); if (ctx->loc_conf[ngx_modules[i]->ctx_index] == NULL) { return NGX_CONF_ERROR; } } } clcf = ctx->loc_conf[ngx_http_core_module.ctx_index]; clcf->loc_conf = ctx->loc_conf; value = cf->args->elts; if (cf->args->nelts == 3) { if (value[1].len == 1 && value[1].data[0] == '=') { clcf->name = value[2]; clcf->exact_match = 1; } else if (value[1].len == 2 && value[1].data[0] == '^' && value[1].data[1] == '~') { clcf->name = value[2]; clcf->noregex = 1; } else if ((value[1].len == 1 && value[1].data[0] == '~') || (value[1].len == 2 && value[1].data[0] == '~' && value[1].data[1] == '*')) {#if (NGX_PCRE) ngx_str_t err; u_char errstr[NGX_MAX_CONF_ERRSTR]; err.len = NGX_MAX_CONF_ERRSTR; err.data = errstr; clcf->regex = ngx_regex_compile(&value[2], value[1].len == 2 ? NGX_REGEX_CASELESS: 0, cf->pool, &err); if (clcf->regex == NULL) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data); return NGX_CONF_ERROR; } clcf->name = value[2];#else ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "the using of the regex \"%V\" " "requires PCRE library", &value[2]); return NGX_CONF_ERROR;#endif } else { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid location modifier \"%V\"", &value[1]); return NGX_CONF_ERROR; } } else { clcf->name = value[1]; if (value[1].data[0] == '@') { clcf->named = 1; } } pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index]; if (pclcf->name.len == 0) { cscf = ctx->srv_conf[ngx_http_core_module.ctx_index]; clcfp = ngx_array_push(&cscf->locations); if (clcfp == NULL) { return NGX_CONF_ERROR; } } else {#if 0 clcf->prev_location = pclcf;#endif if (pclcf->exact_match) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "location \"%V\" could not be inside " "the exact location \"%V\"", &clcf->name, &pclcf->name); return NGX_CONF_ERROR; } if (pclcf->named) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "location \"%V\" could not be inside " "the named location \"%V\"", &clcf->name, &pclcf->name); return NGX_CONF_ERROR; }#if (NGX_PCRE) if (clcf->regex == NULL && ngx_strncmp(clcf->name.data, pclcf->name.data, pclcf->name.len) != 0)#else if (ngx_strncmp(clcf->name.data, pclcf->name.data, pclcf->name.len) != 0)#endif { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "location \"%V\" is outside location \"%V\"", &clcf->name, &pclcf->name); return NGX_CONF_ERROR; } if (pclcf->locations == NULL) { pclcf->locations = ngx_array_create(cf->pool, 2, sizeof(void *)); if (pclcf->locations == NULL) { return NGX_CONF_ERROR; } } clcfp = ngx_array_push(pclcf->locations); if (clcfp == NULL) { return NGX_CONF_ERROR; } } *clcfp = clcf; save = *cf; cf->ctx = ctx; cf->cmd_type = NGX_HTTP_LOC_CONF; rv = ngx_conf_parse(cf, NULL); *cf = save; if (rv != NGX_CONF_OK) { return rv; } if (clcf->locations == NULL) { return rv; } ngx_sort(clcf->locations->elts, (size_t) clcf->locations->nelts, sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations);#if (NGX_PCRE) clcf->regex_start = clcf->locations->nelts; clcfp = clcf->locations->elts; for (i = 0; i < clcf->locations->nelts; i++) { if (clcfp[i]->regex) { clcf->regex_start = i; break; } }#endif return rv;}static ngx_int_tngx_http_core_cmp_locations(const void *one, const void *two){ ngx_int_t rc; ngx_http_core_loc_conf_t *first, *second; first = *(ngx_http_core_loc_conf_t **) one; second = *(ngx_http_core_loc_conf_t **) two; if (first->named && !second->named) { /* shift named locations to the end */ return 1; } if (!first->named && second->named) { /* shift named locations to the end */ return -1; } if (first->named && second->named) { return ngx_strcmp(first->name.data, second->name.data); } if (first->noname && !second->noname) { /* shift no named locations to the end */ return 1; } if (!first->noname && second->noname) { /* shift no named locations to the end */ return -1; } if (first->noname || second->noname) { /* do not sort no named locations */ return 0; }#if (NGX_PCRE) if (first->regex && !second->regex) { /* shift the regex matches to the end */ return 1; } if (!first->regex && second->regex) { /* shift the regex matches to the end */ return -1; } if (first->regex || second->regex) { /* do not sort the regex matches */ return 0; }#endif rc = ngx_strcmp(first->name.data, second->name.data); if (rc == 0 && second->exact_match) { /* an exact match must be before the same inclusive one */ return 1; } return rc;}static char *ngx_http_core_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){ ngx_http_core_loc_conf_t *lcf = conf; char *rv; ngx_conf_t save; if (lcf->types == NULL) { lcf->types = ngx_array_create(cf->pool, 64, sizeof(ngx_hash_key_t)); if (lcf->types == NULL) { return NGX_CONF_ERROR; } } save = *cf; cf->handler = ngx_http_core_type; cf->handler_conf = conf; rv = ngx_conf_parse(cf, NULL); *cf = save; return rv;}static char *ngx_http_core_type(ngx_conf_t *cf, ngx_command_t *dummy, void *conf){ ngx_http_core_loc_conf_t *lcf = conf; ngx_str_t *value, *content_type, *old, file; ngx_uint_t i, n; ngx_hash_key_t *type; value = cf->args->elts; if (ngx_strcmp(value[0].data, "include") == 0) { file = value[1]; if (ngx_conf_full_name(cf->cycle, &file, 1) == NGX_ERROR){ return NGX_CONF_ERROR; } ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); return ngx_conf_parse(cf, &file); } content_type = ngx_palloc(cf->pool, sizeof(ngx_str_t)); if (content_type == NULL) { return NGX_CONF_ERROR; } *content_type = value[0]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -