comparison src/http/ngx_http_core_module.c @ 88:674d333f4296

nginx-0.0.1-2003-05-14-21:13:13 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 14 May 2003 17:13:13 +0000
parents 5f6d848dcbef
children 29bf798b583f
comparison
equal deleted inserted replaced
87:5f6d848dcbef 88:674d333f4296
1 1
2 #include <ngx_config.h> 2 #include <ngx_config.h>
3 3
4 #include <ngx_listen.h> 4 #include <ngx_listen.h>
5
6 #include <ngx_core.h> 5 #include <ngx_core.h>
6 #include <ngx_string.h>
7 #include <ngx_conf_file.h> 7 #include <ngx_conf_file.h>
8 8
9 #include <ngx_http.h> 9 #include <ngx_http.h>
10 #include <ngx_http_config.h> 10 #include <ngx_http_config.h>
11 #include <ngx_http_core_module.h> 11 #include <ngx_http_core_module.h>
28 void *parent, void *child); 28 void *parent, void *child);
29 29
30 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy); 30 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy);
31 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, 31 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd,
32 char *dummy); 32 char *dummy);
33 static char *ngx_types_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy);
33 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, char *conf); 34 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
34 35
35 36
36 static ngx_command_t ngx_http_core_commands[] = { 37 static ngx_command_t ngx_http_core_commands[] = {
37 38
85 86
86 {ngx_string("listen"), 87 {ngx_string("listen"),
87 NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1, 88 NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
88 ngx_set_listen, 89 ngx_set_listen,
89 NGX_HTTP_SRV_CONF_OFFSET, 90 NGX_HTTP_SRV_CONF_OFFSET,
91 0},
92
93 {ngx_string("types"),
94 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
95 ngx_types_block,
96 NGX_HTTP_LOC_CONF_OFFSET,
90 0}, 97 0},
91 98
92 {ngx_string("root"), 99 {ngx_string("root"),
93 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, 100 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
94 ngx_conf_set_str_slot, 101 ngx_conf_set_str_slot,
141 NGX_HTTP_MODULE_TYPE, /* module type */ 148 NGX_HTTP_MODULE_TYPE, /* module type */
142 ngx_http_core_init /* init module */ 149 ngx_http_core_init /* init module */
143 }; 150 };
144 151
145 152
146 int ngx_http_handler(ngx_http_request_t *r) 153 void ngx_http_handler(ngx_http_request_t *r)
147 { 154 {
148 int rc, a, n, i; 155 int rc, a, n, i;
149 ngx_http_handler_pt *h; 156 ngx_http_handler_pt *h;
150 ngx_http_module_t *module; 157 ngx_http_module_t *module;
151 ngx_http_conf_ctx_t *ctx; 158 ngx_http_conf_ctx_t *ctx;
265 272
266 if (rc == NGX_DECLINED) { 273 if (rc == NGX_DECLINED) {
267 continue; 274 continue;
268 } 275 }
269 276
277 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
278 ngx_http_finalize_request(r, rc);
279 return;
280 }
281
270 if (rc == NGX_OK) { 282 if (rc == NGX_OK) {
271 break; 283 rc = r->handler(r);
272 } 284 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
273 285 ngx_http_finalize_request(r, rc);
274 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { 286 }
275 return rc; 287 return;
276 } 288 }
277 } 289 }
278 290
279 return r->handler(r); 291 /* TODO: no handlers found ? */
292 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
293 return;
280 } 294 }
281 295
282 296
283 int ngx_http_core_translate_handler(ngx_http_request_t *r) 297 int ngx_http_core_translate_handler(ngx_http_request_t *r)
284 { 298 {
370 err = ngx_errno; 384 err = ngx_errno;
371 ngx_log_error(NGX_LOG_ERR, r->connection->log, err, 385 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
372 "ngx_http_core_translate_handler: " 386 "ngx_http_core_translate_handler: "
373 ngx_file_type_n " \"%s\" failed", r->file.name.data); 387 ngx_file_type_n " \"%s\" failed", r->file.name.data);
374 388
375 if (err == NGX_ENOENT) { 389 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
376 return NGX_HTTP_NOT_FOUND;
377
378 } else if (err == NGX_ENOTDIR) {
379 return NGX_HTTP_NOT_FOUND; 390 return NGX_HTTP_NOT_FOUND;
380 391
381 } else if (err == NGX_EACCES) { 392 } else if (err == NGX_EACCES) {
382 return NGX_HTTP_FORBIDDEN; 393 return NGX_HTTP_FORBIDDEN;
383 394
396 err = ngx_errno; 407 err = ngx_errno;
397 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, 408 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
398 "ngx_http_core_handler: " 409 "ngx_http_core_handler: "
399 ngx_open_file_n " \"%s\" failed", r->file.name.data); 410 ngx_open_file_n " \"%s\" failed", r->file.name.data);
400 411
401 if (err == NGX_ENOENT) { 412 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
402 return NGX_HTTP_NOT_FOUND;
403
404 } else if (err == NGX_ENOTDIR) {
405 return NGX_HTTP_NOT_FOUND; 413 return NGX_HTTP_NOT_FOUND;
406 414
407 } else if (err == NGX_EACCES) { 415 } else if (err == NGX_EACCES) {
408 return NGX_HTTP_FORBIDDEN; 416 return NGX_HTTP_FORBIDDEN;
409 417
545 /* NEEDED ? */ 553 /* NEEDED ? */
546 r->uri_start = uri.data; 554 r->uri_start = uri.data;
547 r->uri_end = uri.data + uri.len; 555 r->uri_end = uri.data + uri.len;
548 /**/ 556 /**/
549 557
550 return ngx_http_handler(r); 558 ngx_http_handler(r);
559 return 0;
551 } 560 }
552 561
553 562
554 static int ngx_http_core_init(ngx_pool_t *pool) 563 static int ngx_http_core_init(ngx_pool_t *pool)
555 { 564 {
566 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy) 575 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy)
567 { 576 {
568 int i, j; 577 int i, j;
569 char *rv; 578 char *rv;
570 ngx_http_module_t *module; 579 ngx_http_module_t *module;
571 ngx_http_conf_ctx_t *ctx, *prev; 580 ngx_conf_t pcf;
581 ngx_http_conf_ctx_t *ctx, *pctx;
572 ngx_http_core_srv_conf_t *scf; 582 ngx_http_core_srv_conf_t *scf;
573 ngx_http_core_loc_conf_t **plcf; 583 ngx_http_core_loc_conf_t **plcf;
574 584
575 ngx_test_null(ctx, 585 ngx_test_null(ctx,
576 ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)), 586 ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)),
604 module->create_loc_conf(cf->pool), 614 module->create_loc_conf(cf->pool),
605 NGX_CONF_ERROR); 615 NGX_CONF_ERROR);
606 } 616 }
607 } 617 }
608 618
609 prev = cf->ctx; 619 pcf = *cf;
620 pctx = cf->ctx;
610 cf->ctx = ctx; 621 cf->ctx = ctx;
622 cf->cmd_type = NGX_HTTP_SRV_CONF;
611 rv = ngx_conf_parse(cf, NULL); 623 rv = ngx_conf_parse(cf, NULL);
612 cf->ctx = prev; 624 *cf = pcf;
613 625
614 if (rv != NGX_CONF_OK) 626 if (rv != NGX_CONF_OK)
615 return rv; 627 return rv;
616 628
617 629
634 return NGX_CONF_ERROR; 646 return NGX_CONF_ERROR;
635 } 647 }
636 } 648 }
637 649
638 if (module->merge_loc_conf) { 650 if (module->merge_loc_conf) {
639 if (module->merge_loc_conf(cf->pool, 651 rv = module->merge_loc_conf(cf->pool,
640 prev->loc_conf[module->index], 652 pctx->loc_conf[module->index],
641 ctx->loc_conf[module->index]) 653 ctx->loc_conf[module->index]);
642 == NGX_CONF_ERROR) { 654 if (rv != NGX_CONF_OK) {
643 return NGX_CONF_ERROR; 655 return rv;
644 } 656 }
645 657
646 for (j = 0; j < scf->locations.nelts; j++) { 658 for (j = 0; j < scf->locations.nelts; j++) {
647 if (module->merge_loc_conf(cf->pool, 659 rv = module->merge_loc_conf(cf->pool,
648 ctx->loc_conf[module->index], 660 ctx->loc_conf[module->index],
649 plcf[j]->loc_conf[module->index]) 661 plcf[j]->loc_conf[module->index]);
650 == NGX_CONF_ERROR) { 662 if (rv != NGX_CONF_OK) {
651 return NGX_CONF_ERROR; 663 return rv;
652 } 664 }
653 } 665 }
654 } 666 }
655 } 667 }
656 668
662 { 674 {
663 int i; 675 int i;
664 char *rv; 676 char *rv;
665 ngx_str_t *location; 677 ngx_str_t *location;
666 ngx_http_module_t *module; 678 ngx_http_module_t *module;
667 ngx_http_conf_ctx_t *ctx, *prev; 679 ngx_conf_t pcf;
680 ngx_http_conf_ctx_t *ctx, *pctx;
668 ngx_http_core_srv_conf_t *scf; 681 ngx_http_core_srv_conf_t *scf;
669 ngx_http_core_loc_conf_t *lcf, **plcf; 682 ngx_http_core_loc_conf_t *lcf, **plcf;
670 683
671 ngx_test_null(ctx, 684 ngx_test_null(ctx,
672 ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)), 685 ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)),
673 NGX_CONF_ERROR); 686 NGX_CONF_ERROR);
674 687
675 prev = (ngx_http_conf_ctx_t *) cf->ctx; 688 pctx = (ngx_http_conf_ctx_t *) cf->ctx;
676 ctx->srv_conf = prev->srv_conf; 689 ctx->srv_conf = pctx->srv_conf;
677 690
678 ngx_test_null(ctx->loc_conf, 691 ngx_test_null(ctx->loc_conf,
679 ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module), 692 ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module),
680 NGX_CONF_ERROR); 693 NGX_CONF_ERROR);
681 694
703 scf = (ngx_http_core_srv_conf_t *) 716 scf = (ngx_http_core_srv_conf_t *)
704 ctx->srv_conf[ngx_http_core_module_ctx.index]; 717 ctx->srv_conf[ngx_http_core_module_ctx.index];
705 ngx_test_null(plcf, ngx_push_array(&scf->locations), NGX_CONF_ERROR); 718 ngx_test_null(plcf, ngx_push_array(&scf->locations), NGX_CONF_ERROR);
706 *plcf = lcf; 719 *plcf = lcf;
707 720
721 pcf = *cf;
708 cf->ctx = ctx; 722 cf->ctx = ctx;
723 cf->cmd_type = NGX_HTTP_LOC_CONF;
709 rv = ngx_conf_parse(cf, NULL); 724 rv = ngx_conf_parse(cf, NULL);
710 cf->ctx = prev; 725 *cf = pcf;
726
727 return rv;
728 }
729
730
731 static char *ngx_set_type(ngx_conf_t *cf, ngx_command_t *dummy, char *conf)
732 {
733 ngx_http_core_loc_conf_t *lcf = (ngx_http_core_loc_conf_t *) conf;
734
735 int i, key;
736 ngx_str_t *args;
737 ngx_http_type_t *t;
738
739 if (lcf->types == NULL) {
740 ngx_test_null(lcf->types,
741 ngx_palloc(cf->pool, NGX_HTTP_TYPES_HASH_PRIME
742 * sizeof(ngx_array_t)),
743 NGX_CONF_ERROR);
744
745 for (i = 0; i < NGX_HTTP_TYPES_HASH_PRIME; i++) {
746 ngx_init_array(lcf->types[i], cf->pool, 5, sizeof(ngx_http_type_t),
747 NGX_CONF_ERROR);
748 }
749 }
750
751 args = (ngx_str_t *) cf->args->elts;
752
753 for (i = 1; i < cf->args->nelts; i++) {
754 ngx_http_types_hash_key(key, args[i]);
755
756 ngx_test_null(t, ngx_push_array(&lcf->types[key]), NGX_CONF_ERROR);
757 t->exten.len = args[i].len;
758 t->exten.data = args[i].data;
759 t->type.len = args[0].len;
760 t->type.data = args[0].data;
761 }
762
763 return NGX_CONF_OK;
764 }
765
766
767 static char *ngx_types_block(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
768 {
769 char *rv;
770 ngx_conf_t pcf;
771
772 pcf = *cf;
773 cf->handler = ngx_set_type;
774 cf->handler_conf = conf;
775 rv = ngx_conf_parse(cf, NULL);
776 *cf = pcf;
711 777
712 return rv; 778 return rv;
713 } 779 }
714 780
715 781
774 840
775 ngx_test_null(lcf, 841 ngx_test_null(lcf,
776 ngx_pcalloc(pool, sizeof(ngx_http_core_loc_conf_t)), 842 ngx_pcalloc(pool, sizeof(ngx_http_core_loc_conf_t)),
777 NGX_CONF_ERROR); 843 NGX_CONF_ERROR);
778 844
779 lcf->doc_root.len = 4;
780 lcf->doc_root.data = "html";
781
782 lcf->sendfile = 0;
783
784 lcf->send_timeout = 10000;
785 lcf->discarded_buffer_size = 1500;
786 lcf->lingering_time = 30000;
787 lcf->lingering_timeout = 5000;
788
789 /* 845 /*
846 ngx_pcalloc():
847
848 lcf->doc_root.len = 0;
849 lcf->doc_root.data = NULL;
850 lcf->types = NULL;
851 */
852
853 lcf->sendfile = NGX_CONF_UNSET;
854
790 lcf->send_timeout = NGX_CONF_UNSET; 855 lcf->send_timeout = NGX_CONF_UNSET;
791 */ 856 lcf->discarded_buffer_size = NGX_CONF_UNSET;
857 lcf->lingering_time = NGX_CONF_UNSET;
858 lcf->lingering_timeout = NGX_CONF_UNSET;
792 859
793 return lcf; 860 return lcf;
794 } 861 }
862
863
864 static ngx_http_type_t default_types[] = {
865 { ngx_string("html"), ngx_string("text/html") },
866 { ngx_string("gif"), ngx_string("image/gif") },
867 { ngx_string("jpg"), ngx_string("image/jpeg") },
868 { ngx_null_string, ngx_null_string }
869 };
870
795 871
796 static char *ngx_http_core_merge_loc_conf(ngx_pool_t *pool, 872 static char *ngx_http_core_merge_loc_conf(ngx_pool_t *pool,
797 void *parent, void *child) 873 void *parent, void *child)
798 { 874 {
875 ngx_http_core_loc_conf_t *prev = (ngx_http_core_loc_conf_t *) parent;
876 ngx_http_core_loc_conf_t *conf = (ngx_http_core_loc_conf_t *) child;
877
878 int i, key;
879 ngx_http_type_t *t;
880
881 if (conf->doc_root.len == 0) {
882 if (prev->doc_root.len) {
883 conf->doc_root.len = prev->doc_root.len;
884 conf->doc_root.data = prev->doc_root.data;
885
886 } else {
887 conf->doc_root.len = 4;
888 conf->doc_root.data = "html";
889 }
890 }
891
892 if (conf->types == NULL) {
893 if (prev->types) {
894 conf->types = prev->types;
895
896 } else {
897 ngx_test_null(conf->types,
898 ngx_palloc(pool, NGX_HTTP_TYPES_HASH_PRIME
899 * sizeof(ngx_array_t)),
900 NGX_CONF_ERROR);
901
902 for (i = 0; i < NGX_HTTP_TYPES_HASH_PRIME; i++) {
903 ngx_init_array(conf->types[i], pool, 5, sizeof(ngx_http_type_t),
904 NGX_CONF_ERROR);
905 }
906
907 for (i = 0; default_types[i].exten.len; i++) {
908 ngx_http_types_hash_key(key, default_types[i].exten);
909
910 ngx_test_null(t, ngx_push_array(&conf->types[key]),
911 NGX_CONF_ERROR);
912 t->exten.len = default_types[i].exten.len;
913 t->exten.data = default_types[i].exten.data;
914 t->type.len = default_types[i].type.len;
915 t->type.data = default_types[i].type.data;
916 }
917 }
918 }
919
920 ngx_conf_merge(conf->sendfile, prev->sendfile, 0);
921
922 ngx_conf_msec_merge(conf->send_timeout, prev->send_timeout, 10000);
923
924 ngx_conf_size_merge(conf->discarded_buffer_size,
925 prev->discarded_buffer_size, 1500);
926
927 ngx_conf_msec_merge(conf->lingering_time, prev->lingering_time, 30000);
928 ngx_conf_msec_merge(conf->lingering_timeout, prev->lingering_timeout, 5000);
929
799 return NGX_CONF_OK; 930 return NGX_CONF_OK;
800 } 931 }
932
801 933
802 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) 934 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
803 { 935 {
804 ngx_str_t *args; 936 ngx_str_t *args;
805 ngx_http_listen_t *ls; 937 ngx_http_listen_t *ls;
815 ls->file_name = cf->conf_file->file.name; 947 ls->file_name = cf->conf_file->file.name;
816 ls->line = cf->conf_file->line; 948 ls->line = cf->conf_file->line;
817 949
818 args = (ngx_str_t *) cf->args->elts; 950 args = (ngx_str_t *) cf->args->elts;
819 951
820 ls->port = atoi(args[1].data); 952 ls->port = ngx_atoi(args[1].data, args[1].len);
821 if (ls->port < 1 || ls->port > 65536) { 953 if (ls->port < 1 || ls->port > 65536) {
822 return "port must be between 1 and 65535"; 954 return "port must be between 1 and 65535";
823 } 955 }
824 956
825 return NGX_CONF_OK; 957 return NGX_CONF_OK;