comparison src/http/ngx_http.c @ 26:45fe5b98a9de NGINX_0_1_13

nginx 0.1.13 *) Feature: the server_names_hash and server_names_hash_threshold directives. *) Bugfix: the *.domain.tld names in the "server_name" directive did not work. *) Bugfix: the %request_length log parameter logged the incorrect length.
author Igor Sysoev <http://sysoev.ru>
date Tue, 21 Dec 2004 00:00:00 +0300
parents 46833bd150cb
children aab2ea7c0458
comparison
equal deleted inserted replaced
25:21488c53e135 26:45fe5b98a9de
9 #include <ngx_event.h> 9 #include <ngx_event.h>
10 #include <ngx_http.h> 10 #include <ngx_http.h>
11 11
12 12
13 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 13 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
14 static int ngx_cmp_server_names(const void *one, const void *two);
14 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf, 15 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
15 ngx_http_in_port_t *in_port, 16 ngx_http_in_port_t *in_port,
16 ngx_http_listen_t *lscf, 17 ngx_http_listen_t *lscf,
17 ngx_http_core_srv_conf_t *cscf); 18 ngx_http_core_srv_conf_t *cscf);
18 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf, 19 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
67 68
68 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 69 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
69 { 70 {
70 char *rv; 71 char *rv;
71 ngx_uint_t mi, m, s, l, p, a, n; 72 ngx_uint_t mi, m, s, l, p, a, n;
72 ngx_uint_t port_found, addr_found, virtual_names; 73 ngx_uint_t port_found, addr_found, virtual_names, key;
73 ngx_conf_t pcf; 74 ngx_conf_t pcf;
74 ngx_array_t in_ports; 75 ngx_array_t in_ports;
75 ngx_listening_t *ls; 76 ngx_listening_t *ls;
76 ngx_http_listen_t *lscf; 77 ngx_http_listen_t *lscf;
77 ngx_http_module_t *module; 78 ngx_http_module_t *module;
90 #if (NGX_SUPPRESS_WARN) 91 #if (NGX_SUPPRESS_WARN)
91 /* MSVC thinks 'in_ports' may be used without having been initialized */ 92 /* MSVC thinks 'in_ports' may be used without having been initialized */
92 ngx_memzero(&in_ports, sizeof(ngx_array_t)); 93 ngx_memzero(&in_ports, sizeof(ngx_array_t));
93 #endif 94 #endif
94 95
96
95 /* the main http context */ 97 /* the main http context */
98
96 ngx_test_null(ctx, 99 ngx_test_null(ctx,
97 ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)), 100 ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)),
98 NGX_CONF_ERROR); 101 NGX_CONF_ERROR);
99 102
100 *(ngx_http_conf_ctx_t **) conf = ctx; 103 *(ngx_http_conf_ctx_t **) conf = ctx;
109 112
110 ngx_modules[m]->ctx_index = ngx_http_max_module++; 113 ngx_modules[m]->ctx_index = ngx_http_max_module++;
111 } 114 }
112 115
113 /* the main http main_conf, it's the same in the all http contexts */ 116 /* the main http main_conf, it's the same in the all http contexts */
117
114 ngx_test_null(ctx->main_conf, 118 ngx_test_null(ctx->main_conf,
115 ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module), 119 ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module),
116 NGX_CONF_ERROR); 120 NGX_CONF_ERROR);
117 121
118 /* the http null srv_conf, it's used to merge the server{}s' srv_conf's */ 122 /* the http null srv_conf, it's used to merge the server{}s' srv_conf's */
374 ngx_memcpy(inaddr, &in_addr[a], 378 ngx_memcpy(inaddr, &in_addr[a],
375 sizeof(ngx_http_in_addr_t)); 379 sizeof(ngx_http_in_addr_t));
376 380
377 in_addr[a].addr = lscf[l].addr; 381 in_addr[a].addr = lscf[l].addr;
378 in_addr[a].names.elts = NULL; 382 in_addr[a].names.elts = NULL;
383 in_addr[a].hash = NULL;
384 in_addr[a].wildcards.elts = NULL;
379 in_addr[a].default_server = lscf[l].default_server; 385 in_addr[a].default_server = lscf[l].default_server;
380 in_addr[a].core_srv_conf = cscfp[s]; 386 in_addr[a].core_srv_conf = cscfp[s];
381 387
382 if (ngx_http_add_names(cf, &in_addr[a], cscfp[s]) 388 if (ngx_http_add_names(cf, &in_addr[a], cscfp[s])
383 == NGX_ERROR) 389 == NGX_ERROR)
462 virtual_names = 1; 468 virtual_names = 1;
463 break; 469 break;
464 } 470 }
465 } 471 }
466 472
473 if (!virtual_names) {
474 name = in_addr[a].wildcards.elts;
475 for (n = 0; n < in_addr[a].wildcards.nelts; n++) {
476 if (in_addr[a].core_srv_conf != name[n].core_srv_conf
477 || name[n].core_srv_conf->restrict_host_names
478 != NGX_HTTP_RESTRICT_HOST_OFF)
479 {
480 virtual_names = 1;
481 break;
482 }
483 }
484 }
485
467 /* 486 /*
468 * if all name-based servers have the same configuration 487 * if all name-based servers have the same configuration
469 * as the default server, and no servers restrict the host names 488 * as the default server, and no servers restrict the host names
470 * then we do not need to check them at run-time at all 489 * then we do not need to check them at run-time at all
471 */ 490 */
472 491
473 if (!virtual_names) { 492 if (!virtual_names) {
474 in_addr[a].names.nelts = 0; 493 in_addr[a].names.nelts = 0;
494 continue;
495 }
496
497
498 ngx_qsort(in_addr[a].names.elts, in_addr[a].names.nelts,
499 sizeof(ngx_http_server_name_t), ngx_cmp_server_names);
500
501
502 /* create a hash for many names */
503
504 if (in_addr[a].names.nelts > cmcf->server_names_hash_threshold) {
505 in_addr[a].hash = ngx_palloc(cf->pool,
506 cmcf->server_names_hash
507 * sizeof(ngx_array_t));
508 if (in_addr[a].hash == NULL) {
509 return NGX_CONF_ERROR;
510 }
511
512 for (n = 0; n < cmcf->server_names_hash; n++) {
513 if (ngx_array_init(&in_addr[a].hash[n], cf->pool, 5,
514 sizeof(ngx_http_server_name_t)) == NGX_ERROR)
515 {
516 return NGX_CONF_ERROR;
517 }
518 }
519
520 name = in_addr[a].names.elts;
521 for (s = 0; s < in_addr[a].names.nelts; s++) {
522 ngx_http_server_names_hash_key(key, name[s].name.data,
523 name[s].name.len,
524 cmcf->server_names_hash);
525
526 if (!(s_name = ngx_array_push(&in_addr[a].hash[key]))) {
527 return NGX_CONF_ERROR;
528 }
529
530 *s_name = name[s];
531 }
475 } 532 }
476 } 533 }
477 534
478 /* 535 /*
479 * if there's the binding to "*:port" then we need to bind() 536 * if there is the binding to the "*:port" then we need to bind()
480 * to "*:port" only and ignore the other bindings 537 * to the "*:port" only and ignore the other bindings
481 */ 538 */
482 539
483 if (in_addr[a - 1].addr == INADDR_ANY) { 540 if (in_addr[a - 1].addr == INADDR_ANY) {
484 a--; 541 a--;
485 542
602 659
603 return NGX_CONF_OK; 660 return NGX_CONF_OK;
604 } 661 }
605 662
606 663
664 static int ngx_cmp_server_names(const void *one, const void *two)
665 {
666 ngx_http_server_name_t *first = (ngx_http_server_name_t *) one;
667 ngx_http_server_name_t *second = (ngx_http_server_name_t *) two;
668
669 return ngx_strcmp(first->name.data, second->name.data);
670 }
671
672
607 /* 673 /*
608 * add the server address, the server names and the server core module 674 * add the server address, the server names and the server core module
609 * configurations to the port (in_port) 675 * configurations to the port (in_port)
610 */ 676 */
611 677
628 return NGX_ERROR; 694 return NGX_ERROR;
629 } 695 }
630 696
631 in_addr->addr = lscf->addr; 697 in_addr->addr = lscf->addr;
632 in_addr->names.elts = NULL; 698 in_addr->names.elts = NULL;
699 in_addr->hash = NULL;
700 in_addr->wildcards.elts = NULL;
633 in_addr->default_server = lscf->default_server; 701 in_addr->default_server = lscf->default_server;
634 in_addr->core_srv_conf = cscf; 702 in_addr->core_srv_conf = cscf;
635 703
636 #if (NGX_DEBUG) 704 #if (NGX_DEBUG)
637 { 705 {
653 721
654 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf, 722 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
655 ngx_http_in_addr_t *in_addr, 723 ngx_http_in_addr_t *in_addr,
656 ngx_http_core_srv_conf_t *cscf) 724 ngx_http_core_srv_conf_t *cscf)
657 { 725 {
658 ngx_uint_t i; 726 ngx_uint_t i, n;
727 ngx_array_t *array;
659 ngx_http_server_name_t *server_names, *name; 728 ngx_http_server_name_t *server_names, *name;
660 729
661 if (in_addr->names.elts == NULL) { 730 if (in_addr->names.elts == NULL) {
662 if (ngx_array_init(&in_addr->names, cf->pool, 10, 731 if (ngx_array_init(&in_addr->names, cf->pool, 10,
663 sizeof(ngx_http_server_name_t)) == NGX_ERROR) 732 sizeof(ngx_http_server_name_t)) == NGX_ERROR)
664 { 733 {
665 return NGX_ERROR; 734 return NGX_ERROR;
666 } 735 }
667 } 736 }
668 737
738 if (in_addr->wildcards.elts == NULL) {
739 if (ngx_array_init(&in_addr->wildcards, cf->pool, 10,
740 sizeof(ngx_http_server_name_t)) == NGX_ERROR)
741 {
742 return NGX_ERROR;
743 }
744 }
745
669 server_names = cscf->server_names.elts; 746 server_names = cscf->server_names.elts;
670 for (i = 0; i < cscf->server_names.nelts; i++) { 747 for (i = 0; i < cscf->server_names.nelts; i++) {
671 748
749 for (n = 0; n < server_names[i].name.len; n++) {
750 server_names[i].name.data[n] =
751 ngx_tolower(server_names[i].name.data[n]);
752 }
753
672 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0, 754 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
673 "name: %V", &server_names[i].name); 755 "name: %V", &server_names[i].name);
674 756
675 /* TODO: duplicate names can be checked here */ 757 /* TODO: duplicate names can be checked here */
676 758
677 if (!(name = ngx_array_push(&in_addr->names))) { 759
760 if (server_names[i].wildcard) {
761 array = &in_addr->wildcards;
762
763 } else {
764 array = &in_addr->names;
765 }
766
767 if (!(name = ngx_array_push(array))) {
678 return NGX_ERROR; 768 return NGX_ERROR;
679 } 769 }
680 770
681 *name = server_names[i]; 771 *name = server_names[i];
682 } 772 }