Mercurial > hg > nginx
comparison src/http/ngx_http_core_module.c @ 54:27b628ef907e
nginx-0.0.1-2003-01-28-18:56:37 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 28 Jan 2003 15:56:37 +0000 |
parents | d81326c3b21b |
children | cad6c2f43283 |
comparison
equal
deleted
inserted
replaced
53:d1e42f1b8fd4 | 54:27b628ef907e |
---|---|
1 | 1 |
2 #include <ngx_config.h> | 2 #include <ngx_config.h> |
3 | |
4 #include <ngx_listen.h> | |
3 | 5 |
4 #include <ngx_core.h> | 6 #include <ngx_core.h> |
5 #include <ngx_conf_file.h> | 7 #include <ngx_conf_file.h> |
6 | 8 |
7 #include <ngx_http.h> | 9 #include <ngx_http.h> |
18 int ngx_http_static_handler(ngx_http_request_t *r); | 20 int ngx_http_static_handler(ngx_http_request_t *r); |
19 int ngx_http_proxy_handler(ngx_http_request_t *r); | 21 int ngx_http_proxy_handler(ngx_http_request_t *r); |
20 /**/ | 22 /**/ |
21 | 23 |
22 static int ngx_http_core_index_handler(ngx_http_request_t *r); | 24 static int ngx_http_core_index_handler(ngx_http_request_t *r); |
23 | 25 static ngx_http_conf_ctx_t *ngx_http_find_server_conf(ngx_http_request_t *r, |
26 void *addr); | |
24 | 27 |
25 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy); | 28 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy); |
26 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, | 29 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, |
27 char *dummy); | 30 char *dummy); |
28 static void *ngx_http_core_create_srv_conf(ngx_pool_t *pool); | 31 static void *ngx_http_core_create_srv_conf(ngx_pool_t *pool); |
29 static char *ngx_http_core_init_srv_conf(ngx_pool_t *pool, void *conf); | 32 static char *ngx_http_core_init_srv_conf(ngx_pool_t *pool, void *conf); |
30 static void *ngx_http_core_create_loc_conf(ngx_pool_t *pool); | 33 static void *ngx_http_core_create_loc_conf(ngx_pool_t *pool); |
34 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, char *conf); | |
31 | 35 |
32 | 36 |
33 static ngx_command_t ngx_http_core_commands[] = { | 37 static ngx_command_t ngx_http_core_commands[] = { |
34 | 38 |
35 {ngx_string("server"), | 39 {ngx_string("server"), |
42 NGX_HTTP_SRV_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1, | 46 NGX_HTTP_SRV_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1, |
43 ngx_location_block, | 47 ngx_location_block, |
44 0, | 48 0, |
45 0}, | 49 0}, |
46 | 50 |
51 {ngx_string("listen"), | |
52 NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1, | |
53 ngx_set_listen, | |
54 0, | |
55 0}, | |
56 | |
47 {ngx_string("root"), | 57 {ngx_string("root"), |
48 NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1, | 58 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
49 ngx_conf_set_str_slot, | 59 ngx_conf_set_str_slot, |
50 NGX_HTTP_LOC_CONF_OFFSET, | 60 NGX_HTTP_LOC_CONF_OFFSET, |
51 offsetof(ngx_http_core_loc_conf_t, doc_root)}, | 61 offsetof(ngx_http_core_loc_conf_t, doc_root)}, |
52 | 62 |
53 {ngx_string("send_timeout"), | 63 {ngx_string("send_timeout"), |
54 NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1, | 64 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
55 ngx_conf_set_time_slot, | 65 ngx_conf_set_time_slot, |
56 NGX_HTTP_LOC_CONF_OFFSET, | 66 NGX_HTTP_LOC_CONF_OFFSET, |
57 offsetof(ngx_http_core_loc_conf_t, send_timeout)}, | 67 offsetof(ngx_http_core_loc_conf_t, send_timeout)}, |
58 | 68 |
59 {ngx_string(""), 0, NULL, 0, 0} | 69 {ngx_string(""), 0, NULL, 0, 0} |
94 | 104 |
95 r->connection->unexpected_eof = 0; | 105 r->connection->unexpected_eof = 0; |
96 r->lingering_close = 1; | 106 r->lingering_close = 1; |
97 r->keepalive = 1; | 107 r->keepalive = 1; |
98 | 108 |
99 ctx = (ngx_http_conf_ctx_t *) r->connection->ctx; | 109 ngx_log_debug(r->connection->log, "servers: %0x" _ r->connection->servers); |
110 | |
111 if (r->connection->servers == NULL) { | |
112 ctx = (ngx_http_conf_ctx_t *) r->connection->ctx; | |
113 | |
114 } else { | |
115 ctx = ngx_http_find_server_conf(r, r->connection->servers); | |
116 } | |
117 | |
100 r->srv_conf = ctx->srv_conf; | 118 r->srv_conf = ctx->srv_conf; |
101 r->loc_conf = ctx->loc_conf; | 119 r->loc_conf = ctx->loc_conf; |
102 | 120 |
121 ngx_log_debug(r->connection->log, "cxt: %08x" _ ctx); | |
103 ngx_log_debug(r->connection->log, "srv_conf: %0x" _ r->srv_conf); | 122 ngx_log_debug(r->connection->log, "srv_conf: %0x" _ r->srv_conf); |
104 ngx_log_debug(r->connection->log, "loc_conf: %0x" _ r->loc_conf); | 123 ngx_log_debug(r->connection->log, "loc_conf: %0x" _ r->loc_conf); |
105 ngx_log_debug(r->connection->log, "servers: %0x" _ r->connection->servers); | |
106 | 124 |
107 | 125 |
108 #if 1 | 126 #if 1 |
109 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; | 127 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; |
110 #endif | 128 #endif |
129 | |
111 | 130 |
112 /* run translation phase */ | 131 /* run translation phase */ |
113 for (i = 0; ngx_modules[i]; i++) { | 132 for (i = 0; ngx_modules[i]; i++) { |
114 if (ngx_modules[i]->type != NGX_HTTP_MODULE_TYPE) { | 133 if (ngx_modules[i]->type != NGX_HTTP_MODULE_TYPE) { |
115 continue; | 134 continue; |
178 return NGX_OK; | 197 return NGX_OK; |
179 } | 198 } |
180 | 199 |
181 loc_conf = (ngx_http_core_loc_conf_t *) | 200 loc_conf = (ngx_http_core_loc_conf_t *) |
182 ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx); | 201 ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx); |
202 | |
203 ngx_log_debug(r->connection->log, "doc_root: %08x" _ &loc_conf->doc_root); | |
183 | 204 |
184 s_name = (ngx_http_server_name_t *) scf->server_names.elts; | 205 s_name = (ngx_http_server_name_t *) scf->server_names.elts; |
185 | 206 |
186 /* "+ 7" is "http://" */ | 207 /* "+ 7" is "http://" */ |
187 if (loc_conf->doc_root.len > s_name[0].name.len + 7) { | 208 if (loc_conf->doc_root.len > s_name[0].name.len + 7) { |
431 | 452 |
432 return ngx_http_handler(r); | 453 return ngx_http_handler(r); |
433 } | 454 } |
434 | 455 |
435 | 456 |
436 #if 0 | 457 static ngx_http_conf_ctx_t *ngx_http_find_server_conf(ngx_http_request_t *r, |
437 void *ngx_http_find_server_conf(ngx_http_request_t *r) | 458 void *addr) |
438 { | 459 { |
439 int i; | 460 int i, len; |
440 ngx_http_listen_t *fs, *ls; | 461 ngx_http_in_addr_t *in_addr; |
441 ngx_http_server_name_t *n; | 462 ngx_http_server_name_t *name; |
442 | 463 |
443 fs = NULL; | 464 /* AF_INET only */ |
444 ls = (ngx_http_listen_t *) http->ports.elts; | 465 |
445 | 466 /* BUG: need cycle thru addr[]->elts */ |
446 for (i = 0; i < http->ports.nelts; i++) { | 467 |
447 if (s->family != ls[i].family || s->port != ls[i].port) { | 468 in_addr = (ngx_http_in_addr_t *) addr; |
469 | |
470 if (r->headers_in.host == NULL) { | |
471 return in_addr->core_srv_conf->ctx; | |
472 } | |
473 | |
474 len = r->headers_in.host_name.len; | |
475 name = (ngx_http_server_name_t *) in_addr->names.elts; | |
476 for (i = 0; i < in_addr->names.nelts; i++) { | |
477 if (len != name->name.len) { | |
448 continue; | 478 continue; |
449 } | 479 } |
450 | 480 |
451 if (s->family == AF_INET) { | 481 if (ngx_strncasecmp(r->headers_in.host_name.data, |
452 | 482 name->name.data, len) == 0) { |
453 if (ls[i].addr == INADDR_ANY || ls[i].addr == s->addr) { | 483 return name->core_srv_conf->ctx; |
454 fs = &ls[i]; | 484 } |
455 break; | 485 } |
456 } | 486 |
457 | 487 return in_addr->core_srv_conf->ctx; |
458 } else { | 488 } |
459 /* STUB: AF_INET only */ | |
460 } | |
461 } | |
462 | |
463 if (fs == NULL) { | |
464 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
465 "unknown local socket %s:%d", | |
466 s->addr_text.data, s->port); | |
467 return NULL; | |
468 } | |
469 | |
470 if (r->headers_in.host && fs->server_names.nelts) { | |
471 | |
472 n = (ngx_http_server_name_t *) fs->server_names.elts; | |
473 for (i = 0; i < fs->server_names.nelts; i++) { | |
474 if (r->headers_in.host->value.len != n[i].name.len) { | |
475 continue; | |
476 } | |
477 | |
478 if (ngx_strcmp(r->headers_in.host->value.data, n[i].name.data) == 0) { | |
479 return n[i].srv_conf; | |
480 } | |
481 } | |
482 } | |
483 | |
484 return fs->srv_conf; | |
485 } | |
486 #endif | |
487 | 489 |
488 | 490 |
489 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy) | 491 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy) |
490 { | 492 { |
491 int i, j; | 493 int i, j; |
518 | 520 |
519 if (module->create_srv_conf) { | 521 if (module->create_srv_conf) { |
520 ngx_test_null(ctx->srv_conf[module->index], | 522 ngx_test_null(ctx->srv_conf[module->index], |
521 module->create_srv_conf(cf->pool), | 523 module->create_srv_conf(cf->pool), |
522 NGX_CONF_ERROR); | 524 NGX_CONF_ERROR); |
523 ngx_log_debug(cf->log, "srv_conf: %d:%0x" _ | |
524 module->index _ ctx->loc_conf[module->index]); | |
525 } | 525 } |
526 | 526 |
527 if (module->create_loc_conf) { | 527 if (module->create_loc_conf) { |
528 ngx_test_null(ctx->loc_conf[module->index], | 528 ngx_test_null(ctx->loc_conf[module->index], |
529 module->create_loc_conf(cf->pool), | 529 module->create_loc_conf(cf->pool), |
530 NGX_CONF_ERROR); | 530 NGX_CONF_ERROR); |
531 ngx_log_debug(cf->log, "srv loc_conf: %d:%0x" _ | |
532 module->index _ ctx->loc_conf[module->index]); | |
533 } | 531 } |
534 } | 532 } |
535 | 533 |
536 prev = cf->ctx; | 534 prev = cf->ctx; |
537 cf->ctx = ctx; | 535 cf->ctx = ctx; |
569 == NGX_CONF_ERROR) { | 567 == NGX_CONF_ERROR) { |
570 return NGX_CONF_ERROR; | 568 return NGX_CONF_ERROR; |
571 } | 569 } |
572 | 570 |
573 for (j = 0; j < scf->locations.nelts; j++) { | 571 for (j = 0; j < scf->locations.nelts; j++) { |
574 ngx_log_debug(cf->log, "%d:%0x" _ j _ lcf[j]); | |
575 ngx_log_debug(cf->log, "%d:'%s'" _ lcf[j]->name.len _ lcf[j]->name.data); | |
576 if (module->merge_loc_conf(cf->pool, | 572 if (module->merge_loc_conf(cf->pool, |
577 ctx->loc_conf[module->index], | 573 ctx->loc_conf[module->index], |
578 lcf[j]->loc_conf[module->index]) | 574 lcf[j]->loc_conf[module->index]) |
579 == NGX_CONF_ERROR) { | 575 == NGX_CONF_ERROR) { |
580 return NGX_CONF_ERROR; | 576 return NGX_CONF_ERROR; |
589 | 585 |
590 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy) | 586 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy) |
591 { | 587 { |
592 int i; | 588 int i; |
593 char *rv; | 589 char *rv; |
594 void **loc; | |
595 ngx_str_t *location; | 590 ngx_str_t *location; |
596 ngx_http_module_t *module; | 591 ngx_http_module_t *module; |
597 ngx_http_conf_ctx_t *ctx, *prev; | 592 ngx_http_conf_ctx_t *ctx, *prev; |
598 ngx_http_core_srv_conf_t *scf; | 593 ngx_http_core_srv_conf_t *scf; |
599 ngx_http_core_loc_conf_t *lcf; | 594 ngx_http_core_loc_conf_t *lcf, **loc; |
600 | 595 |
601 ngx_test_null(ctx, | 596 ngx_test_null(ctx, |
602 ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)), | 597 ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)), |
603 NGX_CONF_ERROR); | 598 NGX_CONF_ERROR); |
604 | 599 |
618 | 613 |
619 if (module->create_loc_conf) { | 614 if (module->create_loc_conf) { |
620 ngx_test_null(ctx->loc_conf[module->index], | 615 ngx_test_null(ctx->loc_conf[module->index], |
621 module->create_loc_conf(cf->pool), | 616 module->create_loc_conf(cf->pool), |
622 NGX_CONF_ERROR); | 617 NGX_CONF_ERROR); |
623 ngx_log_debug(cf->log, "loc_conf: %d:%0x" _ | |
624 module->index _ ctx->loc_conf[module->index]); | |
625 } | 618 } |
626 } | 619 } |
627 | 620 |
628 lcf = (ngx_http_core_loc_conf_t *) | 621 lcf = (ngx_http_core_loc_conf_t *) |
629 ctx->loc_conf[ngx_http_core_module_ctx.index]; | 622 ctx->loc_conf[ngx_http_core_module.index]; |
630 location = (ngx_str_t *) cf->args->elts; | 623 location = (ngx_str_t *) cf->args->elts; |
631 lcf->name.len = location[1].len; | 624 lcf->name.len = location[1].len; |
632 lcf->name.data = location[1].data; | 625 lcf->name.data = location[1].data; |
633 lcf->loc_conf = ctx->loc_conf; | 626 lcf->loc_conf = ctx->loc_conf; |
634 | 627 |
635 scf = (ngx_http_core_srv_conf_t *) | 628 scf = (ngx_http_core_srv_conf_t *) |
636 ctx->srv_conf[ngx_http_core_module_ctx.index]; | 629 ctx->srv_conf[ngx_http_core_module.index]; |
637 ngx_test_null(loc, ngx_push_array(&scf->locations), NGX_CONF_ERROR); | 630 ngx_test_null(loc, ngx_push_array(&scf->locations), NGX_CONF_ERROR); |
638 *loc = lcf; | 631 *loc = lcf; |
639 | |
640 ngx_log_debug(cf->log, "%0x:%s" _ lcf _ lcf->name.data); | |
641 | 632 |
642 cf->ctx = ctx; | 633 cf->ctx = ctx; |
643 rv = ngx_conf_parse(cf, NULL); | 634 rv = ngx_conf_parse(cf, NULL); |
644 cf->ctx = prev; | 635 cf->ctx = prev; |
645 | 636 |
789 lcf->send_timeout = NGX_CONF_UNSET; | 780 lcf->send_timeout = NGX_CONF_UNSET; |
790 */ | 781 */ |
791 | 782 |
792 return lcf; | 783 return lcf; |
793 } | 784 } |
785 | |
786 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) | |
787 { | |
788 ngx_str_t *args; | |
789 ngx_http_listen_t *ls; | |
790 ngx_http_core_srv_conf_t *scf = (ngx_http_core_srv_conf_t *) conf; | |
791 | |
792 ngx_test_null(ls, ngx_push_array(&scf->listen), NGX_CONF_ERROR); | |
793 | |
794 /* AF_INET only */ | |
795 | |
796 ls->family = AF_INET; | |
797 ls->addr = INADDR_ANY; | |
798 ls->flags = 0; | |
799 ls->conf_file = cf->conf_file; | |
800 ls->line = cf->conf_file->line; | |
801 | |
802 args = (ngx_str_t *) cf->args->elts; | |
803 | |
804 ls->port = atoi(args[1].data); | |
805 if (ls->port < 0) { | |
806 return "port must be greater or equal to zero"; | |
807 } | |
808 | |
809 return NGX_CONF_OK; | |
810 } |