Mercurial > hg > nginx
comparison src/http/modules/ngx_http_index_handler.c @ 99:a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 02 Jun 2003 15:24:30 +0000 |
parents | c9b243802a17 |
children | 6dfda4cf5200 |
comparison
equal
deleted
inserted
replaced
98:c9b243802a17 | 99:a059e1aa65d4 |
---|---|
1 | 1 |
2 #include <ngx_http_index_handler.h> | 2 #include <ngx_config.h> |
3 #include <ngx_core.h> | |
4 #include <ngx_http.h> | |
5 | |
6 | |
7 typedef struct { | |
8 ngx_array_t indices; | |
9 size_t max_index_len; | |
10 } ngx_http_index_conf_t; | |
11 | |
12 | |
13 #define NGX_HTTP_DEFAULT_INDEX "index.html" | |
3 | 14 |
4 | 15 |
5 static int ngx_http_index_test_dir(ngx_http_request_t *r); | 16 static int ngx_http_index_test_dir(ngx_http_request_t *r); |
6 static int ngx_http_index_init(ngx_pool_t *pool); | 17 static int ngx_http_index_init(ngx_pool_t *pool); |
7 static void *ngx_http_index_create_conf(ngx_pool_t *pool); | 18 static void *ngx_http_index_create_conf(ngx_pool_t *pool); |
12 | 23 |
13 | 24 |
14 static ngx_command_t ngx_http_index_commands[] = { | 25 static ngx_command_t ngx_http_index_commands[] = { |
15 | 26 |
16 {ngx_string("index"), | 27 {ngx_string("index"), |
17 NGX_HTTP_LOC_CONF|NGX_CONF_ANY1, | 28 NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, |
18 ngx_http_index_set_index, | 29 ngx_http_index_set_index, |
19 NGX_HTTP_LOC_CONF_OFFSET, | 30 NGX_HTTP_LOC_CONF_OFFSET, |
20 0, | 31 0, |
21 NULL}, | 32 NULL}, |
22 | 33 |
44 ngx_http_index_init /* init module */ | 55 ngx_http_index_init /* init module */ |
45 }; | 56 }; |
46 | 57 |
47 | 58 |
48 /* | 59 /* |
49 Try to open first index file before the test of the directory existence | 60 Try to open the first index file before the directory existence test |
50 because the valid requests should be many more then invalid ones. | 61 because the valid requests should be many more than invalid ones. |
51 If open() failed then stat() should be more quickly because some data | 62 If open() failed then stat() should be more quickly because some data |
52 is already cached in the kernel. | 63 is already cached in the kernel. |
53 Besides Win32 has ERROR_PATH_NOT_FOUND (NGX_ENOTDIR). | 64 Besides Win32 has ERROR_PATH_NOT_FOUND (NGX_ENOTDIR). |
54 Unix has ENOTDIR error, although it less helpfull - it shows only | 65 Unix has ENOTDIR error, although it less helpfull - it shows only |
55 that path contains the usual file in place of the directory. | 66 that path contains the usual file in place of the directory. |
56 */ | 67 */ |
57 | 68 |
58 int ngx_http_index_handler(ngx_http_request_t *r) | 69 int ngx_http_index_handler(ngx_http_request_t *r) |
59 { | 70 { |
60 int i, rc, test_dir; | 71 int i, rc, test_dir, path_not_found; |
61 char *name, *file; | 72 char *name, *file; |
62 ngx_str_t redirect, *index; | 73 ngx_str_t redirect, *index; |
63 ngx_err_t err; | 74 ngx_err_t err; |
64 ngx_fd_t fd; | 75 ngx_fd_t fd; |
65 ngx_http_index_conf_t *icf; | 76 ngx_http_index_conf_t *icf; |
78 clcf->doc_root.len); | 89 clcf->doc_root.len); |
79 file = ngx_cpystrn(redirect.data, r->uri.data, r->uri.len + 1); | 90 file = ngx_cpystrn(redirect.data, r->uri.data, r->uri.len + 1); |
80 r->path.len = file - r->path.data; | 91 r->path.len = file - r->path.data; |
81 | 92 |
82 test_dir = 1; | 93 test_dir = 1; |
94 path_not_found = 1; | |
83 | 95 |
84 index = icf->indices.elts; | 96 index = icf->indices.elts; |
85 for (i = 0; i < icf->indices.nelts; i++) { | 97 for (i = 0; i < icf->indices.nelts; i++) { |
86 | 98 |
87 if (index[i].data[0] != '/') { | 99 if (index[i].data[0] != '/') { |
90 | 102 |
91 } else { | 103 } else { |
92 name = index[i].data; | 104 name = index[i].data; |
93 } | 105 } |
94 | 106 |
95 fd = ngx_open_file(name, NGX_FILE_RDONLY); | 107 fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN); |
96 if (fd == NGX_INVALID_FILE) { | 108 if (fd == NGX_INVALID_FILE) { |
97 err = ngx_errno; | 109 err = ngx_errno; |
98 | 110 |
99 ngx_log_error(NGX_LOG_DEBUG, r->connection->log, err, | 111 ngx_log_error(NGX_LOG_DEBUG, r->connection->log, err, |
100 "DEBUG: " ngx_open_file_n " %s failed", name); | 112 "DEBUG: " ngx_open_file_n " %s failed", name); |
101 | 113 |
102 if (err == NGX_ENOTDIR) { | 114 if (err == NGX_ENOTDIR) { |
103 r->path_not_found = 1; | 115 path_not_found = 1; |
104 | 116 |
105 } else if (err == NGX_EACCES) { | 117 } else if (err == NGX_EACCES) { |
106 r->path_err = err; | 118 r->path_err = err; |
107 return NGX_HTTP_FORBIDDEN; | 119 return NGX_HTTP_FORBIDDEN; |
108 } | 120 } |
109 | 121 |
110 if (test_dir) { | 122 if (test_dir) { |
111 if (r->path_not_found) { | 123 if (path_not_found) { |
112 r->path_err = err; | 124 r->path_err = err; |
113 return NGX_HTTP_NOT_FOUND; | 125 return NGX_HTTP_NOT_FOUND; |
114 } | 126 } |
115 | 127 |
116 rc = ngx_http_index_test_dir(r); | 128 rc = ngx_http_index_test_dir(r); |