3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
20 ngx_array_t *indices; /* array of ngx_http_index_t */
22 } ngx_http_index_loc_conf_t;
25 #define NGX_HTTP_DEFAULT_INDEX "index.html"
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);
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,
41 static ngx_command_t ngx_http_index_commands[] = {
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,
54 static ngx_http_module_t ngx_http_index_module_ctx = {
55 NULL, /* preconfiguration */
56 ngx_http_index_init, /* postconfiguration */
58 NULL, /* create main configuration */
59 NULL, /* init main configuration */
61 NULL, /* create server configuration */
62 NULL, /* merge server configuration */
64 ngx_http_index_create_loc_conf, /* create location configration */
65 ngx_http_index_merge_loc_conf /* merge location configration */
69 ngx_module_t ngx_http_index_module = {
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 */
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.
96 ngx_http_index_handler(ngx_http_request_t *r)
99 size_t len, nlen, root, allocated;
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;
111 if (r->uri.data[r->uri.len - 1] != '/') {
115 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
120 if (r->zero_in_uri) {
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);
133 index = ilcf->indices->elts;
134 for (i = 0; i < ilcf->indices->nelts; i++) {
136 if (index[i].lengths == NULL) {
138 if (index[i].name.data[0] == '/') {
139 return ngx_http_internal_redirect(r, &index[i].name, &r->args);
142 len = ilcf->max_index_len;
143 nlen = index[i].name.len;
146 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
148 e.ip = index[i].lengths->elts;
152 /* 1 byte for terminating '\0' */
156 while (*(uintptr_t *) e.ip) {
157 lcode = *(ngx_http_script_len_code_pt *) e.ip;
163 /* 16 bytes are preallocation */
168 if (len > (size_t) (path.data + allocated - name)) {
170 name = ngx_http_map_uri_to_path(r, &path, &root, len);
175 allocated = path.len;
178 if (index[i].values == NULL) {
180 /* index[i].name.len includes the terminating '\0' */
182 ngx_memcpy(name, index[i].name.data, index[i].name.len);
184 path.len = (name + index[i].name.len - 1) - path.data;
187 e.ip = index[i].values->elts;
190 while (*(uintptr_t *) e.ip) {
191 code = *(ngx_http_script_code_pt *) e.ip;
192 code((ngx_http_script_engine_t *) &e);
198 return ngx_http_internal_redirect(r, &uri, &r->args);
201 path.len = e.pos - path.data;
206 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
207 "open index \"%V\"", &path);
209 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
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;
217 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
220 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, of.err,
221 ngx_open_file_n " \"%s\" failed", path.data);
224 return NGX_HTTP_INTERNAL_SERVER_ERROR;
227 if (of.err == NGX_ENOTDIR || of.err == NGX_EACCES) {
228 return ngx_http_index_error(r, clcf, path.data, of.err);
232 rc = ngx_http_index_test_dir(r, clcf, path.data, name - 1);
241 if (of.err == NGX_ENOENT) {
245 ngx_log_error(NGX_LOG_ERR, r->connection->log, of.err,
246 ngx_open_file_n " \"%s\" failed", path.data);
248 return NGX_HTTP_INTERNAL_SERVER_ERROR;
251 uri.len = r->uri.len + nlen - 1;
254 uri.data = path.data + root;
257 uri.data = ngx_pnalloc(r->pool, uri.len);
258 if (uri.data == NULL) {
259 return NGX_HTTP_INTERNAL_SERVER_ERROR;
262 p = ngx_copy(uri.data, r->uri.data, r->uri.len);
263 ngx_memcpy(p, name, nlen - 1);
266 return ngx_http_internal_redirect(r, &uri, &r->args);
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)
279 ngx_open_file_info_t of;
282 if (c != '/' || path == last) {
283 /* "alias" without trailing slash */
288 dir.len = last - path;
291 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
292 "http index check dir: \"%V\"", &dir);
294 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
297 of.valid = clcf->open_file_cache_valid;
298 of.errors = clcf->open_file_cache_errors;
300 if (ngx_open_cached_file(clcf->open_file_cache, &dir, &of, r->pool)
305 if (of.err == NGX_ENOENT) {
307 return ngx_http_index_error(r, clcf, dir.data, NGX_ENOENT);
310 if (of.err == NGX_EACCES) {
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.
323 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
324 ngx_open_file_n " \"%s\" failed", dir.data);
327 return NGX_HTTP_INTERNAL_SERVER_ERROR;
336 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
337 "\"%s\" is not a directory", dir.data);
339 return NGX_HTTP_INTERNAL_SERVER_ERROR;
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)
347 if (err == NGX_EACCES) {
348 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
349 "\"%s\" is forbidden", file);
351 return NGX_HTTP_FORBIDDEN;
354 if (clcf->log_not_found) {
355 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
356 "\"%s\" is not found", file);
359 return NGX_HTTP_NOT_FOUND;
364 ngx_http_index_create_loc_conf(ngx_conf_t *cf)
366 ngx_http_index_loc_conf_t *conf;
368 conf = ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t));
370 return NGX_CONF_ERROR;
373 conf->indices = NULL;
374 conf->max_index_len = 0;
381 ngx_http_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
383 ngx_http_index_loc_conf_t *prev = parent;
384 ngx_http_index_loc_conf_t *conf = child;
386 ngx_http_index_t *index;
388 if (conf->indices == NULL) {
389 conf->indices = prev->indices;
390 conf->max_index_len = prev->max_index_len;
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;
399 index = ngx_array_push(conf->indices);
401 return NGX_CONF_ERROR;
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;
409 conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX);
419 ngx_http_index_init(ngx_conf_t *cf)
421 ngx_http_handler_pt *h;
422 ngx_http_core_main_conf_t *cmcf;
424 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
426 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
431 *h = ngx_http_index_handler;
437 /* TODO: warn about duplicate indices */
440 ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
442 ngx_http_index_loc_conf_t *ilcf = conf;
446 ngx_http_index_t *index;
447 ngx_http_script_compile_t sc;
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;
456 value = cf->args->elts;
458 for (i = 1; i < cf->args->nelts; i++) {
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");
466 if (value[i].len == 0) {
467 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
468 "index \"%V\" in \"index\" directive is invalid",
470 return NGX_CONF_ERROR;
473 index = ngx_array_push(ilcf->indices);
475 return NGX_CONF_ERROR;
478 index->name.len = value[i].len;
479 index->name.data = value[i].data;
480 index->lengths = NULL;
481 index->values = NULL;
483 n = ngx_http_script_variables_count(&value[i]);
486 if (ilcf->max_index_len < index->name.len) {
487 ilcf->max_index_len = index->name.len;
490 if (index->name.data[0] == '/') {
494 /* include the terminating '\0' to the length to use ngx_copy() */
500 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
503 sc.source = &value[i];
504 sc.lengths = &index->lengths;
505 sc.values = &index->values;
507 sc.complete_lengths = 1;
508 sc.complete_values = 1;
510 if (ngx_http_script_compile(&sc) != NGX_OK) {
511 return NGX_CONF_ERROR;