comparison src/http/modules/ngx_http_index_handler.c @ 47:d81326c3b21b

nginx-0.0.1-2003-01-15-10:02:27 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 15 Jan 2003 07:02:27 +0000
parents f84a648211f4
children 5365b189a97e
comparison
equal deleted inserted replaced
46:f84a648211f4 47:d81326c3b21b
60 ngx_http_index_init /* init module */ 60 ngx_http_index_init /* init module */
61 }; 61 };
62 62
63 63
64 /* 64 /*
65 If the first index file is local (i.e. 'index.html', not '/index.html') then 65 Try to open first index file before the test of the directory existence
66 try to open it before the test of the directory existence because 66 because the valid requests should be many more then invalid ones.
67 the valid requests should be many more then invalid ones. If open() 67 If open() is failed then stat() should be more quickly because some data
68 is failed then stat() should be more quickly because some data
69 is already cached in the kernel. Besides Win32 has ERROR_PATH_NOT_FOUND 68 is already cached in the kernel. Besides Win32 has ERROR_PATH_NOT_FOUND
70 and Unix has ENOTDIR error (although it less helpfull). 69 and Unix has ENOTDIR error (although it less helpfull).
71 */ 70 */
72 71
73 int ngx_http_index_handler(ngx_http_request_t *r) 72 int ngx_http_index_handler(ngx_http_request_t *r)
96 loc.data = ngx_cpystrn(r->path.data, core_cf->doc_root.data, 95 loc.data = ngx_cpystrn(r->path.data, core_cf->doc_root.data,
97 core_cf->doc_root.len + 1); 96 core_cf->doc_root.len + 1);
98 file = ngx_cpystrn(loc.data, r->uri.data, r->uri.len + 1); 97 file = ngx_cpystrn(loc.data, r->uri.data, r->uri.len + 1);
99 r->path.len = file - r->path.data; 98 r->path.len = file - r->path.data;
100 99
101 if (cf->test_dir) { 100 test_dir = 1;
102 rc = ngx_http_index_test_dir(r);
103 if (rc != NGX_OK) {
104 return rc;
105 }
106
107 test_dir = 0;
108
109 } else {
110 test_dir = 1;
111 }
112 101
113 index = (ngx_str_t *) cf->indices->elts; 102 index = (ngx_str_t *) cf->indices->elts;
114 for (i = 0; i < cf->indices->nelts; i++) { 103 for (i = 0; i < cf->indices->nelts; i++) {
115 104
116 if (index[i].data[0] != '/') { 105 if (index[i].data[0] != '/') {
123 112
124 fd = ngx_open_file(name, NGX_FILE_RDONLY); 113 fd = ngx_open_file(name, NGX_FILE_RDONLY);
125 if (fd == NGX_INVALID_FILE) { 114 if (fd == NGX_INVALID_FILE) {
126 err = ngx_errno; 115 err = ngx_errno;
127 116
128 ngx_log_error(NGX_LOG_ERR, r->connection->log, err, 117 ngx_log_error(NGX_LOG_DEBUG, r->connection->log, err,
129 ngx_open_file_n " %s failed", name); 118 "DEBUG: " ngx_open_file_n " %s failed", name);
130 119
131 #if (WIN32) 120 #if (WIN32)
132 if (err == ERROR_PATH_NOT_FOUND) { 121 if (err == ERROR_PATH_NOT_FOUND) {
133 #else 122 #else
134 if (err == NGX_ENOTDIR) { 123 if (err == NGX_ENOTDIR) {
135 #endif 124 #endif
136 r->path_not_found = 1; 125 r->path_not_found = 1;
126
127 } else if (err == NGX_EACCES) {
128 r->path_err = err;
129 return NGX_HTTP_FORBIDDEN;
137 } 130 }
138 131
139 if (test_dir) { 132 if (test_dir) {
140 if (r->path_not_found) { 133 if (r->path_not_found) {
134 r->path_err = err;
141 return NGX_HTTP_NOT_FOUND; 135 return NGX_HTTP_NOT_FOUND;
142 } 136 }
143 137
144 rc = ngx_http_index_test_dir(r); 138 rc = ngx_http_index_test_dir(r);
145 if (rc != NGX_OK) { 139 if (rc != NGX_OK) {
146 return rc; 140 return rc;
147 } 141 }
148 142
149 test_dir = 0; 143 test_dir = 0;
150
151 if (r->path_not_found) {
152 continue;
153 }
154 } 144 }
155 145
156 if (err == NGX_ENOENT) { 146 if (err == NGX_ENOENT) {
157 continue; 147 continue;
158 } 148 }
184 } 174 }
185 175
186 176
187 static int ngx_http_index_test_dir(ngx_http_request_t *r) 177 static int ngx_http_index_test_dir(ngx_http_request_t *r)
188 { 178 {
189 ngx_err_t err;
190
191 r->path.data[r->path.len - 1] = '\0'; 179 r->path.data[r->path.len - 1] = '\0';
180 r->path.data[r->path.len] = '\0';
192 181
193 ngx_log_debug(r->connection->log, "IS_DIR: %s" _ r->path.data); 182 ngx_log_debug(r->connection->log, "IS_DIR: %s" _ r->path.data);
194 183
184 #if 0
185 if (r->path_err == NGX_EACCES) {
186 return NGX_HTTP_FORBIDDEN;
187 }
188 #endif
189
195 if (ngx_file_type(r->path.data, &r->file.info) == -1) { 190 if (ngx_file_type(r->path.data, &r->file.info) == -1) {
196 err = ngx_errno; 191
197 if (err == NGX_ENOENT) { 192 r->path_err = ngx_errno;
193
194 if (r->path_err == NGX_ENOENT) {
195 r->path.data[r->path.len - 1] = '/';
198 return NGX_HTTP_NOT_FOUND; 196 return NGX_HTTP_NOT_FOUND;
199 } 197 }
200 198
201 if (err == NGX_EACCESS) { 199 ngx_log_error(NGX_LOG_CRIT, r->connection->log, r->path_err,
202 return NGX_HTTP_FORBIDDEN; 200 "ngx_http_index_test_dir: "
203 } 201 ngx_file_type_n " %s failed", r->path.data);
204
205 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
206 "ngx_http_index_is_dir: "
207 "stat() %s failed", r->path.data);
208 202
209 return NGX_HTTP_INTERNAL_SERVER_ERROR; 203 return NGX_HTTP_INTERNAL_SERVER_ERROR;
210 } 204 }
211 205
206 r->path.data[r->path.len - 1] = '/';
207
212 if (ngx_is_dir(r->file.info)) { 208 if (ngx_is_dir(r->file.info)) {
213 r->path.data[r->path.len - 1] = '/';
214 return NGX_OK; 209 return NGX_OK;
215 210
216 } else { 211 } else {
217 return NGX_HTTP_NOT_FOUND; 212 return NGX_HTTP_NOT_FOUND;
218 } 213 }
258 index->len = sizeof(NGX_HTTP_INDEX) - 1; 253 index->len = sizeof(NGX_HTTP_INDEX) - 1;
259 index->data = NGX_HTTP_INDEX; 254 index->data = NGX_HTTP_INDEX;
260 conf->max_index_len = sizeof(NGX_HTTP_INDEX); 255 conf->max_index_len = sizeof(NGX_HTTP_INDEX);
261 } 256 }
262 257
263 /* TODO: set conf->test_dir if first index is started with '/' */ 258 /* FAIL: if first index is started with '/' */
264 259
265 return NULL; 260 return NULL;
266 } 261 }
267 262
268 263