3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
13 static ngx_int_t ngx_http_init_phases(ngx_conf_t *cf,
14 ngx_http_core_main_conf_t *cmcf);
15 static ngx_int_t ngx_http_init_headers_in_hash(ngx_conf_t *cf,
16 ngx_http_core_main_conf_t *cmcf);
17 static ngx_int_t ngx_http_init_phase_handlers(ngx_conf_t *cf,
18 ngx_http_core_main_conf_t *cmcf);
20 static ngx_int_t ngx_http_init_server_lists(ngx_conf_t *cf,
21 ngx_array_t *servers, ngx_array_t *in_ports);
22 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
23 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port,
24 ngx_http_listen_t *listen);
25 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
26 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_addr_t *in_addr);
28 static char *ngx_http_merge_locations(ngx_conf_t *cf,
29 ngx_queue_t *locations, void **loc_conf, ngx_http_module_t *module,
30 ngx_uint_t ctx_index);
31 static ngx_int_t ngx_http_init_locations(ngx_conf_t *cf,
32 ngx_http_core_srv_conf_t *cscf, ngx_http_core_loc_conf_t *pclcf);
33 static ngx_int_t ngx_http_init_static_location_trees(ngx_conf_t *cf,
34 ngx_http_core_loc_conf_t *pclcf);
35 static ngx_int_t ngx_http_cmp_locations(const ngx_queue_t *one,
36 const ngx_queue_t *two);
37 static ngx_int_t ngx_http_join_exact_locations(ngx_conf_t *cf,
38 ngx_queue_t *locations);
39 static void ngx_http_create_locations_list(ngx_queue_t *locations,
41 static ngx_http_location_tree_node_t *
42 ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations,
45 static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf,
46 ngx_http_core_main_conf_t *cmcf, ngx_array_t *in_ports);
47 static ngx_int_t ngx_http_cmp_conf_in_addrs(const void *one, const void *two);
48 static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one,
51 static ngx_int_t ngx_http_init_listening(ngx_conf_t *cf,
52 ngx_http_conf_in_port_t *in_port);
54 ngx_uint_t ngx_http_max_module;
57 ngx_int_t (*ngx_http_top_header_filter) (ngx_http_request_t *r);
58 ngx_int_t (*ngx_http_top_body_filter) (ngx_http_request_t *r, ngx_chain_t *ch);
61 ngx_str_t ngx_http_html_default_types[] = {
62 ngx_string("text/html"),
67 static ngx_command_t ngx_http_commands[] = {
70 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
80 static ngx_core_module_t ngx_http_module_ctx = {
87 ngx_module_t ngx_http_module = {
89 &ngx_http_module_ctx, /* module context */
90 ngx_http_commands, /* module directives */
91 NGX_CORE_MODULE, /* module type */
92 NULL, /* init master */
93 NULL, /* init module */
94 NULL, /* init process */
95 NULL, /* init thread */
96 NULL, /* exit thread */
97 NULL, /* exit process */
98 NULL, /* exit master */
104 ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
109 ngx_array_t in_ports;
110 ngx_http_module_t *module;
111 ngx_http_conf_ctx_t *ctx;
112 ngx_http_core_loc_conf_t *clcf;
113 ngx_http_core_srv_conf_t **cscfp;
114 ngx_http_core_main_conf_t *cmcf;
116 /* the main http context */
118 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
120 return NGX_CONF_ERROR;
123 *(ngx_http_conf_ctx_t **) conf = ctx;
126 /* count the number of the http modules and set up their indices */
128 ngx_http_max_module = 0;
129 for (m = 0; ngx_modules[m]; m++) {
130 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
134 ngx_modules[m]->ctx_index = ngx_http_max_module++;
138 /* the http main_conf context, it is the same in the all http contexts */
140 ctx->main_conf = ngx_pcalloc(cf->pool,
141 sizeof(void *) * ngx_http_max_module);
142 if (ctx->main_conf == NULL) {
143 return NGX_CONF_ERROR;
148 * the http null srv_conf context, it is used to merge
149 * the server{}s' srv_conf's
152 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
153 if (ctx->srv_conf == NULL) {
154 return NGX_CONF_ERROR;
159 * the http null loc_conf context, it is used to merge
160 * the server{}s' loc_conf's
163 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
164 if (ctx->loc_conf == NULL) {
165 return NGX_CONF_ERROR;
170 * create the main_conf's, the null srv_conf's, and the null loc_conf's
171 * of the all http modules
174 for (m = 0; ngx_modules[m]; m++) {
175 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
179 module = ngx_modules[m]->ctx;
180 mi = ngx_modules[m]->ctx_index;
182 if (module->create_main_conf) {
183 ctx->main_conf[mi] = module->create_main_conf(cf);
184 if (ctx->main_conf[mi] == NULL) {
185 return NGX_CONF_ERROR;
189 if (module->create_srv_conf) {
190 ctx->srv_conf[mi] = module->create_srv_conf(cf);
191 if (ctx->srv_conf[mi] == NULL) {
192 return NGX_CONF_ERROR;
196 if (module->create_loc_conf) {
197 ctx->loc_conf[mi] = module->create_loc_conf(cf);
198 if (ctx->loc_conf[mi] == NULL) {
199 return NGX_CONF_ERROR;
207 for (m = 0; ngx_modules[m]; m++) {
208 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
212 module = ngx_modules[m]->ctx;
214 if (module->preconfiguration) {
215 if (module->preconfiguration(cf) != NGX_OK) {
216 return NGX_CONF_ERROR;
221 /* parse inside the http{} block */
223 cf->module_type = NGX_HTTP_MODULE;
224 cf->cmd_type = NGX_HTTP_MAIN_CONF;
225 rv = ngx_conf_parse(cf, NULL);
227 if (rv != NGX_CONF_OK) {
232 * init http{} main_conf's, merge the server{}s' srv_conf's
233 * and its location{}s' loc_conf's
236 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
237 cscfp = cmcf->servers.elts;
239 for (m = 0; ngx_modules[m]; m++) {
240 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
244 module = ngx_modules[m]->ctx;
245 mi = ngx_modules[m]->ctx_index;
247 /* init http{} main_conf's */
249 if (module->init_main_conf) {
250 rv = module->init_main_conf(cf, ctx->main_conf[mi]);
251 if (rv != NGX_CONF_OK) {
256 for (s = 0; s < cmcf->servers.nelts; s++) {
258 /* merge the server{}s' srv_conf's */
260 if (module->merge_srv_conf) {
261 rv = module->merge_srv_conf(cf, ctx->srv_conf[mi],
262 cscfp[s]->ctx->srv_conf[mi]);
263 if (rv != NGX_CONF_OK) {
268 if (module->merge_loc_conf) {
270 /* merge the server{}'s loc_conf */
272 rv = module->merge_loc_conf(cf, ctx->loc_conf[mi],
273 cscfp[s]->ctx->loc_conf[mi]);
274 if (rv != NGX_CONF_OK) {
278 /* merge the locations{}' loc_conf's */
280 clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];
282 rv = ngx_http_merge_locations(cf, clcf->locations,
283 cscfp[s]->ctx->loc_conf,
285 if (rv != NGX_CONF_OK) {
293 /* create location trees */
295 for (s = 0; s < cmcf->servers.nelts; s++) {
297 clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];
299 if (ngx_http_init_locations(cf, cscfp[s], clcf) != NGX_OK) {
300 return NGX_CONF_ERROR;
303 if (ngx_http_init_static_location_trees(cf, clcf) != NGX_OK) {
304 return NGX_CONF_ERROR;
309 if (ngx_http_init_phases(cf, cmcf) != NGX_OK) {
310 return NGX_CONF_ERROR;
313 if (ngx_http_init_headers_in_hash(cf, cmcf) != NGX_OK) {
314 return NGX_CONF_ERROR;
318 for (m = 0; ngx_modules[m]; m++) {
319 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
323 module = ngx_modules[m]->ctx;
325 if (module->postconfiguration) {
326 if (module->postconfiguration(cf) != NGX_OK) {
327 return NGX_CONF_ERROR;
332 if (ngx_http_variables_init_vars(cf) != NGX_OK) {
333 return NGX_CONF_ERROR;
337 * http{}'s cf->ctx was needed while the configuration merging
338 * and in postconfiguration process
344 if (ngx_http_init_phase_handlers(cf, cmcf) != NGX_OK) {
345 return NGX_CONF_ERROR;
350 * create the lists of ports, addresses and server names
351 * to find quickly the server core module configuration at run-time
356 if (ngx_http_init_server_lists(cf, &cmcf->servers, &in_ports) != NGX_OK) {
357 return NGX_CONF_ERROR;
361 /* optimize the lists of ports, addresses and server names */
365 if (ngx_http_optimize_servers(cf, cmcf, &in_ports) != NGX_OK) {
366 return NGX_CONF_ERROR;
380 ngx_http_init_phases(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
382 if (ngx_array_init(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers,
383 cf->pool, 1, sizeof(ngx_http_handler_pt))
389 if (ngx_array_init(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers,
390 cf->pool, 1, sizeof(ngx_http_handler_pt))
396 if (ngx_array_init(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers,
397 cf->pool, 1, sizeof(ngx_http_handler_pt))
403 if (ngx_array_init(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers,
404 cf->pool, 1, sizeof(ngx_http_handler_pt))
410 if (ngx_array_init(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers,
411 cf->pool, 2, sizeof(ngx_http_handler_pt))
417 if (ngx_array_init(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers,
418 cf->pool, 4, sizeof(ngx_http_handler_pt))
424 if (ngx_array_init(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers,
425 cf->pool, 1, sizeof(ngx_http_handler_pt))
436 ngx_http_init_headers_in_hash(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
438 ngx_array_t headers_in;
440 ngx_hash_init_t hash;
441 ngx_http_header_t *header;
443 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t))
449 for (header = ngx_http_headers_in; header->name.len; header++) {
450 hk = ngx_array_push(&headers_in);
455 hk->key = header->name;
456 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len);
460 hash.hash = &cmcf->headers_in_hash;
461 hash.key = ngx_hash_key_lc;
463 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
464 hash.name = "headers_in_hash";
465 hash.pool = cf->pool;
466 hash.temp_pool = NULL;
468 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) {
477 ngx_http_init_phase_handlers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
481 ngx_uint_t find_config_index, use_rewrite, use_access;
482 ngx_http_handler_pt *h;
483 ngx_http_phase_handler_t *ph;
484 ngx_http_phase_handler_pt checker;
486 cmcf->phase_engine.server_rewrite_index = (ngx_uint_t) -1;
487 cmcf->phase_engine.location_rewrite_index = (ngx_uint_t) -1;
488 find_config_index = 0;
489 use_rewrite = cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers.nelts ? 1 : 0;
490 use_access = cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers.nelts ? 1 : 0;
492 n = use_rewrite + use_access + cmcf->try_files + 1 /* find config phase */;
494 for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
495 n += cmcf->phases[i].handlers.nelts;
498 ph = ngx_pcalloc(cf->pool,
499 n * sizeof(ngx_http_phase_handler_t) + sizeof(void *));
504 cmcf->phase_engine.handlers = ph;
507 for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
508 h = cmcf->phases[i].handlers.elts;
512 case NGX_HTTP_SERVER_REWRITE_PHASE:
513 if (cmcf->phase_engine.server_rewrite_index == (ngx_uint_t) -1) {
514 cmcf->phase_engine.server_rewrite_index = n;
516 checker = ngx_http_core_generic_phase;
520 case NGX_HTTP_FIND_CONFIG_PHASE:
521 find_config_index = n;
523 ph->checker = ngx_http_core_find_config_phase;
529 case NGX_HTTP_REWRITE_PHASE:
530 if (cmcf->phase_engine.location_rewrite_index == (ngx_uint_t) -1) {
531 cmcf->phase_engine.location_rewrite_index = n;
533 checker = ngx_http_core_generic_phase;
537 case NGX_HTTP_POST_REWRITE_PHASE:
539 ph->checker = ngx_http_core_post_rewrite_phase;
540 ph->next = find_config_index;
547 case NGX_HTTP_ACCESS_PHASE:
548 checker = ngx_http_core_access_phase;
552 case NGX_HTTP_POST_ACCESS_PHASE:
554 ph->checker = ngx_http_core_post_access_phase;
561 case NGX_HTTP_TRY_FILES_PHASE:
562 if (cmcf->try_files) {
563 ph->checker = ngx_http_core_try_files_phase;
570 case NGX_HTTP_CONTENT_PHASE:
571 checker = ngx_http_core_content_phase;
575 checker = ngx_http_core_generic_phase;
578 n += cmcf->phases[i].handlers.nelts;
580 for (j = cmcf->phases[i].handlers.nelts - 1; j >=0; j--) {
581 ph->checker = checker;
593 ngx_http_merge_locations(ngx_conf_t *cf, ngx_queue_t *locations,
594 void **loc_conf, ngx_http_module_t *module, ngx_uint_t ctx_index)
598 ngx_http_core_loc_conf_t *clcf;
599 ngx_http_location_queue_t *lq;
601 if (locations == NULL) {
605 for (q = ngx_queue_head(locations);
606 q != ngx_queue_sentinel(locations);
607 q = ngx_queue_next(q))
609 lq = (ngx_http_location_queue_t *) q;
611 clcf = lq->exact ? lq->exact : lq->inclusive;
613 rv = module->merge_loc_conf(cf, loc_conf[ctx_index],
614 clcf->loc_conf[ctx_index]);
615 if (rv != NGX_CONF_OK) {
619 rv = ngx_http_merge_locations(cf, clcf->locations, clcf->loc_conf,
621 if (rv != NGX_CONF_OK) {
631 ngx_http_init_locations(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
632 ngx_http_core_loc_conf_t *pclcf)
635 ngx_queue_t *q, *locations, *named, tail;
636 ngx_http_core_loc_conf_t *clcf;
637 ngx_http_location_queue_t *lq;
638 ngx_http_core_loc_conf_t **clcfp;
644 locations = pclcf->locations;
646 if (locations == NULL) {
650 ngx_queue_sort(locations, ngx_http_cmp_locations);
659 for (q = ngx_queue_head(locations);
660 q != ngx_queue_sentinel(locations);
661 q = ngx_queue_next(q))
663 lq = (ngx_http_location_queue_t *) q;
665 clcf = lq->exact ? lq->exact : lq->inclusive;
667 if (ngx_http_init_locations(cf, NULL, clcf) != NGX_OK) {
700 if (q != ngx_queue_sentinel(locations)) {
701 ngx_queue_split(locations, q, &tail);
705 clcfp = ngx_palloc(cf->pool,
706 (n + 1) * sizeof(ngx_http_core_loc_conf_t **));
711 cscf->named_locations = clcfp;
714 q != ngx_queue_sentinel(locations);
715 q = ngx_queue_next(q))
717 lq = (ngx_http_location_queue_t *) q;
719 *(clcfp++) = lq->exact;
724 ngx_queue_split(locations, named, &tail);
731 clcfp = ngx_palloc(cf->pool,
732 (r + 1) * sizeof(ngx_http_core_loc_conf_t **));
737 pclcf->regex_locations = clcfp;
740 q != ngx_queue_sentinel(locations);
741 q = ngx_queue_next(q))
743 lq = (ngx_http_location_queue_t *) q;
745 *(clcfp++) = lq->exact;
750 ngx_queue_split(locations, regex, &tail);
760 ngx_http_init_static_location_trees(ngx_conf_t *cf,
761 ngx_http_core_loc_conf_t *pclcf)
763 ngx_queue_t *q, *locations;
764 ngx_http_core_loc_conf_t *clcf;
765 ngx_http_location_queue_t *lq;
767 locations = pclcf->locations;
769 if (locations == NULL) {
773 if (ngx_queue_empty(locations)) {
777 for (q = ngx_queue_head(locations);
778 q != ngx_queue_sentinel(locations);
779 q = ngx_queue_next(q))
781 lq = (ngx_http_location_queue_t *) q;
783 clcf = lq->exact ? lq->exact : lq->inclusive;
785 if (ngx_http_init_static_location_trees(cf, clcf) != NGX_OK) {
790 if (ngx_http_join_exact_locations(cf, locations) != NGX_OK) {
794 ngx_http_create_locations_list(locations, ngx_queue_head(locations));
796 pclcf->static_locations = ngx_http_create_locations_tree(cf, locations, 0);
797 if (pclcf->static_locations == NULL) {
806 ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
807 ngx_http_core_loc_conf_t *clcf)
809 ngx_http_location_queue_t *lq;
811 if (*locations == NULL) {
812 *locations = ngx_palloc(cf->temp_pool,
813 sizeof(ngx_http_location_queue_t));
814 if (*locations == NULL) {
818 ngx_queue_init(*locations);
821 lq = ngx_palloc(cf->temp_pool, sizeof(ngx_http_location_queue_t));
826 if (clcf->exact_match
830 || clcf->named || clcf->noname)
833 lq->inclusive = NULL;
837 lq->inclusive = clcf;
840 lq->name = &clcf->name;
841 lq->file_name = cf->conf_file->file.name.data;
842 lq->line = cf->conf_file->line;
844 ngx_queue_init(&lq->list);
846 ngx_queue_insert_tail(*locations, &lq->queue);
853 ngx_http_cmp_locations(const ngx_queue_t *one, const ngx_queue_t *two)
856 ngx_http_core_loc_conf_t *first, *second;
857 ngx_http_location_queue_t *lq1, *lq2;
859 lq1 = (ngx_http_location_queue_t *) one;
860 lq2 = (ngx_http_location_queue_t *) two;
862 first = lq1->exact ? lq1->exact : lq1->inclusive;
863 second = lq2->exact ? lq2->exact : lq2->inclusive;
865 if (first->noname && !second->noname) {
866 /* shift no named locations to the end */
870 if (!first->noname && second->noname) {
871 /* shift no named locations to the end */
875 if (first->noname || second->noname) {
876 /* do not sort no named locations */
880 if (first->named && !second->named) {
881 /* shift named locations to the end */
885 if (!first->named && second->named) {
886 /* shift named locations to the end */
890 if (first->named && second->named) {
891 return ngx_strcmp(first->name.data, second->name.data);
896 if (first->regex && !second->regex) {
897 /* shift the regex matches to the end */
901 if (!first->regex && second->regex) {
902 /* shift the regex matches to the end */
906 if (first->regex || second->regex) {
907 /* do not sort the regex matches */
913 rc = ngx_strcmp(first->name.data, second->name.data);
915 if (rc == 0 && !first->exact_match && second->exact_match) {
916 /* an exact match must be before the same inclusive one */
925 ngx_http_join_exact_locations(ngx_conf_t *cf, ngx_queue_t *locations)
928 ngx_http_location_queue_t *lq, *lx;
930 q = ngx_queue_head(locations);
932 while (q != ngx_queue_last(locations)) {
934 x = ngx_queue_next(q);
936 lq = (ngx_http_location_queue_t *) q;
937 lx = (ngx_http_location_queue_t *) x;
939 if (ngx_strcmp(lq->name->data, lx->name->data) == 0) {
941 if ((lq->exact && lx->exact) || (lq->inclusive && lx->inclusive)) {
942 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
943 "duplicate location \"%V\" in %s:%ui",
944 lx->name, lx->file_name, lx->line);
949 lq->inclusive = lx->inclusive;
956 q = ngx_queue_next(q);
964 ngx_http_create_locations_list(ngx_queue_t *locations, ngx_queue_t *q)
968 ngx_queue_t *x, tail;
969 ngx_http_location_queue_t *lq, *lx;
971 if (q == ngx_queue_last(locations)) {
975 lq = (ngx_http_location_queue_t *) q;
977 if (lq->inclusive == NULL) {
978 ngx_http_create_locations_list(locations, ngx_queue_next(q));
983 name = lq->name->data;
985 for (x = ngx_queue_next(q);
986 x != ngx_queue_sentinel(locations);
987 x = ngx_queue_next(x))
989 lx = (ngx_http_location_queue_t *) x;
991 if (len > lx->name->len
992 || (ngx_strncmp(name, lx->name->data, len) != 0))
998 q = ngx_queue_next(q);
1001 ngx_http_create_locations_list(locations, x);
1005 ngx_queue_split(locations, q, &tail);
1006 ngx_queue_add(&lq->list, &tail);
1008 if (x == ngx_queue_sentinel(locations)) {
1009 ngx_http_create_locations_list(&lq->list, ngx_queue_head(&lq->list));
1013 ngx_queue_split(&lq->list, x, &tail);
1014 ngx_queue_add(locations, &tail);
1016 ngx_http_create_locations_list(&lq->list, ngx_queue_head(&lq->list));
1018 ngx_http_create_locations_list(locations, x);
1023 * to keep cache locality for left leaf nodes, allocate nodes in following
1024 * order: node, left subtree, right subtree, inclusive subtree
1027 static ngx_http_location_tree_node_t *
1028 ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations,
1032 ngx_queue_t *q, tail;
1033 ngx_http_location_queue_t *lq;
1034 ngx_http_location_tree_node_t *node;
1036 q = ngx_queue_middle(locations);
1038 lq = (ngx_http_location_queue_t *) q;
1039 len = lq->name->len - prefix;
1041 node = ngx_palloc(cf->pool,
1042 offsetof(ngx_http_location_tree_node_t, name) + len);
1050 node->exact = lq->exact;
1051 node->inclusive = lq->inclusive;
1053 node->auto_redirect = (u_char) ((lq->exact && lq->exact->auto_redirect)
1054 || (lq->inclusive && lq->inclusive->auto_redirect));
1056 node->len = (u_char) len;
1057 ngx_memcpy(node->name, &lq->name->data[prefix], len);
1059 ngx_queue_split(locations, q, &tail);
1061 if (ngx_queue_empty(locations)) {
1063 * ngx_queue_split() insures that if left part is empty,
1064 * then right one is empty too
1069 node->left = ngx_http_create_locations_tree(cf, locations, prefix);
1070 if (node->left == NULL) {
1074 ngx_queue_remove(q);
1076 if (ngx_queue_empty(&tail)) {
1080 node->right = ngx_http_create_locations_tree(cf, &tail, prefix);
1081 if (node->right == NULL) {
1087 if (ngx_queue_empty(&lq->list)) {
1091 node->tree = ngx_http_create_locations_tree(cf, &lq->list, prefix + len);
1092 if (node->tree == NULL) {
1101 ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers,
1102 ngx_array_t *in_ports)
1104 ngx_uint_t s, l, p, a;
1105 ngx_http_listen_t *listen;
1106 ngx_http_conf_in_port_t *in_port;
1107 ngx_http_conf_in_addr_t *in_addr;
1108 ngx_http_core_srv_conf_t **cscfp;
1110 if (ngx_array_init(in_ports, cf->temp_pool, 2,
1111 sizeof(ngx_http_conf_in_port_t))
1117 /* "server" directives */
1119 cscfp = servers->elts;
1120 for (s = 0; s < servers->nelts; s++) {
1122 /* "listen" directives */
1124 listen = cscfp[s]->listen.elts;
1125 for (l = 0; l < cscfp[s]->listen.nelts; l++) {
1129 in_port = in_ports->elts;
1130 for (p = 0; p < in_ports->nelts; p++) {
1132 if (listen[l].port != in_port[p].port) {
1136 /* the port is already in the port list */
1138 in_addr = in_port[p].addrs.elts;
1139 for (a = 0; a < in_port[p].addrs.nelts; a++) {
1141 if (listen[l].addr != in_addr[a].addr) {
1145 /* the address is already in the address list */
1147 if (ngx_http_add_names(cf, cscfp[s], &in_addr[a]) != NGX_OK)
1153 * check the duplicate "default" server
1154 * for this address:port
1157 if (listen[l].conf.default_server) {
1159 if (in_addr[a].default_server) {
1160 ngx_log_error(NGX_LOG_ERR, cf->log, 0,
1161 "the duplicate default server in %s:%ui",
1162 listen[l].file_name, listen[l].line);
1167 in_addr[a].core_srv_conf = cscfp[s];
1168 in_addr[a].default_server = 1;
1170 in_addr[a].ssl = listen[l].conf.ssl;
1172 in_addr[a].listen_conf = &listen[l].conf;
1179 * add the address to the addresses list that
1180 * bound to this port
1183 if (ngx_http_add_address(cf, cscfp[s], &in_port[p], &listen[l])
1192 /* add the port to the in_port list */
1194 in_port = ngx_array_push(in_ports);
1195 if (in_port == NULL) {
1199 in_port->port = listen[l].port;
1200 in_port->addrs.elts = NULL;
1202 if (ngx_http_add_address(cf, cscfp[s], in_port, &listen[l])
1219 * add the server address, the server names and the server core module
1220 * configurations to the port (in_port)
1224 ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1225 ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *listen)
1227 ngx_http_conf_in_addr_t *in_addr;
1229 if (in_port->addrs.elts == NULL) {
1230 if (ngx_array_init(&in_port->addrs, cf->temp_pool, 4,
1231 sizeof(ngx_http_conf_in_addr_t))
1238 in_addr = ngx_array_push(&in_port->addrs);
1239 if (in_addr == NULL) {
1243 in_addr->addr = listen->addr;
1244 in_addr->hash.buckets = NULL;
1245 in_addr->hash.size = 0;
1246 in_addr->wc_head = NULL;
1247 in_addr->wc_tail = NULL;
1248 in_addr->names.elts = NULL;
1250 in_addr->nregex = 0;
1251 in_addr->regex = NULL;
1253 in_addr->core_srv_conf = cscf;
1254 in_addr->default_server = listen->conf.default_server;
1255 in_addr->bind = listen->conf.bind;
1257 in_addr->ssl = listen->conf.ssl;
1259 in_addr->listen_conf = &listen->conf;
1261 return ngx_http_add_names(cf, cscf, in_addr);
1266 * add the server names and the server core module
1267 * configurations to the address:port (in_addr)
1271 ngx_http_add_names(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1272 ngx_http_conf_in_addr_t *in_addr)
1275 ngx_http_server_name_t *server_names, *name;
1277 if (in_addr->names.elts == NULL) {
1278 if (ngx_array_init(&in_addr->names, cf->temp_pool, 4,
1279 sizeof(ngx_http_server_name_t))
1286 server_names = cscf->server_names.elts;
1288 for (i = 0; i < cscf->server_names.nelts; i++) {
1290 ngx_strlow(server_names[i].name.data, server_names[i].name.data,
1291 server_names[i].name.len);
1293 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
1294 "name: %V", &server_names[i].name);
1296 name = ngx_array_push(&in_addr->names);
1301 *name = server_names[i];
1309 ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
1310 ngx_array_t *in_ports)
1314 ngx_hash_init_t hash;
1315 ngx_http_server_name_t *name;
1316 ngx_hash_keys_arrays_t ha;
1317 ngx_http_conf_in_port_t *in_port;
1318 ngx_http_conf_in_addr_t *in_addr;
1320 ngx_uint_t regex, i;
1323 in_port = in_ports->elts;
1324 for (p = 0; p < in_ports->nelts; p++) {
1326 ngx_sort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts,
1327 sizeof(ngx_http_conf_in_addr_t), ngx_http_cmp_conf_in_addrs);
1330 * check whether all name-based servers have
1331 * the same configuraiton as the default server
1334 in_addr = in_port[p].addrs.elts;
1335 for (a = 0; a < in_port[p].addrs.nelts; a++) {
1337 name = in_addr[a].names.elts;
1338 for (s = 0; s < in_addr[a].names.nelts; s++) {
1340 if (in_addr[a].core_srv_conf != name[s].core_srv_conf) {
1346 * if all name-based servers have the same configuration
1347 * as the default server, then we do not need to check
1348 * them at run-time at all
1351 in_addr[a].names.nelts = 0;
1357 ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t));
1359 ha.temp_pool = ngx_create_pool(16384, cf->log);
1360 if (ha.temp_pool == NULL) {
1366 if (ngx_hash_keys_array_init(&ha, NGX_HASH_LARGE) != NGX_OK) {
1374 name = in_addr[a].names.elts;
1376 for (s = 0; s < in_addr[a].names.nelts; s++) {
1379 if (name[s].regex) {
1385 rc = ngx_hash_add_key(&ha, &name[s].name, name[s].core_srv_conf,
1386 NGX_HASH_WILDCARD_KEY);
1388 if (rc == NGX_ERROR) {
1392 if (rc == NGX_DECLINED) {
1393 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
1394 "invalid server name or wildcard \"%V\" on %s",
1395 &name[s].name, in_addr[a].listen_conf->addr);
1399 if (rc == NGX_BUSY) {
1400 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
1401 "conflicting server name \"%V\" on %s, ignored",
1402 &name[s].name, in_addr[a].listen_conf->addr);
1406 hash.key = ngx_hash_key_lc;
1407 hash.max_size = cmcf->server_names_hash_max_size;
1408 hash.bucket_size = cmcf->server_names_hash_bucket_size;
1409 hash.name = "server_names_hash";
1410 hash.pool = cf->pool;
1412 if (ha.keys.nelts) {
1413 hash.hash = &in_addr[a].hash;
1414 hash.temp_pool = NULL;
1416 if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK)
1422 if (ha.dns_wc_head.nelts) {
1424 ngx_qsort(ha.dns_wc_head.elts,
1425 (size_t) ha.dns_wc_head.nelts,
1426 sizeof(ngx_hash_key_t),
1427 ngx_http_cmp_dns_wildcards);
1430 hash.temp_pool = ha.temp_pool;
1432 if (ngx_hash_wildcard_init(&hash, ha.dns_wc_head.elts,
1433 ha.dns_wc_head.nelts)
1439 in_addr[a].wc_head = (ngx_hash_wildcard_t *) hash.hash;
1442 if (ha.dns_wc_tail.nelts) {
1444 ngx_qsort(ha.dns_wc_tail.elts,
1445 (size_t) ha.dns_wc_tail.nelts,
1446 sizeof(ngx_hash_key_t),
1447 ngx_http_cmp_dns_wildcards);
1450 hash.temp_pool = ha.temp_pool;
1452 if (ngx_hash_wildcard_init(&hash, ha.dns_wc_tail.elts,
1453 ha.dns_wc_tail.nelts)
1459 in_addr[a].wc_tail = (ngx_hash_wildcard_t *) hash.hash;
1462 ngx_destroy_pool(ha.temp_pool);
1470 in_addr[a].nregex = regex;
1471 in_addr[a].regex = ngx_palloc(cf->pool,
1472 regex * sizeof(ngx_http_server_name_t));
1474 if (in_addr[a].regex == NULL) {
1478 for (i = 0, s = 0; s < in_addr[a].names.nelts; s++) {
1479 if (name[s].regex) {
1480 in_addr[a].regex[i++] = name[s];
1486 if (ngx_http_init_listening(cf, &in_port[p]) != NGX_OK) {
1495 ngx_destroy_pool(ha.temp_pool);
1502 ngx_http_cmp_conf_in_addrs(const void *one, const void *two)
1504 ngx_http_conf_in_addr_t *first, *second;
1506 first = (ngx_http_conf_in_addr_t *) one;
1507 second = (ngx_http_conf_in_addr_t *) two;
1509 if (first->addr == INADDR_ANY) {
1510 /* the INADDR_ANY must be the last resort, shift it to the end */
1514 if (first->bind && !second->bind) {
1515 /* shift explicit bind()ed addresses to the start */
1519 if (!first->bind && second->bind) {
1520 /* shift explicit bind()ed addresses to the start */
1524 /* do not sort by default */
1530 static int ngx_libc_cdecl
1531 ngx_http_cmp_dns_wildcards(const void *one, const void *two)
1533 ngx_hash_key_t *first, *second;
1535 first = (ngx_hash_key_t *) one;
1536 second = (ngx_hash_key_t *) two;
1538 return ngx_strcmp(first->key.data, second->key.data);
1543 ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_in_port_t *in_port)
1545 ngx_uint_t i, a, last, bind_all, done;
1546 ngx_listening_t *ls;
1547 ngx_http_in_port_t *hip;
1548 ngx_http_conf_in_addr_t *in_addr;
1549 ngx_http_virtual_names_t *vn;
1550 ngx_http_core_loc_conf_t *clcf;
1551 ngx_http_core_srv_conf_t *cscf;
1553 in_addr = in_port->addrs.elts;
1554 last = in_port->addrs.nelts;
1557 * if there is a binding to a "*:port" then we need to bind()
1558 * to the "*:port" only and ignore other bindings
1561 if (in_addr[last - 1].addr == INADDR_ANY) {
1562 in_addr[last - 1].bind = 1;
1573 if (!bind_all && !in_addr[a].bind) {
1578 ls = ngx_listening_inet_stream_socket(cf, in_addr[a].addr,
1586 ls->handler = ngx_http_init_connection;
1588 cscf = in_addr[a].core_srv_conf;
1589 ls->pool_size = cscf->connection_pool_size;
1590 ls->post_accept_timeout = cscf->client_header_timeout;
1592 clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];
1594 ls->log = *clcf->err_log;
1595 ls->log.data = &ls->addr_text;
1596 ls->log.handler = ngx_accept_log_error;
1600 ngx_iocp_conf_t *iocpcf;
1602 iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module);
1603 if (iocpcf->acceptex_read) {
1604 ls->post_accept_buffer_size = cscf->client_header_buffer_size;
1609 ls->backlog = in_addr[a].listen_conf->backlog;
1610 ls->rcvbuf = in_addr[a].listen_conf->rcvbuf;
1611 ls->sndbuf = in_addr[a].listen_conf->sndbuf;
1613 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
1614 ls->accept_filter = in_addr[a].listen_conf->accept_filter;
1617 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
1618 ls->deferred_accept = in_addr[a].listen_conf->deferred_accept;
1621 hip = ngx_palloc(cf->pool, sizeof(ngx_http_in_port_t));
1626 hip->port = in_port->port;
1628 hip->port_text.data = ngx_pnalloc(cf->pool, 7);
1629 if (hip->port_text.data == NULL) {
1635 hip->port_text.len = ngx_sprintf(hip->port_text.data, ":%d", hip->port)
1636 - hip->port_text.data;
1638 in_addr = in_port->addrs.elts;
1640 if (in_addr[a].bind && in_addr[a].addr != INADDR_ANY) {
1644 } else if (in_port->addrs.nelts > 1
1645 && in_addr[last - 1].addr == INADDR_ANY)
1655 hip->addrs = ngx_pcalloc(cf->pool,
1656 hip->naddrs * sizeof(ngx_http_in_addr_t));
1657 if (hip->addrs == NULL) {
1661 for (i = 0; i < hip->naddrs; i++) {
1662 hip->addrs[i].addr = in_addr[i].addr;
1663 hip->addrs[i].core_srv_conf = in_addr[i].core_srv_conf;
1666 hip->addrs[i].ssl = in_addr[i].ssl;
1669 if (in_addr[i].hash.buckets == NULL
1670 && (in_addr[i].wc_head == NULL
1671 || in_addr[i].wc_head->hash.buckets == NULL)
1672 && (in_addr[i].wc_head == NULL
1673 || in_addr[i].wc_head->hash.buckets == NULL))
1678 vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
1682 hip->addrs[i].virtual_names = vn;
1684 vn->names.hash = in_addr[i].hash;
1685 vn->names.wc_head = in_addr[i].wc_head;
1686 vn->names.wc_tail = in_addr[i].wc_tail;
1688 vn->nregex = in_addr[i].nregex;
1689 vn->regex = in_addr[i].regex;
1698 in_port->addrs.elts = in_addr;
1709 ngx_http_types_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1713 ngx_array_t **types;
1714 ngx_str_t *value, *default_type;
1715 ngx_uint_t i, n, hash;
1716 ngx_hash_key_t *type;
1718 types = (ngx_array_t **) (p + cmd->offset);
1720 default_type = cmd->post;
1722 if (*types == NULL) {
1723 *types = ngx_array_create(cf->temp_pool, 1, sizeof(ngx_hash_key_t));
1724 if (*types == NULL) {
1725 return NGX_CONF_ERROR;
1729 type = ngx_array_push(*types);
1731 return NGX_CONF_ERROR;
1734 type->key = *default_type;
1735 type->key_hash = ngx_hash_key(default_type->data,
1737 type->value = (void *) 4;
1741 value = cf->args->elts;
1743 for (i = 1; i < cf->args->nelts; i++) {
1745 hash = ngx_hash_strlow(value[i].data, value[i].data, value[i].len);
1746 value[i].data[value[i].len] = '\0';
1748 type = (*types)->elts;
1749 for (n = 0; n < (*types)->nelts; n++) {
1751 if (ngx_strcmp(value[i].data, type[n].key.data) == 0) {
1752 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1753 "duplicate MIME type \"%V\"", &value[i]);
1758 type = ngx_array_push(*types);
1760 return NGX_CONF_ERROR;
1763 type->key = value[i];
1764 type->key_hash = hash;
1765 type->value = (void *) 4;
1773 ngx_http_merge_types(ngx_conf_t *cf, ngx_array_t *keys, ngx_hash_t *types_hash,
1774 ngx_array_t *prev_keys, ngx_hash_t *prev_types_hash,
1775 ngx_str_t *default_types)
1777 ngx_hash_init_t hash;
1781 hash.hash = types_hash;
1783 hash.max_size = 2048;
1784 hash.bucket_size = 64;
1785 hash.name = "test_types_hash";
1786 hash.pool = cf->pool;
1787 hash.temp_pool = NULL;
1789 if (ngx_hash_init(&hash, keys->elts, keys->nelts) != NGX_OK) {
1790 return NGX_CONF_ERROR;
1796 if (prev_types_hash->buckets == NULL) {
1798 if (prev_keys == NULL) {
1800 if (ngx_http_set_default_types(cf, &prev_keys, default_types)
1803 return NGX_CONF_ERROR;
1807 hash.hash = prev_types_hash;
1809 hash.max_size = 2048;
1810 hash.bucket_size = 64;
1811 hash.name = "test_types_hash";
1812 hash.pool = cf->pool;
1813 hash.temp_pool = NULL;
1815 if (ngx_hash_init(&hash, prev_keys->elts, prev_keys->nelts) != NGX_OK) {
1816 return NGX_CONF_ERROR;
1820 *types_hash = *prev_types_hash;
1828 ngx_http_set_default_types(ngx_conf_t *cf, ngx_array_t **types,
1829 ngx_str_t *default_type)
1831 ngx_hash_key_t *type;
1833 *types = ngx_array_create(cf->temp_pool, 1, sizeof(ngx_hash_key_t));
1834 if (*types == NULL) {
1838 while (default_type->len) {
1840 type = ngx_array_push(*types);
1845 type->key = *default_type;
1846 type->key_hash = ngx_hash_key(default_type->data,
1848 type->value = (void *) 4;