50
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 typedef struct {
|
58
|
13 ngx_str_t name;
|
|
14 ngx_array_t *lengths;
|
|
15 ngx_array_t *values;
|
|
16 } ngx_http_index_t;
|
|
17
|
|
18
|
|
19 typedef struct {
|
|
20 ngx_array_t *indices; /* array of ngx_http_index_t */
|
50
|
21 size_t max_index_len;
|
|
22 } ngx_http_index_loc_conf_t;
|
|
23
|
|
24
|
|
25 #define NGX_HTTP_DEFAULT_INDEX "index.html"
|
|
26
|
|
27
|
|
28 static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r,
|
344
|
29 ngx_http_core_loc_conf_t *clcf, u_char *path, u_char *last);
|
394
|
30 static ngx_int_t ngx_http_index_error(ngx_http_request_t *r,
|
|
31 ngx_http_core_loc_conf_t *clcf, u_char *file, ngx_err_t err);
|
50
|
32
|
230
|
33 static ngx_int_t ngx_http_index_init(ngx_conf_t *cf);
|
50
|
34 static void *ngx_http_index_create_loc_conf(ngx_conf_t *cf);
|
|
35 static char *ngx_http_index_merge_loc_conf(ngx_conf_t *cf,
|
58
|
36 void *parent, void *child);
|
50
|
37 static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
|
58
|
38 void *conf);
|
50
|
39
|
|
40
|
|
41 static ngx_command_t ngx_http_index_commands[] = {
|
|
42
|
|
43 { ngx_string("index"),
|
58
|
44 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
50
|
45 ngx_http_index_set_index,
|
|
46 NGX_HTTP_LOC_CONF_OFFSET,
|
|
47 0,
|
|
48 NULL },
|
|
49
|
|
50 ngx_null_command
|
|
51 };
|
|
52
|
|
53
|
216
|
54 static ngx_http_module_t ngx_http_index_module_ctx = {
|
58
|
55 NULL, /* preconfiguration */
|
230
|
56 ngx_http_index_init, /* postconfiguration */
|
50
|
57
|
|
58 NULL, /* create main configuration */
|
|
59 NULL, /* init main configuration */
|
|
60
|
|
61 NULL, /* create server configuration */
|
|
62 NULL, /* merge server configuration */
|
|
63
|
|
64 ngx_http_index_create_loc_conf, /* create location configration */
|
|
65 ngx_http_index_merge_loc_conf /* merge location configration */
|
|
66 };
|
|
67
|
|
68
|
|
69 ngx_module_t ngx_http_index_module = {
|
58
|
70 NGX_MODULE_V1,
|
50
|
71 &ngx_http_index_module_ctx, /* module context */
|
|
72 ngx_http_index_commands, /* module directives */
|
|
73 NGX_HTTP_MODULE, /* module type */
|
90
|
74 NULL, /* init master */
|
230
|
75 NULL, /* init module */
|
90
|
76 NULL, /* init process */
|
|
77 NULL, /* init thread */
|
|
78 NULL, /* exit thread */
|
|
79 NULL, /* exit process */
|
|
80 NULL, /* exit master */
|
|
81 NGX_MODULE_V1_PADDING
|
50
|
82 };
|
|
83
|
|
84
|
|
85 /*
|
|
86 * Try to open the first index file before the test of the directory existence
|
|
87 * because the valid requests should be many more than invalid ones.
|
|
88 * If open() would fail, then stat() should be more quickly because some data
|
|
89 * is already cached in the kernel.
|
|
90 * Besides, Win32 has ERROR_PATH_NOT_FOUND (NGX_ENOTDIR).
|
|
91 * Unix has ENOTDIR error, although it less helpfull - it points only
|
|
92 * that path contains the usual file in place of the directory.
|
|
93 */
|
|
94
|
58
|
95 static ngx_int_t
|
|
96 ngx_http_index_handler(ngx_http_request_t *r)
|
50
|
97 {
|
344
|
98 u_char *p, *name;
|
|
99 size_t len, nlen, root, allocated;
|
58
|
100 ngx_int_t rc;
|
332
|
101 ngx_str_t path, uri;
|
344
|
102 ngx_uint_t i, dir_tested;
|
58
|
103 ngx_http_index_t *index;
|
332
|
104 ngx_open_file_info_t of;
|
58
|
105 ngx_http_script_code_pt code;
|
|
106 ngx_http_script_engine_t e;
|
|
107 ngx_http_core_loc_conf_t *clcf;
|
|
108 ngx_http_index_loc_conf_t *ilcf;
|
|
109 ngx_http_script_len_code_pt lcode;
|
50
|
110
|
|
111 if (r->uri.data[r->uri.len - 1] != '/') {
|
|
112 return NGX_DECLINED;
|
|
113 }
|
|
114
|
272
|
115 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
|
|
116 return NGX_DECLINED;
|
|
117 }
|
|
118
|
50
|
119 /* TODO: Win32 */
|
|
120 if (r->zero_in_uri) {
|
|
121 return NGX_DECLINED;
|
|
122 }
|
|
123
|
332
|
124 ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
|
|
125 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
126
|
344
|
127 allocated = 0;
|
|
128 root = 0;
|
|
129 dir_tested = 0;
|
|
130 name = NULL;
|
|
131 path.data = NULL;
|
50
|
132
|
58
|
133 index = ilcf->indices->elts;
|
344
|
134 for (i = 0; i < ilcf->indices->nelts; i++) {
|
58
|
135
|
|
136 if (index[i].lengths == NULL) {
|
50
|
137
|
58
|
138 if (index[i].name.data[0] == '/') {
|
|
139 return ngx_http_internal_redirect(r, &index[i].name, &r->args);
|
|
140 }
|
50
|
141
|
58
|
142 len = ilcf->max_index_len;
|
344
|
143 nlen = index[i].name.len;
|
50
|
144
|
58
|
145 } else {
|
|
146 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
|
50
|
147
|
58
|
148 e.ip = index[i].lengths->elts;
|
|
149 e.request = r;
|
122
|
150 e.flushed = 1;
|
58
|
151
|
112
|
152 /* 1 byte for terminating '\0' */
|
106
|
153
|
112
|
154 len = 1;
|
50
|
155
|
58
|
156 while (*(uintptr_t *) e.ip) {
|
|
157 lcode = *(ngx_http_script_len_code_pt *) e.ip;
|
|
158 len += lcode(&e);
|
|
159 }
|
50
|
160
|
344
|
161 nlen = len;
|
112
|
162
|
|
163 /* 16 bytes are preallocation */
|
|
164
|
|
165 len += 16;
|
58
|
166 }
|
50
|
167
|
344
|
168 if (len > (size_t) (path.data + allocated - name)) {
|
106
|
169
|
344
|
170 name = ngx_http_map_uri_to_path(r, &path, &root, len);
|
|
171 if (name == NULL) {
|
106
|
172 return NGX_ERROR;
|
50
|
173 }
|
106
|
174
|
344
|
175 allocated = path.len;
|
50
|
176 }
|
|
177
|
58
|
178 if (index[i].values == NULL) {
|
106
|
179
|
|
180 /* index[i].name.len includes the terminating '\0' */
|
|
181
|
344
|
182 ngx_memcpy(name, index[i].name.data, index[i].name.len);
|
50
|
183
|
344
|
184 path.len = (name + index[i].name.len - 1) - path.data;
|
332
|
185
|
58
|
186 } else {
|
|
187 e.ip = index[i].values->elts;
|
344
|
188 e.pos = name;
|
50
|
189
|
58
|
190 while (*(uintptr_t *) e.ip) {
|
|
191 code = *(ngx_http_script_code_pt *) e.ip;
|
|
192 code((ngx_http_script_engine_t *) &e);
|
50
|
193 }
|
|
194
|
344
|
195 if (*name == '/') {
|
|
196 uri.len = nlen - 1;
|
|
197 uri.data = name;
|
|
198 return ngx_http_internal_redirect(r, &uri, &r->args);
|
50
|
199 }
|
|
200
|
332
|
201 path.len = e.pos - path.data;
|
|
202
|
58
|
203 *e.pos++ = '\0';
|
50
|
204 }
|
|
205
|
430
|
206 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
207 "open index \"%V\"", &path);
|
50
|
208
|
384
|
209 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
210
|
390
|
211 of.directio = clcf->directio;
|
358
|
212 of.valid = clcf->open_file_cache_valid;
|
|
213 of.min_uses = clcf->open_file_cache_min_uses;
|
332
|
214 of.errors = clcf->open_file_cache_errors;
|
|
215 of.events = clcf->open_file_cache_events;
|
112
|
216
|
362
|
217 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
|
|
218 != NGX_OK)
|
|
219 {
|
430
|
220 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, of.err,
|
344
|
221 ngx_open_file_n " \"%s\" failed", path.data);
|
50
|
222
|
332
|
223 if (of.err == 0) {
|
|
224 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
225 }
|
50
|
226
|
344
|
227 if (of.err == NGX_ENOTDIR || of.err == NGX_EACCES) {
|
394
|
228 return ngx_http_index_error(r, clcf, path.data, of.err);
|
50
|
229 }
|
|
230
|
344
|
231 if (!dir_tested) {
|
|
232 rc = ngx_http_index_test_dir(r, clcf, path.data, name - 1);
|
50
|
233
|
|
234 if (rc != NGX_OK) {
|
|
235 return rc;
|
|
236 }
|
|
237
|
344
|
238 dir_tested = 1;
|
50
|
239 }
|
|
240
|
332
|
241 if (of.err == NGX_ENOENT) {
|
50
|
242 continue;
|
|
243 }
|
|
244
|
430
|
245 ngx_log_error(NGX_LOG_ERR, r->connection->log, of.err,
|
344
|
246 ngx_open_file_n " \"%s\" failed", path.data);
|
50
|
247
|
|
248 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
249 }
|
|
250
|
344
|
251 uri.len = r->uri.len + nlen - 1;
|
106
|
252
|
|
253 if (!clcf->alias) {
|
344
|
254 uri.data = path.data + root;
|
106
|
255
|
|
256 } else {
|
382
|
257 uri.data = ngx_pnalloc(r->pool, uri.len);
|
106
|
258 if (uri.data == NULL) {
|
|
259 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
260 }
|
|
261
|
344
|
262 p = ngx_copy(uri.data, r->uri.data, r->uri.len);
|
|
263 ngx_memcpy(p, name, nlen - 1);
|
50
|
264 }
|
|
265
|
106
|
266 return ngx_http_internal_redirect(r, &uri, &r->args);
|
50
|
267 }
|
|
268
|
|
269 return NGX_DECLINED;
|
|
270 }
|
|
271
|
|
272
|
58
|
273 static ngx_int_t
|
332
|
274 ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_core_loc_conf_t *clcf,
|
344
|
275 u_char *path, u_char *last)
|
58
|
276 {
|
332
|
277 u_char c;
|
344
|
278 ngx_str_t dir;
|
332
|
279 ngx_open_file_info_t of;
|
58
|
280
|
344
|
281 c = *last;
|
346
|
282 if (c != '/' || path == last) {
|
344
|
283 /* "alias" without trailing slash */
|
|
284 c = *(++last);
|
|
285 }
|
|
286 *last = '\0';
|
50
|
287
|
344
|
288 dir.len = last - path;
|
|
289 dir.data = path;
|
50
|
290
|
332
|
291 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
344
|
292 "http index check dir: \"%V\"", &dir);
|
50
|
293
|
384
|
294 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
295
|
332
|
296 of.test_dir = 1;
|
358
|
297 of.valid = clcf->open_file_cache_valid;
|
332
|
298 of.errors = clcf->open_file_cache_errors;
|
50
|
299
|
344
|
300 if (ngx_open_cached_file(clcf->open_file_cache, &dir, &of, r->pool)
|
332
|
301 != NGX_OK)
|
|
302 {
|
|
303 if (of.err) {
|
|
304
|
|
305 if (of.err == NGX_ENOENT) {
|
344
|
306 *last = c;
|
394
|
307 return ngx_http_index_error(r, clcf, dir.data, NGX_ENOENT);
|
332
|
308 }
|
|
309
|
398
|
310 if (of.err == NGX_EACCES) {
|
|
311
|
|
312 *last = c;
|
|
313
|
|
314 /*
|
|
315 * ngx_http_index_test_dir() is called after the first index
|
|
316 * file testing has returned an error distinct from NGX_EACCES.
|
|
317 * This means that directory searching is allowed.
|
|
318 */
|
|
319
|
|
320 return NGX_OK;
|
|
321 }
|
|
322
|
332
|
323 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
|
344
|
324 ngx_open_file_n " \"%s\" failed", dir.data);
|
50
|
325 }
|
|
326
|
|
327 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
328 }
|
|
329
|
344
|
330 *last = c;
|
50
|
331
|
332
|
332 if (of.is_dir) {
|
50
|
333 return NGX_OK;
|
|
334 }
|
|
335
|
58
|
336 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
344
|
337 "\"%s\" is not a directory", dir.data);
|
58
|
338
|
|
339 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
50
|
340 }
|
|
341
|
|
342
|
58
|
343 static ngx_int_t
|
394
|
344 ngx_http_index_error(ngx_http_request_t *r, ngx_http_core_loc_conf_t *clcf,
|
|
345 u_char *file, ngx_err_t err)
|
50
|
346 {
|
|
347 if (err == NGX_EACCES) {
|
|
348 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
344
|
349 "\"%s\" is forbidden", file);
|
126
|
350
|
50
|
351 return NGX_HTTP_FORBIDDEN;
|
|
352 }
|
|
353
|
394
|
354 if (clcf->log_not_found) {
|
|
355 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
|
356 "\"%s\" is not found", file);
|
|
357 }
|
58
|
358
|
50
|
359 return NGX_HTTP_NOT_FOUND;
|
|
360 }
|
|
361
|
|
362
|
58
|
363 static void *
|
|
364 ngx_http_index_create_loc_conf(ngx_conf_t *cf)
|
|
365 {
|
|
366 ngx_http_index_loc_conf_t *conf;
|
|
367
|
|
368 conf = ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t));
|
|
369 if (conf == NULL) {
|
|
370 return NGX_CONF_ERROR;
|
|
371 }
|
|
372
|
|
373 conf->indices = NULL;
|
230
|
374 conf->max_index_len = 0;
|
58
|
375
|
|
376 return conf;
|
|
377 }
|
|
378
|
|
379
|
|
380 static char *
|
|
381 ngx_http_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
382 {
|
|
383 ngx_http_index_loc_conf_t *prev = parent;
|
|
384 ngx_http_index_loc_conf_t *conf = child;
|
|
385
|
|
386 ngx_http_index_t *index;
|
|
387
|
|
388 if (conf->indices == NULL) {
|
|
389 conf->indices = prev->indices;
|
|
390 conf->max_index_len = prev->max_index_len;
|
|
391 }
|
|
392
|
|
393 if (conf->indices == NULL) {
|
|
394 conf->indices = ngx_array_create(cf->pool, 1, sizeof(ngx_http_index_t));
|
|
395 if (conf->indices == NULL) {
|
|
396 return NGX_CONF_ERROR;
|
|
397 }
|
|
398
|
|
399 index = ngx_array_push(conf->indices);
|
|
400 if (index == NULL) {
|
|
401 return NGX_CONF_ERROR;
|
|
402 }
|
|
403
|
|
404 index->name.len = sizeof(NGX_HTTP_DEFAULT_INDEX);
|
|
405 index->name.data = (u_char *) NGX_HTTP_DEFAULT_INDEX;
|
|
406 index->lengths = NULL;
|
|
407 index->values = NULL;
|
|
408
|
|
409 conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX);
|
|
410
|
|
411 return NGX_CONF_OK;
|
|
412 }
|
|
413
|
|
414 return NGX_CONF_OK;
|
|
415 }
|
|
416
|
|
417
|
230
|
418 static ngx_int_t
|
|
419 ngx_http_index_init(ngx_conf_t *cf)
|
|
420 {
|
|
421 ngx_http_handler_pt *h;
|
|
422 ngx_http_core_main_conf_t *cmcf;
|
|
423
|
|
424 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
425
|
|
426 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
427 if (h == NULL) {
|
|
428 return NGX_ERROR;
|
|
429 }
|
|
430
|
|
431 *h = ngx_http_index_handler;
|
|
432
|
|
433 return NGX_OK;
|
|
434 }
|
|
435
|
|
436
|
58
|
437 /* TODO: warn about duplicate indices */
|
|
438
|
|
439 static char *
|
|
440 ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
441 {
|
|
442 ngx_http_index_loc_conf_t *ilcf = conf;
|
|
443
|
230
|
444 ngx_str_t *value;
|
58
|
445 ngx_uint_t i, n;
|
|
446 ngx_http_index_t *index;
|
|
447 ngx_http_script_compile_t sc;
|
|
448
|
|
449 if (ilcf->indices == NULL) {
|
|
450 ilcf->indices = ngx_array_create(cf->pool, 2, sizeof(ngx_http_index_t));
|
|
451 if (ilcf->indices == NULL) {
|
|
452 return NGX_CONF_ERROR;
|
|
453 }
|
|
454 }
|
|
455
|
|
456 value = cf->args->elts;
|
|
457
|
|
458 for (i = 1; i < cf->args->nelts; i++) {
|
344
|
459
|
58
|
460 if (value[i].data[0] == '/' && i != cf->args->nelts - 1) {
|
344
|
461 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
58
|
462 "only the last index in \"index\" directive "
|
344
|
463 "should be absolute");
|
58
|
464 }
|
|
465
|
|
466 if (value[i].len == 0) {
|
|
467 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
468 "index \"%V\" in \"index\" directive is invalid",
|
|
469 &value[1]);
|
|
470 return NGX_CONF_ERROR;
|
|
471 }
|
|
472
|
|
473 index = ngx_array_push(ilcf->indices);
|
|
474 if (index == NULL) {
|
|
475 return NGX_CONF_ERROR;
|
|
476 }
|
|
477
|
|
478 index->name.len = value[i].len;
|
|
479 index->name.data = value[i].data;
|
|
480 index->lengths = NULL;
|
|
481 index->values = NULL;
|
|
482
|
|
483 n = ngx_http_script_variables_count(&value[i]);
|
|
484
|
|
485 if (n == 0) {
|
230
|
486 if (ilcf->max_index_len < index->name.len) {
|
58
|
487 ilcf->max_index_len = index->name.len;
|
|
488 }
|
|
489
|
344
|
490 if (index->name.data[0] == '/') {
|
|
491 continue;
|
|
492 }
|
|
493
|
122
|
494 /* include the terminating '\0' to the length to use ngx_copy() */
|
106
|
495 index->name.len++;
|
|
496
|
58
|
497 continue;
|
|
498 }
|
|
499
|
|
500 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
501
|
|
502 sc.cf = cf;
|
|
503 sc.source = &value[i];
|
|
504 sc.lengths = &index->lengths;
|
|
505 sc.values = &index->values;
|
|
506 sc.variables = n;
|
|
507 sc.complete_lengths = 1;
|
|
508 sc.complete_values = 1;
|
|
509
|
|
510 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
511 return NGX_CONF_ERROR;
|
|
512 }
|
|
513 }
|
|
514
|
|
515 return NGX_CONF_OK;
|
|
516 }
|