Mercurial > hg > nginx
comparison src/http/ngx_http_core_module.c @ 97:70d2345a903f
nginx-0.0.1-2003-05-29-17:02:09 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 29 May 2003 13:02:09 +0000 |
parents | a23d010f356d |
children | c9b243802a17 |
comparison
equal
deleted
inserted
replaced
96:a23d010f356d | 97:70d2345a903f |
---|---|
26 static char *ngx_http_core_merge_loc_conf(ngx_pool_t *pool, | 26 static char *ngx_http_core_merge_loc_conf(ngx_pool_t *pool, |
27 void *parent, void *child); | 27 void *parent, void *child); |
28 | 28 |
29 static int ngx_http_core_init(ngx_pool_t *pool); | 29 static int ngx_http_core_init(ngx_pool_t *pool); |
30 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy); | 30 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy); |
31 static int ngx_cmp_locations(const void *first, const void *second); | |
31 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, | 32 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, |
32 void *dummy); | 33 void *dummy); |
33 static char *ngx_types_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | 34 static char *ngx_types_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
34 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | 35 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
36 static char *ngx_set_server_name(ngx_conf_t *cf, ngx_command_t *cmd, | |
37 void *conf); | |
35 | 38 |
36 | 39 |
37 static ngx_command_t ngx_http_core_commands[] = { | 40 static ngx_command_t ngx_http_core_commands[] = { |
38 | 41 |
39 {ngx_string("server"), | 42 {ngx_string("server"), |
40 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, | 43 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, |
41 ngx_server_block, | 44 ngx_server_block, |
42 0, | 45 0, |
43 0, | 46 0, |
44 NULL,}, | 47 NULL}, |
45 | 48 |
46 {ngx_string("connection_pool_size"), | 49 {ngx_string("connection_pool_size"), |
47 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, | 50 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, |
48 ngx_conf_set_size_slot, | 51 ngx_conf_set_size_slot, |
49 NGX_HTTP_MAIN_CONF_OFFSET, | 52 NGX_HTTP_MAIN_CONF_OFFSET, |
93 NULL}, | 96 NULL}, |
94 | 97 |
95 {ngx_string("listen"), | 98 {ngx_string("listen"), |
96 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1, | 99 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1, |
97 ngx_set_listen, | 100 ngx_set_listen, |
101 NGX_HTTP_SRV_CONF_OFFSET, | |
102 0, | |
103 NULL}, | |
104 | |
105 {ngx_string("server_name"), | |
106 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_ANY, | |
107 ngx_set_server_name, | |
98 NGX_HTTP_SRV_CONF_OFFSET, | 108 NGX_HTTP_SRV_CONF_OFFSET, |
99 0, | 109 0, |
100 NULL}, | 110 NULL}, |
101 | 111 |
102 {ngx_string("types"), | 112 {ngx_string("types"), |
213 #endif | 223 #endif |
214 if (r->uri.len < clcfp[i]->name.len) { | 224 if (r->uri.len < clcfp[i]->name.len) { |
215 continue; | 225 continue; |
216 } | 226 } |
217 | 227 |
218 rc = ngx_rstrncmp(r->uri.data, clcfp[i]->name.data, | 228 rc = ngx_strncmp(r->uri.data, clcfp[i]->name.data, |
219 clcfp[i]->name.len); | 229 clcfp[i]->name.len); |
230 | |
231 ngx_log_debug(r->connection->log, "rc: %d" _ rc); | |
220 | 232 |
221 if (rc < 0) { | 233 if (rc < 0) { |
222 break; | 234 break; |
223 } | 235 } |
224 | 236 |
233 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; | 245 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; |
234 } | 246 } |
235 | 247 |
236 /* run translation phase */ | 248 /* run translation phase */ |
237 | 249 |
238 h = (ngx_http_handler_pt *) ngx_http_translate_handlers.elts; | 250 h = ngx_http_translate_handlers.elts; |
239 for (i = ngx_http_translate_handlers.nelts; i > 0; /* void */) { | 251 for (i = ngx_http_translate_handlers.nelts - 1; i >= 0; i--) { |
240 rc = h[--i](r); | 252 |
253 rc = h[i](r); | |
241 | 254 |
242 if (rc == NGX_DECLINED) { | 255 if (rc == NGX_DECLINED) { |
243 continue; | 256 continue; |
244 } | 257 } |
245 | 258 |
455 ngx_http_close_request(r, 0); | 468 ngx_http_close_request(r, 0); |
456 return NGX_OK; | 469 return NGX_OK; |
457 } | 470 } |
458 | 471 |
459 | 472 |
460 int ngx_http_internal_redirect(ngx_http_request_t *r, ngx_str_t uri) | 473 int ngx_http_internal_redirect(ngx_http_request_t *r, |
461 { | 474 ngx_str_t *uri, ngx_str_t *args) |
462 ngx_log_debug(r->connection->log, "internal redirect: '%s'" _ uri.data); | 475 { |
463 | 476 int i; |
464 r->uri.len = uri.len; | 477 |
465 r->uri.data = uri.data; | 478 ngx_log_debug(r->connection->log, "internal redirect: '%s'" _ uri->data); |
466 | 479 |
467 /* BROKEN, NEEDED ? */ | 480 r->uri.len = uri->len; |
468 /* r->exten */ | 481 r->uri.data = uri->data; |
469 r->uri_start = uri.data; | 482 |
470 r->uri_end = uri.data + uri.len; | 483 if (args) { |
471 /**/ | 484 r->args.len = args->len; |
485 r->args.data = args->data; | |
486 } | |
487 | |
488 r->exten.len = 0; | |
489 r->exten.data = NULL; | |
490 | |
491 for (i = uri->len - 1; i > 1; i--) { | |
492 if (uri->data[i] == '.' && uri->data[i - 1] != '/') { | |
493 r->exten.len = uri->len - i - 1; | |
494 | |
495 if (r->exten.len > 0) { | |
496 ngx_test_null(r->exten.data, | |
497 ngx_palloc(r->pool, r->exten.len + 1), | |
498 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
499 | |
500 ngx_cpystrn(r->exten.data, &uri->data[i + 1], r->exten.len + 1); | |
501 } | |
502 | |
503 break; | |
504 | |
505 } else if (uri->data[i] == '/') { | |
506 break; | |
507 } | |
508 } | |
472 | 509 |
473 ngx_http_handler(r); | 510 ngx_http_handler(r); |
474 return 0; | 511 |
512 return NGX_OK; | |
475 } | 513 } |
476 | 514 |
477 | 515 |
478 static int ngx_http_core_init(ngx_pool_t *pool) | 516 static int ngx_http_core_init(ngx_pool_t *pool) |
479 { | 517 { |
552 cf->ctx = ctx; | 590 cf->ctx = ctx; |
553 cf->cmd_type = NGX_HTTP_SRV_CONF; | 591 cf->cmd_type = NGX_HTTP_SRV_CONF; |
554 rv = ngx_conf_parse(cf, NULL); | 592 rv = ngx_conf_parse(cf, NULL); |
555 *cf = pcf; | 593 *cf = pcf; |
556 | 594 |
595 if (rv != NGX_CONF_OK) { | |
596 return rv; | |
597 } | |
598 | |
599 ngx_qsort(cscf->locations.elts, cscf->locations.nelts, | |
600 sizeof(void *), ngx_cmp_locations); | |
601 | |
557 return rv; | 602 return rv; |
603 } | |
604 | |
605 | |
606 static int ngx_cmp_locations(const void *first, const void *second) | |
607 { | |
608 ngx_http_core_loc_conf_t *one = *(ngx_http_core_loc_conf_t **) first; | |
609 ngx_http_core_loc_conf_t *two = *(ngx_http_core_loc_conf_t **) second; | |
610 | |
611 return ngx_strcmp(one->name.data, two->name.data); | |
558 } | 612 } |
559 | 613 |
560 | 614 |
561 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) | 615 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) |
562 { | 616 { |
722 void *parent, void *child) | 776 void *parent, void *child) |
723 { | 777 { |
724 ngx_http_core_srv_conf_t *prev = (ngx_http_core_srv_conf_t *) parent; | 778 ngx_http_core_srv_conf_t *prev = (ngx_http_core_srv_conf_t *) parent; |
725 ngx_http_core_srv_conf_t *conf = (ngx_http_core_srv_conf_t *) child; | 779 ngx_http_core_srv_conf_t *conf = (ngx_http_core_srv_conf_t *) child; |
726 | 780 |
781 ngx_err_t err; | |
727 ngx_http_listen_t *l; | 782 ngx_http_listen_t *l; |
728 ngx_http_server_name_t *n; | 783 ngx_http_server_name_t *n; |
729 | 784 |
730 /* TODO: it does not merge, it init only */ | 785 /* TODO: it does not merge, it init only */ |
731 | 786 |
738 | 793 |
739 if (conf->server_names.nelts == 0) { | 794 if (conf->server_names.nelts == 0) { |
740 ngx_test_null(n, ngx_push_array(&conf->server_names), NGX_CONF_ERROR); | 795 ngx_test_null(n, ngx_push_array(&conf->server_names), NGX_CONF_ERROR); |
741 ngx_test_null(n->name.data, ngx_palloc(pool, NGX_MAXHOSTNAMELEN), | 796 ngx_test_null(n->name.data, ngx_palloc(pool, NGX_MAXHOSTNAMELEN), |
742 NGX_CONF_ERROR); | 797 NGX_CONF_ERROR); |
798 | |
743 if (gethostname(n->name.data, NGX_MAXHOSTNAMELEN) == -1) { | 799 if (gethostname(n->name.data, NGX_MAXHOSTNAMELEN) == -1) { |
744 /* TODO: need ngx_errno here */ | 800 err = ngx_errno; |
745 return "gethostname() failed"; | 801 ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1, |
746 } | 802 "gethostname() failed (%d: %s)", |
803 err, ngx_strerror(err)); | |
804 return ngx_conf_errstr; | |
805 } | |
806 | |
747 n->name.len = ngx_strlen(n->name.data); | 807 n->name.len = ngx_strlen(n->name.data); |
748 n->core_srv_conf = conf; | 808 n->core_srv_conf = conf; |
749 } | 809 } |
750 | 810 |
751 ngx_conf_merge_size_value(conf->request_pool_size, | 811 ngx_conf_merge_size_value(conf->request_pool_size, |
866 struct hostent *h; | 926 struct hostent *h; |
867 ngx_str_t *args; | 927 ngx_str_t *args; |
868 ngx_http_listen_t *ls; | 928 ngx_http_listen_t *ls; |
869 | 929 |
870 /* TODO: check duplicate 'listen' directives, | 930 /* TODO: check duplicate 'listen' directives, |
871 add resolved name to server names */ | 931 add resolved name to server names ??? */ |
872 | 932 |
873 ngx_test_null(ls, ngx_push_array(&scf->listen), NGX_CONF_ERROR); | 933 ngx_test_null(ls, ngx_push_array(&scf->listen), NGX_CONF_ERROR); |
874 | 934 |
875 /* AF_INET only */ | 935 /* AF_INET only */ |
876 | 936 |
877 ls->family = AF_INET; | 937 ls->family = AF_INET; |
878 ls->flags = 0; | 938 ls->flags = 0; |
879 ls->file_name = cf->conf_file->file.name; | 939 ls->file_name = cf->conf_file->file.name; |
880 ls->line = cf->conf_file->line; | 940 ls->line = cf->conf_file->line; |
881 | 941 |
882 args = (ngx_str_t *) cf->args->elts; | 942 args = cf->args->elts; |
883 addr = args[1].data; | 943 addr = args[1].data; |
884 | 944 |
885 for (p = 0; p < args[1].len; p++) { | 945 for (p = 0; p < args[1].len; p++) { |
886 if (addr[p] == ':') { | 946 if (addr[p] == ':') { |
887 addr[p++] = '\0'; | 947 addr[p++] = '\0'; |
888 | |
889 ls->addr = inet_addr(addr); | |
890 if (ls->addr == INADDR_NONE) { | |
891 h = gethostbyname(addr); | |
892 | |
893 if (h == NULL || h->h_addr_list[0] == NULL) { | |
894 return "can not resolve host name"; | |
895 } | |
896 | |
897 ls->addr = *(u_int32_t *)(h->h_addr_list[0]); | |
898 } | |
899 | |
900 break; | 948 break; |
901 } | 949 } |
902 } | 950 } |
903 | 951 |
904 if (p == args[1].len) { | 952 if (p == args[1].len) { |
953 /* no ":" in the "listen" */ | |
954 p = 0; | |
955 } | |
956 | |
957 ls->port = ngx_atoi(&addr[p], args[1].len - p); | |
958 if (ls->port == NGX_ERROR && p == 0) { | |
959 | |
960 /* "listen host" */ | |
961 ls->port = 80; | |
962 | |
963 } else if ((ls->port == NGX_ERROR && p != 0) /* "listen host:NONNUMBER" */ | |
964 || (ls->port < 1 || ls->port > 65536)) { /* "listen 99999" */ | |
965 | |
966 ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1, | |
967 "invalid port \"%s\", " | |
968 "it must be a number between 1 and 65535", | |
969 &addr[p]); | |
970 return ngx_conf_errstr; | |
971 | |
972 } else if (p == 0) { | |
905 ls->addr = INADDR_ANY; | 973 ls->addr = INADDR_ANY; |
906 p = 0; | 974 return NGX_CONF_OK; |
907 } | 975 } |
908 | 976 |
909 ls->port = ngx_atoi(&addr[p], args[1].len - p); | 977 ls->addr = inet_addr(addr); |
910 if (ls->port < 1 || ls->port > 65536) { | 978 if (ls->addr == INADDR_NONE) { |
911 return "port must be between 1 and 65535"; | 979 h = gethostbyname(addr); |
980 | |
981 if (h == NULL || h->h_addr_list[0] == NULL) { | |
982 ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1, | |
983 "can not resolve host \"%s\"", addr); | |
984 return ngx_conf_errstr; | |
985 } | |
986 | |
987 ls->addr = *(u_int32_t *)(h->h_addr_list[0]); | |
912 } | 988 } |
913 | 989 |
914 return NGX_CONF_OK; | 990 return NGX_CONF_OK; |
915 } | 991 } |
992 | |
993 | |
994 static char *ngx_set_server_name(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
995 { | |
996 ngx_http_core_srv_conf_t *scf = (ngx_http_core_srv_conf_t *) conf; | |
997 | |
998 ngx_str_t *args; | |
999 ngx_http_server_name_t *sn; | |
1000 | |
1001 /* TODO: several names */ | |
1002 /* TODO: warn about duplicate 'server_name' directives */ | |
1003 | |
1004 ngx_test_null(sn, ngx_push_array(&scf->server_names), NGX_CONF_ERROR); | |
1005 | |
1006 args = cf->args->elts; | |
1007 | |
1008 sn->name.len = args[1].len; | |
1009 sn->name.data = args[1].data; | |
1010 sn->core_srv_conf = scf; | |
1011 | |
1012 return NGX_CONF_OK; | |
1013 } |