1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_event.h>
10 #include <ngx_http.h>
11
12
13 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
14 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
15 ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *lscf,
16 ngx_http_core_srv_conf_t *cscf);
17 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
18 ngx_http_conf_in_addr_t *in_addr, ngx_http_core_srv_conf_t *cscf);
19 static char *ngx_http_merge_locations(ngx_conf_t *cf,
20 ngx_array_t *locations, void **loc_conf, ngx_http_module_t *module,
21 ngx_uint_t ctx_index);
22 static int ngx_http_cmp_conf_in_addrs(const void *one, const void *two);
23 static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one,
24 const void *two);
25
26 ngx_uint_t ngx_http_max_module;
27
28 ngx_uint_t ngx_http_total_requests;
29 uint64_t ngx_http_total_sent;
30
31
32 ngx_int_t (*ngx_http_top_header_filter) (ngx_http_request_t *r);
33 ngx_int_t (*ngx_http_top_body_filter) (ngx_http_request_t *r, ngx_chain_t *ch);
34
35
36 static ngx_command_t ngx_http_commands[] = {
37
38 { ngx_string("http"),
39 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
40 ngx_http_block,
41 0,
42 0,
43 NULL },
44
45 ngx_null_command
46 };
47
48
49 static ngx_core_module_t ngx_http_module_ctx = {
50 ngx_string("http"),
51 NULL,
52 NULL
53 };
54
55
56 ngx_module_t ngx_http_module = {
57 NGX_MODULE_V1,
58 &ngx_http_module_ctx, /* module context */
59 ngx_http_commands, /* module directives */
60 NGX_CORE_MODULE, /* module type */
61 NULL, /* init master */
62 NULL, /* init module */
63 NULL, /* init process */
64 NULL, /* init thread */
65 NULL, /* exit thread */
66 NULL, /* exit process */
67 NULL, /* exit master */
68 NGX_MODULE_V1_PADDING
69 };
70
71
72 static char *
73 ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
74 {
75 char *rv;
76 ngx_int_t rc, j;
77 ngx_uint_t mi, m, s, l, p, a, i, n;
78 ngx_uint_t find_config_index, use_rewrite, use_access;
79 ngx_uint_t last, bind_all, done;
80 ngx_conf_t pcf;
81 ngx_array_t headers_in, in_ports;
82 ngx_hash_key_t *hk;
83 ngx_hash_init_t hash;
84 ngx_listening_t *ls;
85 ngx_http_listen_t *lscf;
86 ngx_http_module_t *module;
87 ngx_http_header_t *header;
88 ngx_http_in_port_t *hip;
89 ngx_http_handler_pt *h;
90 ngx_http_conf_ctx_t *ctx;
91 ngx_http_conf_in_port_t *in_port;
92 ngx_http_conf_in_addr_t *in_addr;
93 ngx_hash_keys_arrays_t ha;
94 ngx_http_server_name_t *name;
95 ngx_http_phase_handler_t *ph;
96 ngx_http_virtual_names_t *vn;
97 ngx_http_core_srv_conf_t **cscfp, *cscf;
98 ngx_http_core_loc_conf_t *clcf;
99 ngx_http_phase_handler_pt checker;
100 ngx_http_core_main_conf_t *cmcf;
101 #if (NGX_PCRE)
102 ngx_uint_t regex;
103 #endif
104
105 /* the main http context */
106
107 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
108 if (ctx == NULL) {
109 return NGX_CONF_ERROR;
110 }
111
112 *(ngx_http_conf_ctx_t **) conf = ctx;
113
114
115 /* count the number of the http modules and set up their indices */
116
117 ngx_http_max_module = 0;
118 for (m = 0; ngx_modules[m]; m++) {
119 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
120 continue;
121 }
122
123 ngx_modules[m]->ctx_index = ngx_http_max_module++;
124 }
125
126
127 /* the http main_conf context, it is the same in the all http contexts */
128
129 ctx->main_conf = ngx_pcalloc(cf->pool,
130 sizeof(void *) * ngx_http_max_module);
131 if (ctx->main_conf == NULL) {
132 return NGX_CONF_ERROR;
133 }
134
135
136 /*
137 * the http null srv_conf context, it is used to merge
138 * the server{}s' srv_conf's
139 */
140
141 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
142 if (ctx->srv_conf == NULL) {
143 return NGX_CONF_ERROR;
144 }
145
146
147 /*
148 * the http null loc_conf context, it is used to merge
149 * the server{}s' loc_conf's
150 */
151
152 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
153 if (ctx->loc_conf == NULL) {
154 return NGX_CONF_ERROR;
155 }
156
157
158 /*
159 * create the main_conf's, the null srv_conf's, and the null loc_conf's
160 * of the all http modules
161 */
162
163 for (m = 0; ngx_modules[m]; m++) {
164 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
165 continue;
166 }
167
168 module = ngx_modules[m]->ctx;
169 mi = ngx_modules[m]->ctx_index;
170
171 if (module->create_main_conf) {
172 ctx->main_conf[mi] = module->create_main_conf(cf);
173 if (ctx->main_conf[mi] == NULL) {
174 return NGX_CONF_ERROR;
175 }
176 }
177
178 if (module->create_srv_conf) {
179 ctx->srv_conf[mi] = module->create_srv_conf(cf);
180 if (ctx->srv_conf[mi] == NULL) {
181 return NGX_CONF_ERROR;
182 }
183 }
184
185 if (module->create_loc_conf) {
186 ctx->loc_conf[mi] = module->create_loc_conf(cf);
187 if (ctx->loc_conf[mi] == NULL) {
188 return NGX_CONF_ERROR;
189 }
190 }
191 }
192
193 pcf = *cf;
194 cf->ctx = ctx;
195
196 for (m = 0; ngx_modules[m]; m++) {
197 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
198 continue;
199 }
200
201 module = ngx_modules[m]->ctx;
202 mi = ngx_modules[m]->ctx_index;
203
204 if (module->preconfiguration) {
205 if (module->preconfiguration(cf) != NGX_OK) {
206 return NGX_CONF_ERROR;
207 }
208 }
209 }
210
211 /* parse inside the http{} block */
212
213 cf->module_type = NGX_HTTP_MODULE;
214 cf->cmd_type = NGX_HTTP_MAIN_CONF;
215 rv = ngx_conf_parse(cf, NULL);
216
217 if (rv != NGX_CONF_OK) {
218 *cf = pcf;
219 return rv;
220 }
221
222 /*
223 * init http{} main_conf's, merge the server{}s' srv_conf's
224 * and its location{}s' loc_conf's
225 */
226
227 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
228 cscfp = cmcf->servers.elts;
229
230 for (m = 0; ngx_modules[m]; m++) {
231 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
232 continue;
233 }
234
235 module = ngx_modules[m]->ctx;
236 mi = ngx_modules[m]->ctx_index;
237
238 /* init http{} main_conf's */
239
240 if (module->init_main_conf) {
241 rv = module->init_main_conf(cf, ctx->main_conf[mi]);
242 if (rv != NGX_CONF_OK) {
243 *cf = pcf;
244 return rv;
245 }
246 }
247
248 for (s = 0; s < cmcf->servers.nelts; s++) {
249
250 /* merge the server{}s' srv_conf's */
251
252 if (module->merge_srv_conf) {
253 rv = module->merge_srv_conf(cf,
254 ctx->srv_conf[mi],
255 cscfp[s]->ctx->srv_conf[mi]);
256 if (rv != NGX_CONF_OK) {
257 *cf = pcf;
258 return rv;
259 }
260 }
261
262 if (module->merge_loc_conf) {
263
264 /* merge the server{}'s loc_conf */
265
266 rv = module->merge_loc_conf(cf,
267 ctx->loc_conf[mi],
268 cscfp[s]->ctx->loc_conf[mi]);
269 if (rv != NGX_CONF_OK) {
270 *cf = pcf;
271 return rv;
272 }
273
274 /* merge the locations{}' loc_conf's */
275
276 rv = ngx_http_merge_locations(cf, &cscfp[s]->locations,
277 cscfp[s]->ctx->loc_conf,
278 module, mi);
279 if (rv != NGX_CONF_OK) {
280 *cf = pcf;
281 return rv;
282 }
283 }
284 }
285 }
286
287
288 /* init lists of the handlers */
289
290 if (ngx_array_init(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers,
291 cf->pool, 1, sizeof(ngx_http_handler_pt))
292 != NGX_OK)
293 {
294 return NGX_CONF_ERROR;
295 }
296
297
298 if (ngx_array_init(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers,
299 cf->pool, 1, sizeof(ngx_http_handler_pt))
300 != NGX_OK)
301 {
302 return NGX_CONF_ERROR;
303 }
304
305
306 if (ngx_array_init(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers,
307 cf->pool, 1, sizeof(ngx_http_handler_pt))
308 != NGX_OK)
309 {
310 return NGX_CONF_ERROR;
311 }
312
313
314 if (ngx_array_init(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers,
315 cf->pool, 1, sizeof(ngx_http_handler_pt))
316 != NGX_OK)
317 {
318 return NGX_CONF_ERROR;
319 }
320
321
322 if (ngx_array_init(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers,
323 cf->pool, 2, sizeof(ngx_http_handler_pt))
324 != NGX_OK)
325 {
326 return NGX_CONF_ERROR;
327 }
328
329
330 if (ngx_array_init(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers,
331 cf->pool, 4, sizeof(ngx_http_handler_pt))
332 != NGX_OK)
333 {
334 return NGX_CONF_ERROR;
335 }
336
337
338 if (ngx_array_init(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers,
339 cf->pool, 1, sizeof(ngx_http_handler_pt))
340 != NGX_OK)
341 {
342 return NGX_CONF_ERROR;
343 }
344
345
346 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t))
347 != NGX_OK)
348 {
349 return NGX_CONF_ERROR;
350 }
351
352 for (header = ngx_http_headers_in; header->name.len; header++) {
353 hk = ngx_array_push(&headers_in);
354 if (hk == NULL) {
355 return NGX_CONF_ERROR;
356 }
357
358 hk->key = header->name;
359 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len);
360 hk->value = header;
361 }
362
363 hash.hash = &cmcf->headers_in_hash;
364 hash.key = ngx_hash_key_lc;
365 hash.max_size = 512;
366 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
367 hash.name = "headers_in_hash";
368 hash.pool = cf->pool;
369 hash.temp_pool = NULL;
370
371 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) {
372 return NGX_CONF_ERROR;
373 }
374
375
376 for (m = 0; ngx_modules[m]; m++) {
377 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
378 continue;
379 }
380
381 module = ngx_modules[m]->ctx;
382 mi = ngx_modules[m]->ctx_index;
383
384 if (module->postconfiguration) {
385 if (module->postconfiguration(cf) != NGX_OK) {
386 return NGX_CONF_ERROR;
387 }
388 }
389 }
390
391 if (ngx_http_variables_init_vars(cf) != NGX_OK) {
392 return NGX_CONF_ERROR;
393 }
394
395 /*
396 * http{}'s cf->ctx was needed while the configuration merging
397 * and in postconfiguration process
398 */
399
400 *cf = pcf;
401
402
403 cmcf->phase_engine.server_rewrite_index = (ngx_uint_t) -1;
404 cmcf->phase_engine.location_rewrite_index = (ngx_uint_t) -1;
405 find_config_index = 0;
406 use_rewrite = cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers.nelts ? 1 : 0;
407 use_access = cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers.nelts ? 1 : 0;
408
409 n = use_rewrite + use_access + 1; /* find config phase */
410
411 for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
412 n += cmcf->phases[i].handlers.nelts;
413 }
414
415 ph = ngx_pcalloc(cf->pool,
416 n * sizeof(ngx_http_phase_handler_t) + sizeof(void *));
417 if (ph == NULL) {
418 return NGX_CONF_ERROR;
419 }
420
421 cmcf->phase_engine.handlers = ph;
422 n = 0;
423
424 for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
425 h = cmcf->phases[i].handlers.elts;
426
427 switch (i) {
428
429 case NGX_HTTP_SERVER_REWRITE_PHASE:
430 if (cmcf->phase_engine.server_rewrite_index == (ngx_uint_t) -1) {
431 cmcf->phase_engine.server_rewrite_index = n;
432 }
433 checker = ngx_http_core_generic_phase;
434
435 break;
436
437 case NGX_HTTP_FIND_CONFIG_PHASE:
438 find_config_index = n;
439
440 ph->checker = ngx_http_core_find_config_phase;
441 n++;
442 ph++;
443
444 continue;
445
446 case NGX_HTTP_REWRITE_PHASE:
447 if (cmcf->phase_engine.location_rewrite_index == (ngx_uint_t) -1) {
448 cmcf->phase_engine.location_rewrite_index = n;
449 }
450 checker = ngx_http_core_generic_phase;
451
452 break;
453
454 case NGX_HTTP_POST_REWRITE_PHASE:
455 if (use_rewrite) {
456 ph->checker = ngx_http_core_post_rewrite_phase;
457 ph->next = find_config_index;
458 n++;
459 ph++;
460 }
461
462 continue;
463
464 case NGX_HTTP_ACCESS_PHASE:
465 checker = ngx_http_core_access_phase;
466 n++;
467 break;
468
469 case NGX_HTTP_POST_ACCESS_PHASE:
470 if (use_access) {
471 ph->checker = ngx_http_core_post_access_phase;
472 ph->next = n;
473 ph++;
474 }
475
476 continue;
477
478 case NGX_HTTP_CONTENT_PHASE:
479 checker = ngx_http_core_content_phase;
480 break;
481
482 default:
483 checker = ngx_http_core_generic_phase;
484 }
485
486 n += cmcf->phases[i].handlers.nelts;
487
488 for (j = cmcf->phases[i].handlers.nelts - 1; j >=0; j--) {
489 ph->checker = checker;
490 ph->handler = h[j];
491 ph->next = n;
492 ph++;
493 }
494 }
495
496
497 /*
498 * create the lists of ports, addresses and server names
499 * to quickly find the server core module configuration at run-time
500 */
501
502 if (ngx_array_init(&in_ports, cf->temp_pool, 2,
503 sizeof(ngx_http_conf_in_port_t))
504 != NGX_OK)
505 {
506 return NGX_CONF_ERROR;
507 }
508
509 /* "server" directives */
510
511 cscfp = cmcf->servers.elts;
512 for (s = 0; s < cmcf->servers.nelts; s++) {
513
514 /* "listen" directives */
515
516 lscf = cscfp[s]->listen.elts;
517 for (l = 0; l < cscfp[s]->listen.nelts; l++) {
518
519 /* AF_INET only */
520
521 in_port = in_ports.elts;
522 for (p = 0; p < in_ports.nelts; p++) {
523
524 if (lscf[l].port != in_port[p].port) {
525 continue;
526 }
527
528 /* the port is already in the port list */
529
530 in_addr = in_port[p].addrs.elts;
531 for (a = 0; a < in_port[p].addrs.nelts; a++) {
532
533 if (lscf[l].addr != in_addr[a].addr) {
534 continue;
535 }
536
537 /* the address is already in the address list */
538
539 if (ngx_http_add_names(cf, &in_addr[a], cscfp[s]) != NGX_OK)
540 {
541 return NGX_CONF_ERROR;
542 }
543
544 /*
545 * check the duplicate "default" server
546 * for this address:port
547 */
548
549 if (lscf[l].conf.default_server) {
550
551 if (in_addr[a].default_server) {
552 ngx_log_error(NGX_LOG_ERR, cf->log, 0,
553 "the duplicate default server in %s:%ui",
554 &lscf[l].file_name, lscf[l].line);
555
556 return NGX_CONF_ERROR;
557 }
558
559 in_addr[a].core_srv_conf = cscfp[s];
560 in_addr[a].default_server = 1;
561 }
562
563 goto found;
564 }
565
566 /*
567 * add the address to the addresses list that
568 * bound to this port
569 */
570
571 if (ngx_http_add_address(cf, &in_port[p], &lscf[l], cscfp[s])
572 != NGX_OK)
573 {
574 return NGX_CONF_ERROR;
575 }
576
577 goto found;
578 }
579
580 /* add the port to the in_port list */
581
582 in_port = ngx_array_push(&in_ports);
583 if (in_port == NULL) {
584 return NGX_CONF_ERROR;
585 }
586
587 in_port->port = lscf[l].port;
588 in_port->addrs.elts = NULL;
589
590 if (ngx_http_add_address(cf, in_port, &lscf[l], cscfp[s]) != NGX_OK)
591 {
592 return NGX_CONF_ERROR;
593 }
594
595 found:
596
597 continue;
598 }
599 }
600
601
602 /* optimize the lists of ports, addresses and server names */
603
604 /* AF_INET only */
605
606 in_port = in_ports.elts;
607 for (p = 0; p < in_ports.nelts; p++) {
608
609 ngx_sort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts,
610 sizeof(ngx_http_conf_in_addr_t), ngx_http_cmp_conf_in_addrs);
611
612 /*
613 * check whether all name-based servers have the same configuraiton
614 * as the default server,
615 * or some servers disable optimizing the server names
616 */
617
618 in_addr = in_port[p].addrs.elts;
619 for (a = 0; a < in_port[p].addrs.nelts; a++) {
620
621 name = in_addr[a].names.elts;
622 for (s = 0; s < in_addr[a].names.nelts; s++) {
623
624 if (in_addr[a].core_srv_conf != name[s].core_srv_conf
625 || name[s].core_srv_conf->optimize_server_names == 0)
626 {
627 goto virtual_names;
628 }
629 }
630
631 /*
632 * if all name-based servers have the same configuration
633 * as the default server,
634 * and no servers disable optimizing the server names
635 * then we do not need to check them at run-time at all
636 */
637
638 in_addr[a].names.nelts = 0;
639
640 continue;
641
642 virtual_names:
643
644 ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t));
645
646 ha.temp_pool = ngx_create_pool(16384, cf->log);
647 if (ha.temp_pool == NULL) {
648 return NGX_CONF_ERROR;
649 }
650
651 ha.pool = cf->pool;
652
653 if (ngx_hash_keys_array_init(&ha, NGX_HASH_LARGE) != NGX_OK) {
654 ngx_destroy_pool(ha.temp_pool);
655 return NGX_CONF_ERROR;
656 }
657
658 #if (NGX_PCRE)
659 regex = 0;
660 #endif
661
662 name = in_addr[a].names.elts;
663
664 for (s = 0; s < in_addr[a].names.nelts; s++) {
665
666 #if (NGX_PCRE)
667 if (name[s].regex) {
668 regex++;
669 continue;
670 }
671 #endif
672
673 rc = ngx_hash_add_key(&ha, &name[s].name, name[s].core_srv_conf,
674 NGX_HASH_WILDCARD_KEY);
675
676 if (rc == NGX_ERROR) {
677 return NGX_CONF_ERROR;
678 }
679
680 if (rc == NGX_DECLINED) {
681 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
682 "invalid server name or wildcard \"%V\" on %s",
683 &name[s].name, in_addr[a].listen_conf->addr);
684 return NGX_CONF_ERROR;
685 }
686
687 if (rc == NGX_BUSY) {
688 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
689 "conflicting server name \"%V\" on %s, ignored",
690 &name[s].name, in_addr[a].listen_conf->addr);
691 }
692 }
693
694 hash.key = ngx_hash_key_lc;
695 hash.max_size = cmcf->server_names_hash_max_size;
696 hash.bucket_size = cmcf->server_names_hash_bucket_size;
697 hash.name = "server_names_hash";
698 hash.pool = cf->pool;
699
700 if (ha.keys.nelts) {
701 hash.hash = &in_addr[a].hash;
702 hash.temp_pool = NULL;
703
704 if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK)
705 {
706 ngx_destroy_pool(ha.temp_pool);
707 return NGX_CONF_ERROR;
708 }
709 }
710
711 if (ha.dns_wc_head.nelts) {
712
713 ngx_qsort(ha.dns_wc_head.elts,
714 (size_t) ha.dns_wc_head.nelts,
715 sizeof(ngx_hash_key_t),
716 ngx_http_cmp_dns_wildcards);
717
718 hash.hash = NULL;
719 hash.temp_pool = ha.temp_pool;
720
721 if (ngx_hash_wildcard_init(&hash, ha.dns_wc_head.elts,
722 ha.dns_wc_head.nelts)
723 != NGX_OK)
724 {
725 ngx_destroy_pool(ha.temp_pool);
726 return NGX_CONF_ERROR;
727 }
728
729 in_addr[a].wc_head = (ngx_hash_wildcard_t *) hash.hash;
730 }
731
732 if (ha.dns_wc_tail.nelts) {
733
734 ngx_qsort(ha.dns_wc_tail.elts,
735 (size_t) ha.dns_wc_tail.nelts,
736 sizeof(ngx_hash_key_t),
737 ngx_http_cmp_dns_wildcards);
738
739 hash.hash = NULL;
740 hash.temp_pool = ha.temp_pool;
741
742 if (ngx_hash_wildcard_init(&hash, ha.dns_wc_tail.elts,
743 ha.dns_wc_tail.nelts)
744 != NGX_OK)
745 {
746 ngx_destroy_pool(ha.temp_pool);
747 return NGX_CONF_ERROR;
748 }
749
750 in_addr[a].wc_tail = (ngx_hash_wildcard_t *) hash.hash;
751 }
752
753 ngx_destroy_pool(ha.temp_pool);
754
755 #if (NGX_PCRE)
756
757 if (regex == 0) {
758 continue;
759 }
760
761 in_addr[a].nregex = regex;
762 in_addr[a].regex = ngx_palloc(cf->pool,
763 regex * sizeof(ngx_http_server_name_t));
764
765 if (in_addr[a].regex == NULL) {
766 return NGX_CONF_ERROR;
767 }
768
769 for (i = 0, s = 0; s < in_addr[a].names.nelts; s++) {
770 if (name[s].regex) {
771 in_addr[a].regex[i++] = name[s];
772 }
773 }
774 #endif
775 }
776
777 in_addr = in_port[p].addrs.elts;
778 last = in_port[p].addrs.nelts;
779
780 /*
781 * if there is the binding to the "*:port" then we need to bind()
782 * to the "*:port" only and ignore the other bindings
783 */
784
785 if (in_addr[last - 1].addr == INADDR_ANY) {
786 in_addr[last - 1].bind = 1;
787 bind_all = 0;
788
789 } else {
790 bind_all = 1;
791 }
792
793 for (a = 0; a < last; /* void */ ) {
794
795 if (!bind_all && !in_addr[a].bind) {
796 a++;
797 continue;
798 }
799
800 ls = ngx_listening_inet_stream_socket(cf, in_addr[a].addr,
801 in_port[p].port);
802 if (ls == NULL) {
803 return NGX_CONF_ERROR;
804 }
805
806 ls->addr_ntop = 1;
807
808 ls->handler = ngx_http_init_connection;
809
810 cscf = in_addr[a].core_srv_conf;
811 ls->pool_size = cscf->connection_pool_size;
812 ls->post_accept_timeout = cscf->client_header_timeout;
813
814 clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];
815
816 ls->log = *clcf->err_log;
817 ls->log.data = &ls->addr_text;
818 ls->log.handler = ngx_accept_log_error;
819
820 #if (NGX_WIN32)
821 {
822 ngx_iocp_conf_t *iocpcf;
823
824 iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module);
825 if (iocpcf->acceptex_read) {
826 ls->post_accept_buffer_size = cscf->client_header_buffer_size;
827 }
828 }
829 #endif
830
831 ls->backlog = in_addr[a].listen_conf->backlog;
832 ls->rcvbuf = in_addr[a].listen_conf->rcvbuf;
833 ls->sndbuf = in_addr[a].listen_conf->sndbuf;
834
835 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
836 ls->accept_filter = in_addr[a].listen_conf->accept_filter;
837 #endif
838
839 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
840 ls->deferred_accept = in_addr[a].listen_conf->deferred_accept;
841 #endif
842
843 hip = ngx_palloc(cf->pool, sizeof(ngx_http_in_port_t));
844 if (hip == NULL) {
845 return NGX_CONF_ERROR;
846 }
847
848 hip->port = in_port[p].port;
849
850 hip->port_text.data = ngx_palloc(cf->pool, 7);
851 if (hip->port_text.data == NULL) {
852 return NGX_CONF_ERROR;
853 }
854
855 ls->servers = hip;
856
857 hip->port_text.len = ngx_sprintf(hip->port_text.data, ":%d",
858 hip->port)
859 - hip->port_text.data;
860
861 in_addr = in_port[p].addrs.elts;
862
863 if (in_addr[a].bind && in_addr[a].addr != INADDR_ANY) {
864 hip->naddrs = 1;
865 done = 0;
866
867 } else if (in_port[p].addrs.nelts > 1
868 && in_addr[last - 1].addr == INADDR_ANY)
869 {
870 hip->naddrs = last;
871 done = 1;
872
873 } else {
874 hip->naddrs = 1;
875 done = 0;
876 }
877
878 #if 0
879 ngx_log_error(NGX_LOG_ALERT, cf->log, 0,
880 "%ui: %V %d %ui %ui",
881 a, &ls->addr_text, in_addr[a].bind,
882 hip->naddrs, last);
883 #endif
884
885 hip->addrs = ngx_pcalloc(cf->pool,
886 hip->naddrs * sizeof(ngx_http_in_addr_t));
887 if (hip->addrs == NULL) {
888 return NGX_CONF_ERROR;
889 }
890
891 for (i = 0; i < hip->naddrs; i++) {
892 hip->addrs[i].addr = in_addr[i].addr;
893 hip->addrs[i].core_srv_conf = in_addr[i].core_srv_conf;
894
895 if (in_addr[i].hash.buckets == NULL
896 && (in_addr[i].wc_head == NULL
897 || in_addr[i].wc_head->hash.buckets == NULL)
898 && (in_addr[i].wc_head == NULL
899 || in_addr[i].wc_head->hash.buckets == NULL))
900 {
901 continue;
902 }
903
904 vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
905 if (vn == NULL) {
906 return NGX_CONF_ERROR;
907 }
908 hip->addrs[i].virtual_names = vn;
909
910 vn->names.hash = in_addr[i].hash;
911 vn->names.wc_head = in_addr[i].wc_head;
912 vn->names.wc_tail = in_addr[i].wc_tail;
913 #if (NGX_PCRE)
914 vn->nregex = in_addr[i].nregex;
915 vn->regex = in_addr[i].regex;
916 #endif
917 }
918
919 if (done) {
920 break;
921 }
922
923 in_addr++;
924 in_port[p].addrs.elts = in_addr;
925 last--;
926
927 a = 0;
928 }
929 }
930
931 #if 0
932 {
933 u_char address[20];
934 ngx_uint_t p, a;
935
936 in_port = in_ports.elts;
937 for (p = 0; p < in_ports.nelts; p++) {
938 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
939 "port: %d %p", in_port[p].port, &in_port[p]);
940 in_addr = in_port[p].addrs.elts;
941 for (a = 0; a < in_port[p].addrs.nelts; a++) {
942 ngx_inet_ntop(AF_INET, &in_addr[a].addr, address, 20);
943 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, cf->log, 0,
944 "%s:%d %p",
945 address, in_port[p].port, in_addr[a].core_srv_conf);
946 name = in_addr[a].names.elts;
947 for (n = 0; n < in_addr[a].names.nelts; n++) {
948 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, cf->log, 0,
949 "%s:%d %V %p",
950 address, in_port[p].port, &name[n].name,
951 name[n].core_srv_conf);
952 }
953 }
954 }
955 }
956 #endif
957
958 return NGX_CONF_OK;
959 }
960
961
962 /*
963 * add the server address, the server names and the server core module
964 * configurations to the port (in_port)
965 */
966
967 static ngx_int_t
968 ngx_http_add_address(ngx_conf_t *cf, ngx_http_conf_in_port_t *in_port,
969 ngx_http_listen_t *lscf, ngx_http_core_srv_conf_t *cscf)
970 {
971 ngx_http_conf_in_addr_t *in_addr;
972
973 if (in_port->addrs.elts == NULL) {
974 if (ngx_array_init(&in_port->addrs, cf->temp_pool, 4,
975 sizeof(ngx_http_conf_in_addr_t))
976 != NGX_OK)
977 {
978 return NGX_ERROR;
979 }
980 }
981
982 in_addr = ngx_array_push(&in_port->addrs);
983 if (in_addr == NULL) {
984 return NGX_ERROR;
985 }
986
987 in_addr->addr = lscf->addr;
988 in_addr->hash.buckets = NULL;
989 in_addr->hash.size = 0;
990 in_addr->wc_head = NULL;
991 in_addr->wc_tail = NULL;
992 in_addr->names.elts = NULL;
993 #if (NGX_PCRE)
994 in_addr->nregex = 0;
995 in_addr->regex = NULL;
996 #endif
997 in_addr->core_srv_conf = cscf;
998 in_addr->default_server = lscf->conf.default_server;
999 in_addr->bind = lscf->conf.bind;
1000 in_addr->listen_conf = &lscf->conf;
1001
1002 #if (NGX_DEBUG)
1003 {
1004 u_char text[20];
1005 ngx_inet_ntop(AF_INET, &in_addr->addr, text, 20);
1006 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0, "address: %s:%d",
1007 text, in_port->port);
1008 }
1009 #endif
1010
1011 return ngx_http_add_names(cf, in_addr, cscf);
1012 }
1013
1014
1015 /*
1016 * add the server names and the server core module
1017 * configurations to the address:port (in_addr)
1018 */
1019
1020 static ngx_int_t
1021 ngx_http_add_names(ngx_conf_t *cf, ngx_http_conf_in_addr_t *in_addr,
1022 ngx_http_core_srv_conf_t *cscf)
1023 {
1024 ngx_uint_t i, n;
1025 ngx_http_server_name_t *server_names, *name;
1026
1027 if (in_addr->names.elts == NULL) {
1028 if (ngx_array_init(&in_addr->names, cf->temp_pool, 4,
1029 sizeof(ngx_http_server_name_t))
1030 != NGX_OK)
1031 {
1032 return NGX_ERROR;
1033 }
1034 }
1035
1036 server_names = cscf->server_names.elts;
1037
1038 for (i = 0; i < cscf->server_names.nelts; i++) {
1039
1040 for (n = 0; n < server_names[i].name.len; n++) {
1041 server_names[i].name.data[n] =
1042 ngx_tolower(server_names[i].name.data[n]);
1043 }
1044
1045 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
1046 "name: %V", &server_names[i].name);
1047
1048 name = ngx_array_push(&in_addr->names);
1049 if (name == NULL) {
1050 return NGX_ERROR;
1051 }
1052
1053 *name = server_names[i];
1054 }
1055
1056 return NGX_OK;
1057 }
1058
1059
1060 static char *
1061 ngx_http_merge_locations(ngx_conf_t *cf, ngx_array_t *locations,
1062 void **loc_conf, ngx_http_module_t *module, ngx_uint_t ctx_index)
1063 {
1064 char *rv;
1065 ngx_uint_t i;
1066 ngx_http_core_loc_conf_t **clcfp;
1067
1068 clcfp = locations->elts;
1069
1070 for (i = 0; i < locations->nelts; i++) {
1071 rv = module->merge_loc_conf(cf, loc_conf[ctx_index],
1072 clcfp[i]->loc_conf[ctx_index]);
1073 if (rv != NGX_CONF_OK) {
1074 return rv;
1075 }
1076
1077 if (clcfp[i]->locations == NULL) {
1078 continue;
1079 }
1080
1081 rv = ngx_http_merge_locations(cf, clcfp[i]->locations,
1082 clcfp[i]->loc_conf, module, ctx_index);
1083 if (rv != NGX_CONF_OK) {
1084 return rv;
1085 }
1086 }
1087
1088 return NGX_CONF_OK;
1089 }
1090
1091
1092 static int
1093 ngx_http_cmp_conf_in_addrs(const void *one, const void *two)
1094 {
1095 ngx_http_conf_in_addr_t *first, *second;
1096
1097 first = (ngx_http_conf_in_addr_t *) one;
1098 second = (ngx_http_conf_in_addr_t *) two;
1099
1100 if (first->addr == INADDR_ANY) {
1101 /* the INADDR_ANY must be the last resort, shift it to the end */
1102 return 1;
1103 }
1104
1105 if (first->bind && !second->bind) {
1106 /* shift explicit bind()ed addresses to the start */
1107 return -1;
1108 }
1109
1110 if (!first->bind && second->bind) {
1111 /* shift explicit bind()ed addresses to the start */
1112 return 1;
1113 }
1114
1115 /* do not sort by default */
1116
1117 return 0;
1118 }
1119
1120
1121 static int ngx_libc_cdecl
1122 ngx_http_cmp_dns_wildcards(const void *one, const void *two)
1123 {
1124 ngx_hash_key_t *first, *second;
1125
1126 first = (ngx_hash_key_t *) one;
1127 second = (ngx_hash_key_t *) two;
1128
1129 return ngx_strcmp(first->key.data, second->key.data);
1130 }
1131
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.