upstream nginx-0.7.34
[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, root, reserve, 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     /* suppress MSVC warning */
132     path.data = NULL;
133
134     index = ilcf->indices->elts;
135     for (i = 0; i < ilcf->indices->nelts; i++) {
136
137         if (index[i].lengths == NULL) {
138
139             if (index[i].name.data[0] == '/') {
140                 return ngx_http_internal_redirect(r, &index[i].name, &r->args);
141             }
142
143             reserve = ilcf->max_index_len;
144             len = index[i].name.len;
145
146         } else {
147             ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
148
149             e.ip = index[i].lengths->elts;
150             e.request = r;
151             e.flushed = 1;
152
153             /* 1 is for terminating '\0' as in static names */
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             /* 16 bytes are preallocation */
162
163             reserve = len + 16;
164         }
165
166         if (reserve > allocated) {
167
168             name = ngx_http_map_uri_to_path(r, &path, &root, reserve);
169             if (name == NULL) {
170                 return NGX_ERROR;
171             }
172
173             allocated = path.data + path.len - name;
174         }
175
176         if (index[i].values == NULL) {
177
178             /* index[i].name.len includes the terminating '\0' */
179
180             ngx_memcpy(name, index[i].name.data, index[i].name.len);
181
182             path.len = (name + index[i].name.len - 1) - path.data;
183
184         } else {
185             e.ip = index[i].values->elts;
186             e.pos = name;
187
188             while (*(uintptr_t *) e.ip) {
189                 code = *(ngx_http_script_code_pt *) e.ip;
190                 code((ngx_http_script_engine_t *) &e);
191             }
192
193             if (*name == '/') {
194                 uri.len = len - 1;
195                 uri.data = name;
196                 return ngx_http_internal_redirect(r, &uri, &r->args);
197             }
198
199             path.len = e.pos - path.data;
200
201             *e.pos++ = '\0';
202         }
203
204         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
205                        "open index \"%V\"", &path);
206
207         ngx_memzero(&of, sizeof(ngx_open_file_info_t));
208
209         of.directio = clcf->directio;
210         of.valid = clcf->open_file_cache_valid;
211         of.min_uses = clcf->open_file_cache_min_uses;
212         of.errors = clcf->open_file_cache_errors;
213         of.events = clcf->open_file_cache_events;
214
215         if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
216             != NGX_OK)
217         {
218             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, of.err,
219                            ngx_open_file_n " \"%s\" failed", path.data);
220
221             if (of.err == 0) {
222                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
223             }
224
225             if (of.err == NGX_ENOTDIR || of.err == NGX_EACCES) {
226                 return ngx_http_index_error(r, clcf, path.data, of.err);
227             }
228
229             if (!dir_tested) {
230                 rc = ngx_http_index_test_dir(r, clcf, path.data, name - 1);
231
232                 if (rc != NGX_OK) {
233                     return rc;
234                 }
235
236                 dir_tested = 1;
237             }
238
239             if (of.err == NGX_ENOENT) {
240                 continue;
241             }
242
243             ngx_log_error(NGX_LOG_ERR, r->connection->log, of.err,
244                           ngx_open_file_n " \"%s\" failed", path.data);
245
246             return NGX_HTTP_INTERNAL_SERVER_ERROR;
247         }
248
249         uri.len = r->uri.len + len - 1;
250
251         if (!clcf->alias) {
252             uri.data = path.data + root;
253
254         } else {
255             uri.data = ngx_pnalloc(r->pool, uri.len);
256             if (uri.data == NULL) {
257                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
258             }
259
260             p = ngx_copy(uri.data, r->uri.data, r->uri.len);
261             ngx_memcpy(p, name, len - 1);
262         }
263
264         return ngx_http_internal_redirect(r, &uri, &r->args);
265     }
266
267     return NGX_DECLINED;
268 }
269
270
271 static ngx_int_t
272 ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_core_loc_conf_t *clcf,
273     u_char *path, u_char *last)
274 {
275     u_char                c;
276     ngx_str_t             dir;
277     ngx_open_file_info_t  of;
278
279     c = *last;
280     if (c != '/' || path == last) {
281         /* "alias" without trailing slash */
282         c = *(++last);
283     }
284     *last = '\0';
285
286     dir.len = last - path;
287     dir.data = path;
288
289     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
290                    "http index check dir: \"%V\"", &dir);
291
292     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
293
294     of.test_dir = 1;
295     of.valid = clcf->open_file_cache_valid;
296     of.errors = clcf->open_file_cache_errors;
297
298     if (ngx_open_cached_file(clcf->open_file_cache, &dir, &of, r->pool)
299         != NGX_OK)
300     {
301         if (of.err) {
302
303             if (of.err == NGX_ENOENT) {
304                 *last = c;
305                 return ngx_http_index_error(r, clcf, dir.data, NGX_ENOENT);
306             }
307
308             if (of.err == NGX_EACCES) {
309
310                 *last = c;
311
312                 /*
313                  * ngx_http_index_test_dir() is called after the first index
314                  * file testing has returned an error distinct from NGX_EACCES.
315                  * This means that directory searching is allowed.
316                  */
317
318                 return NGX_OK;
319             }
320
321             ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
322                           ngx_open_file_n " \"%s\" failed", dir.data);
323         }
324
325         return NGX_HTTP_INTERNAL_SERVER_ERROR;
326     }
327
328     *last = c;
329
330     if (of.is_dir) {
331         return NGX_OK;
332     }
333
334     ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
335                   "\"%s\" is not a directory", dir.data);
336
337     return NGX_HTTP_INTERNAL_SERVER_ERROR;
338 }
339
340
341 static ngx_int_t
342 ngx_http_index_error(ngx_http_request_t *r, ngx_http_core_loc_conf_t  *clcf,
343     u_char *file, ngx_err_t err)
344 {
345     if (err == NGX_EACCES) {
346         ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
347                       "\"%s\" is forbidden", file);
348
349         return NGX_HTTP_FORBIDDEN;
350     }
351
352     if (clcf->log_not_found) {
353         ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
354                       "\"%s\" is not found", file);
355     }
356
357     return NGX_HTTP_NOT_FOUND;
358 }
359
360
361 static void *
362 ngx_http_index_create_loc_conf(ngx_conf_t *cf)
363 {
364     ngx_http_index_loc_conf_t  *conf;
365
366     conf = ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t));
367     if (conf == NULL) {
368         return NGX_CONF_ERROR;
369     }
370
371     conf->indices = NULL;
372     conf->max_index_len = 0;
373
374     return conf;
375 }
376
377
378 static char *
379 ngx_http_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
380 {
381     ngx_http_index_loc_conf_t  *prev = parent;
382     ngx_http_index_loc_conf_t  *conf = child;
383
384     ngx_http_index_t  *index;
385
386     if (conf->indices == NULL) {
387         conf->indices = prev->indices;
388         conf->max_index_len = prev->max_index_len;
389     }
390
391     if (conf->indices == NULL) {
392         conf->indices = ngx_array_create(cf->pool, 1, sizeof(ngx_http_index_t));
393         if (conf->indices == NULL) {
394             return NGX_CONF_ERROR;
395         }
396
397         index = ngx_array_push(conf->indices);
398         if (index == NULL) {
399             return NGX_CONF_ERROR;
400         }
401
402         index->name.len = sizeof(NGX_HTTP_DEFAULT_INDEX);
403         index->name.data = (u_char *) NGX_HTTP_DEFAULT_INDEX;
404         index->lengths = NULL;
405         index->values = NULL;
406
407         conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX);
408
409         return NGX_CONF_OK;
410     }
411
412     return NGX_CONF_OK;
413 }
414
415
416 static ngx_int_t
417 ngx_http_index_init(ngx_conf_t *cf)
418 {
419     ngx_http_handler_pt        *h;
420     ngx_http_core_main_conf_t  *cmcf;
421
422     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
423
424     h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
425     if (h == NULL) {
426         return NGX_ERROR;
427     }
428
429     *h = ngx_http_index_handler;
430
431     return NGX_OK;
432 }
433
434
435 /* TODO: warn about duplicate indices */
436
437 static char *
438 ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
439 {
440     ngx_http_index_loc_conf_t *ilcf = conf;
441
442     ngx_str_t                  *value;
443     ngx_uint_t                  i, n;
444     ngx_http_index_t           *index;
445     ngx_http_script_compile_t   sc;
446
447     if (ilcf->indices == NULL) {
448         ilcf->indices = ngx_array_create(cf->pool, 2, sizeof(ngx_http_index_t));
449         if (ilcf->indices == NULL) {
450             return NGX_CONF_ERROR;
451         }
452     }
453
454     value = cf->args->elts;
455
456     for (i = 1; i < cf->args->nelts; i++) {
457
458         if (value[i].data[0] == '/' && i != cf->args->nelts - 1) {
459             ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
460                                "only the last index in \"index\" directive "
461                                "should be absolute");
462         }
463
464         if (value[i].len == 0) {
465             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
466                                "index \"%V\" in \"index\" directive is invalid",
467                                &value[1]);
468             return NGX_CONF_ERROR;
469         }
470
471         index = ngx_array_push(ilcf->indices);
472         if (index == NULL) {
473             return NGX_CONF_ERROR;
474         }
475
476         index->name.len = value[i].len;
477         index->name.data = value[i].data;
478         index->lengths = NULL;
479         index->values = NULL;
480
481         n = ngx_http_script_variables_count(&value[i]);
482
483         if (n == 0) {
484             if (ilcf->max_index_len < index->name.len) {
485                 ilcf->max_index_len = index->name.len;
486             }
487
488             if (index->name.data[0] == '/') {
489                 continue;
490             }
491
492             /* include the terminating '\0' to the length to use ngx_memcpy() */
493             index->name.len++;
494
495             continue;
496         }
497
498         ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
499
500         sc.cf = cf;
501         sc.source = &value[i];
502         sc.lengths = &index->lengths;
503         sc.values = &index->values;
504         sc.variables = n;
505         sc.complete_lengths = 1;
506         sc.complete_values = 1;
507
508         if (ngx_http_script_compile(&sc) != NGX_OK) {
509             return NGX_CONF_ERROR;
510         }
511     }
512
513     return NGX_CONF_OK;
514 }