comparison src/http/ngx_http_core_module.c @ 71:59229033ae93

nginx-0.0.1-2003-04-08-19:40:10 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 08 Apr 2003 15:40:10 +0000
parents e43f406e4525
children 17ab1af8c3dd
comparison
equal deleted inserted replaced
70:e320bf51c4e3 71:59229033ae93
22 static int ngx_http_core_init(ngx_pool_t *pool); 22 static int ngx_http_core_init(ngx_pool_t *pool);
23 23
24 static void *ngx_http_core_create_srv_conf(ngx_pool_t *pool); 24 static void *ngx_http_core_create_srv_conf(ngx_pool_t *pool);
25 static char *ngx_http_core_init_srv_conf(ngx_pool_t *pool, void *conf); 25 static char *ngx_http_core_init_srv_conf(ngx_pool_t *pool, void *conf);
26 static void *ngx_http_core_create_loc_conf(ngx_pool_t *pool); 26 static void *ngx_http_core_create_loc_conf(ngx_pool_t *pool);
27 static char *ngx_http_core_merge_loc_conf(ngx_pool_t *pool,
28 void *parent, void *child);
27 29
28 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);
29 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,
30 char *dummy); 32 char *dummy);
31 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, char *conf); 33 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
76 addressof(ngx_http_large_client_header)}, 78 addressof(ngx_http_large_client_header)},
77 79
78 {ngx_string("location"), 80 {ngx_string("location"),
79 NGX_HTTP_SRV_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1, 81 NGX_HTTP_SRV_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
80 ngx_location_block, 82 ngx_location_block,
81 0, 83 NGX_HTTP_SRV_CONF_OFFSET,
82 0}, 84 0},
83 85
84 {ngx_string("listen"), 86 {ngx_string("listen"),
85 NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1, 87 NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
86 ngx_set_listen, 88 ngx_set_listen,
87 0, 89 NGX_HTTP_SRV_CONF_OFFSET,
88 0}, 90 0},
89 91
90 {ngx_string("root"), 92 {ngx_string("root"),
91 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, 93 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
92 ngx_conf_set_str_slot, 94 ngx_conf_set_str_slot,
93 NGX_HTTP_LOC_CONF_OFFSET, 95 NGX_HTTP_LOC_CONF_OFFSET,
94 offsetof(ngx_http_core_loc_conf_t, doc_root)}, 96 offsetof(ngx_http_core_loc_conf_t, doc_root)},
95 97
98 {ngx_string("sendfile"),
99 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
100 ngx_conf_set_flag_slot,
101 NGX_HTTP_LOC_CONF_OFFSET,
102 offsetof(ngx_http_core_loc_conf_t, sendfile)},
103
96 {ngx_string("send_timeout"), 104 {ngx_string("send_timeout"),
97 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, 105 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
98 ngx_conf_set_time_slot, 106 ngx_conf_set_time_slot,
99 NGX_HTTP_LOC_CONF_OFFSET, 107 NGX_HTTP_LOC_CONF_OFFSET,
100 offsetof(ngx_http_core_loc_conf_t, send_timeout)}, 108 offsetof(ngx_http_core_loc_conf_t, send_timeout)},
101 109
102 {ngx_string("lingering_time"), 110 {ngx_string("lingering_time"),
103 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, 111 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
104 ngx_conf_set_time_slot, 112 ngx_conf_set_time_slot,
105 NGX_HTTP_LOC_CONF_OFFSET, 113 NGX_HTTP_LOC_CONF_OFFSET,
106 offsetof(ngx_http_core_loc_conf_t, lingering_time)}, 114 offsetof(ngx_http_core_loc_conf_t, lingering_time)},
107 115
108 {ngx_string("lingering_timeout"), 116 {ngx_string("lingering_timeout"),
109 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, 117 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
110 ngx_conf_set_time_slot, 118 ngx_conf_set_time_slot,
111 NGX_HTTP_LOC_CONF_OFFSET, 119 NGX_HTTP_LOC_CONF_OFFSET,
112 offsetof(ngx_http_core_loc_conf_t, lingering_timeout)}, 120 offsetof(ngx_http_core_loc_conf_t, lingering_timeout)},
113 121
114 {ngx_null_string, 0, NULL, 0, 0} 122 {ngx_null_string, 0, NULL, 0, 0}
115 }; 123 };
116 124
117 125
118 ngx_http_module_t ngx_http_core_module_ctx = { 126 ngx_http_module_t ngx_http_core_module_ctx = {
127 NGX_HTTP_MODULE,
128
119 ngx_http_core_create_srv_conf, /* create server config */ 129 ngx_http_core_create_srv_conf, /* create server config */
120 ngx_http_core_init_srv_conf, /* init server config */ 130 ngx_http_core_init_srv_conf, /* init server config */
121 131
122 ngx_http_core_create_loc_conf, /* create location config */ 132 ngx_http_core_create_loc_conf, /* create location config */
123 NULL, /* merge location config */ 133 ngx_http_core_merge_loc_conf /* merge location config */
124
125 NULL /* init filters */
126 }; 134 };
127 135
128 136
129 ngx_module_t ngx_http_core_module = { 137 ngx_module_t ngx_http_core_module = {
130 0, /* module index */ 138 0, /* module index */
135 }; 143 };
136 144
137 145
138 int ngx_http_handler(ngx_http_request_t *r) 146 int ngx_http_handler(ngx_http_request_t *r)
139 { 147 {
140 int rc, a, n, i; 148 int rc, a, n, i;
141 ngx_http_handler_pt *h; 149 ngx_http_handler_pt *h;
142 ngx_http_module_t *module; 150 ngx_http_module_t *module;
143 ngx_http_conf_ctx_t *ctx; 151 ngx_http_conf_ctx_t *ctx;
144 ngx_http_in_port_t *in_port; 152 ngx_http_in_port_t *in_port;
145 ngx_http_in_addr_t *in_addr; 153 ngx_http_in_addr_t *in_addr;
146 ngx_http_server_name_t *name; 154 ngx_http_server_name_t *name;
155 ngx_http_core_srv_conf_t *scf;
156 ngx_http_core_loc_conf_t *lcf, **plcf;
147 157
148 r->connection->unexpected_eof = 0; 158 r->connection->unexpected_eof = 0;
149 159
150 r->keepalive = 1; 160 r->keepalive = 1;
151 r->lingering_close = 1; 161 r->lingering_close = 1;
152 162
153 #if 1 163 #if 0
154 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; 164 ngx_log_debug(r->connection->log, "servers: %0x" _ r->connection->servers);
155 #endif 165 #endif
156 166
157 ngx_log_debug(r->connection->log, "servers: %0x" _ r->connection->servers); 167 /* find server config */
158 168
159 if (r->connection->servers == NULL) { 169 if (r->connection->servers == NULL) {
160 ctx = (ngx_http_conf_ctx_t *) r->connection->ctx; 170 ctx = (ngx_http_conf_ctx_t *) r->connection->ctx;
161 171
162 } else { 172 } else {
204 } 214 }
205 215
206 r->srv_conf = ctx->srv_conf; 216 r->srv_conf = ctx->srv_conf;
207 r->loc_conf = ctx->loc_conf; 217 r->loc_conf = ctx->loc_conf;
208 218
219 #if 0
209 ngx_log_debug(r->connection->log, "cxt: %08x" _ ctx); 220 ngx_log_debug(r->connection->log, "cxt: %08x" _ ctx);
210 ngx_log_debug(r->connection->log, "srv_conf: %0x" _ r->srv_conf); 221 ngx_log_debug(r->connection->log, "srv_conf: %0x" _ r->srv_conf);
211 ngx_log_debug(r->connection->log, "loc_conf: %0x" _ r->loc_conf); 222 ngx_log_debug(r->connection->log, "loc_conf: %0x" _ r->loc_conf);
223 #endif
224
225 /* run rewrite url phase */
226
227
228 /* find location config */
229
230 scf = (ngx_http_core_srv_conf_t *)
231 ngx_http_get_module_srv_conf(r, ngx_http_core_module_ctx);
232
233 plcf = (ngx_http_core_loc_conf_t **) scf->locations.elts;
234 for (i = 0; i < scf->locations.nelts; i++) {
235 #if 0
236 ngx_log_debug(r->connection->log, "trans: %s" _ plcf[i]->name.data);
237 #endif
238 if (r->uri.len < plcf[i]->name.len) {
239 continue;
240 }
241
242 rc = ngx_strncmp(r->uri.data, plcf[i]->name.data, plcf[i]->name.len);
243
244 if (rc < 0) {
245 break;
246 }
247
248 if (rc == 0) {
249 r->loc_conf = plcf[i]->loc_conf;
250 }
251 }
252
253 lcf = (ngx_http_core_loc_conf_t *)
254 ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
255
256 if (lcf->sendfile == 0) {
257 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY;
258 }
212 259
213 /* run translation phase */ 260 /* run translation phase */
214 261
215 h = (ngx_http_handler_pt *) ngx_http_translate_handlers.elts; 262 h = (ngx_http_handler_pt *) ngx_http_translate_handlers.elts;
216 for (i = 0; i < ngx_http_translate_handlers.nelts; i++) { 263 for (i = ngx_http_translate_handlers.nelts; i > 0; /* void */) {
217 rc = h[i](r); 264 rc = h[--i](r);
218 265
219 if (rc == NGX_DECLINED) { 266 if (rc == NGX_DECLINED) {
220 continue; 267 continue;
221 } 268 }
222 269
239 char *buf, *location, *last; 286 char *buf, *location, *last;
240 ngx_err_t err; 287 ngx_err_t err;
241 ngx_table_elt_t *h; 288 ngx_table_elt_t *h;
242 ngx_http_server_name_t *s_name; 289 ngx_http_server_name_t *s_name;
243 ngx_http_core_srv_conf_t *scf; 290 ngx_http_core_srv_conf_t *scf;
244 ngx_http_core_loc_conf_t *lcf, **plcf; 291 ngx_http_core_loc_conf_t *lcf;
292
293 lcf = (ngx_http_core_loc_conf_t *)
294 ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
295
296 if (lcf->handler) {
297 r->handler = lcf->handler;
298 return NGX_OK;
299 }
245 300
246 scf = (ngx_http_core_srv_conf_t *) 301 scf = (ngx_http_core_srv_conf_t *)
247 ngx_http_get_module_srv_conf(r, ngx_http_core_module); 302 ngx_http_get_module_srv_conf(r, ngx_http_core_module_ctx);
248
249 /* find location config */
250 plcf = (ngx_http_core_loc_conf_t **) scf->locations.elts;
251 for (i = 0; i < scf->locations.nelts; i++) {
252 ngx_log_debug(r->connection->log, "trans: %s" _ plcf[i]->name.data);
253 if (r->uri.len < plcf[i]->name.len) {
254 continue;
255 }
256
257 rc = ngx_strncmp(r->uri.data, plcf[i]->name.data, plcf[i]->name.len);
258
259 if (rc < 0) {
260 break;
261 }
262
263 if (rc == 0) {
264 r->loc_conf = plcf[i]->loc_conf;
265 }
266 }
267
268 #if 0
269 /* STUB */ r->handler = ngx_http_proxy_handler;
270 return NGX_OK;
271 #endif
272 303
273 if (r->uri.data[r->uri.len - 1] == '/') { 304 if (r->uri.data[r->uri.len - 1] == '/') {
274 r->handler = ngx_http_core_index_handler; 305 r->handler = ngx_http_core_index_handler;
275 return NGX_OK; 306 return NGX_OK;
276 } 307 }
277
278 lcf = (ngx_http_core_loc_conf_t *)
279 ngx_http_get_module_loc_conf(r, ngx_http_core_module);
280 308
281 ngx_log_debug(r->connection->log, "doc_root: %08x" _ &lcf->doc_root); 309 ngx_log_debug(r->connection->log, "doc_root: %08x" _ &lcf->doc_root);
282 310
283 s_name = (ngx_http_server_name_t *) scf->server_names.elts; 311 s_name = (ngx_http_server_name_t *) scf->server_names.elts;
284 312
438 r->headers_out.location = h; 466 r->headers_out.location = h;
439 467
440 return NGX_HTTP_MOVED_PERMANENTLY; 468 return NGX_HTTP_MOVED_PERMANENTLY;
441 } 469 }
442 470
443 /* TODO: r->handler = lcf->default_handler; */ 471 r->handler = ngx_http_static_handler;
444 /* STUB */ r->handler = ngx_http_static_handler;
445 472
446 return NGX_OK; 473 return NGX_OK;
447 } 474 }
448 475
449 476
451 { 478 {
452 int i, rc; 479 int i, rc;
453 ngx_http_handler_pt *h; 480 ngx_http_handler_pt *h;
454 481
455 h = (ngx_http_handler_pt *) ngx_http_index_handlers.elts; 482 h = (ngx_http_handler_pt *) ngx_http_index_handlers.elts;
456 for (i = 0; i < ngx_http_index_handlers.nelts; i++) { 483 for (i = ngx_http_index_handlers.nelts; i > 0; /* void */) {
457 rc = h[i](r); 484 rc = h[--i](r);
458 485
459 if (rc != NGX_DECLINED) { 486 if (rc != NGX_DECLINED) {
460 487
461 if (rc == NGX_HTTP_NOT_FOUND) { 488 if (rc == NGX_HTTP_NOT_FOUND) {
462 ngx_log_error(NGX_LOG_ERR, r->connection->log, r->path_err, 489 ngx_log_error(NGX_LOG_ERR, r->connection->log, r->path_err,
543 c->write->timer_set = 0; 570 c->write->timer_set = 0;
544 } 571 }
545 572
546 ngx_log_debug(c->log, "http closed"); 573 ngx_log_debug(c->log, "http closed");
547 574
548 return NGX_DONE; 575 return NGX_ERROR; /* to close connection */
549 } 576 }
550 577
551 578
552 int ngx_http_internal_redirect(ngx_http_request_t *r, ngx_str_t uri) 579 int ngx_http_internal_redirect(ngx_http_request_t *r, ngx_str_t uri)
553 { 580 {
606 } 633 }
607 634
608 module = (ngx_http_module_t *) ngx_modules[i]->ctx; 635 module = (ngx_http_module_t *) ngx_modules[i]->ctx;
609 636
610 if (module->create_srv_conf) { 637 if (module->create_srv_conf) {
611 ngx_test_null(ctx->srv_conf[ngx_modules[i]->index], 638 ngx_test_null(ctx->srv_conf[module->index],
612 module->create_srv_conf(cf->pool), 639 module->create_srv_conf(cf->pool),
613 NGX_CONF_ERROR); 640 NGX_CONF_ERROR);
614 } 641 }
615 642
616 if (module->create_loc_conf) { 643 if (module->create_loc_conf) {
617 ngx_test_null(ctx->loc_conf[ngx_modules[i]->index], 644 ngx_test_null(ctx->loc_conf[module->index],
618 module->create_loc_conf(cf->pool), 645 module->create_loc_conf(cf->pool),
619 NGX_CONF_ERROR); 646 NGX_CONF_ERROR);
620 } 647 }
621 } 648 }
622 649
627 654
628 if (rv != NGX_CONF_OK) 655 if (rv != NGX_CONF_OK)
629 return rv; 656 return rv;
630 657
631 658
632 scf = ctx->srv_conf[ngx_http_core_module.index]; 659 scf = ctx->srv_conf[ngx_http_core_module_ctx.index];
633 scf->ctx = ctx; 660 scf->ctx = ctx;
634 661
635 plcf = (ngx_http_core_loc_conf_t **)scf->locations.elts; 662 plcf = (ngx_http_core_loc_conf_t **)scf->locations.elts;
636 663
637 for (i = 0; ngx_modules[i]; i++) { 664 for (i = 0; ngx_modules[i]; i++) {
641 668
642 module = (ngx_http_module_t *) ngx_modules[i]->ctx; 669 module = (ngx_http_module_t *) ngx_modules[i]->ctx;
643 670
644 if (module->init_srv_conf) { 671 if (module->init_srv_conf) {
645 if (module->init_srv_conf(cf->pool, 672 if (module->init_srv_conf(cf->pool,
646 ctx->srv_conf[ngx_modules[i]->index]) 673 ctx->srv_conf[module->index])
647 == NGX_CONF_ERROR) { 674 == NGX_CONF_ERROR) {
648 return NGX_CONF_ERROR; 675 return NGX_CONF_ERROR;
649 } 676 }
650 } 677 }
651 678
652 if (module->merge_loc_conf) { 679 if (module->merge_loc_conf) {
653 if (module->merge_loc_conf(cf->pool, 680 if (module->merge_loc_conf(cf->pool,
654 prev->loc_conf[ngx_modules[i]->index], 681 prev->loc_conf[module->index],
655 ctx->loc_conf[ngx_modules[i]->index]) 682 ctx->loc_conf[module->index])
656 == NGX_CONF_ERROR) { 683 == NGX_CONF_ERROR) {
657 return NGX_CONF_ERROR; 684 return NGX_CONF_ERROR;
658 } 685 }
659 686
660 for (j = 0; j < scf->locations.nelts; j++) { 687 for (j = 0; j < scf->locations.nelts; j++) {
661 if (module->merge_loc_conf(cf->pool, 688 if (module->merge_loc_conf(cf->pool,
662 ctx->loc_conf[ngx_modules[i]->index], 689 ctx->loc_conf[module->index],
663 plcf[j]->loc_conf[ngx_modules[i]->index]) 690 plcf[j]->loc_conf[module->index])
664 == NGX_CONF_ERROR) { 691 == NGX_CONF_ERROR) {
665 return NGX_CONF_ERROR; 692 return NGX_CONF_ERROR;
666 } 693 }
667 } 694 }
668 } 695 }
699 } 726 }
700 727
701 module = (ngx_http_module_t *) ngx_modules[i]->ctx; 728 module = (ngx_http_module_t *) ngx_modules[i]->ctx;
702 729
703 if (module->create_loc_conf) { 730 if (module->create_loc_conf) {
704 ngx_test_null(ctx->loc_conf[ngx_modules[i]->index], 731 ngx_test_null(ctx->loc_conf[module->index],
705 module->create_loc_conf(cf->pool), 732 module->create_loc_conf(cf->pool),
706 NGX_CONF_ERROR); 733 NGX_CONF_ERROR);
707 } 734 }
708 } 735 }
709 736
710 lcf = (ngx_http_core_loc_conf_t *) 737 lcf = (ngx_http_core_loc_conf_t *)
711 ctx->loc_conf[ngx_http_core_module.index]; 738 ctx->loc_conf[ngx_http_core_module_ctx.index];
712 location = (ngx_str_t *) cf->args->elts; 739 location = (ngx_str_t *) cf->args->elts;
713 lcf->name.len = location[1].len; 740 lcf->name.len = location[1].len;
714 lcf->name.data = location[1].data; 741 lcf->name.data = location[1].data;
715 lcf->loc_conf = ctx->loc_conf; 742 lcf->loc_conf = ctx->loc_conf;
716 743
717 scf = (ngx_http_core_srv_conf_t *) 744 scf = (ngx_http_core_srv_conf_t *)
718 ctx->srv_conf[ngx_http_core_module.index]; 745 ctx->srv_conf[ngx_http_core_module_ctx.index];
719 ngx_test_null(plcf, ngx_push_array(&scf->locations), NGX_CONF_ERROR); 746 ngx_test_null(plcf, ngx_push_array(&scf->locations), NGX_CONF_ERROR);
720 *plcf = lcf; 747 *plcf = lcf;
721 748
722 cf->ctx = ctx; 749 cf->ctx = ctx;
723 rv = ngx_conf_parse(cf, NULL); 750 rv = ngx_conf_parse(cf, NULL);
791 NGX_CONF_ERROR); 818 NGX_CONF_ERROR);
792 819
793 lcf->doc_root.len = 4; 820 lcf->doc_root.len = 4;
794 lcf->doc_root.data = "html"; 821 lcf->doc_root.data = "html";
795 822
823 lcf->sendfile = 0;
824
796 lcf->send_timeout = 10000; 825 lcf->send_timeout = 10000;
797 lcf->discarded_buffer_size = 1500; 826 lcf->discarded_buffer_size = 1500;
798 lcf->lingering_time = 30000; 827 lcf->lingering_time = 30000;
799 lcf->lingering_timeout = 5000; 828 lcf->lingering_timeout = 5000;
800 829
801 /* 830 /*
802 lcf->send_timeout = NGX_CONF_UNSET; 831 lcf->send_timeout = NGX_CONF_UNSET;
803 */ 832 */
804 833
805 return lcf; 834 return lcf;
835 }
836
837 static char *ngx_http_core_merge_loc_conf(ngx_pool_t *pool,
838 void *parent, void *child)
839 {
840 return NGX_CONF_OK;
806 } 841 }
807 842
808 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) 843 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
809 { 844 {
810 ngx_str_t *args; 845 ngx_str_t *args;