936ec8db9bd842ef668df815168e2a6b4008bbb9
[nginx.git] / nginx / src / http / modules / ngx_http_index_module.c
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 {
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 */
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,
29     ngx_http_core_loc_conf_t *clcf, u_char *path, u_char *last);
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);
32
33 static ngx_int_t ngx_http_index_init(ngx_conf_t *cf);
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,
36     void *parent, void *child);
37 static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
38     void *conf);
39
40
41 static ngx_command_t  ngx_http_index_commands[] = {
42
43     { ngx_string("index"),
44       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
45       ngx_http_index_set_index,
46       NGX_HTTP_LOC_CONF_OFFSET,
47       0,
48       NULL },
49
50       ngx_null_command
51 };
52
53
54 static ngx_http_module_t  ngx_http_index_module_ctx = {
55     NULL,                                  /* preconfiguration */
56     ngx_http_index_init,                   /* postconfiguration */
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 = {
70     NGX_MODULE_V1,
71     &ngx_http_index_module_ctx,            /* module context */
72     ngx_http_index_commands,               /* module directives */
73     NGX_HTTP_MODULE,                       /* module type */
74     NULL,                                  /* init master */
75     NULL,                                  /* init module */
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
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
95 static ngx_int_t
96 ngx_http_index_handler(ngx_http_request_t *r)
97 {
98     u_char                       *p, *name;
99     size_t                        len, nlen, root, allocated;
100     ngx_int_t                     rc;
101     ngx_str_t                     path, uri;
102     ngx_uint_t                    i, dir_tested;
103     ngx_http_index_t             *index;
104     ngx_open_file_info_t          of;
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;
110
111     if (r->uri.data[r->uri.len - 1] != '/') {
112         return NGX_DECLINED;
113     }
114
115     if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
116         return NGX_DECLINED;
117     }
118
119     /* TODO: Win32 */
120     if (r->zero_in_uri) {
121         return NGX_DECLINED;
122     }
123
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
127     allocated = 0;
128     root = 0;
129     dir_tested = 0;
130     name = NULL;
131     path.data = NULL;
132
133     index = ilcf->indices->elts;
134     for (i = 0; i < ilcf->indices->nelts; i++) {
135
136         if (index[i].lengths == NULL) {
137
138             if (index[i].name.data[0] == '/') {
139                 return ngx_http_internal_redirect(r, &index[i].name, &r->args);
140             }
141
142             len = ilcf->max_index_len;
143             nlen = index[i].name.len;
144
145         } else {
146             ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
147
148             e.ip = index[i].lengths->elts;
149             e.request = r;
150             e.flushed = 1;
151
152             /* 1 byte for terminating '\0' */
153
154             len = 1;
155
156             while (*(uintptr_t *) e.ip) {
157                 lcode = *(ngx_http_script_len_code_pt *) e.ip;
158                 len += lcode(&e);
159             }
160
161             nlen = len;
162
163             /* 16 bytes are preallocation */
164
165             len += 16;
166         }
167
168         if (len > (size_t) (path.data + allocated - name)) {
169
170             name = ngx_http_map_uri_to_path(r, &path, &root, len);
171             if (name == NULL) {
172                 return NGX_ERROR;
173             }
174
175             allocated = path.len;
176         }
177
178         if (index[i].values == NULL) {
179
180             /* index[i].name.len includes the terminating '\0' */
181
182             ngx_memcpy(name, index[i].name.data, index[i].name.len);
183
184             path.len = (name + index[i].name.len - 1) - path.data;
185
186         } else {
187             e.ip = index[i].values->elts;
188             e.pos = name;
189
190             while (*(uintptr_t *) e.ip) {
191                 code = *(ngx_http_script_code_pt *) e.ip;
192                 code((ngx_http_script_engine_t *) &e);
193             }
194
195             if (*name == '/') {
196                 uri.len = nlen - 1;
197                 uri.data = name;
198                 return ngx_http_internal_redirect(r, &uri, &r->args);
199             }
200
201             path.len = e.pos - path.data;
202
203             *e.pos++ = '\0';
204         }
205
206         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
207                        "open index \"%V\"", &path);
208
209         ngx_memzero(&of, sizeof(ngx_open_file_info_t));
210
211         of.directio = clcf->directio;
212         of.valid = clcf->open_file_cache_valid;
213         of.min_uses = clcf->open_file_cache_min_uses;
214         of.errors = clcf->open_file_cache_errors;
215         of.events = clcf->open_file_cache_events;
216
217         if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
218             != NGX_OK)
219         {
220             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, of.err,
221                            ngx_open_file_n " \"%s\" failed", path.data);
222
223             if (of.err == 0) {
224                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
225             }
226
227             if (of.err == NGX_ENOTDIR || of.err == NGX_EACCES) {
228                 return ngx_http_index_error(r, clcf, path.data, of.err);
229             }
230
231             if (!dir_tested) {
232                 rc = ngx_http_index_test_dir(r, clcf, path.data, name - 1);
233
234                 if (rc != NGX_OK) {
235                     return rc;
236                 }
237
238                 dir_tested = 1;
239             }
240
241             if (of.err == NGX_ENOENT) {
242                 continue;
243             }
244
245             ngx_log_error(NGX_LOG_ERR, r->connection->log, of.err,
246                           ngx_open_file_n " \"%s\" failed", path.data);
247
248             return NGX_HTTP_INTERNAL_SERVER_ERROR;
249         }
250
251         uri.len = r->uri.len + nlen - 1;
252
253         if (!clcf->alias) {
254             uri.data = path.data + root;
255
256         } else {
257             uri.data = ngx_pnalloc(r->pool, uri.len);
258             if (uri.data == NULL) {
259                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
260             }
261
262             p = ngx_copy(uri.data, r->uri.data, r->uri.len);
263             ngx_memcpy(p, name, nlen - 1);
264         }
265
266         return ngx_http_internal_redirect(r, &uri, &r->args);
267     }
268
269     return NGX_DECLINED;
270 }
271
272
273 static ngx_int_t
274 ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_core_loc_conf_t *clcf,
275     u_char *path, u_char *last)
276 {
277     u_char                c;
278     ngx_str_t             dir;
279     ngx_open_file_info_t  of;
280
281     c = *last;
282     if (c != '/' || path == last) {
283         /* "alias" without trailing slash */
284         c = *(++last);
285     }
286     *last = '\0';
287
288     dir.len = last - path;
289     dir.data = path;
290
291     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
292                    "http index check dir: \"%V\"", &dir);
293
294     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
295
296     of.test_dir = 1;
297     of.valid = clcf->open_file_cache_valid;
298     of.errors = clcf->open_file_cache_errors;
299
300     if (ngx_open_cached_file(clcf->open_file_cache, &dir, &of, r->pool)
301         != NGX_OK)
302     {
303         if (of.err) {
304
305             if (of.err == NGX_ENOENT) {
306                 *last = c;
307                 return ngx_http_index_error(r, clcf, dir.data, NGX_ENOENT);
308             }
309
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
323             ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
324                           ngx_open_file_n " \"%s\" failed", dir.data);
325         }
326
327         return NGX_HTTP_INTERNAL_SERVER_ERROR;
328     }
329
330     *last = c;
331
332     if (of.is_dir) {
333         return NGX_OK;
334     }
335
336     ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
337                   "\"%s\" is not a directory", dir.data);
338
339     return NGX_HTTP_INTERNAL_SERVER_ERROR;
340 }
341
342
343 static ngx_int_t
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)
346 {
347     if (err == NGX_EACCES) {
348         ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
349                       "\"%s\" is forbidden", file);
350
351         return NGX_HTTP_FORBIDDEN;
352     }
353
354     if (clcf->log_not_found) {
355         ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
356                       "\"%s\" is not found", file);
357     }
358
359     return NGX_HTTP_NOT_FOUND;
360 }
361
362
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;
374     conf->max_index_len = 0;
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
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
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
444     ngx_str_t                  *value;
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++) {
459
460         if (value[i].data[0] == '/' && i != cf->args->nelts - 1) {
461             ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
462                                "only the last index in \"index\" directive "
463                                "should be absolute");
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) {
486             if (ilcf->max_index_len < index->name.len) {
487                 ilcf->max_index_len = index->name.len;
488             }
489
490             if (index->name.data[0] == '/') {
491                 continue;
492             }
493
494             /* include the terminating '\0' to the length to use ngx_copy() */
495             index->name.len++;
496
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 }