Mercurial > hg > nginx
comparison src/http/ngx_http_core_module.c @ 148:5afee0074707
nginx-0.0.1-2003-10-17-00:19:16 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 16 Oct 2003 20:19:16 +0000 |
parents | ef8c87afcfc5 |
children | fb48bf4fea1c |
comparison
equal
deleted
inserted
replaced
147:be71fca7f9d7 | 148:5afee0074707 |
---|---|
4 #include <ngx_event.h> | 4 #include <ngx_event.h> |
5 #include <ngx_http.h> | 5 #include <ngx_http.h> |
6 #include <nginx.h> | 6 #include <nginx.h> |
7 | 7 |
8 | 8 |
9 /* STUB */ | |
10 int ngx_http_static_handler(ngx_http_request_t *r); | |
11 /**/ | |
12 | 9 |
13 static void ngx_http_phase_event_handler(ngx_event_t *rev); | 10 static void ngx_http_phase_event_handler(ngx_event_t *rev); |
14 static void ngx_http_run_phases(ngx_http_request_t *r); | 11 static void ngx_http_run_phases(ngx_http_request_t *r); |
15 | 12 |
16 static void *ngx_http_core_create_main_conf(ngx_conf_t *cf); | 13 static void *ngx_http_core_create_main_conf(ngx_conf_t *cf); |
302 rc = r->content_handler(r); | 299 rc = r->content_handler(r); |
303 ngx_http_finalize_request(r, rc); | 300 ngx_http_finalize_request(r, rc); |
304 return; | 301 return; |
305 } | 302 } |
306 | 303 |
307 /* TODO: no handlers found ? */ | |
308 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | 304 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
309 return; | 305 return; |
310 } | 306 } |
311 | 307 |
312 | 308 |
344 } | 340 } |
345 } | 341 } |
346 | 342 |
347 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | 343 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
348 | 344 |
349 if ((ngx_io.flags & NGX_IO_SENDFILE) == 0 || clcf->sendfile == 0) { | 345 if (!(ngx_io.flags & NGX_IO_SENDFILE) || !clcf->sendfile) { |
350 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; | 346 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; |
351 } | 347 } |
352 | 348 |
353 return NGX_OK; | |
354 } | |
355 | |
356 | |
357 int ngx_http_core_translate_handler(ngx_http_request_t *r) | |
358 { | |
359 char *location, *last; | |
360 ngx_err_t err; | |
361 ngx_table_elt_t *h; | |
362 ngx_http_core_srv_conf_t *cscf; | |
363 ngx_http_core_loc_conf_t *clcf; | |
364 | |
365 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
366 | |
367 if (clcf->handler) { | 349 if (clcf->handler) { |
350 /* | |
351 * if the location already has content handler then skip | |
352 * the translation phase | |
353 */ | |
354 | |
368 r->content_handler = clcf->handler; | 355 r->content_handler = clcf->handler; |
369 return NGX_OK; | 356 r->phase++; |
370 } | 357 } |
371 | |
372 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); | |
373 | |
374 if (r->uri.data[r->uri.len - 1] == '/') { | |
375 if (r->path.data == NULL) { | |
376 ngx_test_null(r->path.data, | |
377 ngx_palloc(r->pool, | |
378 clcf->doc_root.len + r->uri.len), | |
379 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
380 | |
381 ngx_cpystrn(ngx_cpymem(r->path.data, clcf->doc_root.data, | |
382 clcf->doc_root.len), | |
383 r->uri.data, r->uri.len + 1); | |
384 | |
385 } else { | |
386 r->path.data[r->path.len] = '\0'; | |
387 } | |
388 | |
389 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
390 "directory index of \"%s\" is forbidden", r->path.data); | |
391 | |
392 return NGX_HTTP_FORBIDDEN; | |
393 } | |
394 | |
395 /* "+ 2" is for trailing '/' in redirect and '\0' */ | |
396 ngx_test_null(r->file.name.data, | |
397 ngx_palloc(r->pool, clcf->doc_root.len + r->uri.len + 2), | |
398 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
399 | |
400 location = ngx_cpymem(r->file.name.data, clcf->doc_root.data, | |
401 clcf->doc_root.len), | |
402 | |
403 last = ngx_cpystrn(location, r->uri.data, r->uri.len + 1); | |
404 | |
405 ngx_log_debug(r->connection->log, "HTTP filename: '%s'" _ r->file.name.data); | |
406 | |
407 #if (WIN9X) | |
408 | |
409 /* | |
410 * There is no way to open a file or a directory in Win9X with | |
411 * one syscall: Win9X has no FILE_FLAG_BACKUP_SEMANTICS flag. | |
412 * so we need to check its type before the opening | |
413 */ | |
414 | |
415 r->file.info.dwFileAttributes = GetFileAttributes(r->file.name.data); | |
416 if (r->file.info.dwFileAttributes == INVALID_FILE_ATTRIBUTES) { | |
417 err = ngx_errno; | |
418 ngx_log_error(NGX_LOG_ERR, r->connection->log, err, | |
419 ngx_file_type_n " \"%s\" failed", r->file.name.data); | |
420 | |
421 if (err == NGX_ENOENT || err == NGX_ENOTDIR) { | |
422 return NGX_HTTP_NOT_FOUND; | |
423 | |
424 } else if (err == NGX_EACCES) { | |
425 return NGX_HTTP_FORBIDDEN; | |
426 | |
427 } else { | |
428 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
429 } | |
430 } | |
431 | |
432 #else | |
433 | |
434 if (r->file.fd == NGX_INVALID_FILE) { | |
435 r->file.fd = ngx_open_file(r->file.name.data, | |
436 NGX_FILE_RDONLY, NGX_FILE_OPEN); | |
437 } | |
438 | |
439 if (r->file.fd == NGX_INVALID_FILE) { | |
440 err = ngx_errno; | |
441 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, | |
442 ngx_open_file_n " \"%s\" failed", r->file.name.data); | |
443 | |
444 if (err == NGX_ENOENT || err == NGX_ENOTDIR) { | |
445 return NGX_HTTP_NOT_FOUND; | |
446 | |
447 } else if (err == NGX_EACCES) { | |
448 return NGX_HTTP_FORBIDDEN; | |
449 | |
450 } else { | |
451 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
452 } | |
453 } | |
454 | |
455 ngx_log_debug(r->connection->log, "FILE: %d" _ r->file.fd); | |
456 | |
457 if (!r->file.info_valid) { | |
458 if (ngx_stat_fd(r->file.fd, &r->file.info) == NGX_FILE_ERROR) { | |
459 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | |
460 ngx_stat_fd_n " \"%s\" failed", r->file.name.data); | |
461 | |
462 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) { | |
463 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
464 ngx_close_file_n " \"%s\" failed", | |
465 r->file.name.data); | |
466 } | |
467 | |
468 r->file.fd = NGX_INVALID_FILE; | |
469 | |
470 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
471 } | |
472 | |
473 r->file.info_valid = 1; | |
474 } | |
475 #endif | |
476 | |
477 if (ngx_is_dir(r->file.info)) { | |
478 ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ r->file.name.data); | |
479 | |
480 #if !(WIN9X) | |
481 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) { | |
482 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
483 ngx_close_file_n " \"%s\" failed", r->file.name.data); | |
484 } | |
485 | |
486 r->file.fd = NGX_INVALID_FILE; | |
487 #endif | |
488 | |
489 /* BROKEN: need to include server name */ | |
490 | |
491 ngx_test_null(h, ngx_push_table(r->headers_out.headers), | |
492 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
493 | |
494 *last++ = '/'; | |
495 *last = '\0'; | |
496 h->key.len = 8; | |
497 h->key.data = "Location" ; | |
498 h->value.len = last - location; | |
499 h->value.data = location; | |
500 r->headers_out.location = h; | |
501 | |
502 return NGX_HTTP_MOVED_PERMANENTLY; | |
503 } | |
504 | |
505 r->content_handler = ngx_http_static_handler; | |
506 | 358 |
507 return NGX_OK; | 359 return NGX_OK; |
508 } | 360 } |
509 | 361 |
510 | 362 |
607 #endif | 459 #endif |
608 | 460 |
609 | 461 |
610 static int ngx_http_core_init(ngx_cycle_t *cycle) | 462 static int ngx_http_core_init(ngx_cycle_t *cycle) |
611 { | 463 { |
464 #if 0 | |
612 ngx_http_handler_pt *h; | 465 ngx_http_handler_pt *h; |
466 #endif | |
613 ngx_http_conf_ctx_t *ctx; | 467 ngx_http_conf_ctx_t *ctx; |
614 ngx_http_core_main_conf_t *cmcf; | 468 ngx_http_core_main_conf_t *cmcf; |
615 | 469 |
616 ctx = (ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index]; | 470 ctx = (ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index]; |
617 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index]; | 471 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index]; |
618 | |
619 ngx_test_null(h, ngx_push_array( | |
620 &cmcf->phases[NGX_HTTP_TRANSLATE_PHASE].handlers), | |
621 NGX_ERROR); | |
622 *h = ngx_http_core_translate_handler; | |
623 | 472 |
624 #if 0 | 473 #if 0 |
625 ngx_test_null(h, ngx_push_array( | 474 ngx_test_null(h, ngx_push_array( |
626 &cmcf->phases[NGX_HTTP_TRANSLATE_PHASE].handlers), | 475 &cmcf->phases[NGX_HTTP_TRANSLATE_PHASE].handlers), |
627 NGX_ERROR); | 476 NGX_ERROR); |