Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_http.h>
0011 
0012 
0013 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
0014 static ngx_int_t ngx_http_init_phases(ngx_conf_t *cf,
0015     ngx_http_core_main_conf_t *cmcf);
0016 static ngx_int_t ngx_http_init_headers_in_hash(ngx_conf_t *cf,
0017     ngx_http_core_main_conf_t *cmcf);
0018 static ngx_int_t ngx_http_init_phase_handlers(ngx_conf_t *cf,
0019     ngx_http_core_main_conf_t *cmcf);
0020 
0021 static ngx_int_t ngx_http_add_addresses(ngx_conf_t *cf,
0022     ngx_http_core_srv_conf_t *cscf, ngx_http_conf_port_t *port,
0023     ngx_http_listen_opt_t *lsopt);
0024 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
0025     ngx_http_core_srv_conf_t *cscf, ngx_http_conf_port_t *port,
0026     ngx_http_listen_opt_t *lsopt);
0027 static ngx_int_t ngx_http_add_server(ngx_conf_t *cf,
0028     ngx_http_core_srv_conf_t *cscf, ngx_http_conf_addr_t *addr);
0029 
0030 static char *ngx_http_merge_servers(ngx_conf_t *cf,
0031     ngx_http_core_main_conf_t *cmcf, ngx_http_module_t *module,
0032     ngx_uint_t ctx_index);
0033 static char *ngx_http_merge_locations(ngx_conf_t *cf,
0034     ngx_queue_t *locations, void **loc_conf, ngx_http_module_t *module,
0035     ngx_uint_t ctx_index);
0036 static ngx_int_t ngx_http_init_locations(ngx_conf_t *cf,
0037     ngx_http_core_srv_conf_t *cscf, ngx_http_core_loc_conf_t *pclcf);
0038 static ngx_int_t ngx_http_init_static_location_trees(ngx_conf_t *cf,
0039     ngx_http_core_loc_conf_t *pclcf);
0040 static ngx_int_t ngx_http_cmp_locations(const ngx_queue_t *one,
0041     const ngx_queue_t *two);
0042 static ngx_int_t ngx_http_join_exact_locations(ngx_conf_t *cf,
0043     ngx_queue_t *locations);
0044 static void ngx_http_create_locations_list(ngx_queue_t *locations,
0045     ngx_queue_t *q);
0046 static ngx_http_location_tree_node_t *
0047     ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations,
0048     size_t prefix);
0049 
0050 static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf,
0051     ngx_http_core_main_conf_t *cmcf, ngx_array_t *ports);
0052 static ngx_int_t ngx_http_server_names(ngx_conf_t *cf,
0053     ngx_http_core_main_conf_t *cmcf, ngx_http_conf_addr_t *addr);
0054 static ngx_int_t ngx_http_cmp_conf_addrs(const void *one, const void *two);
0055 static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one,
0056     const void *two);
0057 
0058 static ngx_int_t ngx_http_init_listening(ngx_conf_t *cf,
0059     ngx_http_conf_port_t *port);
0060 static ngx_listening_t *ngx_http_add_listening(ngx_conf_t *cf,
0061     ngx_http_conf_addr_t *addr);
0062 static ngx_int_t ngx_http_add_addrs(ngx_conf_t *cf, ngx_http_port_t *hport,
0063     ngx_http_conf_addr_t *addr);
0064 #if (NGX_HAVE_INET6)
0065 static ngx_int_t ngx_http_add_addrs6(ngx_conf_t *cf, ngx_http_port_t *hport,
0066     ngx_http_conf_addr_t *addr);
0067 #endif
0068 
0069 ngx_uint_t   ngx_http_max_module;
0070 
0071 
0072 ngx_http_output_header_filter_pt  ngx_http_top_header_filter;
0073 ngx_http_output_body_filter_pt    ngx_http_top_body_filter;
0074 ngx_http_request_body_filter_pt   ngx_http_top_request_body_filter;
0075 
0076 
0077 ngx_str_t  ngx_http_html_default_types[] = {
0078     ngx_string("text/html"),
0079     ngx_null_string
0080 };
0081 
0082 
0083 static ngx_command_t  ngx_http_commands[] = {
0084 
0085     { ngx_string("http"),
0086       NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
0087       ngx_http_block,
0088       0,
0089       0,
0090       NULL },
0091 
0092       ngx_null_command
0093 };
0094 
0095 
0096 static ngx_core_module_t  ngx_http_module_ctx = {
0097     ngx_string("http"),
0098     NULL,
0099     NULL
0100 };
0101 
0102 
0103 ngx_module_t  ngx_http_module = {
0104     NGX_MODULE_V1,
0105     &ngx_http_module_ctx,                  /* module context */
0106     ngx_http_commands,                     /* module directives */
0107     NGX_CORE_MODULE,                       /* module type */
0108     NULL,                                  /* init master */
0109     NULL,                                  /* init module */
0110     NULL,                                  /* init process */
0111     NULL,                                  /* init thread */
0112     NULL,                                  /* exit thread */
0113     NULL,                                  /* exit process */
0114     NULL,                                  /* exit master */
0115     NGX_MODULE_V1_PADDING
0116 };
0117 
0118 
0119 static char *
0120 ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0121 {
0122     char                        *rv;
0123     ngx_uint_t                   mi, m, s;
0124     ngx_conf_t                   pcf;
0125     ngx_http_module_t           *module;
0126     ngx_http_conf_ctx_t         *ctx;
0127     ngx_http_core_loc_conf_t    *clcf;
0128     ngx_http_core_srv_conf_t   **cscfp;
0129     ngx_http_core_main_conf_t   *cmcf;
0130 
0131     if (*(ngx_http_conf_ctx_t **) conf) {
0132         return "is duplicate";
0133     }
0134 
0135     /* the main http context */
0136 
0137     ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
0138     if (ctx == NULL) {
0139         return NGX_CONF_ERROR;
0140     }
0141 
0142     *(ngx_http_conf_ctx_t **) conf = ctx;
0143 
0144 
0145     /* count the number of the http modules and set up their indices */
0146 
0147     ngx_http_max_module = ngx_count_modules(cf->cycle, NGX_HTTP_MODULE);
0148 
0149 
0150     /* the http main_conf context, it is the same in the all http contexts */
0151 
0152     ctx->main_conf = ngx_pcalloc(cf->pool,
0153                                  sizeof(void *) * ngx_http_max_module);
0154     if (ctx->main_conf == NULL) {
0155         return NGX_CONF_ERROR;
0156     }
0157 
0158 
0159     /*
0160      * the http null srv_conf context, it is used to merge
0161      * the server{}s' srv_conf's
0162      */
0163 
0164     ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
0165     if (ctx->srv_conf == NULL) {
0166         return NGX_CONF_ERROR;
0167     }
0168 
0169 
0170     /*
0171      * the http null loc_conf context, it is used to merge
0172      * the server{}s' loc_conf's
0173      */
0174 
0175     ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
0176     if (ctx->loc_conf == NULL) {
0177         return NGX_CONF_ERROR;
0178     }
0179 
0180 
0181     /*
0182      * create the main_conf's, the null srv_conf's, and the null loc_conf's
0183      * of the all http modules
0184      */
0185 
0186     for (m = 0; cf->cycle->modules[m]; m++) {
0187         if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
0188             continue;
0189         }
0190 
0191         module = cf->cycle->modules[m]->ctx;
0192         mi = cf->cycle->modules[m]->ctx_index;
0193 
0194         if (module->create_main_conf) {
0195             ctx->main_conf[mi] = module->create_main_conf(cf);
0196             if (ctx->main_conf[mi] == NULL) {
0197                 return NGX_CONF_ERROR;
0198             }
0199         }
0200 
0201         if (module->create_srv_conf) {
0202             ctx->srv_conf[mi] = module->create_srv_conf(cf);
0203             if (ctx->srv_conf[mi] == NULL) {
0204                 return NGX_CONF_ERROR;
0205             }
0206         }
0207 
0208         if (module->create_loc_conf) {
0209             ctx->loc_conf[mi] = module->create_loc_conf(cf);
0210             if (ctx->loc_conf[mi] == NULL) {
0211                 return NGX_CONF_ERROR;
0212             }
0213         }
0214     }
0215 
0216     pcf = *cf;
0217     cf->ctx = ctx;
0218 
0219     for (m = 0; cf->cycle->modules[m]; m++) {
0220         if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
0221             continue;
0222         }
0223 
0224         module = cf->cycle->modules[m]->ctx;
0225 
0226         if (module->preconfiguration) {
0227             if (module->preconfiguration(cf) != NGX_OK) {
0228                 return NGX_CONF_ERROR;
0229             }
0230         }
0231     }
0232 
0233     /* parse inside the http{} block */
0234 
0235     cf->module_type = NGX_HTTP_MODULE;
0236     cf->cmd_type = NGX_HTTP_MAIN_CONF;
0237     rv = ngx_conf_parse(cf, NULL);
0238 
0239     if (rv != NGX_CONF_OK) {
0240         goto failed;
0241     }
0242 
0243     /*
0244      * init http{} main_conf's, merge the server{}s' srv_conf's
0245      * and its location{}s' loc_conf's
0246      */
0247 
0248     cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
0249     cscfp = cmcf->servers.elts;
0250 
0251     for (m = 0; cf->cycle->modules[m]; m++) {
0252         if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
0253             continue;
0254         }
0255 
0256         module = cf->cycle->modules[m]->ctx;
0257         mi = cf->cycle->modules[m]->ctx_index;
0258 
0259         /* init http{} main_conf's */
0260 
0261         if (module->init_main_conf) {
0262             rv = module->init_main_conf(cf, ctx->main_conf[mi]);
0263             if (rv != NGX_CONF_OK) {
0264                 goto failed;
0265             }
0266         }
0267 
0268         rv = ngx_http_merge_servers(cf, cmcf, module, mi);
0269         if (rv != NGX_CONF_OK) {
0270             goto failed;
0271         }
0272     }
0273 
0274 
0275     /* create location trees */
0276 
0277     for (s = 0; s < cmcf->servers.nelts; s++) {
0278 
0279         clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];
0280 
0281         if (ngx_http_init_locations(cf, cscfp[s], clcf) != NGX_OK) {
0282             return NGX_CONF_ERROR;
0283         }
0284 
0285         if (ngx_http_init_static_location_trees(cf, clcf) != NGX_OK) {
0286             return NGX_CONF_ERROR;
0287         }
0288     }
0289 
0290 
0291     if (ngx_http_init_phases(cf, cmcf) != NGX_OK) {
0292         return NGX_CONF_ERROR;
0293     }
0294 
0295     if (ngx_http_init_headers_in_hash(cf, cmcf) != NGX_OK) {
0296         return NGX_CONF_ERROR;
0297     }
0298 
0299 
0300     for (m = 0; cf->cycle->modules[m]; m++) {
0301         if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
0302             continue;
0303         }
0304 
0305         module = cf->cycle->modules[m]->ctx;
0306 
0307         if (module->postconfiguration) {
0308             if (module->postconfiguration(cf) != NGX_OK) {
0309                 return NGX_CONF_ERROR;
0310             }
0311         }
0312     }
0313 
0314     if (ngx_http_variables_init_vars(cf) != NGX_OK) {
0315         return NGX_CONF_ERROR;
0316     }
0317 
0318     /*
0319      * http{}'s cf->ctx was needed while the configuration merging
0320      * and in postconfiguration process
0321      */
0322 
0323     *cf = pcf;
0324 
0325 
0326     if (ngx_http_init_phase_handlers(cf, cmcf) != NGX_OK) {
0327         return NGX_CONF_ERROR;
0328     }
0329 
0330 
0331     /* optimize the lists of ports, addresses and server names */
0332 
0333     if (ngx_http_optimize_servers(cf, cmcf, cmcf->ports) != NGX_OK) {
0334         return NGX_CONF_ERROR;
0335     }
0336 
0337     return NGX_CONF_OK;
0338 
0339 failed:
0340 
0341     *cf = pcf;
0342 
0343     return rv;
0344 }
0345 
0346 
0347 static ngx_int_t
0348 ngx_http_init_phases(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
0349 {
0350     if (ngx_array_init(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers,
0351                        cf->pool, 1, sizeof(ngx_http_handler_pt))
0352         != NGX_OK)
0353     {
0354         return NGX_ERROR;
0355     }
0356 
0357     if (ngx_array_init(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers,
0358                        cf->pool, 1, sizeof(ngx_http_handler_pt))
0359         != NGX_OK)
0360     {
0361         return NGX_ERROR;
0362     }
0363 
0364     if (ngx_array_init(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers,
0365                        cf->pool, 1, sizeof(ngx_http_handler_pt))
0366         != NGX_OK)
0367     {
0368         return NGX_ERROR;
0369     }
0370 
0371     if (ngx_array_init(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers,
0372                        cf->pool, 1, sizeof(ngx_http_handler_pt))
0373         != NGX_OK)
0374     {
0375         return NGX_ERROR;
0376     }
0377 
0378     if (ngx_array_init(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers,
0379                        cf->pool, 2, sizeof(ngx_http_handler_pt))
0380         != NGX_OK)
0381     {
0382         return NGX_ERROR;
0383     }
0384 
0385     if (ngx_array_init(&cmcf->phases[NGX_HTTP_PRECONTENT_PHASE].handlers,
0386                        cf->pool, 2, sizeof(ngx_http_handler_pt))
0387         != NGX_OK)
0388     {
0389         return NGX_ERROR;
0390     }
0391 
0392     if (ngx_array_init(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers,
0393                        cf->pool, 4, sizeof(ngx_http_handler_pt))
0394         != NGX_OK)
0395     {
0396         return NGX_ERROR;
0397     }
0398 
0399     if (ngx_array_init(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers,
0400                        cf->pool, 1, sizeof(ngx_http_handler_pt))
0401         != NGX_OK)
0402     {
0403         return NGX_ERROR;
0404     }
0405 
0406     return NGX_OK;
0407 }
0408 
0409 
0410 static ngx_int_t
0411 ngx_http_init_headers_in_hash(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
0412 {
0413     ngx_array_t         headers_in;
0414     ngx_hash_key_t     *hk;
0415     ngx_hash_init_t     hash;
0416     ngx_http_header_t  *header;
0417 
0418     if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t))
0419         != NGX_OK)
0420     {
0421         return NGX_ERROR;
0422     }
0423 
0424     for (header = ngx_http_headers_in; header->name.len; header++) {
0425         hk = ngx_array_push(&headers_in);
0426         if (hk == NULL) {
0427             return NGX_ERROR;
0428         }
0429 
0430         hk->key = header->name;
0431         hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len);
0432         hk->value = header;
0433     }
0434 
0435     hash.hash = &cmcf->headers_in_hash;
0436     hash.key = ngx_hash_key_lc;
0437     hash.max_size = 512;
0438     hash.bucket_size = ngx_align(64, ngx_cacheline_size);
0439     hash.name = "headers_in_hash";
0440     hash.pool = cf->pool;
0441     hash.temp_pool = NULL;
0442 
0443     if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) {
0444         return NGX_ERROR;
0445     }
0446 
0447     return NGX_OK;
0448 }
0449 
0450 
0451 static ngx_int_t
0452 ngx_http_init_phase_handlers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
0453 {
0454     ngx_int_t                   j;
0455     ngx_uint_t                  i, n;
0456     ngx_uint_t                  find_config_index, use_rewrite, use_access;
0457     ngx_http_handler_pt        *h;
0458     ngx_http_phase_handler_t   *ph;
0459     ngx_http_phase_handler_pt   checker;
0460 
0461     cmcf->phase_engine.server_rewrite_index = (ngx_uint_t) -1;
0462     cmcf->phase_engine.location_rewrite_index = (ngx_uint_t) -1;
0463     find_config_index = 0;
0464     use_rewrite = cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers.nelts ? 1 : 0;
0465     use_access = cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers.nelts ? 1 : 0;
0466 
0467     n = 1                  /* find config phase */
0468         + use_rewrite      /* post rewrite phase */
0469         + use_access;      /* post access phase */
0470 
0471     for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
0472         n += cmcf->phases[i].handlers.nelts;
0473     }
0474 
0475     ph = ngx_pcalloc(cf->pool,
0476                      n * sizeof(ngx_http_phase_handler_t) + sizeof(void *));
0477     if (ph == NULL) {
0478         return NGX_ERROR;
0479     }
0480 
0481     cmcf->phase_engine.handlers = ph;
0482     n = 0;
0483 
0484     for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
0485         h = cmcf->phases[i].handlers.elts;
0486 
0487         switch (i) {
0488 
0489         case NGX_HTTP_SERVER_REWRITE_PHASE:
0490             if (cmcf->phase_engine.server_rewrite_index == (ngx_uint_t) -1) {
0491                 cmcf->phase_engine.server_rewrite_index = n;
0492             }
0493             checker = ngx_http_core_rewrite_phase;
0494 
0495             break;
0496 
0497         case NGX_HTTP_FIND_CONFIG_PHASE:
0498             find_config_index = n;
0499 
0500             ph->checker = ngx_http_core_find_config_phase;
0501             n++;
0502             ph++;
0503 
0504             continue;
0505 
0506         case NGX_HTTP_REWRITE_PHASE:
0507             if (cmcf->phase_engine.location_rewrite_index == (ngx_uint_t) -1) {
0508                 cmcf->phase_engine.location_rewrite_index = n;
0509             }
0510             checker = ngx_http_core_rewrite_phase;
0511 
0512             break;
0513 
0514         case NGX_HTTP_POST_REWRITE_PHASE:
0515             if (use_rewrite) {
0516                 ph->checker = ngx_http_core_post_rewrite_phase;
0517                 ph->next = find_config_index;
0518                 n++;
0519                 ph++;
0520             }
0521 
0522             continue;
0523 
0524         case NGX_HTTP_ACCESS_PHASE:
0525             checker = ngx_http_core_access_phase;
0526             n++;
0527             break;
0528 
0529         case NGX_HTTP_POST_ACCESS_PHASE:
0530             if (use_access) {
0531                 ph->checker = ngx_http_core_post_access_phase;
0532                 ph->next = n;
0533                 ph++;
0534             }
0535 
0536             continue;
0537 
0538         case NGX_HTTP_CONTENT_PHASE:
0539             checker = ngx_http_core_content_phase;
0540             break;
0541 
0542         default:
0543             checker = ngx_http_core_generic_phase;
0544         }
0545 
0546         n += cmcf->phases[i].handlers.nelts;
0547 
0548         for (j = cmcf->phases[i].handlers.nelts - 1; j >= 0; j--) {
0549             ph->checker = checker;
0550             ph->handler = h[j];
0551             ph->next = n;
0552             ph++;
0553         }
0554     }
0555 
0556     return NGX_OK;
0557 }
0558 
0559 
0560 static char *
0561 ngx_http_merge_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
0562     ngx_http_module_t *module, ngx_uint_t ctx_index)
0563 {
0564     char                        *rv;
0565     ngx_uint_t                   s;
0566     ngx_http_conf_ctx_t         *ctx, saved;
0567     ngx_http_core_loc_conf_t    *clcf;
0568     ngx_http_core_srv_conf_t   **cscfp;
0569 
0570     cscfp = cmcf->servers.elts;
0571     ctx = (ngx_http_conf_ctx_t *) cf->ctx;
0572     saved = *ctx;
0573     rv = NGX_CONF_OK;
0574 
0575     for (s = 0; s < cmcf->servers.nelts; s++) {
0576 
0577         /* merge the server{}s' srv_conf's */
0578 
0579         ctx->srv_conf = cscfp[s]->ctx->srv_conf;
0580 
0581         if (module->merge_srv_conf) {
0582             rv = module->merge_srv_conf(cf, saved.srv_conf[ctx_index],
0583                                         cscfp[s]->ctx->srv_conf[ctx_index]);
0584             if (rv != NGX_CONF_OK) {
0585                 goto failed;
0586             }
0587         }
0588 
0589         if (module->merge_loc_conf) {
0590 
0591             /* merge the server{}'s loc_conf */
0592 
0593             ctx->loc_conf = cscfp[s]->ctx->loc_conf;
0594 
0595             rv = module->merge_loc_conf(cf, saved.loc_conf[ctx_index],
0596                                         cscfp[s]->ctx->loc_conf[ctx_index]);
0597             if (rv != NGX_CONF_OK) {
0598                 goto failed;
0599             }
0600 
0601             /* merge the locations{}' loc_conf's */
0602 
0603             clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];
0604 
0605             rv = ngx_http_merge_locations(cf, clcf->locations,
0606                                           cscfp[s]->ctx->loc_conf,
0607                                           module, ctx_index);
0608             if (rv != NGX_CONF_OK) {
0609                 goto failed;
0610             }
0611         }
0612     }
0613 
0614 failed:
0615 
0616     *ctx = saved;
0617 
0618     return rv;
0619 }
0620 
0621 
0622 static char *
0623 ngx_http_merge_locations(ngx_conf_t *cf, ngx_queue_t *locations,
0624     void **loc_conf, ngx_http_module_t *module, ngx_uint_t ctx_index)
0625 {
0626     char                       *rv;
0627     ngx_queue_t                *q;
0628     ngx_http_conf_ctx_t        *ctx, saved;
0629     ngx_http_core_loc_conf_t   *clcf;
0630     ngx_http_location_queue_t  *lq;
0631 
0632     if (locations == NULL) {
0633         return NGX_CONF_OK;
0634     }
0635 
0636     ctx = (ngx_http_conf_ctx_t *) cf->ctx;
0637     saved = *ctx;
0638 
0639     for (q = ngx_queue_head(locations);
0640          q != ngx_queue_sentinel(locations);
0641          q = ngx_queue_next(q))
0642     {
0643         lq = (ngx_http_location_queue_t *) q;
0644 
0645         clcf = lq->exact ? lq->exact : lq->inclusive;
0646         ctx->loc_conf = clcf->loc_conf;
0647 
0648         rv = module->merge_loc_conf(cf, loc_conf[ctx_index],
0649                                     clcf->loc_conf[ctx_index]);
0650         if (rv != NGX_CONF_OK) {
0651             return rv;
0652         }
0653 
0654         rv = ngx_http_merge_locations(cf, clcf->locations, clcf->loc_conf,
0655                                       module, ctx_index);
0656         if (rv != NGX_CONF_OK) {
0657             return rv;
0658         }
0659     }
0660 
0661     *ctx = saved;
0662 
0663     return NGX_CONF_OK;
0664 }
0665 
0666 
0667 static ngx_int_t
0668 ngx_http_init_locations(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
0669     ngx_http_core_loc_conf_t *pclcf)
0670 {
0671     ngx_uint_t                   n;
0672     ngx_queue_t                 *q, *locations, *named, tail;
0673     ngx_http_core_loc_conf_t    *clcf;
0674     ngx_http_location_queue_t   *lq;
0675     ngx_http_core_loc_conf_t   **clcfp;
0676 #if (NGX_PCRE)
0677     ngx_uint_t                   r;
0678     ngx_queue_t                 *regex;
0679 #endif
0680 
0681     locations = pclcf->locations;
0682 
0683     if (locations == NULL) {
0684         return NGX_OK;
0685     }
0686 
0687     ngx_queue_sort(locations, ngx_http_cmp_locations);
0688 
0689     named = NULL;
0690     n = 0;
0691 #if (NGX_PCRE)
0692     regex = NULL;
0693     r = 0;
0694 #endif
0695 
0696     for (q = ngx_queue_head(locations);
0697          q != ngx_queue_sentinel(locations);
0698          q = ngx_queue_next(q))
0699     {
0700         lq = (ngx_http_location_queue_t *) q;
0701 
0702         clcf = lq->exact ? lq->exact : lq->inclusive;
0703 
0704         if (ngx_http_init_locations(cf, NULL, clcf) != NGX_OK) {
0705             return NGX_ERROR;
0706         }
0707 
0708 #if (NGX_PCRE)
0709 
0710         if (clcf->regex) {
0711             r++;
0712 
0713             if (regex == NULL) {
0714                 regex = q;
0715             }
0716 
0717             continue;
0718         }
0719 
0720 #endif
0721 
0722         if (clcf->named) {
0723             n++;
0724 
0725             if (named == NULL) {
0726                 named = q;
0727             }
0728 
0729             continue;
0730         }
0731 
0732         if (clcf->noname) {
0733             break;
0734         }
0735     }
0736 
0737     if (q != ngx_queue_sentinel(locations)) {
0738         ngx_queue_split(locations, q, &tail);
0739     }
0740 
0741     if (named) {
0742         clcfp = ngx_palloc(cf->pool,
0743                            (n + 1) * sizeof(ngx_http_core_loc_conf_t *));
0744         if (clcfp == NULL) {
0745             return NGX_ERROR;
0746         }
0747 
0748         cscf->named_locations = clcfp;
0749 
0750         for (q = named;
0751              q != ngx_queue_sentinel(locations);
0752              q = ngx_queue_next(q))
0753         {
0754             lq = (ngx_http_location_queue_t *) q;
0755 
0756             *(clcfp++) = lq->exact;
0757         }
0758 
0759         *clcfp = NULL;
0760 
0761         ngx_queue_split(locations, named, &tail);
0762     }
0763 
0764 #if (NGX_PCRE)
0765 
0766     if (regex) {
0767 
0768         clcfp = ngx_palloc(cf->pool,
0769                            (r + 1) * sizeof(ngx_http_core_loc_conf_t *));
0770         if (clcfp == NULL) {
0771             return NGX_ERROR;
0772         }
0773 
0774         pclcf->regex_locations = clcfp;
0775 
0776         for (q = regex;
0777              q != ngx_queue_sentinel(locations);
0778              q = ngx_queue_next(q))
0779         {
0780             lq = (ngx_http_location_queue_t *) q;
0781 
0782             *(clcfp++) = lq->exact;
0783         }
0784 
0785         *clcfp = NULL;
0786 
0787         ngx_queue_split(locations, regex, &tail);
0788     }
0789 
0790 #endif
0791 
0792     return NGX_OK;
0793 }
0794 
0795 
0796 static ngx_int_t
0797 ngx_http_init_static_location_trees(ngx_conf_t *cf,
0798     ngx_http_core_loc_conf_t *pclcf)
0799 {
0800     ngx_queue_t                *q, *locations;
0801     ngx_http_core_loc_conf_t   *clcf;
0802     ngx_http_location_queue_t  *lq;
0803 
0804     locations = pclcf->locations;
0805 
0806     if (locations == NULL) {
0807         return NGX_OK;
0808     }
0809 
0810     if (ngx_queue_empty(locations)) {
0811         return NGX_OK;
0812     }
0813 
0814     for (q = ngx_queue_head(locations);
0815          q != ngx_queue_sentinel(locations);
0816          q = ngx_queue_next(q))
0817     {
0818         lq = (ngx_http_location_queue_t *) q;
0819 
0820         clcf = lq->exact ? lq->exact : lq->inclusive;
0821 
0822         if (ngx_http_init_static_location_trees(cf, clcf) != NGX_OK) {
0823             return NGX_ERROR;
0824         }
0825     }
0826 
0827     if (ngx_http_join_exact_locations(cf, locations) != NGX_OK) {
0828         return NGX_ERROR;
0829     }
0830 
0831     ngx_http_create_locations_list(locations, ngx_queue_head(locations));
0832 
0833     pclcf->static_locations = ngx_http_create_locations_tree(cf, locations, 0);
0834     if (pclcf->static_locations == NULL) {
0835         return NGX_ERROR;
0836     }
0837 
0838     return NGX_OK;
0839 }
0840 
0841 
0842 ngx_int_t
0843 ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
0844     ngx_http_core_loc_conf_t *clcf)
0845 {
0846     ngx_http_location_queue_t  *lq;
0847 
0848     if (*locations == NULL) {
0849         *locations = ngx_palloc(cf->temp_pool,
0850                                 sizeof(ngx_http_location_queue_t));
0851         if (*locations == NULL) {
0852             return NGX_ERROR;
0853         }
0854 
0855         ngx_queue_init(*locations);
0856     }
0857 
0858     lq = ngx_palloc(cf->temp_pool, sizeof(ngx_http_location_queue_t));
0859     if (lq == NULL) {
0860         return NGX_ERROR;
0861     }
0862 
0863     if (clcf->exact_match
0864 #if (NGX_PCRE)
0865         || clcf->regex
0866 #endif
0867         || clcf->named || clcf->noname)
0868     {
0869         lq->exact = clcf;
0870         lq->inclusive = NULL;
0871 
0872     } else {
0873         lq->exact = NULL;
0874         lq->inclusive = clcf;
0875     }
0876 
0877     lq->name = &clcf->name;
0878     lq->file_name = cf->conf_file->file.name.data;
0879     lq->line = cf->conf_file->line;
0880 
0881     ngx_queue_init(&lq->list);
0882 
0883     ngx_queue_insert_tail(*locations, &lq->queue);
0884 
0885     return NGX_OK;
0886 }
0887 
0888 
0889 static ngx_int_t
0890 ngx_http_cmp_locations(const ngx_queue_t *one, const ngx_queue_t *two)
0891 {
0892     ngx_int_t                   rc;
0893     ngx_http_core_loc_conf_t   *first, *second;
0894     ngx_http_location_queue_t  *lq1, *lq2;
0895 
0896     lq1 = (ngx_http_location_queue_t *) one;
0897     lq2 = (ngx_http_location_queue_t *) two;
0898 
0899     first = lq1->exact ? lq1->exact : lq1->inclusive;
0900     second = lq2->exact ? lq2->exact : lq2->inclusive;
0901 
0902     if (first->noname && !second->noname) {
0903         /* shift no named locations to the end */
0904         return 1;
0905     }
0906 
0907     if (!first->noname && second->noname) {
0908         /* shift no named locations to the end */
0909         return -1;
0910     }
0911 
0912     if (first->noname || second->noname) {
0913         /* do not sort no named locations */
0914         return 0;
0915     }
0916 
0917     if (first->named && !second->named) {
0918         /* shift named locations to the end */
0919         return 1;
0920     }
0921 
0922     if (!first->named && second->named) {
0923         /* shift named locations to the end */
0924         return -1;
0925     }
0926 
0927     if (first->named && second->named) {
0928         return ngx_strcmp(first->name.data, second->name.data);
0929     }
0930 
0931 #if (NGX_PCRE)
0932 
0933     if (first->regex && !second->regex) {
0934         /* shift the regex matches to the end */
0935         return 1;
0936     }
0937 
0938     if (!first->regex && second->regex) {
0939         /* shift the regex matches to the end */
0940         return -1;
0941     }
0942 
0943     if (first->regex || second->regex) {
0944         /* do not sort the regex matches */
0945         return 0;
0946     }
0947 
0948 #endif
0949 
0950     rc = ngx_filename_cmp(first->name.data, second->name.data,
0951                           ngx_min(first->name.len, second->name.len) + 1);
0952 
0953     if (rc == 0 && !first->exact_match && second->exact_match) {
0954         /* an exact match must be before the same inclusive one */
0955         return 1;
0956     }
0957 
0958     return rc;
0959 }
0960 
0961 
0962 static ngx_int_t
0963 ngx_http_join_exact_locations(ngx_conf_t *cf, ngx_queue_t *locations)
0964 {
0965     ngx_queue_t                *q, *x;
0966     ngx_http_location_queue_t  *lq, *lx;
0967 
0968     q = ngx_queue_head(locations);
0969 
0970     while (q != ngx_queue_last(locations)) {
0971 
0972         x = ngx_queue_next(q);
0973 
0974         lq = (ngx_http_location_queue_t *) q;
0975         lx = (ngx_http_location_queue_t *) x;
0976 
0977         if (lq->name->len == lx->name->len
0978             && ngx_filename_cmp(lq->name->data, lx->name->data, lx->name->len)
0979                == 0)
0980         {
0981             if ((lq->exact && lx->exact) || (lq->inclusive && lx->inclusive)) {
0982                 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
0983                               "duplicate location \"%V\" in %s:%ui",
0984                               lx->name, lx->file_name, lx->line);
0985 
0986                 return NGX_ERROR;
0987             }
0988 
0989             lq->inclusive = lx->inclusive;
0990 
0991             ngx_queue_remove(x);
0992 
0993             continue;
0994         }
0995 
0996         q = ngx_queue_next(q);
0997     }
0998 
0999     return NGX_OK;
1000 }
1001 
1002 
1003 static void
1004 ngx_http_create_locations_list(ngx_queue_t *locations, ngx_queue_t *q)
1005 {
1006     u_char                     *name;
1007     size_t                      len;
1008     ngx_queue_t                *x, tail;
1009     ngx_http_location_queue_t  *lq, *lx;
1010 
1011     if (q == ngx_queue_last(locations)) {
1012         return;
1013     }
1014 
1015     lq = (ngx_http_location_queue_t *) q;
1016 
1017     if (lq->inclusive == NULL) {
1018         ngx_http_create_locations_list(locations, ngx_queue_next(q));
1019         return;
1020     }
1021 
1022     len = lq->name->len;
1023     name = lq->name->data;
1024 
1025     for (x = ngx_queue_next(q);
1026          x != ngx_queue_sentinel(locations);
1027          x = ngx_queue_next(x))
1028     {
1029         lx = (ngx_http_location_queue_t *) x;
1030 
1031         if (len > lx->name->len
1032             || ngx_filename_cmp(name, lx->name->data, len) != 0)
1033         {
1034             break;
1035         }
1036     }
1037 
1038     q = ngx_queue_next(q);
1039 
1040     if (q == x) {
1041         ngx_http_create_locations_list(locations, x);
1042         return;
1043     }
1044 
1045     ngx_queue_split(locations, q, &tail);
1046     ngx_queue_add(&lq->list, &tail);
1047 
1048     if (x == ngx_queue_sentinel(locations)) {
1049         ngx_http_create_locations_list(&lq->list, ngx_queue_head(&lq->list));
1050         return;
1051     }
1052 
1053     ngx_queue_split(&lq->list, x, &tail);
1054     ngx_queue_add(locations, &tail);
1055 
1056     ngx_http_create_locations_list(&lq->list, ngx_queue_head(&lq->list));
1057 
1058     ngx_http_create_locations_list(locations, x);
1059 }
1060 
1061 
1062 /*
1063  * to keep cache locality for left leaf nodes, allocate nodes in following
1064  * order: node, left subtree, right subtree, inclusive subtree
1065  */
1066 
1067 static ngx_http_location_tree_node_t *
1068 ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations,
1069     size_t prefix)
1070 {
1071     size_t                          len;
1072     ngx_queue_t                    *q, tail;
1073     ngx_http_location_queue_t      *lq;
1074     ngx_http_location_tree_node_t  *node;
1075 
1076     q = ngx_queue_middle(locations);
1077 
1078     lq = (ngx_http_location_queue_t *) q;
1079     len = lq->name->len - prefix;
1080 
1081     node = ngx_palloc(cf->pool,
1082                       offsetof(ngx_http_location_tree_node_t, name) + len);
1083     if (node == NULL) {
1084         return NULL;
1085     }
1086 
1087     node->left = NULL;
1088     node->right = NULL;
1089     node->tree = NULL;
1090     node->exact = lq->exact;
1091     node->inclusive = lq->inclusive;
1092 
1093     node->auto_redirect = (u_char) ((lq->exact && lq->exact->auto_redirect)
1094                            || (lq->inclusive && lq->inclusive->auto_redirect));
1095 
1096     node->len = (u_char) len;
1097     ngx_memcpy(node->name, &lq->name->data[prefix], len);
1098 
1099     ngx_queue_split(locations, q, &tail);
1100 
1101     if (ngx_queue_empty(locations)) {
1102         /*
1103          * ngx_queue_split() insures that if left part is empty,
1104          * then right one is empty too
1105          */
1106         goto inclusive;
1107     }
1108 
1109     node->left = ngx_http_create_locations_tree(cf, locations, prefix);
1110     if (node->left == NULL) {
1111         return NULL;
1112     }
1113 
1114     ngx_queue_remove(q);
1115 
1116     if (ngx_queue_empty(&tail)) {
1117         goto inclusive;
1118     }
1119 
1120     node->right = ngx_http_create_locations_tree(cf, &tail, prefix);
1121     if (node->right == NULL) {
1122         return NULL;
1123     }
1124 
1125 inclusive:
1126 
1127     if (ngx_queue_empty(&lq->list)) {
1128         return node;
1129     }
1130 
1131     node->tree = ngx_http_create_locations_tree(cf, &lq->list, prefix + len);
1132     if (node->tree == NULL) {
1133         return NULL;
1134     }
1135 
1136     return node;
1137 }
1138 
1139 
1140 ngx_int_t
1141 ngx_http_add_listen(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1142     ngx_http_listen_opt_t *lsopt)
1143 {
1144     in_port_t                   p;
1145     ngx_uint_t                  i;
1146     struct sockaddr            *sa;
1147     ngx_http_conf_port_t       *port;
1148     ngx_http_core_main_conf_t  *cmcf;
1149 
1150     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
1151 
1152     if (cmcf->ports == NULL) {
1153         cmcf->ports = ngx_array_create(cf->temp_pool, 2,
1154                                        sizeof(ngx_http_conf_port_t));
1155         if (cmcf->ports == NULL) {
1156             return NGX_ERROR;
1157         }
1158     }
1159 
1160     sa = &lsopt->sockaddr.sockaddr;
1161     p = ngx_inet_get_port(sa);
1162 
1163     port = cmcf->ports->elts;
1164     for (i = 0; i < cmcf->ports->nelts; i++) {
1165 
1166         if (p != port[i].port || sa->sa_family != port[i].family) {
1167             continue;
1168         }
1169 
1170         /* a port is already in the port list */
1171 
1172         return ngx_http_add_addresses(cf, cscf, &port[i], lsopt);
1173     }
1174 
1175     /* add a port to the port list */
1176 
1177     port = ngx_array_push(cmcf->ports);
1178     if (port == NULL) {
1179         return NGX_ERROR;
1180     }
1181 
1182     port->family = sa->sa_family;
1183     port->port = p;
1184     port->addrs.elts = NULL;
1185 
1186     return ngx_http_add_address(cf, cscf, port, lsopt);
1187 }
1188 
1189 
1190 static ngx_int_t
1191 ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1192     ngx_http_conf_port_t *port, ngx_http_listen_opt_t *lsopt)
1193 {
1194     ngx_uint_t             i, default_server, proxy_protocol;
1195     ngx_http_conf_addr_t  *addr;
1196 #if (NGX_HTTP_SSL)
1197     ngx_uint_t             ssl;
1198 #endif
1199 #if (NGX_HTTP_V2)
1200     ngx_uint_t             http2;
1201 #endif
1202 
1203     /*
1204      * we cannot compare whole sockaddr struct's as kernel
1205      * may fill some fields in inherited sockaddr struct's
1206      */
1207 
1208     addr = port->addrs.elts;
1209 
1210     for (i = 0; i < port->addrs.nelts; i++) {
1211 
1212         if (ngx_cmp_sockaddr(&lsopt->sockaddr.sockaddr, lsopt->socklen,
1213                              &addr[i].opt.sockaddr.sockaddr,
1214                              addr[i].opt.socklen, 0)
1215             != NGX_OK)
1216         {
1217             continue;
1218         }
1219 
1220         /* the address is already in the address list */
1221 
1222         if (ngx_http_add_server(cf, cscf, &addr[i]) != NGX_OK) {
1223             return NGX_ERROR;
1224         }
1225 
1226         /* preserve default_server bit during listen options overwriting */
1227         default_server = addr[i].opt.default_server;
1228 
1229         proxy_protocol = lsopt->proxy_protocol || addr[i].opt.proxy_protocol;
1230 
1231 #if (NGX_HTTP_SSL)
1232         ssl = lsopt->ssl || addr[i].opt.ssl;
1233 #endif
1234 #if (NGX_HTTP_V2)
1235         http2 = lsopt->http2 || addr[i].opt.http2;
1236 #endif
1237 
1238         if (lsopt->set) {
1239 
1240             if (addr[i].opt.set) {
1241                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1242                         "duplicate listen options for %s", addr[i].opt.addr);
1243                 return NGX_ERROR;
1244             }
1245 
1246             addr[i].opt = *lsopt;
1247         }
1248 
1249         /* check the duplicate "default" server for this address:port */
1250 
1251         if (lsopt->default_server) {
1252 
1253             if (default_server) {
1254                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1255                         "a duplicate default server for %s", addr[i].opt.addr);
1256                 return NGX_ERROR;
1257             }
1258 
1259             default_server = 1;
1260             addr[i].default_server = cscf;
1261         }
1262 
1263         addr[i].opt.default_server = default_server;
1264         addr[i].opt.proxy_protocol = proxy_protocol;
1265 #if (NGX_HTTP_SSL)
1266         addr[i].opt.ssl = ssl;
1267 #endif
1268 #if (NGX_HTTP_V2)
1269         addr[i].opt.http2 = http2;
1270 #endif
1271 
1272         return NGX_OK;
1273     }
1274 
1275     /* add the address to the addresses list that bound to this port */
1276 
1277     return ngx_http_add_address(cf, cscf, port, lsopt);
1278 }
1279 
1280 
1281 /*
1282  * add the server address, the server names and the server core module
1283  * configurations to the port list
1284  */
1285 
1286 static ngx_int_t
1287 ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1288     ngx_http_conf_port_t *port, ngx_http_listen_opt_t *lsopt)
1289 {
1290     ngx_http_conf_addr_t  *addr;
1291 
1292     if (port->addrs.elts == NULL) {
1293         if (ngx_array_init(&port->addrs, cf->temp_pool, 4,
1294                            sizeof(ngx_http_conf_addr_t))
1295             != NGX_OK)
1296         {
1297             return NGX_ERROR;
1298         }
1299     }
1300 
1301 #if (NGX_HTTP_V2 && NGX_HTTP_SSL                                              \
1302      && !defined TLSEXT_TYPE_application_layer_protocol_negotiation           \
1303      && !defined TLSEXT_TYPE_next_proto_neg)
1304 
1305     if (lsopt->http2 && lsopt->ssl) {
1306         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1307                            "nginx was built with OpenSSL that lacks ALPN "
1308                            "and NPN support, HTTP/2 is not enabled for %s",
1309                            lsopt->addr);
1310     }
1311 
1312 #endif
1313 
1314     addr = ngx_array_push(&port->addrs);
1315     if (addr == NULL) {
1316         return NGX_ERROR;
1317     }
1318 
1319     addr->opt = *lsopt;
1320     addr->hash.buckets = NULL;
1321     addr->hash.size = 0;
1322     addr->wc_head = NULL;
1323     addr->wc_tail = NULL;
1324 #if (NGX_PCRE)
1325     addr->nregex = 0;
1326     addr->regex = NULL;
1327 #endif
1328     addr->default_server = cscf;
1329     addr->servers.elts = NULL;
1330 
1331     return ngx_http_add_server(cf, cscf, addr);
1332 }
1333 
1334 
1335 /* add the server core module configuration to the address:port */
1336 
1337 static ngx_int_t
1338 ngx_http_add_server(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1339     ngx_http_conf_addr_t *addr)
1340 {
1341     ngx_uint_t                  i;
1342     ngx_http_core_srv_conf_t  **server;
1343 
1344     if (addr->servers.elts == NULL) {
1345         if (ngx_array_init(&addr->servers, cf->temp_pool, 4,
1346                            sizeof(ngx_http_core_srv_conf_t *))
1347             != NGX_OK)
1348         {
1349             return NGX_ERROR;
1350         }
1351 
1352     } else {
1353         server = addr->servers.elts;
1354         for (i = 0; i < addr->servers.nelts; i++) {
1355             if (server[i] == cscf) {
1356                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1357                                    "a duplicate listen %s", addr->opt.addr);
1358                 return NGX_ERROR;
1359             }
1360         }
1361     }
1362 
1363     server = ngx_array_push(&addr->servers);
1364     if (server == NULL) {
1365         return NGX_ERROR;
1366     }
1367 
1368     *server = cscf;
1369 
1370     return NGX_OK;
1371 }
1372 
1373 
1374 static ngx_int_t
1375 ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
1376     ngx_array_t *ports)
1377 {
1378     ngx_uint_t             p, a;
1379     ngx_http_conf_port_t  *port;
1380     ngx_http_conf_addr_t  *addr;
1381 
1382     if (ports == NULL) {
1383         return NGX_OK;
1384     }
1385 
1386     port = ports->elts;
1387     for (p = 0; p < ports->nelts; p++) {
1388 
1389         ngx_sort(port[p].addrs.elts, (size_t) port[p].addrs.nelts,
1390                  sizeof(ngx_http_conf_addr_t), ngx_http_cmp_conf_addrs);
1391 
1392         /*
1393          * check whether all name-based servers have the same
1394          * configuration as a default server for given address:port
1395          */
1396 
1397         addr = port[p].addrs.elts;
1398         for (a = 0; a < port[p].addrs.nelts; a++) {
1399 
1400             if (addr[a].servers.nelts > 1
1401 #if (NGX_PCRE)
1402                 || addr[a].default_server->captures
1403 #endif
1404                )
1405             {
1406                 if (ngx_http_server_names(cf, cmcf, &addr[a]) != NGX_OK) {
1407                     return NGX_ERROR;
1408                 }
1409             }
1410         }
1411 
1412         if (ngx_http_init_listening(cf, &port[p]) != NGX_OK) {
1413             return NGX_ERROR;
1414         }
1415     }
1416 
1417     return NGX_OK;
1418 }
1419 
1420 
1421 static ngx_int_t
1422 ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
1423     ngx_http_conf_addr_t *addr)
1424 {
1425     ngx_int_t                   rc;
1426     ngx_uint_t                  n, s;
1427     ngx_hash_init_t             hash;
1428     ngx_hash_keys_arrays_t      ha;
1429     ngx_http_server_name_t     *name;
1430     ngx_http_core_srv_conf_t  **cscfp;
1431 #if (NGX_PCRE)
1432     ngx_uint_t                  regex, i;
1433 
1434     regex = 0;
1435 #endif
1436 
1437     ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t));
1438 
1439     ha.temp_pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, cf->log);
1440     if (ha.temp_pool == NULL) {
1441         return NGX_ERROR;
1442     }
1443 
1444     ha.pool = cf->pool;
1445 
1446     if (ngx_hash_keys_array_init(&ha, NGX_HASH_LARGE) != NGX_OK) {
1447         goto failed;
1448     }
1449 
1450     cscfp = addr->servers.elts;
1451 
1452     for (s = 0; s < addr->servers.nelts; s++) {
1453 
1454         name = cscfp[s]->server_names.elts;
1455 
1456         for (n = 0; n < cscfp[s]->server_names.nelts; n++) {
1457 
1458 #if (NGX_PCRE)
1459             if (name[n].regex) {
1460                 regex++;
1461                 continue;
1462             }
1463 #endif
1464 
1465             rc = ngx_hash_add_key(&ha, &name[n].name, name[n].server,
1466                                   NGX_HASH_WILDCARD_KEY);
1467 
1468             if (rc == NGX_ERROR) {
1469                 return NGX_ERROR;
1470             }
1471 
1472             if (rc == NGX_DECLINED) {
1473                 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
1474                               "invalid server name or wildcard \"%V\" on %s",
1475                               &name[n].name, addr->opt.addr);
1476                 return NGX_ERROR;
1477             }
1478 
1479             if (rc == NGX_BUSY) {
1480                 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
1481                               "conflicting server name \"%V\" on %s, ignored",
1482                               &name[n].name, addr->opt.addr);
1483             }
1484         }
1485     }
1486 
1487     hash.key = ngx_hash_key_lc;
1488     hash.max_size = cmcf->server_names_hash_max_size;
1489     hash.bucket_size = cmcf->server_names_hash_bucket_size;
1490     hash.name = "server_names_hash";
1491     hash.pool = cf->pool;
1492 
1493     if (ha.keys.nelts) {
1494         hash.hash = &addr->hash;
1495         hash.temp_pool = NULL;
1496 
1497         if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK) {
1498             goto failed;
1499         }
1500     }
1501 
1502     if (ha.dns_wc_head.nelts) {
1503 
1504         ngx_qsort(ha.dns_wc_head.elts, (size_t) ha.dns_wc_head.nelts,
1505                   sizeof(ngx_hash_key_t), ngx_http_cmp_dns_wildcards);
1506 
1507         hash.hash = NULL;
1508         hash.temp_pool = ha.temp_pool;
1509 
1510         if (ngx_hash_wildcard_init(&hash, ha.dns_wc_head.elts,
1511                                    ha.dns_wc_head.nelts)
1512             != NGX_OK)
1513         {
1514             goto failed;
1515         }
1516 
1517         addr->wc_head = (ngx_hash_wildcard_t *) hash.hash;
1518     }
1519 
1520     if (ha.dns_wc_tail.nelts) {
1521 
1522         ngx_qsort(ha.dns_wc_tail.elts, (size_t) ha.dns_wc_tail.nelts,
1523                   sizeof(ngx_hash_key_t), ngx_http_cmp_dns_wildcards);
1524 
1525         hash.hash = NULL;
1526         hash.temp_pool = ha.temp_pool;
1527 
1528         if (ngx_hash_wildcard_init(&hash, ha.dns_wc_tail.elts,
1529                                    ha.dns_wc_tail.nelts)
1530             != NGX_OK)
1531         {
1532             goto failed;
1533         }
1534 
1535         addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash;
1536     }
1537 
1538     ngx_destroy_pool(ha.temp_pool);
1539 
1540 #if (NGX_PCRE)
1541 
1542     if (regex == 0) {
1543         return NGX_OK;
1544     }
1545 
1546     addr->nregex = regex;
1547     addr->regex = ngx_palloc(cf->pool, regex * sizeof(ngx_http_server_name_t));
1548     if (addr->regex == NULL) {
1549         return NGX_ERROR;
1550     }
1551 
1552     i = 0;
1553 
1554     for (s = 0; s < addr->servers.nelts; s++) {
1555 
1556         name = cscfp[s]->server_names.elts;
1557 
1558         for (n = 0; n < cscfp[s]->server_names.nelts; n++) {
1559             if (name[n].regex) {
1560                 addr->regex[i++] = name[n];
1561             }
1562         }
1563     }
1564 
1565 #endif
1566 
1567     return NGX_OK;
1568 
1569 failed:
1570 
1571     ngx_destroy_pool(ha.temp_pool);
1572 
1573     return NGX_ERROR;
1574 }
1575 
1576 
1577 static ngx_int_t
1578 ngx_http_cmp_conf_addrs(const void *one, const void *two)
1579 {
1580     ngx_http_conf_addr_t  *first, *second;
1581 
1582     first = (ngx_http_conf_addr_t *) one;
1583     second = (ngx_http_conf_addr_t *) two;
1584 
1585     if (first->opt.wildcard) {
1586         /* a wildcard address must be the last resort, shift it to the end */
1587         return 1;
1588     }
1589 
1590     if (second->opt.wildcard) {
1591         /* a wildcard address must be the last resort, shift it to the end */
1592         return -1;
1593     }
1594 
1595     if (first->opt.bind && !second->opt.bind) {
1596         /* shift explicit bind()ed addresses to the start */
1597         return -1;
1598     }
1599 
1600     if (!first->opt.bind && second->opt.bind) {
1601         /* shift explicit bind()ed addresses to the start */
1602         return 1;
1603     }
1604 
1605     /* do not sort by default */
1606 
1607     return 0;
1608 }
1609 
1610 
1611 static int ngx_libc_cdecl
1612 ngx_http_cmp_dns_wildcards(const void *one, const void *two)
1613 {
1614     ngx_hash_key_t  *first, *second;
1615 
1616     first = (ngx_hash_key_t *) one;
1617     second = (ngx_hash_key_t *) two;
1618 
1619     return ngx_dns_strcmp(first->key.data, second->key.data);
1620 }
1621 
1622 
1623 static ngx_int_t
1624 ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_port_t *port)
1625 {
1626     ngx_uint_t                 i, last, bind_wildcard;
1627     ngx_listening_t           *ls;
1628     ngx_http_port_t           *hport;
1629     ngx_http_conf_addr_t      *addr;
1630 
1631     addr = port->addrs.elts;
1632     last = port->addrs.nelts;
1633 
1634     /*
1635      * If there is a binding to an "*:port" then we need to bind() to
1636      * the "*:port" only and ignore other implicit bindings.  The bindings
1637      * have been already sorted: explicit bindings are on the start, then
1638      * implicit bindings go, and wildcard binding is in the end.
1639      */
1640 
1641     if (addr[last - 1].opt.wildcard) {
1642         addr[last - 1].opt.bind = 1;
1643         bind_wildcard = 1;
1644 
1645     } else {
1646         bind_wildcard = 0;
1647     }
1648 
1649     i = 0;
1650 
1651     while (i < last) {
1652 
1653         if (bind_wildcard && !addr[i].opt.bind) {
1654             i++;
1655             continue;
1656         }
1657 
1658         ls = ngx_http_add_listening(cf, &addr[i]);
1659         if (ls == NULL) {
1660             return NGX_ERROR;
1661         }
1662 
1663         hport = ngx_pcalloc(cf->pool, sizeof(ngx_http_port_t));
1664         if (hport == NULL) {
1665             return NGX_ERROR;
1666         }
1667 
1668         ls->servers = hport;
1669 
1670         hport->naddrs = i + 1;
1671 
1672         switch (ls->sockaddr->sa_family) {
1673 
1674 #if (NGX_HAVE_INET6)
1675         case AF_INET6:
1676             if (ngx_http_add_addrs6(cf, hport, addr) != NGX_OK) {
1677                 return NGX_ERROR;
1678             }
1679             break;
1680 #endif
1681         default: /* AF_INET */
1682             if (ngx_http_add_addrs(cf, hport, addr) != NGX_OK) {
1683                 return NGX_ERROR;
1684             }
1685             break;
1686         }
1687 
1688         if (ngx_clone_listening(cf, ls) != NGX_OK) {
1689             return NGX_ERROR;
1690         }
1691 
1692         addr++;
1693         last--;
1694     }
1695 
1696     return NGX_OK;
1697 }
1698 
1699 
1700 static ngx_listening_t *
1701 ngx_http_add_listening(ngx_conf_t *cf, ngx_http_conf_addr_t *addr)
1702 {
1703     ngx_listening_t           *ls;
1704     ngx_http_core_loc_conf_t  *clcf;
1705     ngx_http_core_srv_conf_t  *cscf;
1706 
1707     ls = ngx_create_listening(cf, &addr->opt.sockaddr.sockaddr,
1708                               addr->opt.socklen);
1709     if (ls == NULL) {
1710         return NULL;
1711     }
1712 
1713     ls->addr_ntop = 1;
1714 
1715     ls->handler = ngx_http_init_connection;
1716 
1717     cscf = addr->default_server;
1718     ls->pool_size = cscf->connection_pool_size;
1719     ls->post_accept_timeout = cscf->client_header_timeout;
1720 
1721     clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];
1722 
1723     ls->logp = clcf->error_log;
1724     ls->log.data = &ls->addr_text;
1725     ls->log.handler = ngx_accept_log_error;
1726 
1727 #if (NGX_WIN32)
1728     {
1729     ngx_iocp_conf_t  *iocpcf = NULL;
1730 
1731     if (ngx_get_conf(cf->cycle->conf_ctx, ngx_events_module)) {
1732         iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module);
1733     }
1734     if (iocpcf && iocpcf->acceptex_read) {
1735         ls->post_accept_buffer_size = cscf->client_header_buffer_size;
1736     }
1737     }
1738 #endif
1739 
1740     ls->backlog = addr->opt.backlog;
1741     ls->rcvbuf = addr->opt.rcvbuf;
1742     ls->sndbuf = addr->opt.sndbuf;
1743 
1744     ls->keepalive = addr->opt.so_keepalive;
1745 #if (NGX_HAVE_KEEPALIVE_TUNABLE)
1746     ls->keepidle = addr->opt.tcp_keepidle;
1747     ls->keepintvl = addr->opt.tcp_keepintvl;
1748     ls->keepcnt = addr->opt.tcp_keepcnt;
1749 #endif
1750 
1751 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
1752     ls->accept_filter = addr->opt.accept_filter;
1753 #endif
1754 
1755 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
1756     ls->deferred_accept = addr->opt.deferred_accept;
1757 #endif
1758 
1759 #if (NGX_HAVE_INET6)
1760     ls->ipv6only = addr->opt.ipv6only;
1761 #endif
1762 
1763 #if (NGX_HAVE_SETFIB)
1764     ls->setfib = addr->opt.setfib;
1765 #endif
1766 
1767 #if (NGX_HAVE_TCP_FASTOPEN)
1768     ls->fastopen = addr->opt.fastopen;
1769 #endif
1770 
1771 #if (NGX_HAVE_REUSEPORT)
1772     ls->reuseport = addr->opt.reuseport;
1773 #endif
1774 
1775     return ls;
1776 }
1777 
1778 
1779 static ngx_int_t
1780 ngx_http_add_addrs(ngx_conf_t *cf, ngx_http_port_t *hport,
1781     ngx_http_conf_addr_t *addr)
1782 {
1783     ngx_uint_t                 i;
1784     ngx_http_in_addr_t        *addrs;
1785     struct sockaddr_in        *sin;
1786     ngx_http_virtual_names_t  *vn;
1787 
1788     hport->addrs = ngx_pcalloc(cf->pool,
1789                                hport->naddrs * sizeof(ngx_http_in_addr_t));
1790     if (hport->addrs == NULL) {
1791         return NGX_ERROR;
1792     }
1793 
1794     addrs = hport->addrs;
1795 
1796     for (i = 0; i < hport->naddrs; i++) {
1797 
1798         sin = &addr[i].opt.sockaddr.sockaddr_in;
1799         addrs[i].addr = sin->sin_addr.s_addr;
1800         addrs[i].conf.default_server = addr[i].default_server;
1801 #if (NGX_HTTP_SSL)
1802         addrs[i].conf.ssl = addr[i].opt.ssl;
1803 #endif
1804 #if (NGX_HTTP_V2)
1805         addrs[i].conf.http2 = addr[i].opt.http2;
1806 #endif
1807         addrs[i].conf.proxy_protocol = addr[i].opt.proxy_protocol;
1808 
1809         if (addr[i].hash.buckets == NULL
1810             && (addr[i].wc_head == NULL
1811                 || addr[i].wc_head->hash.buckets == NULL)
1812             && (addr[i].wc_tail == NULL
1813                 || addr[i].wc_tail->hash.buckets == NULL)
1814 #if (NGX_PCRE)
1815             && addr[i].nregex == 0
1816 #endif
1817             )
1818         {
1819             continue;
1820         }
1821 
1822         vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
1823         if (vn == NULL) {
1824             return NGX_ERROR;
1825         }
1826 
1827         addrs[i].conf.virtual_names = vn;
1828 
1829         vn->names.hash = addr[i].hash;
1830         vn->names.wc_head = addr[i].wc_head;
1831         vn->names.wc_tail = addr[i].wc_tail;
1832 #if (NGX_PCRE)
1833         vn->nregex = addr[i].nregex;
1834         vn->regex = addr[i].regex;
1835 #endif
1836     }
1837 
1838     return NGX_OK;
1839 }
1840 
1841 
1842 #if (NGX_HAVE_INET6)
1843 
1844 static ngx_int_t
1845 ngx_http_add_addrs6(ngx_conf_t *cf, ngx_http_port_t *hport,
1846     ngx_http_conf_addr_t *addr)
1847 {
1848     ngx_uint_t                 i;
1849     ngx_http_in6_addr_t       *addrs6;
1850     struct sockaddr_in6       *sin6;
1851     ngx_http_virtual_names_t  *vn;
1852 
1853     hport->addrs = ngx_pcalloc(cf->pool,
1854                                hport->naddrs * sizeof(ngx_http_in6_addr_t));
1855     if (hport->addrs == NULL) {
1856         return NGX_ERROR;
1857     }
1858 
1859     addrs6 = hport->addrs;
1860 
1861     for (i = 0; i < hport->naddrs; i++) {
1862 
1863         sin6 = &addr[i].opt.sockaddr.sockaddr_in6;
1864         addrs6[i].addr6 = sin6->sin6_addr;
1865         addrs6[i].conf.default_server = addr[i].default_server;
1866 #if (NGX_HTTP_SSL)
1867         addrs6[i].conf.ssl = addr[i].opt.ssl;
1868 #endif
1869 #if (NGX_HTTP_V2)
1870         addrs6[i].conf.http2 = addr[i].opt.http2;
1871 #endif
1872         addrs6[i].conf.proxy_protocol = addr[i].opt.proxy_protocol;
1873 
1874         if (addr[i].hash.buckets == NULL
1875             && (addr[i].wc_head == NULL
1876                 || addr[i].wc_head->hash.buckets == NULL)
1877             && (addr[i].wc_tail == NULL
1878                 || addr[i].wc_tail->hash.buckets == NULL)
1879 #if (NGX_PCRE)
1880             && addr[i].nregex == 0
1881 #endif
1882             )
1883         {
1884             continue;
1885         }
1886 
1887         vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
1888         if (vn == NULL) {
1889             return NGX_ERROR;
1890         }
1891 
1892         addrs6[i].conf.virtual_names = vn;
1893 
1894         vn->names.hash = addr[i].hash;
1895         vn->names.wc_head = addr[i].wc_head;
1896         vn->names.wc_tail = addr[i].wc_tail;
1897 #if (NGX_PCRE)
1898         vn->nregex = addr[i].nregex;
1899         vn->regex = addr[i].regex;
1900 #endif
1901     }
1902 
1903     return NGX_OK;
1904 }
1905 
1906 #endif
1907 
1908 
1909 char *
1910 ngx_http_types_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1911 {
1912     char  *p = conf;
1913 
1914     ngx_array_t     **types;
1915     ngx_str_t        *value, *default_type;
1916     ngx_uint_t        i, n, hash;
1917     ngx_hash_key_t   *type;
1918 
1919     types = (ngx_array_t **) (p + cmd->offset);
1920 
1921     if (*types == (void *) -1) {
1922         return NGX_CONF_OK;
1923     }
1924 
1925     default_type = cmd->post;
1926 
1927     if (*types == NULL) {
1928         *types = ngx_array_create(cf->temp_pool, 1, sizeof(ngx_hash_key_t));
1929         if (*types == NULL) {
1930             return NGX_CONF_ERROR;
1931         }
1932 
1933         if (default_type) {
1934             type = ngx_array_push(*types);
1935             if (type == NULL) {
1936                 return NGX_CONF_ERROR;
1937             }
1938 
1939             type->key = *default_type;
1940             type->key_hash = ngx_hash_key(default_type->data,
1941                                           default_type->len);
1942             type->value = (void *) 4;
1943         }
1944     }
1945 
1946     value = cf->args->elts;
1947 
1948     for (i = 1; i < cf->args->nelts; i++) {
1949 
1950         if (value[i].len == 1 && value[i].data[0] == '*') {
1951             *types = (void *) -1;
1952             return NGX_CONF_OK;
1953         }
1954 
1955         hash = ngx_hash_strlow(value[i].data, value[i].data, value[i].len);
1956         value[i].data[value[i].len] = '\0';
1957 
1958         type = (*types)->elts;
1959         for (n = 0; n < (*types)->nelts; n++) {
1960 
1961             if (ngx_strcmp(value[i].data, type[n].key.data) == 0) {
1962                 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1963                                    "duplicate MIME type \"%V\"", &value[i]);
1964                 goto next;
1965             }
1966         }
1967 
1968         type = ngx_array_push(*types);
1969         if (type == NULL) {
1970             return NGX_CONF_ERROR;
1971         }
1972 
1973         type->key = value[i];
1974         type->key_hash = hash;
1975         type->value = (void *) 4;
1976 
1977     next:
1978 
1979         continue;
1980     }
1981 
1982     return NGX_CONF_OK;
1983 }
1984 
1985 
1986 char *
1987 ngx_http_merge_types(ngx_conf_t *cf, ngx_array_t **keys, ngx_hash_t *types_hash,
1988     ngx_array_t **prev_keys, ngx_hash_t *prev_types_hash,
1989     ngx_str_t *default_types)
1990 {
1991     ngx_hash_init_t  hash;
1992 
1993     if (*keys) {
1994 
1995         if (*keys == (void *) -1) {
1996             return NGX_CONF_OK;
1997         }
1998 
1999         hash.hash = types_hash;
2000         hash.key = NULL;
2001         hash.max_size = 2048;
2002         hash.bucket_size = 64;
2003         hash.name = "test_types_hash";
2004         hash.pool = cf->pool;
2005         hash.temp_pool = NULL;
2006 
2007         if (ngx_hash_init(&hash, (*keys)->elts, (*keys)->nelts) != NGX_OK) {
2008             return NGX_CONF_ERROR;
2009         }
2010 
2011         return NGX_CONF_OK;
2012     }
2013 
2014     if (prev_types_hash->buckets == NULL) {
2015 
2016         if (*prev_keys == NULL) {
2017 
2018             if (ngx_http_set_default_types(cf, prev_keys, default_types)
2019                 != NGX_OK)
2020             {
2021                 return NGX_CONF_ERROR;
2022             }
2023 
2024         } else if (*prev_keys == (void *) -1) {
2025             *keys = *prev_keys;
2026             return NGX_CONF_OK;
2027         }
2028 
2029         hash.hash = prev_types_hash;
2030         hash.key = NULL;
2031         hash.max_size = 2048;
2032         hash.bucket_size = 64;
2033         hash.name = "test_types_hash";
2034         hash.pool = cf->pool;
2035         hash.temp_pool = NULL;
2036 
2037         if (ngx_hash_init(&hash, (*prev_keys)->elts, (*prev_keys)->nelts)
2038             != NGX_OK)
2039         {
2040             return NGX_CONF_ERROR;
2041         }
2042     }
2043 
2044     *types_hash = *prev_types_hash;
2045 
2046     return NGX_CONF_OK;
2047 
2048 }
2049 
2050 
2051 ngx_int_t
2052 ngx_http_set_default_types(ngx_conf_t *cf, ngx_array_t **types,
2053     ngx_str_t *default_type)
2054 {
2055     ngx_hash_key_t  *type;
2056 
2057     *types = ngx_array_create(cf->temp_pool, 1, sizeof(ngx_hash_key_t));
2058     if (*types == NULL) {
2059         return NGX_ERROR;
2060     }
2061 
2062     while (default_type->len) {
2063 
2064         type = ngx_array_push(*types);
2065         if (type == NULL) {
2066             return NGX_ERROR;
2067         }
2068 
2069         type->key = *default_type;
2070         type->key_hash = ngx_hash_key(default_type->data,
2071                                       default_type->len);
2072         type->value = (void *) 4;
2073 
2074         default_type++;
2075     }
2076 
2077     return NGX_OK;
2078 }