Mercurial > hg > nginx
comparison src/http/ngx_http_core_module.c @ 143:5526213be452
nginx-0.0.1-2003-10-10-19:10:50 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 10 Oct 2003 15:10:50 +0000 |
parents | cd54bcbaf3b5 |
children | ef8c87afcfc5 |
comparison
equal
deleted
inserted
replaced
142:cb77c084acdb | 143:5526213be452 |
---|---|
1 | 1 |
2 #include <ngx_config.h> | 2 #include <ngx_config.h> |
3 #include <ngx_core.h> | 3 #include <ngx_core.h> |
4 #include <ngx_event.h> | |
4 #include <ngx_http.h> | 5 #include <ngx_http.h> |
5 #include <nginx.h> | 6 #include <nginx.h> |
6 | 7 |
7 | 8 |
8 /* STUB */ | 9 /* STUB */ |
9 int ngx_http_static_handler(ngx_http_request_t *r); | 10 int ngx_http_static_handler(ngx_http_request_t *r); |
10 | 11 |
12 static void ngx_http_phase_event_handler(ngx_event_t *rev); | |
13 static void ngx_http_run_phases(ngx_http_request_t *r); | |
11 | 14 |
12 static int ngx_http_core_index_handler(ngx_http_request_t *r); | 15 static int ngx_http_core_index_handler(ngx_http_request_t *r); |
13 | 16 |
14 static void *ngx_http_core_create_main_conf(ngx_conf_t *cf); | 17 static void *ngx_http_core_create_main_conf(ngx_conf_t *cf); |
15 static char *ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf); | 18 static char *ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf); |
199 }; | 202 }; |
200 | 203 |
201 | 204 |
202 void ngx_http_handler(ngx_http_request_t *r) | 205 void ngx_http_handler(ngx_http_request_t *r) |
203 { | 206 { |
204 int rc, i; | 207 ngx_http_log_ctx_t *lcx; |
205 ngx_http_log_ctx_t *lcx; | |
206 ngx_http_handler_pt *h; | |
207 ngx_http_core_loc_conf_t *clcf, **clcfp; | |
208 ngx_http_core_srv_conf_t *cscf; | |
209 ngx_http_core_main_conf_t *cmcf; | |
210 | 208 |
211 r->connection->unexpected_eof = 0; | 209 r->connection->unexpected_eof = 0; |
212 | 210 |
213 lcx = r->connection->log->data; | 211 lcx = r->connection->log->data; |
214 lcx->action = NULL; | 212 lcx->action = NULL; |
219 r->lingering_close = 1; | 217 r->lingering_close = 1; |
220 } | 218 } |
221 | 219 |
222 /* TEST STUB */ r->lingering_close = 1; | 220 /* TEST STUB */ r->lingering_close = 1; |
223 | 221 |
224 | 222 r->connection->write->event_handler = ngx_http_phase_event_handler; |
225 /* TODO: run rewrite url phase */ | 223 |
226 | 224 r->phase = 0; |
227 | 225 r->phase_handler = 0; |
228 /* find location config */ | 226 |
227 ngx_http_run_phases(r); | |
228 | |
229 return; | |
230 } | |
231 | |
232 | |
233 static void ngx_http_phase_event_handler(ngx_event_t *ev) | |
234 { | |
235 ngx_connection_t *c; | |
236 ngx_http_request_t *r; | |
237 | |
238 c = ev->data; | |
239 r = c->data; | |
240 | |
241 ngx_http_run_phases(r); | |
242 | |
243 return; | |
244 } | |
245 | |
246 | |
247 static void ngx_http_run_phases(ngx_http_request_t *r) | |
248 { | |
249 int rc; | |
250 ngx_http_handler_pt *h; | |
251 ngx_http_core_main_conf_t *cmcf; | |
252 | |
253 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
254 | |
255 rc = NGX_DECLINED; | |
256 | |
257 for (/* void */; r->phase < NGX_HTTP_LAST_PHASE; r->phase++) { | |
258 | |
259 h = cmcf->phases[r->phase].handlers.elts; | |
260 for (r->phase_handler = cmcf->phases[r->phase].handlers.nelts - 1; | |
261 r->phase_handler >= 0; | |
262 r->phase_handler--) | |
263 { | |
264 rc = h[r->phase_handler](r); | |
265 | |
266 if (rc == NGX_DECLINED) { | |
267 continue; | |
268 } | |
269 | |
270 if (rc == NGX_AGAIN) { | |
271 return; | |
272 } | |
273 | |
274 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | |
275 ngx_http_finalize_request(r, rc); | |
276 return; | |
277 } | |
278 | |
279 if (rc == NGX_OK && cmcf->phases[r->phase].type == NGX_OK) { | |
280 break; | |
281 } | |
282 } | |
283 | |
284 if (cmcf->phases[r->phase].post_handler) { | |
285 rc = cmcf->phases[r->phase].post_handler(r); | |
286 | |
287 if (rc == NGX_AGAIN) { | |
288 return; | |
289 } | |
290 | |
291 if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) { | |
292 ngx_http_finalize_request(r, rc); | |
293 return; | |
294 } | |
295 } | |
296 } | |
297 | |
298 if (r->content_handler) { | |
299 r->connection->write->event_handler = ngx_http_writer; | |
300 rc = r->content_handler(r); | |
301 ngx_http_finalize_request(r, rc); | |
302 return; | |
303 } | |
304 | |
305 /* TODO: no handlers found ? */ | |
306 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
307 return; | |
308 } | |
309 | |
310 | |
311 int ngx_http_find_location_config(ngx_http_request_t *r) | |
312 { | |
313 int i, rc; | |
314 ngx_http_core_loc_conf_t *clcf, **clcfp; | |
315 ngx_http_core_srv_conf_t *cscf; | |
229 | 316 |
230 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); | 317 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); |
231 | 318 |
232 clcfp = cscf->locations.elts; | 319 clcfp = cscf->locations.elts; |
233 for (i = 0; i < cscf->locations.nelts; i++) { | 320 for (i = 0; i < cscf->locations.nelts; i++) { |
259 | 346 |
260 if ((ngx_io.flags & NGX_IO_SENDFILE) == 0 || clcf->sendfile == 0) { | 347 if ((ngx_io.flags & NGX_IO_SENDFILE) == 0 || clcf->sendfile == 0) { |
261 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; | 348 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; |
262 } | 349 } |
263 | 350 |
264 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | 351 return NGX_OK; |
265 | |
266 /* run translation phase */ | |
267 | |
268 h = cmcf->translate_handlers.elts; | |
269 for (i = cmcf->translate_handlers.nelts - 1; i >= 0; i--) { | |
270 | |
271 rc = h[i](r); | |
272 | |
273 if (rc == NGX_DECLINED) { | |
274 continue; | |
275 } | |
276 | |
277 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | |
278 ngx_http_finalize_request(r, rc); | |
279 return; | |
280 } | |
281 | |
282 if (rc == NGX_OK) { | |
283 rc = r->handler(r); | |
284 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | |
285 ngx_http_finalize_request(r, rc); | |
286 } | |
287 return; | |
288 } | |
289 } | |
290 | |
291 /* TODO: no handlers found ? */ | |
292 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
293 return; | |
294 } | 352 } |
295 | 353 |
296 | 354 |
297 int ngx_http_core_translate_handler(ngx_http_request_t *r) | 355 int ngx_http_core_translate_handler(ngx_http_request_t *r) |
298 { | 356 { |
303 ngx_http_core_loc_conf_t *clcf; | 361 ngx_http_core_loc_conf_t *clcf; |
304 | 362 |
305 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | 363 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
306 | 364 |
307 if (clcf->handler) { | 365 if (clcf->handler) { |
308 r->handler = clcf->handler; | 366 r->content_handler = clcf->handler; |
309 return NGX_OK; | 367 return NGX_OK; |
310 } | 368 } |
311 | 369 |
312 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); | 370 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); |
313 | 371 |
314 if (r->uri.data[r->uri.len - 1] == '/') { | 372 if (r->uri.data[r->uri.len - 1] == '/') { |
315 r->handler = ngx_http_core_index_handler; | 373 r->content_handler = ngx_http_core_index_handler; |
316 return NGX_OK; | 374 return NGX_OK; |
317 } | 375 } |
318 | 376 |
319 /* "+ 2" is for trailing '/' in redirect and '\0' */ | 377 /* "+ 2" is for trailing '/' in redirect and '\0' */ |
320 ngx_test_null(r->file.name.data, | 378 ngx_test_null(r->file.name.data, |
422 r->headers_out.location = h; | 480 r->headers_out.location = h; |
423 | 481 |
424 return NGX_HTTP_MOVED_PERMANENTLY; | 482 return NGX_HTTP_MOVED_PERMANENTLY; |
425 } | 483 } |
426 | 484 |
427 r->handler = ngx_http_static_handler; | 485 r->content_handler = ngx_http_static_handler; |
428 | 486 |
429 return NGX_OK; | 487 return NGX_OK; |
430 } | 488 } |
431 | 489 |
432 | 490 |
546 ngx_http_core_main_conf_t *cmcf; | 604 ngx_http_core_main_conf_t *cmcf; |
547 | 605 |
548 ctx = (ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index]; | 606 ctx = (ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index]; |
549 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index]; | 607 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index]; |
550 | 608 |
551 ngx_test_null(h, ngx_push_array(&cmcf->translate_handlers), NGX_ERROR); | 609 ngx_test_null(h, ngx_push_array( |
610 &cmcf->phases[NGX_HTTP_TRANSLATE_PHASE].handlers), | |
611 NGX_ERROR); | |
552 | 612 |
553 *h = ngx_http_core_translate_handler; | 613 *h = ngx_http_core_translate_handler; |
554 | 614 |
555 return NGX_OK; | 615 return NGX_OK; |
556 } | 616 } |
754 static void *ngx_http_core_create_main_conf(ngx_conf_t *cf) | 814 static void *ngx_http_core_create_main_conf(ngx_conf_t *cf) |
755 { | 815 { |
756 ngx_http_core_main_conf_t *cmcf; | 816 ngx_http_core_main_conf_t *cmcf; |
757 | 817 |
758 ngx_test_null(cmcf, | 818 ngx_test_null(cmcf, |
759 ngx_palloc(cf->pool, sizeof(ngx_http_core_main_conf_t)), | 819 ngx_pcalloc(cf->pool, sizeof(ngx_http_core_main_conf_t)), |
760 NGX_CONF_ERROR); | 820 NGX_CONF_ERROR); |
761 | 821 |
762 ngx_init_array(cmcf->servers, cf->pool, | 822 ngx_init_array(cmcf->servers, cf->pool, |
763 5, sizeof(ngx_http_core_srv_conf_t *), | 823 5, sizeof(ngx_http_core_srv_conf_t *), |
764 NGX_CONF_ERROR); | 824 NGX_CONF_ERROR); |