Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.12 ]​[ nginx-1.16.0 ]​

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;
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, lsopt->socklen,
1213                              addr[i].opt.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 %V",
1243                                    &addr[i].opt.addr_text);
1244                 return NGX_ERROR;
1245             }
1246 
1247             addr[i].opt = *lsopt;
1248         }
1249 
1250         /* check the duplicate "default" server for this address:port */
1251 
1252         if (lsopt->default_server) {
1253 
1254             if (default_server) {
1255                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1256                                    "a duplicate default server for %V",
1257                                    &addr[i].opt.addr_text);
1258                 return NGX_ERROR;
1259             }
1260 
1261             default_server = 1;
1262             addr[i].default_server = cscf;
1263         }
1264 
1265         addr[i].opt.default_server = default_server;
1266         addr[i].opt.proxy_protocol = proxy_protocol;
1267 #if (NGX_HTTP_SSL)
1268         addr[i].opt.ssl = ssl;
1269 #endif
1270 #if (NGX_HTTP_V2)
1271         addr[i].opt.http2 = http2;
1272 #endif
1273 
1274         return NGX_OK;
1275     }
1276 
1277     /* add the address to the addresses list that bound to this port */
1278 
1279     return ngx_http_add_address(cf, cscf, port, lsopt);
1280 }
1281 
1282 
1283 /*
1284  * add the server address, the server names and the server core module
1285  * configurations to the port list
1286  */
1287 
1288 static ngx_int_t
1289 ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1290     ngx_http_conf_port_t *port, ngx_http_listen_opt_t *lsopt)
1291 {
1292     ngx_http_conf_addr_t  *addr;
1293 
1294     if (port->addrs.elts == NULL) {
1295         if (ngx_array_init(&port->addrs, cf->temp_pool, 4,
1296                            sizeof(ngx_http_conf_addr_t))
1297             != NGX_OK)
1298         {
1299             return NGX_ERROR;
1300         }
1301     }
1302 
1303 #if (NGX_HTTP_V2 && NGX_HTTP_SSL                                              \
1304      && !defined TLSEXT_TYPE_application_layer_protocol_negotiation           \
1305      && !defined TLSEXT_TYPE_next_proto_neg)
1306 
1307     if (lsopt->http2 && lsopt->ssl) {
1308         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1309                            "nginx was built with OpenSSL that lacks ALPN "
1310                            "and NPN support, HTTP/2 is not enabled for %V",
1311                            &lsopt->addr_text);
1312     }
1313 
1314 #endif
1315 
1316     addr = ngx_array_push(&port->addrs);
1317     if (addr == NULL) {
1318         return NGX_ERROR;
1319     }
1320 
1321     addr->opt = *lsopt;
1322     addr->hash.buckets = NULL;
1323     addr->hash.size = 0;
1324     addr->wc_head = NULL;
1325     addr->wc_tail = NULL;
1326 #if (NGX_PCRE)
1327     addr->nregex = 0;
1328     addr->regex = NULL;
1329 #endif
1330     addr->default_server = cscf;
1331     addr->servers.elts = NULL;
1332 
1333     return ngx_http_add_server(cf, cscf, addr);
1334 }
1335 
1336 
1337 /* add the server core module configuration to the address:port */
1338 
1339 static ngx_int_t
1340 ngx_http_add_server(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1341     ngx_http_conf_addr_t *addr)
1342 {
1343     ngx_uint_t                  i;
1344     ngx_http_core_srv_conf_t  **server;
1345 
1346     if (addr->servers.elts == NULL) {
1347         if (ngx_array_init(&addr->servers, cf->temp_pool, 4,
1348                            sizeof(ngx_http_core_srv_conf_t *))
1349             != NGX_OK)
1350         {
1351             return NGX_ERROR;
1352         }
1353 
1354     } else {
1355         server = addr->servers.elts;
1356         for (i = 0; i < addr->servers.nelts; i++) {
1357             if (server[i] == cscf) {
1358                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1359                                    "a duplicate listen %V",
1360                                    &addr->opt.addr_text);
1361                 return NGX_ERROR;
1362             }
1363         }
1364     }
1365 
1366     server = ngx_array_push(&addr->servers);
1367     if (server == NULL) {
1368         return NGX_ERROR;
1369     }
1370 
1371     *server = cscf;
1372 
1373     return NGX_OK;
1374 }
1375 
1376 
1377 static ngx_int_t
1378 ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
1379     ngx_array_t *ports)
1380 {
1381     ngx_uint_t             p, a;
1382     ngx_http_conf_port_t  *port;
1383     ngx_http_conf_addr_t  *addr;
1384 
1385     if (ports == NULL) {
1386         return NGX_OK;
1387     }
1388 
1389     port = ports->elts;
1390     for (p = 0; p < ports->nelts; p++) {
1391 
1392         ngx_sort(port[p].addrs.elts, (size_t) port[p].addrs.nelts,
1393                  sizeof(ngx_http_conf_addr_t), ngx_http_cmp_conf_addrs);
1394 
1395         /*
1396          * check whether all name-based servers have the same
1397          * configuration as a default server for given address:port
1398          */
1399 
1400         addr = port[p].addrs.elts;
1401         for (a = 0; a < port[p].addrs.nelts; a++) {
1402 
1403             if (addr[a].servers.nelts > 1
1404 #if (NGX_PCRE)
1405                 || addr[a].default_server->captures
1406 #endif
1407                )
1408             {
1409                 if (ngx_http_server_names(cf, cmcf, &addr[a]) != NGX_OK) {
1410                     return NGX_ERROR;
1411                 }
1412             }
1413         }
1414 
1415         if (ngx_http_init_listening(cf, &port[p]) != NGX_OK) {
1416             return NGX_ERROR;
1417         }
1418     }
1419 
1420     return NGX_OK;
1421 }
1422 
1423 
1424 static ngx_int_t
1425 ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
1426     ngx_http_conf_addr_t *addr)
1427 {
1428     ngx_int_t                   rc;
1429     ngx_uint_t                  n, s;
1430     ngx_hash_init_t             hash;
1431     ngx_hash_keys_arrays_t      ha;
1432     ngx_http_server_name_t     *name;
1433     ngx_http_core_srv_conf_t  **cscfp;
1434 #if (NGX_PCRE)
1435     ngx_uint_t                  regex, i;
1436 
1437     regex = 0;
1438 #endif
1439 
1440     ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t));
1441 
1442     ha.temp_pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, cf->log);
1443     if (ha.temp_pool == NULL) {
1444         return NGX_ERROR;
1445     }
1446 
1447     ha.pool = cf->pool;
1448 
1449     if (ngx_hash_keys_array_init(&ha, NGX_HASH_LARGE) != NGX_OK) {
1450         goto failed;
1451     }
1452 
1453     cscfp = addr->servers.elts;
1454 
1455     for (s = 0; s < addr->servers.nelts; s++) {
1456 
1457         name = cscfp[s]->server_names.elts;
1458 
1459         for (n = 0; n < cscfp[s]->server_names.nelts; n++) {
1460 
1461 #if (NGX_PCRE)
1462             if (name[n].regex) {
1463                 regex++;
1464                 continue;
1465             }
1466 #endif
1467 
1468             rc = ngx_hash_add_key(&ha, &name[n].name, name[n].server,
1469                                   NGX_HASH_WILDCARD_KEY);
1470 
1471             if (rc == NGX_ERROR) {
1472                 return NGX_ERROR;
1473             }
1474 
1475             if (rc == NGX_DECLINED) {
1476                 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
1477                               "invalid server name or wildcard \"%V\" on %V",
1478                               &name[n].name, &addr->opt.addr_text);
1479                 return NGX_ERROR;
1480             }
1481 
1482             if (rc == NGX_BUSY) {
1483                 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
1484                               "conflicting server name \"%V\" on %V, ignored",
1485                               &name[n].name, &addr->opt.addr_text);
1486             }
1487         }
1488     }
1489 
1490     hash.key = ngx_hash_key_lc;
1491     hash.max_size = cmcf->server_names_hash_max_size;
1492     hash.bucket_size = cmcf->server_names_hash_bucket_size;
1493     hash.name = "server_names_hash";
1494     hash.pool = cf->pool;
1495 
1496     if (ha.keys.nelts) {
1497         hash.hash = &addr->hash;
1498         hash.temp_pool = NULL;
1499 
1500         if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK) {
1501             goto failed;
1502         }
1503     }
1504 
1505     if (ha.dns_wc_head.nelts) {
1506 
1507         ngx_qsort(ha.dns_wc_head.elts, (size_t) ha.dns_wc_head.nelts,
1508                   sizeof(ngx_hash_key_t), ngx_http_cmp_dns_wildcards);
1509 
1510         hash.hash = NULL;
1511         hash.temp_pool = ha.temp_pool;
1512 
1513         if (ngx_hash_wildcard_init(&hash, ha.dns_wc_head.elts,
1514                                    ha.dns_wc_head.nelts)
1515             != NGX_OK)
1516         {
1517             goto failed;
1518         }
1519 
1520         addr->wc_head = (ngx_hash_wildcard_t *) hash.hash;
1521     }
1522 
1523     if (ha.dns_wc_tail.nelts) {
1524 
1525         ngx_qsort(ha.dns_wc_tail.elts, (size_t) ha.dns_wc_tail.nelts,
1526                   sizeof(ngx_hash_key_t), ngx_http_cmp_dns_wildcards);
1527 
1528         hash.hash = NULL;
1529         hash.temp_pool = ha.temp_pool;
1530 
1531         if (ngx_hash_wildcard_init(&hash, ha.dns_wc_tail.elts,
1532                                    ha.dns_wc_tail.nelts)
1533             != NGX_OK)
1534         {
1535             goto failed;
1536         }
1537 
1538         addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash;
1539     }
1540 
1541     ngx_destroy_pool(ha.temp_pool);
1542 
1543 #if (NGX_PCRE)
1544 
1545     if (regex == 0) {
1546         return NGX_OK;
1547     }
1548 
1549     addr->nregex = regex;
1550     addr->regex = ngx_palloc(cf->pool, regex * sizeof(ngx_http_server_name_t));
1551     if (addr->regex == NULL) {
1552         return NGX_ERROR;
1553     }
1554 
1555     i = 0;
1556 
1557     for (s = 0; s < addr->servers.nelts; s++) {
1558 
1559         name = cscfp[s]->server_names.elts;
1560 
1561         for (n = 0; n < cscfp[s]->server_names.nelts; n++) {
1562             if (name[n].regex) {
1563                 addr->regex[i++] = name[n];
1564             }
1565         }
1566     }
1567 
1568 #endif
1569 
1570     return NGX_OK;
1571 
1572 failed:
1573 
1574     ngx_destroy_pool(ha.temp_pool);
1575 
1576     return NGX_ERROR;
1577 }
1578 
1579 
1580 static ngx_int_t
1581 ngx_http_cmp_conf_addrs(const void *one, const void *two)
1582 {
1583     ngx_http_conf_addr_t  *first, *second;
1584 
1585     first = (ngx_http_conf_addr_t *) one;
1586     second = (ngx_http_conf_addr_t *) two;
1587 
1588     if (first->opt.wildcard) {
1589         /* a wildcard address must be the last resort, shift it to the end */
1590         return 1;
1591     }
1592 
1593     if (second->opt.wildcard) {
1594         /* a wildcard address must be the last resort, shift it to the end */
1595         return -1;
1596     }
1597 
1598     if (first->opt.bind && !second->opt.bind) {
1599         /* shift explicit bind()ed addresses to the start */
1600         return -1;
1601     }
1602 
1603     if (!first->opt.bind && second->opt.bind) {
1604         /* shift explicit bind()ed addresses to the start */
1605         return 1;
1606     }
1607 
1608     /* do not sort by default */
1609 
1610     return 0;
1611 }
1612 
1613 
1614 static int ngx_libc_cdecl
1615 ngx_http_cmp_dns_wildcards(const void *one, const void *two)
1616 {
1617     ngx_hash_key_t  *first, *second;
1618 
1619     first = (ngx_hash_key_t *) one;
1620     second = (ngx_hash_key_t *) two;
1621 
1622     return ngx_dns_strcmp(first->key.data, second->key.data);
1623 }
1624 
1625 
1626 static ngx_int_t
1627 ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_port_t *port)
1628 {
1629     ngx_uint_t                 i, last, bind_wildcard;
1630     ngx_listening_t           *ls;
1631     ngx_http_port_t           *hport;
1632     ngx_http_conf_addr_t      *addr;
1633 
1634     addr = port->addrs.elts;
1635     last = port->addrs.nelts;
1636 
1637     /*
1638      * If there is a binding to an "*:port" then we need to bind() to
1639      * the "*:port" only and ignore other implicit bindings.  The bindings
1640      * have been already sorted: explicit bindings are on the start, then
1641      * implicit bindings go, and wildcard binding is in the end.
1642      */
1643 
1644     if (addr[last - 1].opt.wildcard) {
1645         addr[last - 1].opt.bind = 1;
1646         bind_wildcard = 1;
1647 
1648     } else {
1649         bind_wildcard = 0;
1650     }
1651 
1652     i = 0;
1653 
1654     while (i < last) {
1655 
1656         if (bind_wildcard && !addr[i].opt.bind) {
1657             i++;
1658             continue;
1659         }
1660 
1661         ls = ngx_http_add_listening(cf, &addr[i]);
1662         if (ls == NULL) {
1663             return NGX_ERROR;
1664         }
1665 
1666         hport = ngx_pcalloc(cf->pool, sizeof(ngx_http_port_t));
1667         if (hport == NULL) {
1668             return NGX_ERROR;
1669         }
1670 
1671         ls->servers = hport;
1672 
1673         hport->naddrs = i + 1;
1674 
1675         switch (ls->sockaddr->sa_family) {
1676 
1677 #if (NGX_HAVE_INET6)
1678         case AF_INET6:
1679             if (ngx_http_add_addrs6(cf, hport, addr) != NGX_OK) {
1680                 return NGX_ERROR;
1681             }
1682             break;
1683 #endif
1684         default: /* AF_INET */
1685             if (ngx_http_add_addrs(cf, hport, addr) != NGX_OK) {
1686                 return NGX_ERROR;
1687             }
1688             break;
1689         }
1690 
1691         addr++;
1692         last--;
1693     }
1694 
1695     return NGX_OK;
1696 }
1697 
1698 
1699 static ngx_listening_t *
1700 ngx_http_add_listening(ngx_conf_t *cf, ngx_http_conf_addr_t *addr)
1701 {
1702     ngx_listening_t           *ls;
1703     ngx_http_core_loc_conf_t  *clcf;
1704     ngx_http_core_srv_conf_t  *cscf;
1705 
1706     ls = ngx_create_listening(cf, addr->opt.sockaddr, addr->opt.socklen);
1707     if (ls == NULL) {
1708         return NULL;
1709     }
1710 
1711     ls->addr_ntop = 1;
1712 
1713     ls->handler = ngx_http_init_connection;
1714 
1715     cscf = addr->default_server;
1716     ls->pool_size = cscf->connection_pool_size;
1717     ls->post_accept_timeout = cscf->client_header_timeout;
1718 
1719     clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];
1720 
1721     ls->logp = clcf->error_log;
1722     ls->log.data = &ls->addr_text;
1723     ls->log.handler = ngx_accept_log_error;
1724 
1725 #if (NGX_WIN32)
1726     {
1727     ngx_iocp_conf_t  *iocpcf = NULL;
1728 
1729     if (ngx_get_conf(cf->cycle->conf_ctx, ngx_events_module)) {
1730         iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module);
1731     }
1732     if (iocpcf && iocpcf->acceptex_read) {
1733         ls->post_accept_buffer_size = cscf->client_header_buffer_size;
1734     }
1735     }
1736 #endif
1737 
1738     ls->backlog = addr->opt.backlog;
1739     ls->rcvbuf = addr->opt.rcvbuf;
1740     ls->sndbuf = addr->opt.sndbuf;
1741 
1742     ls->keepalive = addr->opt.so_keepalive;
1743 #if (NGX_HAVE_KEEPALIVE_TUNABLE)
1744     ls->keepidle = addr->opt.tcp_keepidle;
1745     ls->keepintvl = addr->opt.tcp_keepintvl;
1746     ls->keepcnt = addr->opt.tcp_keepcnt;
1747 #endif
1748 
1749 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
1750     ls->accept_filter = addr->opt.accept_filter;
1751 #endif
1752 
1753 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
1754     ls->deferred_accept = addr->opt.deferred_accept;
1755 #endif
1756 
1757 #if (NGX_HAVE_INET6)
1758     ls->ipv6only = addr->opt.ipv6only;
1759 #endif
1760 
1761 #if (NGX_HAVE_SETFIB)
1762     ls->setfib = addr->opt.setfib;
1763 #endif
1764 
1765 #if (NGX_HAVE_TCP_FASTOPEN)
1766     ls->fastopen = addr->opt.fastopen;
1767 #endif
1768 
1769 #if (NGX_HAVE_REUSEPORT)
1770     ls->reuseport = addr->opt.reuseport;
1771 #endif
1772 
1773     return ls;
1774 }
1775 
1776 
1777 static ngx_int_t
1778 ngx_http_add_addrs(ngx_conf_t *cf, ngx_http_port_t *hport,
1779     ngx_http_conf_addr_t *addr)
1780 {
1781     ngx_uint_t                 i;
1782     ngx_http_in_addr_t        *addrs;
1783     struct sockaddr_in        *sin;
1784     ngx_http_virtual_names_t  *vn;
1785 
1786     hport->addrs = ngx_pcalloc(cf->pool,
1787                                hport->naddrs * sizeof(ngx_http_in_addr_t));
1788     if (hport->addrs == NULL) {
1789         return NGX_ERROR;
1790     }
1791 
1792     addrs = hport->addrs;
1793 
1794     for (i = 0; i < hport->naddrs; i++) {
1795 
1796         sin = (struct sockaddr_in *) addr[i].opt.sockaddr;
1797         addrs[i].addr = sin->sin_addr.s_addr;
1798         addrs[i].conf.default_server = addr[i].default_server;
1799 #if (NGX_HTTP_SSL)
1800         addrs[i].conf.ssl = addr[i].opt.ssl;
1801 #endif
1802 #if (NGX_HTTP_V2)
1803         addrs[i].conf.http2 = addr[i].opt.http2;
1804 #endif
1805         addrs[i].conf.proxy_protocol = addr[i].opt.proxy_protocol;
1806 
1807         if (addr[i].hash.buckets == NULL
1808             && (addr[i].wc_head == NULL
1809                 || addr[i].wc_head->hash.buckets == NULL)
1810             && (addr[i].wc_tail == NULL
1811                 || addr[i].wc_tail->hash.buckets == NULL)
1812 #if (NGX_PCRE)
1813             && addr[i].nregex == 0
1814 #endif
1815             )
1816         {
1817             continue;
1818         }
1819 
1820         vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
1821         if (vn == NULL) {
1822             return NGX_ERROR;
1823         }
1824 
1825         addrs[i].conf.virtual_names = vn;
1826 
1827         vn->names.hash = addr[i].hash;
1828         vn->names.wc_head = addr[i].wc_head;
1829         vn->names.wc_tail = addr[i].wc_tail;
1830 #if (NGX_PCRE)
1831         vn->nregex = addr[i].nregex;
1832         vn->regex = addr[i].regex;
1833 #endif
1834     }
1835 
1836     return NGX_OK;
1837 }
1838 
1839 
1840 #if (NGX_HAVE_INET6)
1841 
1842 static ngx_int_t
1843 ngx_http_add_addrs6(ngx_conf_t *cf, ngx_http_port_t *hport,
1844     ngx_http_conf_addr_t *addr)
1845 {
1846     ngx_uint_t                 i;
1847     ngx_http_in6_addr_t       *addrs6;
1848     struct sockaddr_in6       *sin6;
1849     ngx_http_virtual_names_t  *vn;
1850 
1851     hport->addrs = ngx_pcalloc(cf->pool,
1852                                hport->naddrs * sizeof(ngx_http_in6_addr_t));
1853     if (hport->addrs == NULL) {
1854         return NGX_ERROR;
1855     }
1856 
1857     addrs6 = hport->addrs;
1858 
1859     for (i = 0; i < hport->naddrs; i++) {
1860 
1861         sin6 = (struct sockaddr_in6 *) addr[i].opt.sockaddr;
1862         addrs6[i].addr6 = sin6->sin6_addr;
1863         addrs6[i].conf.default_server = addr[i].default_server;
1864 #if (NGX_HTTP_SSL)
1865         addrs6[i].conf.ssl = addr[i].opt.ssl;
1866 #endif
1867 #if (NGX_HTTP_V2)
1868         addrs6[i].conf.http2 = addr[i].opt.http2;
1869 #endif
1870         addrs6[i].conf.proxy_protocol = addr[i].opt.proxy_protocol;
1871 
1872         if (addr[i].hash.buckets == NULL
1873             && (addr[i].wc_head == NULL
1874                 || addr[i].wc_head->hash.buckets == NULL)
1875             && (addr[i].wc_tail == NULL
1876                 || addr[i].wc_tail->hash.buckets == NULL)
1877 #if (NGX_PCRE)
1878             && addr[i].nregex == 0
1879 #endif
1880             )
1881         {
1882             continue;
1883         }
1884 
1885         vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
1886         if (vn == NULL) {
1887             return NGX_ERROR;
1888         }
1889 
1890         addrs6[i].conf.virtual_names = vn;
1891 
1892         vn->names.hash = addr[i].hash;
1893         vn->names.wc_head = addr[i].wc_head;
1894         vn->names.wc_tail = addr[i].wc_tail;
1895 #if (NGX_PCRE)
1896         vn->nregex = addr[i].nregex;
1897         vn->regex = addr[i].regex;
1898 #endif
1899     }
1900 
1901     return NGX_OK;
1902 }
1903 
1904 #endif
1905 
1906 
1907 char *
1908 ngx_http_types_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1909 {
1910     char  *p = conf;
1911 
1912     ngx_array_t     **types;
1913     ngx_str_t        *value, *default_type;
1914     ngx_uint_t        i, n, hash;
1915     ngx_hash_key_t   *type;
1916 
1917     types = (ngx_array_t **) (p + cmd->offset);
1918 
1919     if (*types == (void *) -1) {
1920         return NGX_CONF_OK;
1921     }
1922 
1923     default_type = cmd->post;
1924 
1925     if (*types == NULL) {
1926         *types = ngx_array_create(cf->temp_pool, 1, sizeof(ngx_hash_key_t));
1927         if (*types == NULL) {
1928             return NGX_CONF_ERROR;
1929         }
1930 
1931         if (default_type) {
1932             type = ngx_array_push(*types);
1933             if (type == NULL) {
1934                 return NGX_CONF_ERROR;
1935             }
1936 
1937             type->key = *default_type;
1938             type->key_hash = ngx_hash_key(default_type->data,
1939                                           default_type->len);
1940             type->value = (void *) 4;
1941         }
1942     }
1943 
1944     value = cf->args->elts;
1945 
1946     for (i = 1; i < cf->args->nelts; i++) {
1947 
1948         if (value[i].len == 1 && value[i].data[0] == '*') {
1949             *types = (void *) -1;
1950             return NGX_CONF_OK;
1951         }
1952 
1953         hash = ngx_hash_strlow(value[i].data, value[i].data, value[i].len);
1954         value[i].data[value[i].len] = '\0';
1955 
1956         type = (*types)->elts;
1957         for (n = 0; n < (*types)->nelts; n++) {
1958 
1959             if (ngx_strcmp(value[i].data, type[n].key.data) == 0) {
1960                 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1961                                    "duplicate MIME type \"%V\"", &value[i]);
1962                 goto next;
1963             }
1964         }
1965 
1966         type = ngx_array_push(*types);
1967         if (type == NULL) {
1968             return NGX_CONF_ERROR;
1969         }
1970 
1971         type->key = value[i];
1972         type->key_hash = hash;
1973         type->value = (void *) 4;
1974 
1975     next:
1976 
1977         continue;
1978     }
1979 
1980     return NGX_CONF_OK;
1981 }
1982 
1983 
1984 char *
1985 ngx_http_merge_types(ngx_conf_t *cf, ngx_array_t **keys, ngx_hash_t *types_hash,
1986     ngx_array_t **prev_keys, ngx_hash_t *prev_types_hash,
1987     ngx_str_t *default_types)
1988 {
1989     ngx_hash_init_t  hash;
1990 
1991     if (*keys) {
1992 
1993         if (*keys == (void *) -1) {
1994             return NGX_CONF_OK;
1995         }
1996 
1997         hash.hash = types_hash;
1998         hash.key = NULL;
1999         hash.max_size = 2048;
2000         hash.bucket_size = 64;
2001         hash.name = "test_types_hash";
2002         hash.pool = cf->pool;
2003         hash.temp_pool = NULL;
2004 
2005         if (ngx_hash_init(&hash, (*keys)->elts, (*keys)->nelts) != NGX_OK) {
2006             return NGX_CONF_ERROR;
2007         }
2008 
2009         return NGX_CONF_OK;
2010     }
2011 
2012     if (prev_types_hash->buckets == NULL) {
2013 
2014         if (*prev_keys == NULL) {
2015 
2016             if (ngx_http_set_default_types(cf, prev_keys, default_types)
2017                 != NGX_OK)
2018             {
2019                 return NGX_CONF_ERROR;
2020             }
2021 
2022         } else if (*prev_keys == (void *) -1) {
2023             *keys = *prev_keys;
2024             return NGX_CONF_OK;
2025         }
2026 
2027         hash.hash = prev_types_hash;
2028         hash.key = NULL;
2029         hash.max_size = 2048;
2030         hash.bucket_size = 64;
2031         hash.name = "test_types_hash";
2032         hash.pool = cf->pool;
2033         hash.temp_pool = NULL;
2034 
2035         if (ngx_hash_init(&hash, (*prev_keys)->elts, (*prev_keys)->nelts)
2036             != NGX_OK)
2037         {
2038             return NGX_CONF_ERROR;
2039         }
2040     }
2041 
2042     *types_hash = *prev_types_hash;
2043 
2044     return NGX_CONF_OK;
2045 
2046 }
2047 
2048 
2049 ngx_int_t
2050 ngx_http_set_default_types(ngx_conf_t *cf, ngx_array_t **types,
2051     ngx_str_t *default_type)
2052 {
2053     ngx_hash_key_t  *type;
2054 
2055     *types = ngx_array_create(cf->temp_pool, 1, sizeof(ngx_hash_key_t));
2056     if (*types == NULL) {
2057         return NGX_ERROR;
2058     }
2059 
2060     while (default_type->len) {
2061 
2062         type = ngx_array_push(*types);
2063         if (type == NULL) {
2064             return NGX_ERROR;
2065         }
2066 
2067         type->key = *default_type;
2068         type->key_hash = ngx_hash_key(default_type->data,
2069                                       default_type->len);
2070         type->value = (void *) 4;
2071 
2072         default_type++;
2073     }
2074 
2075     return NGX_OK;
2076 }