src/http/ngx_http.c - nginx-1.7.10

Global variables defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>


  8. static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  9. static ngx_int_t ngx_http_init_phases(ngx_conf_t *cf,
  10.     ngx_http_core_main_conf_t *cmcf);
  11. static ngx_int_t ngx_http_init_headers_in_hash(ngx_conf_t *cf,
  12.     ngx_http_core_main_conf_t *cmcf);
  13. static ngx_int_t ngx_http_init_phase_handlers(ngx_conf_t *cf,
  14.     ngx_http_core_main_conf_t *cmcf);

  15. static ngx_int_t ngx_http_add_addresses(ngx_conf_t *cf,
  16.     ngx_http_core_srv_conf_t *cscf, ngx_http_conf_port_t *port,
  17.     ngx_http_listen_opt_t *lsopt);
  18. static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
  19.     ngx_http_core_srv_conf_t *cscf, ngx_http_conf_port_t *port,
  20.     ngx_http_listen_opt_t *lsopt);
  21. static ngx_int_t ngx_http_add_server(ngx_conf_t *cf,
  22.     ngx_http_core_srv_conf_t *cscf, ngx_http_conf_addr_t *addr);

  23. static char *ngx_http_merge_servers(ngx_conf_t *cf,
  24.     ngx_http_core_main_conf_t *cmcf, ngx_http_module_t *module,
  25.     ngx_uint_t ctx_index);
  26. static char *ngx_http_merge_locations(ngx_conf_t *cf,
  27.     ngx_queue_t *locations, void **loc_conf, ngx_http_module_t *module,
  28.     ngx_uint_t ctx_index);
  29. static ngx_int_t ngx_http_init_locations(ngx_conf_t *cf,
  30.     ngx_http_core_srv_conf_t *cscf, ngx_http_core_loc_conf_t *pclcf);
  31. static ngx_int_t ngx_http_init_static_location_trees(ngx_conf_t *cf,
  32.     ngx_http_core_loc_conf_t *pclcf);
  33. static ngx_int_t ngx_http_cmp_locations(const ngx_queue_t *one,
  34.     const ngx_queue_t *two);
  35. static ngx_int_t ngx_http_join_exact_locations(ngx_conf_t *cf,
  36.     ngx_queue_t *locations);
  37. static void ngx_http_create_locations_list(ngx_queue_t *locations,
  38.     ngx_queue_t *q);
  39. static ngx_http_location_tree_node_t *
  40.     ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations,
  41.     size_t prefix);

  42. static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf,
  43.     ngx_http_core_main_conf_t *cmcf, ngx_array_t *ports);
  44. static ngx_int_t ngx_http_server_names(ngx_conf_t *cf,
  45.     ngx_http_core_main_conf_t *cmcf, ngx_http_conf_addr_t *addr);
  46. static ngx_int_t ngx_http_cmp_conf_addrs(const void *one, const void *two);
  47. static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one,
  48.     const void *two);

  49. static ngx_int_t ngx_http_init_listening(ngx_conf_t *cf,
  50.     ngx_http_conf_port_t *port);
  51. static ngx_listening_t *ngx_http_add_listening(ngx_conf_t *cf,
  52.     ngx_http_conf_addr_t *addr);
  53. static ngx_int_t ngx_http_add_addrs(ngx_conf_t *cf, ngx_http_port_t *hport,
  54.     ngx_http_conf_addr_t *addr);
  55. #if (NGX_HAVE_INET6)
  56. static ngx_int_t ngx_http_add_addrs6(ngx_conf_t *cf, ngx_http_port_t *hport,
  57.     ngx_http_conf_addr_t *addr);
  58. #endif

  59. ngx_uint_t   ngx_http_max_module;


  60. ngx_int_t  (*ngx_http_top_header_filter) (ngx_http_request_t *r);
  61. ngx_int_t  (*ngx_http_top_body_filter) (ngx_http_request_t *r, ngx_chain_t *ch);


  62. ngx_str_t  ngx_http_html_default_types[] = {
  63.     ngx_string("text/html"),
  64.     ngx_null_string
  65. };


  66. static ngx_command_t  ngx_http_commands[] = {

  67.     { ngx_string("http"),
  68.       NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
  69.       ngx_http_block,
  70.       0,
  71.       0,
  72.       NULL },

  73.       ngx_null_command
  74. };


  75. static ngx_core_module_t  ngx_http_module_ctx = {
  76.     ngx_string("http"),
  77.     NULL,
  78.     NULL
  79. };


  80. ngx_module_t  ngx_http_module = {
  81.     NGX_MODULE_V1,
  82.     &ngx_http_module_ctx,                  /* module context */
  83.     ngx_http_commands,                     /* module directives */
  84.     NGX_CORE_MODULE,                       /* module type */
  85.     NULL,                                  /* init master */
  86.     NULL,                                  /* init module */
  87.     NULL,                                  /* init process */
  88.     NULL,                                  /* init thread */
  89.     NULL,                                  /* exit thread */
  90.     NULL,                                  /* exit process */
  91.     NULL,                                  /* exit master */
  92.     NGX_MODULE_V1_PADDING
  93. };


  94. static char *
  95. ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  96. {
  97.     char                        *rv;
  98.     ngx_uint_t                   mi, m, s;
  99.     ngx_conf_t                   pcf;
  100.     ngx_http_module_t           *module;
  101.     ngx_http_conf_ctx_t         *ctx;
  102.     ngx_http_core_loc_conf_t    *clcf;
  103.     ngx_http_core_srv_conf_t   **cscfp;
  104.     ngx_http_core_main_conf_t   *cmcf;

  105.     /* the main http context */

  106.     ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
  107.     if (ctx == NULL) {
  108.         return NGX_CONF_ERROR;
  109.     }

  110.     *(ngx_http_conf_ctx_t **) conf = ctx;


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

  112.     ngx_http_max_module = 0;
  113.     for (m = 0; ngx_modules[m]; m++) {
  114.         if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
  115.             continue;
  116.         }

  117.         ngx_modules[m]->ctx_index = ngx_http_max_module++;
  118.     }


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

  120.     ctx->main_conf = ngx_pcalloc(cf->pool,
  121.                                  sizeof(void *) * ngx_http_max_module);
  122.     if (ctx->main_conf == NULL) {
  123.         return NGX_CONF_ERROR;
  124.     }


  125.     /*
  126.      * the http null srv_conf context, it is used to merge
  127.      * the server{}s' srv_conf's
  128.      */

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


  133.     /*
  134.      * the http null loc_conf context, it is used to merge
  135.      * the server{}s' loc_conf's
  136.      */

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


  141.     /*
  142.      * create the main_conf's, the null srv_conf's, and the null loc_conf's
  143.      * of the all http modules
  144.      */

  145.     for (m = 0; ngx_modules[m]; m++) {
  146.         if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
  147.             continue;
  148.         }

  149.         module = ngx_modules[m]->ctx;
  150.         mi = ngx_modules[m]->ctx_index;

  151.         if (module->create_main_conf) {
  152.             ctx->main_conf[mi] = module->create_main_conf(cf);
  153.             if (ctx->main_conf[mi] == NULL) {
  154.                 return NGX_CONF_ERROR;
  155.             }
  156.         }

  157.         if (module->create_srv_conf) {
  158.             ctx->srv_conf[mi] = module->create_srv_conf(cf);
  159.             if (ctx->srv_conf[mi] == NULL) {
  160.                 return NGX_CONF_ERROR;
  161.             }
  162.         }

  163.         if (module->create_loc_conf) {
  164.             ctx->loc_conf[mi] = module->create_loc_conf(cf);
  165.             if (ctx->loc_conf[mi] == NULL) {
  166.                 return NGX_CONF_ERROR;
  167.             }
  168.         }
  169.     }

  170.     pcf = *cf;
  171.     cf->ctx = ctx;

  172.     for (m = 0; ngx_modules[m]; m++) {
  173.         if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
  174.             continue;
  175.         }

  176.         module = ngx_modules[m]->ctx;

  177.         if (module->preconfiguration) {
  178.             if (module->preconfiguration(cf) != NGX_OK) {
  179.                 return NGX_CONF_ERROR;
  180.             }
  181.         }
  182.     }

  183.     /* parse inside the http{} block */

  184.     cf->module_type = NGX_HTTP_MODULE;
  185.     cf->cmd_type = NGX_HTTP_MAIN_CONF;
  186.     rv = ngx_conf_parse(cf, NULL);

  187.     if (rv != NGX_CONF_OK) {
  188.         goto failed;
  189.     }

  190.     /*
  191.      * init http{} main_conf's, merge the server{}s' srv_conf's
  192.      * and its location{}s' loc_conf's
  193.      */

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

  196.     for (m = 0; ngx_modules[m]; m++) {
  197.         if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
  198.             continue;
  199.         }

  200.         module = ngx_modules[m]->ctx;
  201.         mi = ngx_modules[m]->ctx_index;

  202.         /* init http{} main_conf's */

  203.         if (module->init_main_conf) {
  204.             rv = module->init_main_conf(cf, ctx->main_conf[mi]);
  205.             if (rv != NGX_CONF_OK) {
  206.                 goto failed;
  207.             }
  208.         }

  209.         rv = ngx_http_merge_servers(cf, cmcf, module, mi);
  210.         if (rv != NGX_CONF_OK) {
  211.             goto failed;
  212.         }
  213.     }


  214.     /* create location trees */

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

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

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

  220.         if (ngx_http_init_static_location_trees(cf, clcf) != NGX_OK) {
  221.             return NGX_CONF_ERROR;
  222.         }
  223.     }


  224.     if (ngx_http_init_phases(cf, cmcf) != NGX_OK) {
  225.         return NGX_CONF_ERROR;
  226.     }

  227.     if (ngx_http_init_headers_in_hash(cf, cmcf) != NGX_OK) {
  228.         return NGX_CONF_ERROR;
  229.     }


  230.     for (m = 0; ngx_modules[m]; m++) {
  231.         if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
  232.             continue;
  233.         }

  234.         module = ngx_modules[m]->ctx;

  235.         if (module->postconfiguration) {
  236.             if (module->postconfiguration(cf) != NGX_OK) {
  237.                 return NGX_CONF_ERROR;
  238.             }
  239.         }
  240.     }

  241.     if (ngx_http_variables_init_vars(cf) != NGX_OK) {
  242.         return NGX_CONF_ERROR;
  243.     }

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

  248.     *cf = pcf;


  249.     if (ngx_http_init_phase_handlers(cf, cmcf) != NGX_OK) {
  250.         return NGX_CONF_ERROR;
  251.     }


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

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

  256.     return NGX_CONF_OK;

  257. failed:

  258.     *cf = pcf;

  259.     return rv;
  260. }


  261. static ngx_int_t
  262. ngx_http_init_phases(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
  263. {
  264.     if (ngx_array_init(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers,
  265.                        cf->pool, 1, sizeof(ngx_http_handler_pt))
  266.         != NGX_OK)
  267.     {
  268.         return NGX_ERROR;
  269.     }

  270.     if (ngx_array_init(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers,
  271.                        cf->pool, 1, sizeof(ngx_http_handler_pt))
  272.         != NGX_OK)
  273.     {
  274.         return NGX_ERROR;
  275.     }

  276.     if (ngx_array_init(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers,
  277.                        cf->pool, 1, sizeof(ngx_http_handler_pt))
  278.         != NGX_OK)
  279.     {
  280.         return NGX_ERROR;
  281.     }

  282.     if (ngx_array_init(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers,
  283.                        cf->pool, 1, sizeof(ngx_http_handler_pt))
  284.         != NGX_OK)
  285.     {
  286.         return NGX_ERROR;
  287.     }

  288.     if (ngx_array_init(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers,
  289.                        cf->pool, 2, sizeof(ngx_http_handler_pt))
  290.         != NGX_OK)
  291.     {
  292.         return NGX_ERROR;
  293.     }

  294.     if (ngx_array_init(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers,
  295.                        cf->pool, 4, sizeof(ngx_http_handler_pt))
  296.         != NGX_OK)
  297.     {
  298.         return NGX_ERROR;
  299.     }

  300.     if (ngx_array_init(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers,
  301.                        cf->pool, 1, sizeof(ngx_http_handler_pt))
  302.         != NGX_OK)
  303.     {
  304.         return NGX_ERROR;
  305.     }

  306.     return NGX_OK;
  307. }


  308. static ngx_int_t
  309. ngx_http_init_headers_in_hash(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
  310. {
  311.     ngx_array_t         headers_in;
  312.     ngx_hash_key_t     *hk;
  313.     ngx_hash_init_t     hash;
  314.     ngx_http_header_t  *header;

  315.     if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t))
  316.         != NGX_OK)
  317.     {
  318.         return NGX_ERROR;
  319.     }

  320.     for (header = ngx_http_headers_in; header->name.len; header++) {
  321.         hk = ngx_array_push(&headers_in);
  322.         if (hk == NULL) {
  323.             return NGX_ERROR;
  324.         }

  325.         hk->key = header->name;
  326.         hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len);
  327.         hk->value = header;
  328.     }

  329.     hash.hash = &cmcf->headers_in_hash;
  330.     hash.key = ngx_hash_key_lc;
  331.     hash.max_size = 512;
  332.     hash.bucket_size = ngx_align(64, ngx_cacheline_size);
  333.     hash.name = "headers_in_hash";
  334.     hash.pool = cf->pool;
  335.     hash.temp_pool = NULL;

  336.     if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) {
  337.         return NGX_ERROR;
  338.     }

  339.     return NGX_OK;
  340. }


  341. static ngx_int_t
  342. ngx_http_init_phase_handlers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
  343. {
  344.     ngx_int_t                   j;
  345.     ngx_uint_t                  i, n;
  346.     ngx_uint_t                  find_config_index, use_rewrite, use_access;
  347.     ngx_http_handler_pt        *h;
  348.     ngx_http_phase_handler_t   *ph;
  349.     ngx_http_phase_handler_pt   checker;

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

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

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

  359.     ph = ngx_pcalloc(cf->pool,
  360.                      n * sizeof(ngx_http_phase_handler_t) + sizeof(void *));
  361.     if (ph == NULL) {
  362.         return NGX_ERROR;
  363.     }

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

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

  368.         switch (i) {

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

  374.             break;

  375.         case NGX_HTTP_FIND_CONFIG_PHASE:
  376.             find_config_index = n;

  377.             ph->checker = ngx_http_core_find_config_phase;
  378.             n++;
  379.             ph++;

  380.             continue;

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

  386.             break;

  387.         case NGX_HTTP_POST_REWRITE_PHASE:
  388.             if (use_rewrite) {
  389.                 ph->checker = ngx_http_core_post_rewrite_phase;
  390.                 ph->next = find_config_index;
  391.                 n++;
  392.                 ph++;
  393.             }

  394.             continue;

  395.         case NGX_HTTP_ACCESS_PHASE:
  396.             checker = ngx_http_core_access_phase;
  397.             n++;
  398.             break;

  399.         case NGX_HTTP_POST_ACCESS_PHASE:
  400.             if (use_access) {
  401.                 ph->checker = ngx_http_core_post_access_phase;
  402.                 ph->next = n;
  403.                 ph++;
  404.             }

  405.             continue;

  406.         case NGX_HTTP_TRY_FILES_PHASE:
  407.             if (cmcf->try_files) {
  408.                 ph->checker = ngx_http_core_try_files_phase;
  409.                 n++;
  410.                 ph++;
  411.             }

  412.             continue;

  413.         case NGX_HTTP_CONTENT_PHASE:
  414.             checker = ngx_http_core_content_phase;
  415.             break;

  416.         default:
  417.             checker = ngx_http_core_generic_phase;
  418.         }

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

  420.         for (j = cmcf->phases[i].handlers.nelts - 1; j >=0; j--) {
  421.             ph->checker = checker;
  422.             ph->handler = h[j];
  423.             ph->next = n;
  424.             ph++;
  425.         }
  426.     }

  427.     return NGX_OK;
  428. }


  429. static char *
  430. ngx_http_merge_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
  431.     ngx_http_module_t *module, ngx_uint_t ctx_index)
  432. {
  433.     char                        *rv;
  434.     ngx_uint_t                   s;
  435.     ngx_http_conf_ctx_t         *ctx, saved;
  436.     ngx_http_core_loc_conf_t    *clcf;
  437.     ngx_http_core_srv_conf_t   **cscfp;

  438.     cscfp = cmcf->servers.elts;
  439.     ctx = (ngx_http_conf_ctx_t *) cf->ctx;
  440.     saved = *ctx;
  441.     rv = NGX_CONF_OK;

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

  443.         /* merge the server{}s' srv_conf's */

  444.         ctx->srv_conf = cscfp[s]->ctx->srv_conf;

  445.         if (module->merge_srv_conf) {
  446.             rv = module->merge_srv_conf(cf, saved.srv_conf[ctx_index],
  447.                                         cscfp[s]->ctx->srv_conf[ctx_index]);
  448.             if (rv != NGX_CONF_OK) {
  449.                 goto failed;
  450.             }
  451.         }

  452.         if (module->merge_loc_conf) {

  453.             /* merge the server{}'s loc_conf */

  454.             ctx->loc_conf = cscfp[s]->ctx->loc_conf;

  455.             rv = module->merge_loc_conf(cf, saved.loc_conf[ctx_index],
  456.                                         cscfp[s]->ctx->loc_conf[ctx_index]);
  457.             if (rv != NGX_CONF_OK) {
  458.                 goto failed;
  459.             }

  460.             /* merge the locations{}' loc_conf's */

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

  462.             rv = ngx_http_merge_locations(cf, clcf->locations,
  463.                                           cscfp[s]->ctx->loc_conf,
  464.                                           module, ctx_index);
  465.             if (rv != NGX_CONF_OK) {
  466.                 goto failed;
  467.             }
  468.         }
  469.     }

  470. failed:

  471.     *ctx = saved;

  472.     return rv;
  473. }


  474. static char *
  475. ngx_http_merge_locations(ngx_conf_t *cf, ngx_queue_t *locations,
  476.     void **loc_conf, ngx_http_module_t *module, ngx_uint_t ctx_index)
  477. {
  478.     char                       *rv;
  479.     ngx_queue_t                *q;
  480.     ngx_http_conf_ctx_t        *ctx, saved;
  481.     ngx_http_core_loc_conf_t   *clcf;
  482.     ngx_http_location_queue_t  *lq;

  483.     if (locations == NULL) {
  484.         return NGX_CONF_OK;
  485.     }

  486.     ctx = (ngx_http_conf_ctx_t *) cf->ctx;
  487.     saved = *ctx;

  488.     for (q = ngx_queue_head(locations);
  489.          q != ngx_queue_sentinel(locations);
  490.          q = ngx_queue_next(q))
  491.     {
  492.         lq = (ngx_http_location_queue_t *) q;

  493.         clcf = lq->exact ? lq->exact : lq->inclusive;
  494.         ctx->loc_conf = clcf->loc_conf;

  495.         rv = module->merge_loc_conf(cf, loc_conf[ctx_index],
  496.                                     clcf->loc_conf[ctx_index]);
  497.         if (rv != NGX_CONF_OK) {
  498.             return rv;
  499.         }

  500.         rv = ngx_http_merge_locations(cf, clcf->locations, clcf->loc_conf,
  501.                                       module, ctx_index);
  502.         if (rv != NGX_CONF_OK) {
  503.             return rv;
  504.         }
  505.     }

  506.     *ctx = saved;

  507.     return NGX_CONF_OK;
  508. }


  509. static ngx_int_t
  510. ngx_http_init_locations(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
  511.     ngx_http_core_loc_conf_t *pclcf)
  512. {
  513.     ngx_uint_t                   n;
  514.     ngx_queue_t                 *q, *locations, *named, tail;
  515.     ngx_http_core_loc_conf_t    *clcf;
  516.     ngx_http_location_queue_t   *lq;
  517.     ngx_http_core_loc_conf_t   **clcfp;
  518. #if (NGX_PCRE)
  519.     ngx_uint_t                   r;
  520.     ngx_queue_t                 *regex;
  521. #endif

  522.     locations = pclcf->locations;

  523.     if (locations == NULL) {
  524.         return NGX_OK;
  525.     }

  526.     ngx_queue_sort(locations, ngx_http_cmp_locations);

  527.     named = NULL;
  528.     n = 0;
  529. #if (NGX_PCRE)
  530.     regex = NULL;
  531.     r = 0;
  532. #endif

  533.     for (q = ngx_queue_head(locations);
  534.          q != ngx_queue_sentinel(locations);
  535.          q = ngx_queue_next(q))
  536.     {
  537.         lq = (ngx_http_location_queue_t *) q;

  538.         clcf = lq->exact ? lq->exact : lq->inclusive;

  539.         if (ngx_http_init_locations(cf, NULL, clcf) != NGX_OK) {
  540.             return NGX_ERROR;
  541.         }

  542. #if (NGX_PCRE)

  543.         if (clcf->regex) {
  544.             r++;

  545.             if (regex == NULL) {
  546.                 regex = q;
  547.             }

  548.             continue;
  549.         }

  550. #endif

  551.         if (clcf->named) {
  552.             n++;

  553.             if (named == NULL) {
  554.                 named = q;
  555.             }

  556.             continue;
  557.         }

  558.         if (clcf->noname) {
  559.             break;
  560.         }
  561.     }

  562.     if (q != ngx_queue_sentinel(locations)) {
  563.         ngx_queue_split(locations, q, &tail);
  564.     }

  565.     if (named) {
  566.         clcfp = ngx_palloc(cf->pool,
  567.                            (n + 1) * sizeof(ngx_http_core_loc_conf_t *));
  568.         if (clcfp == NULL) {
  569.             return NGX_ERROR;
  570.         }

  571.         cscf->named_locations = clcfp;

  572.         for (q = named;
  573.              q != ngx_queue_sentinel(locations);
  574.              q = ngx_queue_next(q))
  575.         {
  576.             lq = (ngx_http_location_queue_t *) q;

  577.             *(clcfp++) = lq->exact;
  578.         }

  579.         *clcfp = NULL;

  580.         ngx_queue_split(locations, named, &tail);
  581.     }

  582. #if (NGX_PCRE)

  583.     if (regex) {

  584.         clcfp = ngx_palloc(cf->pool,
  585.                            (r + 1) * sizeof(ngx_http_core_loc_conf_t *));
  586.         if (clcfp == NULL) {
  587.             return NGX_ERROR;
  588.         }

  589.         pclcf->regex_locations = clcfp;

  590.         for (q = regex;
  591.              q != ngx_queue_sentinel(locations);
  592.              q = ngx_queue_next(q))
  593.         {
  594.             lq = (ngx_http_location_queue_t *) q;

  595.             *(clcfp++) = lq->exact;
  596.         }

  597.         *clcfp = NULL;

  598.         ngx_queue_split(locations, regex, &tail);
  599.     }

  600. #endif

  601.     return NGX_OK;
  602. }


  603. static ngx_int_t
  604. ngx_http_init_static_location_trees(ngx_conf_t *cf,
  605.     ngx_http_core_loc_conf_t *pclcf)
  606. {
  607.     ngx_queue_t                *q, *locations;
  608.     ngx_http_core_loc_conf_t   *clcf;
  609.     ngx_http_location_queue_t  *lq;

  610.     locations = pclcf->locations;

  611.     if (locations == NULL) {
  612.         return NGX_OK;
  613.     }

  614.     if (ngx_queue_empty(locations)) {
  615.         return NGX_OK;
  616.     }

  617.     for (q = ngx_queue_head(locations);
  618.          q != ngx_queue_sentinel(locations);
  619.          q = ngx_queue_next(q))
  620.     {
  621.         lq = (ngx_http_location_queue_t *) q;

  622.         clcf = lq->exact ? lq->exact : lq->inclusive;

  623.         if (ngx_http_init_static_location_trees(cf, clcf) != NGX_OK) {
  624.             return NGX_ERROR;
  625.         }
  626.     }

  627.     if (ngx_http_join_exact_locations(cf, locations) != NGX_OK) {
  628.         return NGX_ERROR;
  629.     }

  630.     ngx_http_create_locations_list(locations, ngx_queue_head(locations));

  631.     pclcf->static_locations = ngx_http_create_locations_tree(cf, locations, 0);
  632.     if (pclcf->static_locations == NULL) {
  633.         return NGX_ERROR;
  634.     }

  635.     return NGX_OK;
  636. }


  637. ngx_int_t
  638. ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
  639.     ngx_http_core_loc_conf_t *clcf)
  640. {
  641.     ngx_http_location_queue_t  *lq;

  642.     if (*locations == NULL) {
  643.         *locations = ngx_palloc(cf->temp_pool,
  644.                                 sizeof(ngx_http_location_queue_t));
  645.         if (*locations == NULL) {
  646.             return NGX_ERROR;
  647.         }

  648.         ngx_queue_init(*locations);
  649.     }

  650.     lq = ngx_palloc(cf->temp_pool, sizeof(ngx_http_location_queue_t));
  651.     if (lq == NULL) {
  652.         return NGX_ERROR;
  653.     }

  654.     if (clcf->exact_match
  655. #if (NGX_PCRE)
  656.         || clcf->regex
  657. #endif
  658.         || clcf->named || clcf->noname)
  659.     {
  660.         lq->exact = clcf;
  661.         lq->inclusive = NULL;

  662.     } else {
  663.         lq->exact = NULL;
  664.         lq->inclusive = clcf;
  665.     }

  666.     lq->name = &clcf->name;
  667.     lq->file_name = cf->conf_file->file.name.data;
  668.     lq->line = cf->conf_file->line;

  669.     ngx_queue_init(&lq->list);

  670.     ngx_queue_insert_tail(*locations, &lq->queue);

  671.     return NGX_OK;
  672. }


  673. static ngx_int_t
  674. ngx_http_cmp_locations(const ngx_queue_t *one, const ngx_queue_t *two)
  675. {
  676.     ngx_int_t                   rc;
  677.     ngx_http_core_loc_conf_t   *first, *second;
  678.     ngx_http_location_queue_t  *lq1, *lq2;

  679.     lq1 = (ngx_http_location_queue_t *) one;
  680.     lq2 = (ngx_http_location_queue_t *) two;

  681.     first = lq1->exact ? lq1->exact : lq1->inclusive;
  682.     second = lq2->exact ? lq2->exact : lq2->inclusive;

  683.     if (first->noname && !second->noname) {
  684.         /* shift no named locations to the end */
  685.         return 1;
  686.     }

  687.     if (!first->noname && second->noname) {
  688.         /* shift no named locations to the end */
  689.         return -1;
  690.     }

  691.     if (first->noname || second->noname) {
  692.         /* do not sort no named locations */
  693.         return 0;
  694.     }

  695.     if (first->named && !second->named) {
  696.         /* shift named locations to the end */
  697.         return 1;
  698.     }

  699.     if (!first->named && second->named) {
  700.         /* shift named locations to the end */
  701.         return -1;
  702.     }

  703.     if (first->named && second->named) {
  704.         return ngx_strcmp(first->name.data, second->name.data);
  705.     }

  706. #if (NGX_PCRE)

  707.     if (first->regex && !second->regex) {
  708.         /* shift the regex matches to the end */
  709.         return 1;
  710.     }

  711.     if (!first->regex && second->regex) {
  712.         /* shift the regex matches to the end */
  713.         return -1;
  714.     }

  715.     if (first->regex || second->regex) {
  716.         /* do not sort the regex matches */
  717.         return 0;
  718.     }

  719. #endif

  720.     rc = ngx_filename_cmp(first->name.data, second->name.data,
  721.                           ngx_min(first->name.len, second->name.len) + 1);

  722.     if (rc == 0 && !first->exact_match && second->exact_match) {
  723.         /* an exact match must be before the same inclusive one */
  724.         return 1;
  725.     }

  726.     return rc;
  727. }


  728. static ngx_int_t
  729. ngx_http_join_exact_locations(ngx_conf_t *cf, ngx_queue_t *locations)
  730. {
  731.     ngx_queue_t                *q, *x;
  732.     ngx_http_location_queue_t  *lq, *lx;

  733.     q = ngx_queue_head(locations);

  734.     while (q != ngx_queue_last(locations)) {

  735.         x = ngx_queue_next(q);

  736.         lq = (ngx_http_location_queue_t *) q;
  737.         lx = (ngx_http_location_queue_t *) x;

  738.         if (lq->name->len == lx->name->len
  739.             && ngx_filename_cmp(lq->name->data, lx->name->data, lx->name->len)
  740.                == 0)
  741.         {
  742.             if ((lq->exact && lx->exact) || (lq->inclusive && lx->inclusive)) {
  743.                 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  744.                               "duplicate location \"%V\" in %s:%ui",
  745.                               lx->name, lx->file_name, lx->line);

  746.                 return NGX_ERROR;
  747.             }

  748.             lq->inclusive = lx->inclusive;

  749.             ngx_queue_remove(x);

  750.             continue;
  751.         }

  752.         q = ngx_queue_next(q);
  753.     }

  754.     return NGX_OK;
  755. }


  756. static void
  757. ngx_http_create_locations_list(ngx_queue_t *locations, ngx_queue_t *q)
  758. {
  759.     u_char                     *name;
  760.     size_t                      len;
  761.     ngx_queue_t                *x, tail;
  762.     ngx_http_location_queue_t  *lq, *lx;

  763.     if (q == ngx_queue_last(locations)) {
  764.         return;
  765.     }

  766.     lq = (ngx_http_location_queue_t *) q;

  767.     if (lq->inclusive == NULL) {
  768.         ngx_http_create_locations_list(locations, ngx_queue_next(q));
  769.         return;
  770.     }

  771.     len = lq->name->len;
  772.     name = lq->name->data;

  773.     for (x = ngx_queue_next(q);
  774.          x != ngx_queue_sentinel(locations);
  775.          x = ngx_queue_next(x))
  776.     {
  777.         lx = (ngx_http_location_queue_t *) x;

  778.         if (len > lx->name->len
  779.             || ngx_filename_cmp(name, lx->name->data, len) != 0)
  780.         {
  781.             break;
  782.         }
  783.     }

  784.     q = ngx_queue_next(q);

  785.     if (q == x) {
  786.         ngx_http_create_locations_list(locations, x);
  787.         return;
  788.     }

  789.     ngx_queue_split(locations, q, &tail);
  790.     ngx_queue_add(&lq->list, &tail);

  791.     if (x == ngx_queue_sentinel(locations)) {
  792.         ngx_http_create_locations_list(&lq->list, ngx_queue_head(&lq->list));
  793.         return;
  794.     }

  795.     ngx_queue_split(&lq->list, x, &tail);
  796.     ngx_queue_add(locations, &tail);

  797.     ngx_http_create_locations_list(&lq->list, ngx_queue_head(&lq->list));

  798.     ngx_http_create_locations_list(locations, x);
  799. }


  800. /*
  801. * to keep cache locality for left leaf nodes, allocate nodes in following
  802. * order: node, left subtree, right subtree, inclusive subtree
  803. */

  804. static ngx_http_location_tree_node_t *
  805. ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations,
  806.     size_t prefix)
  807. {
  808.     size_t                          len;
  809.     ngx_queue_t                    *q, tail;
  810.     ngx_http_location_queue_t      *lq;
  811.     ngx_http_location_tree_node_t  *node;

  812.     q = ngx_queue_middle(locations);

  813.     lq = (ngx_http_location_queue_t *) q;
  814.     len = lq->name->len - prefix;

  815.     node = ngx_palloc(cf->pool,
  816.                       offsetof(ngx_http_location_tree_node_t, name) + len);
  817.     if (node == NULL) {
  818.         return NULL;
  819.     }

  820.     node->left = NULL;
  821.     node->right = NULL;
  822.     node->tree = NULL;
  823.     node->exact = lq->exact;
  824.     node->inclusive = lq->inclusive;

  825.     node->auto_redirect = (u_char) ((lq->exact && lq->exact->auto_redirect)
  826.                            || (lq->inclusive && lq->inclusive->auto_redirect));

  827.     node->len = (u_char) len;
  828.     ngx_memcpy(node->name, &lq->name->data[prefix], len);

  829.     ngx_queue_split(locations, q, &tail);

  830.     if (ngx_queue_empty(locations)) {
  831.         /*
  832.          * ngx_queue_split() insures that if left part is empty,
  833.          * then right one is empty too
  834.          */
  835.         goto inclusive;
  836.     }

  837.     node->left = ngx_http_create_locations_tree(cf, locations, prefix);
  838.     if (node->left == NULL) {
  839.         return NULL;
  840.     }

  841.     ngx_queue_remove(q);

  842.     if (ngx_queue_empty(&tail)) {
  843.         goto inclusive;
  844.     }

  845.     node->right = ngx_http_create_locations_tree(cf, &tail, prefix);
  846.     if (node->right == NULL) {
  847.         return NULL;
  848.     }

  849. inclusive:

  850.     if (ngx_queue_empty(&lq->list)) {
  851.         return node;
  852.     }

  853.     node->tree = ngx_http_create_locations_tree(cf, &lq->list, prefix + len);
  854.     if (node->tree == NULL) {
  855.         return NULL;
  856.     }

  857.     return node;
  858. }


  859. ngx_int_t
  860. ngx_http_add_listen(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
  861.     ngx_http_listen_opt_t *lsopt)
  862. {
  863.     in_port_t                   p;
  864.     ngx_uint_t                  i;
  865.     struct sockaddr            *sa;
  866.     struct sockaddr_in         *sin;
  867.     ngx_http_conf_port_t       *port;
  868.     ngx_http_core_main_conf_t  *cmcf;
  869. #if (NGX_HAVE_INET6)
  870.     struct sockaddr_in6        *sin6;
  871. #endif

  872.     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

  873.     if (cmcf->ports == NULL) {
  874.         cmcf->ports = ngx_array_create(cf->temp_pool, 2,
  875.                                        sizeof(ngx_http_conf_port_t));
  876.         if (cmcf->ports == NULL) {
  877.             return NGX_ERROR;
  878.         }
  879.     }

  880.     sa = &lsopt->u.sockaddr;

  881.     switch (sa->sa_family) {

  882. #if (NGX_HAVE_INET6)
  883.     case AF_INET6:
  884.         sin6 = &lsopt->u.sockaddr_in6;
  885.         p = sin6->sin6_port;
  886.         break;
  887. #endif

  888. #if (NGX_HAVE_UNIX_DOMAIN)
  889.     case AF_UNIX:
  890.         p = 0;
  891.         break;
  892. #endif

  893.     default: /* AF_INET */
  894.         sin = &lsopt->u.sockaddr_in;
  895.         p = sin->sin_port;
  896.         break;
  897.     }

  898.     port = cmcf->ports->elts;
  899.     for (i = 0; i < cmcf->ports->nelts; i++) {

  900.         if (p != port[i].port || sa->sa_family != port[i].family) {
  901.             continue;
  902.         }

  903.         /* a port is already in the port list */

  904.         return ngx_http_add_addresses(cf, cscf, &port[i], lsopt);
  905.     }

  906.     /* add a port to the port list */

  907.     port = ngx_array_push(cmcf->ports);
  908.     if (port == NULL) {
  909.         return NGX_ERROR;
  910.     }

  911.     port->family = sa->sa_family;
  912.     port->port = p;
  913.     port->addrs.elts = NULL;

  914.     return ngx_http_add_address(cf, cscf, port, lsopt);
  915. }


  916. static ngx_int_t
  917. ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
  918.     ngx_http_conf_port_t *port, ngx_http_listen_opt_t *lsopt)
  919. {
  920.     u_char                *p;
  921.     size_t                 len, off;
  922.     ngx_uint_t             i, default_server;
  923.     struct sockaddr       *sa;
  924.     ngx_http_conf_addr_t  *addr;
  925. #if (NGX_HAVE_UNIX_DOMAIN)
  926.     struct sockaddr_un    *saun;
  927. #endif
  928. #if (NGX_HTTP_SSL)
  929.     ngx_uint_t             ssl;
  930. #endif
  931. #if (NGX_HTTP_SPDY)
  932.     ngx_uint_t             spdy;
  933. #endif

  934.     /*
  935.      * we cannot compare whole sockaddr struct's as kernel
  936.      * may fill some fields in inherited sockaddr struct's
  937.      */

  938.     sa = &lsopt->u.sockaddr;

  939.     switch (sa->sa_family) {

  940. #if (NGX_HAVE_INET6)
  941.     case AF_INET6:
  942.         off = offsetof(struct sockaddr_in6, sin6_addr);
  943.         len = 16;
  944.         break;
  945. #endif

  946. #if (NGX_HAVE_UNIX_DOMAIN)
  947.     case AF_UNIX:
  948.         off = offsetof(struct sockaddr_un, sun_path);
  949.         len = sizeof(saun->sun_path);
  950.         break;
  951. #endif

  952.     default: /* AF_INET */
  953.         off = offsetof(struct sockaddr_in, sin_addr);
  954.         len = 4;
  955.         break;
  956.     }

  957.     p = lsopt->u.sockaddr_data + off;

  958.     addr = port->addrs.elts;

  959.     for (i = 0; i < port->addrs.nelts; i++) {

  960.         if (ngx_memcmp(p, addr[i].opt.u.sockaddr_data + off, len) != 0) {
  961.             continue;
  962.         }

  963.         /* the address is already in the address list */

  964.         if (ngx_http_add_server(cf, cscf, &addr[i]) != NGX_OK) {
  965.             return NGX_ERROR;
  966.         }

  967.         /* preserve default_server bit during listen options overwriting */
  968.         default_server = addr[i].opt.default_server;

  969. #if (NGX_HTTP_SSL)
  970.         ssl = lsopt->ssl || addr[i].opt.ssl;
  971. #endif
  972. #if (NGX_HTTP_SPDY)
  973.         spdy = lsopt->spdy || addr[i].opt.spdy;
  974. #endif

  975.         if (lsopt->set) {

  976.             if (addr[i].opt.set) {
  977.                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  978.                         "duplicate listen options for %s", addr[i].opt.addr);
  979.                 return NGX_ERROR;
  980.             }

  981.             addr[i].opt = *lsopt;
  982.         }

  983.         /* check the duplicate "default" server for this address:port */

  984.         if (lsopt->default_server) {

  985.             if (default_server) {
  986.                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  987.                         "a duplicate default server for %s", addr[i].opt.addr);
  988.                 return NGX_ERROR;
  989.             }

  990.             default_server = 1;
  991.             addr[i].default_server = cscf;
  992.         }

  993.         addr[i].opt.default_server = default_server;
  994. #if (NGX_HTTP_SSL)
  995.         addr[i].opt.ssl = ssl;
  996. #endif
  997. #if (NGX_HTTP_SPDY)
  998.         addr[i].opt.spdy = spdy;
  999. #endif

  1000.         return NGX_OK;
  1001.     }

  1002.     /* add the address to the addresses list that bound to this port */

  1003.     return ngx_http_add_address(cf, cscf, port, lsopt);
  1004. }


  1005. /*
  1006. * add the server address, the server names and the server core module
  1007. * configurations to the port list
  1008. */

  1009. static ngx_int_t
  1010. ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
  1011.     ngx_http_conf_port_t *port, ngx_http_listen_opt_t *lsopt)
  1012. {
  1013.     ngx_http_conf_addr_t  *addr;

  1014.     if (port->addrs.elts == NULL) {
  1015.         if (ngx_array_init(&port->addrs, cf->temp_pool, 4,
  1016.                            sizeof(ngx_http_conf_addr_t))
  1017.             != NGX_OK)
  1018.         {
  1019.             return NGX_ERROR;
  1020.         }
  1021.     }

  1022. #if (NGX_HTTP_SPDY && NGX_HTTP_SSL                                            \
  1023.      && !defined TLSEXT_TYPE_application_layer_protocol_negotiation           \
  1024.      && !defined TLSEXT_TYPE_next_proto_neg)
  1025.     if (lsopt->spdy && lsopt->ssl) {
  1026.         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
  1027.                            "nginx was built without OpenSSL ALPN or NPN "
  1028.                            "support, SPDY is not enabled for %s", lsopt->addr);
  1029.     }
  1030. #endif

  1031.     addr = ngx_array_push(&port->addrs);
  1032.     if (addr == NULL) {
  1033.         return NGX_ERROR;
  1034.     }

  1035.     addr->opt = *lsopt;
  1036.     addr->hash.buckets = NULL;
  1037.     addr->hash.size = 0;
  1038.     addr->wc_head = NULL;
  1039.     addr->wc_tail = NULL;
  1040. #if (NGX_PCRE)
  1041.     addr->nregex = 0;
  1042.     addr->regex = NULL;
  1043. #endif
  1044.     addr->default_server = cscf;
  1045.     addr->servers.elts = NULL;

  1046.     return ngx_http_add_server(cf, cscf, addr);
  1047. }


  1048. /* add the server core module configuration to the address:port */

  1049. static ngx_int_t
  1050. ngx_http_add_server(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
  1051.     ngx_http_conf_addr_t *addr)
  1052. {
  1053.     ngx_uint_t                  i;
  1054.     ngx_http_core_srv_conf_t  **server;

  1055.     if (addr->servers.elts == NULL) {
  1056.         if (ngx_array_init(&addr->servers, cf->temp_pool, 4,
  1057.                            sizeof(ngx_http_core_srv_conf_t *))
  1058.             != NGX_OK)
  1059.         {
  1060.             return NGX_ERROR;
  1061.         }

  1062.     } else {
  1063.         server = addr->servers.elts;
  1064.         for (i = 0; i < addr->servers.nelts; i++) {
  1065.             if (server[i] == cscf) {
  1066.                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1067.                                    "a duplicate listen %s", addr->opt.addr);
  1068.                 return NGX_ERROR;
  1069.             }
  1070.         }
  1071.     }

  1072.     server = ngx_array_push(&addr->servers);
  1073.     if (server == NULL) {
  1074.         return NGX_ERROR;
  1075.     }

  1076.     *server = cscf;

  1077.     return NGX_OK;
  1078. }


  1079. static ngx_int_t
  1080. ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
  1081.     ngx_array_t *ports)
  1082. {
  1083.     ngx_uint_t             p, a;
  1084.     ngx_http_conf_port_t  *port;
  1085.     ngx_http_conf_addr_t  *addr;

  1086.     if (ports == NULL) {
  1087.         return NGX_OK;
  1088.     }

  1089.     port = ports->elts;
  1090.     for (p = 0; p < ports->nelts; p++) {

  1091.         ngx_sort(port[p].addrs.elts, (size_t) port[p].addrs.nelts,
  1092.                  sizeof(ngx_http_conf_addr_t), ngx_http_cmp_conf_addrs);

  1093.         /*
  1094.          * check whether all name-based servers have the same
  1095.          * configuration as a default server for given address:port
  1096.          */

  1097.         addr = port[p].addrs.elts;
  1098.         for (a = 0; a < port[p].addrs.nelts; a++) {

  1099.             if (addr[a].servers.nelts > 1
  1100. #if (NGX_PCRE)
  1101.                 || addr[a].default_server->captures
  1102. #endif
  1103.                )
  1104.             {
  1105.                 if (ngx_http_server_names(cf, cmcf, &addr[a]) != NGX_OK) {
  1106.                     return NGX_ERROR;
  1107.                 }
  1108.             }
  1109.         }

  1110.         if (ngx_http_init_listening(cf, &port[p]) != NGX_OK) {
  1111.             return NGX_ERROR;
  1112.         }
  1113.     }

  1114.     return NGX_OK;
  1115. }


  1116. static ngx_int_t
  1117. ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
  1118.     ngx_http_conf_addr_t *addr)
  1119. {
  1120.     ngx_int_t                   rc;
  1121.     ngx_uint_t                  n, s;
  1122.     ngx_hash_init_t             hash;
  1123.     ngx_hash_keys_arrays_t      ha;
  1124.     ngx_http_server_name_t     *name;
  1125.     ngx_http_core_srv_conf_t  **cscfp;
  1126. #if (NGX_PCRE)
  1127.     ngx_uint_t                  regex, i;

  1128.     regex = 0;
  1129. #endif

  1130.     ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t));

  1131.     ha.temp_pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, cf->log);
  1132.     if (ha.temp_pool == NULL) {
  1133.         return NGX_ERROR;
  1134.     }

  1135.     ha.pool = cf->pool;

  1136.     if (ngx_hash_keys_array_init(&ha, NGX_HASH_LARGE) != NGX_OK) {
  1137.         goto failed;
  1138.     }

  1139.     cscfp = addr->servers.elts;

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

  1141.         name = cscfp[s]->server_names.elts;

  1142.         for (n = 0; n < cscfp[s]->server_names.nelts; n++) {

  1143. #if (NGX_PCRE)
  1144.             if (name[n].regex) {
  1145.                 regex++;
  1146.                 continue;
  1147.             }
  1148. #endif

  1149.             rc = ngx_hash_add_key(&ha, &name[n].name, name[n].server,
  1150.                                   NGX_HASH_WILDCARD_KEY);

  1151.             if (rc == NGX_ERROR) {
  1152.                 return NGX_ERROR;
  1153.             }

  1154.             if (rc == NGX_DECLINED) {
  1155.                 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  1156.                               "invalid server name or wildcard \"%V\" on %s",
  1157.                               &name[n].name, addr->opt.addr);
  1158.                 return NGX_ERROR;
  1159.             }

  1160.             if (rc == NGX_BUSY) {
  1161.                 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
  1162.                               "conflicting server name \"%V\" on %s, ignored",
  1163.                               &name[n].name, addr->opt.addr);
  1164.             }
  1165.         }
  1166.     }

  1167.     hash.key = ngx_hash_key_lc;
  1168.     hash.max_size = cmcf->server_names_hash_max_size;
  1169.     hash.bucket_size = cmcf->server_names_hash_bucket_size;
  1170.     hash.name = "server_names_hash";
  1171.     hash.pool = cf->pool;

  1172.     if (ha.keys.nelts) {
  1173.         hash.hash = &addr->hash;
  1174.         hash.temp_pool = NULL;

  1175.         if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK) {
  1176.             goto failed;
  1177.         }
  1178.     }

  1179.     if (ha.dns_wc_head.nelts) {

  1180.         ngx_qsort(ha.dns_wc_head.elts, (size_t) ha.dns_wc_head.nelts,
  1181.                   sizeof(ngx_hash_key_t), ngx_http_cmp_dns_wildcards);

  1182.         hash.hash = NULL;
  1183.         hash.temp_pool = ha.temp_pool;

  1184.         if (ngx_hash_wildcard_init(&hash, ha.dns_wc_head.elts,
  1185.                                    ha.dns_wc_head.nelts)
  1186.             != NGX_OK)
  1187.         {
  1188.             goto failed;
  1189.         }

  1190.         addr->wc_head = (ngx_hash_wildcard_t *) hash.hash;
  1191.     }

  1192.     if (ha.dns_wc_tail.nelts) {

  1193.         ngx_qsort(ha.dns_wc_tail.elts, (size_t) ha.dns_wc_tail.nelts,
  1194.                   sizeof(ngx_hash_key_t), ngx_http_cmp_dns_wildcards);

  1195.         hash.hash = NULL;
  1196.         hash.temp_pool = ha.temp_pool;

  1197.         if (ngx_hash_wildcard_init(&hash, ha.dns_wc_tail.elts,
  1198.                                    ha.dns_wc_tail.nelts)
  1199.             != NGX_OK)
  1200.         {
  1201.             goto failed;
  1202.         }

  1203.         addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash;
  1204.     }

  1205.     ngx_destroy_pool(ha.temp_pool);

  1206. #if (NGX_PCRE)

  1207.     if (regex == 0) {
  1208.         return NGX_OK;
  1209.     }

  1210.     addr->nregex = regex;
  1211.     addr->regex = ngx_palloc(cf->pool, regex * sizeof(ngx_http_server_name_t));
  1212.     if (addr->regex == NULL) {
  1213.         return NGX_ERROR;
  1214.     }

  1215.     i = 0;

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

  1217.         name = cscfp[s]->server_names.elts;

  1218.         for (n = 0; n < cscfp[s]->server_names.nelts; n++) {
  1219.             if (name[n].regex) {
  1220.                 addr->regex[i++] = name[n];
  1221.             }
  1222.         }
  1223.     }

  1224. #endif

  1225.     return NGX_OK;

  1226. failed:

  1227.     ngx_destroy_pool(ha.temp_pool);

  1228.     return NGX_ERROR;
  1229. }


  1230. static ngx_int_t
  1231. ngx_http_cmp_conf_addrs(const void *one, const void *two)
  1232. {
  1233.     ngx_http_conf_addr_t  *first, *second;

  1234.     first = (ngx_http_conf_addr_t *) one;
  1235.     second = (ngx_http_conf_addr_t *) two;

  1236.     if (first->opt.wildcard) {
  1237.         /* a wildcard address must be the last resort, shift it to the end */
  1238.         return 1;
  1239.     }

  1240.     if (second->opt.wildcard) {
  1241.         /* a wildcard address must be the last resort, shift it to the end */
  1242.         return -1;
  1243.     }

  1244.     if (first->opt.bind && !second->opt.bind) {
  1245.         /* shift explicit bind()ed addresses to the start */
  1246.         return -1;
  1247.     }

  1248.     if (!first->opt.bind && second->opt.bind) {
  1249.         /* shift explicit bind()ed addresses to the start */
  1250.         return 1;
  1251.     }

  1252.     /* do not sort by default */

  1253.     return 0;
  1254. }


  1255. static int ngx_libc_cdecl
  1256. ngx_http_cmp_dns_wildcards(const void *one, const void *two)
  1257. {
  1258.     ngx_hash_key_t  *first, *second;

  1259.     first = (ngx_hash_key_t *) one;
  1260.     second = (ngx_hash_key_t *) two;

  1261.     return ngx_dns_strcmp(first->key.data, second->key.data);
  1262. }


  1263. static ngx_int_t
  1264. ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_port_t *port)
  1265. {
  1266.     ngx_uint_t                 i, last, bind_wildcard;
  1267.     ngx_listening_t           *ls;
  1268.     ngx_http_port_t           *hport;
  1269.     ngx_http_conf_addr_t      *addr;

  1270.     addr = port->addrs.elts;
  1271.     last = port->addrs.nelts;

  1272.     /*
  1273.      * If there is a binding to an "*:port" then we need to bind() to
  1274.      * the "*:port" only and ignore other implicit bindings.  The bindings
  1275.      * have been already sorted: explicit bindings are on the start, then
  1276.      * implicit bindings go, and wildcard binding is in the end.
  1277.      */

  1278.     if (addr[last - 1].opt.wildcard) {
  1279.         addr[last - 1].opt.bind = 1;
  1280.         bind_wildcard = 1;

  1281.     } else {
  1282.         bind_wildcard = 0;
  1283.     }

  1284.     i = 0;

  1285.     while (i < last) {

  1286.         if (bind_wildcard && !addr[i].opt.bind) {
  1287.             i++;
  1288.             continue;
  1289.         }

  1290.         ls = ngx_http_add_listening(cf, &addr[i]);
  1291.         if (ls == NULL) {
  1292.             return NGX_ERROR;
  1293.         }

  1294.         hport = ngx_pcalloc(cf->pool, sizeof(ngx_http_port_t));
  1295.         if (hport == NULL) {
  1296.             return NGX_ERROR;
  1297.         }

  1298.         ls->servers = hport;

  1299.         if (i == last - 1) {
  1300.             hport->naddrs = last;

  1301.         } else {
  1302.             hport->naddrs = 1;
  1303.             i = 0;
  1304.         }

  1305.         switch (ls->sockaddr->sa_family) {

  1306. #if (NGX_HAVE_INET6)
  1307.         case AF_INET6:
  1308.             if (ngx_http_add_addrs6(cf, hport, addr) != NGX_OK) {
  1309.                 return NGX_ERROR;
  1310.             }
  1311.             break;
  1312. #endif
  1313.         default: /* AF_INET */
  1314.             if (ngx_http_add_addrs(cf, hport, addr) != NGX_OK) {
  1315.                 return NGX_ERROR;
  1316.             }
  1317.             break;
  1318.         }

  1319.         addr++;
  1320.         last--;
  1321.     }

  1322.     return NGX_OK;
  1323. }


  1324. static ngx_listening_t *
  1325. ngx_http_add_listening(ngx_conf_t *cf, ngx_http_conf_addr_t *addr)
  1326. {
  1327.     ngx_listening_t           *ls;
  1328.     ngx_http_core_loc_conf_t  *clcf;
  1329.     ngx_http_core_srv_conf_t  *cscf;

  1330.     ls = ngx_create_listening(cf, &addr->opt.u.sockaddr, addr->opt.socklen);
  1331.     if (ls == NULL) {
  1332.         return NULL;
  1333.     }

  1334.     ls->addr_ntop = 1;

  1335.     ls->handler = ngx_http_init_connection;

  1336.     cscf = addr->default_server;
  1337.     ls->pool_size = cscf->connection_pool_size;
  1338.     ls->post_accept_timeout = cscf->client_header_timeout;

  1339.     clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];

  1340.     ls->logp = clcf->error_log;
  1341.     ls->log.data = &ls->addr_text;
  1342.     ls->log.handler = ngx_accept_log_error;

  1343. #if (NGX_WIN32)
  1344.     {
  1345.     ngx_iocp_conf_t  *iocpcf = NULL;

  1346.     if (ngx_get_conf(cf->cycle->conf_ctx, ngx_events_module)) {
  1347.         iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module);
  1348.     }
  1349.     if (iocpcf && iocpcf->acceptex_read) {
  1350.         ls->post_accept_buffer_size = cscf->client_header_buffer_size;
  1351.     }
  1352.     }
  1353. #endif

  1354.     ls->backlog = addr->opt.backlog;
  1355.     ls->rcvbuf = addr->opt.rcvbuf;
  1356.     ls->sndbuf = addr->opt.sndbuf;

  1357.     ls->keepalive = addr->opt.so_keepalive;
  1358. #if (NGX_HAVE_KEEPALIVE_TUNABLE)
  1359.     ls->keepidle = addr->opt.tcp_keepidle;
  1360.     ls->keepintvl = addr->opt.tcp_keepintvl;
  1361.     ls->keepcnt = addr->opt.tcp_keepcnt;
  1362. #endif

  1363. #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
  1364.     ls->accept_filter = addr->opt.accept_filter;
  1365. #endif

  1366. #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
  1367.     ls->deferred_accept = addr->opt.deferred_accept;
  1368. #endif

  1369. #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
  1370.     ls->ipv6only = addr->opt.ipv6only;
  1371. #endif

  1372. #if (NGX_HAVE_SETFIB)
  1373.     ls->setfib = addr->opt.setfib;
  1374. #endif

  1375. #if (NGX_HAVE_TCP_FASTOPEN)
  1376.     ls->fastopen = addr->opt.fastopen;
  1377. #endif

  1378.     return ls;
  1379. }


  1380. static ngx_int_t
  1381. ngx_http_add_addrs(ngx_conf_t *cf, ngx_http_port_t *hport,
  1382.     ngx_http_conf_addr_t *addr)
  1383. {
  1384.     ngx_uint_t                 i;
  1385.     ngx_http_in_addr_t        *addrs;
  1386.     struct sockaddr_in        *sin;
  1387.     ngx_http_virtual_names_t  *vn;

  1388.     hport->addrs = ngx_pcalloc(cf->pool,
  1389.                                hport->naddrs * sizeof(ngx_http_in_addr_t));
  1390.     if (hport->addrs == NULL) {
  1391.         return NGX_ERROR;
  1392.     }

  1393.     addrs = hport->addrs;

  1394.     for (i = 0; i < hport->naddrs; i++) {

  1395.         sin = &addr[i].opt.u.sockaddr_in;
  1396.         addrs[i].addr = sin->sin_addr.s_addr;
  1397.         addrs[i].conf.default_server = addr[i].default_server;
  1398. #if (NGX_HTTP_SSL)
  1399.         addrs[i].conf.ssl = addr[i].opt.ssl;
  1400. #endif
  1401. #if (NGX_HTTP_SPDY)
  1402.         addrs[i].conf.spdy = addr[i].opt.spdy;
  1403. #endif
  1404.         addrs[i].conf.proxy_protocol = addr[i].opt.proxy_protocol;

  1405.         if (addr[i].hash.buckets == NULL
  1406.             && (addr[i].wc_head == NULL
  1407.                 || addr[i].wc_head->hash.buckets == NULL)
  1408.             && (addr[i].wc_tail == NULL
  1409.                 || addr[i].wc_tail->hash.buckets == NULL)
  1410. #if (NGX_PCRE)
  1411.             && addr[i].nregex == 0
  1412. #endif
  1413.             )
  1414.         {
  1415.             continue;
  1416.         }

  1417.         vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
  1418.         if (vn == NULL) {
  1419.             return NGX_ERROR;
  1420.         }

  1421.         addrs[i].conf.virtual_names = vn;

  1422.         vn->names.hash = addr[i].hash;
  1423.         vn->names.wc_head = addr[i].wc_head;
  1424.         vn->names.wc_tail = addr[i].wc_tail;
  1425. #if (NGX_PCRE)
  1426.         vn->nregex = addr[i].nregex;
  1427.         vn->regex = addr[i].regex;
  1428. #endif
  1429.     }

  1430.     return NGX_OK;
  1431. }


  1432. #if (NGX_HAVE_INET6)

  1433. static ngx_int_t
  1434. ngx_http_add_addrs6(ngx_conf_t *cf, ngx_http_port_t *hport,
  1435.     ngx_http_conf_addr_t *addr)
  1436. {
  1437.     ngx_uint_t                 i;
  1438.     ngx_http_in6_addr_t       *addrs6;
  1439.     struct sockaddr_in6       *sin6;
  1440.     ngx_http_virtual_names_t  *vn;

  1441.     hport->addrs = ngx_pcalloc(cf->pool,
  1442.                                hport->naddrs * sizeof(ngx_http_in6_addr_t));
  1443.     if (hport->addrs == NULL) {
  1444.         return NGX_ERROR;
  1445.     }

  1446.     addrs6 = hport->addrs;

  1447.     for (i = 0; i < hport->naddrs; i++) {

  1448.         sin6 = &addr[i].opt.u.sockaddr_in6;
  1449.         addrs6[i].addr6 = sin6->sin6_addr;
  1450.         addrs6[i].conf.default_server = addr[i].default_server;
  1451. #if (NGX_HTTP_SSL)
  1452.         addrs6[i].conf.ssl = addr[i].opt.ssl;
  1453. #endif
  1454. #if (NGX_HTTP_SPDY)
  1455.         addrs6[i].conf.spdy = addr[i].opt.spdy;
  1456. #endif

  1457.         if (addr[i].hash.buckets == NULL
  1458.             && (addr[i].wc_head == NULL
  1459.                 || addr[i].wc_head->hash.buckets == NULL)
  1460.             && (addr[i].wc_tail == NULL
  1461.                 || addr[i].wc_tail->hash.buckets == NULL)
  1462. #if (NGX_PCRE)
  1463.             && addr[i].nregex == 0
  1464. #endif
  1465.             )
  1466.         {
  1467.             continue;
  1468.         }

  1469.         vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
  1470.         if (vn == NULL) {
  1471.             return NGX_ERROR;
  1472.         }

  1473.         addrs6[i].conf.virtual_names = vn;

  1474.         vn->names.hash = addr[i].hash;
  1475.         vn->names.wc_head = addr[i].wc_head;
  1476.         vn->names.wc_tail = addr[i].wc_tail;
  1477. #if (NGX_PCRE)
  1478.         vn->nregex = addr[i].nregex;
  1479.         vn->regex = addr[i].regex;
  1480. #endif
  1481.     }

  1482.     return NGX_OK;
  1483. }

  1484. #endif


  1485. char *
  1486. ngx_http_types_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1487. {
  1488.     char  *p = conf;

  1489.     ngx_array_t     **types;
  1490.     ngx_str_t        *value, *default_type;
  1491.     ngx_uint_t        i, n, hash;
  1492.     ngx_hash_key_t   *type;

  1493.     types = (ngx_array_t **) (p + cmd->offset);

  1494.     if (*types == (void *) -1) {
  1495.         return NGX_CONF_OK;
  1496.     }

  1497.     default_type = cmd->post;

  1498.     if (*types == NULL) {
  1499.         *types = ngx_array_create(cf->temp_pool, 1, sizeof(ngx_hash_key_t));
  1500.         if (*types == NULL) {
  1501.             return NGX_CONF_ERROR;
  1502.         }

  1503.         if (default_type) {
  1504.             type = ngx_array_push(*types);
  1505.             if (type == NULL) {
  1506.                 return NGX_CONF_ERROR;
  1507.             }

  1508.             type->key = *default_type;
  1509.             type->key_hash = ngx_hash_key(default_type->data,
  1510.                                           default_type->len);
  1511.             type->value = (void *) 4;
  1512.         }
  1513.     }

  1514.     value = cf->args->elts;

  1515.     for (i = 1; i < cf->args->nelts; i++) {

  1516.         if (value[i].len == 1 && value[i].data[0] == '*') {
  1517.             *types = (void *) -1;
  1518.             return NGX_CONF_OK;
  1519.         }

  1520.         hash = ngx_hash_strlow(value[i].data, value[i].data, value[i].len);
  1521.         value[i].data[value[i].len] = '\0';

  1522.         type = (*types)->elts;
  1523.         for (n = 0; n < (*types)->nelts; n++) {

  1524.             if (ngx_strcmp(value[i].data, type[n].key.data) == 0) {
  1525.                 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
  1526.                                    "duplicate MIME type \"%V\"", &value[i]);
  1527.                 goto next;
  1528.             }
  1529.         }

  1530.         type = ngx_array_push(*types);
  1531.         if (type == NULL) {
  1532.             return NGX_CONF_ERROR;
  1533.         }

  1534.         type->key = value[i];
  1535.         type->key_hash = hash;
  1536.         type->value = (void *) 4;

  1537.     next:

  1538.         continue;
  1539.     }

  1540.     return NGX_CONF_OK;
  1541. }


  1542. char *
  1543. ngx_http_merge_types(ngx_conf_t *cf, ngx_array_t **keys, ngx_hash_t *types_hash,
  1544.     ngx_array_t **prev_keys, ngx_hash_t *prev_types_hash,
  1545.     ngx_str_t *default_types)
  1546. {
  1547.     ngx_hash_init_t  hash;

  1548.     if (*keys) {

  1549.         if (*keys == (void *) -1) {
  1550.             return NGX_CONF_OK;
  1551.         }

  1552.         hash.hash = types_hash;
  1553.         hash.key = NULL;
  1554.         hash.max_size = 2048;
  1555.         hash.bucket_size = 64;
  1556.         hash.name = "test_types_hash";
  1557.         hash.pool = cf->pool;
  1558.         hash.temp_pool = NULL;

  1559.         if (ngx_hash_init(&hash, (*keys)->elts, (*keys)->nelts) != NGX_OK) {
  1560.             return NGX_CONF_ERROR;
  1561.         }

  1562.         return NGX_CONF_OK;
  1563.     }

  1564.     if (prev_types_hash->buckets == NULL) {

  1565.         if (*prev_keys == NULL) {

  1566.             if (ngx_http_set_default_types(cf, prev_keys, default_types)
  1567.                 != NGX_OK)
  1568.             {
  1569.                 return NGX_CONF_ERROR;
  1570.             }

  1571.         } else if (*prev_keys == (void *) -1) {
  1572.             *keys = *prev_keys;
  1573.             return NGX_CONF_OK;
  1574.         }

  1575.         hash.hash = prev_types_hash;
  1576.         hash.key = NULL;
  1577.         hash.max_size = 2048;
  1578.         hash.bucket_size = 64;
  1579.         hash.name = "test_types_hash";
  1580.         hash.pool = cf->pool;
  1581.         hash.temp_pool = NULL;

  1582.         if (ngx_hash_init(&hash, (*prev_keys)->elts, (*prev_keys)->nelts)
  1583.             != NGX_OK)
  1584.         {
  1585.             return NGX_CONF_ERROR;
  1586.         }
  1587.     }

  1588.     *types_hash = *prev_types_hash;

  1589.     return NGX_CONF_OK;

  1590. }


  1591. ngx_int_t
  1592. ngx_http_set_default_types(ngx_conf_t *cf, ngx_array_t **types,
  1593.     ngx_str_t *default_type)
  1594. {
  1595.     ngx_hash_key_t  *type;

  1596.     *types = ngx_array_create(cf->temp_pool, 1, sizeof(ngx_hash_key_t));
  1597.     if (*types == NULL) {
  1598.         return NGX_ERROR;
  1599.     }

  1600.     while (default_type->len) {

  1601.         type = ngx_array_push(*types);
  1602.         if (type == NULL) {
  1603.             return NGX_ERROR;
  1604.         }

  1605.         type->key = *default_type;
  1606.         type->key_hash = ngx_hash_key(default_type->data,
  1607.                                       default_type->len);
  1608.         type->value = (void *) 4;

  1609.         default_type++;
  1610.     }

  1611.     return NGX_OK;
  1612. }