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, root, reserve, 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);
131 /* suppress MSVC warning */
134 index = ilcf->indices->elts;
135 for (i = 0; i < ilcf->indices->nelts; i++) {
137 if (index[i].lengths == NULL) {
139 if (index[i].name.data[0] == '/') {
140 return ngx_http_internal_redirect(r, &index[i].name, &r->args);
143 reserve = ilcf->max_index_len;
144 len = index[i].name.len;
147 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
149 e.ip = index[i].lengths->elts;
153 /* 1 is for terminating '\0' as in static names */
156 while (*(uintptr_t *) e.ip) {
157 lcode = *(ngx_http_script_len_code_pt *) e.ip;
161 /* 16 bytes are preallocation */
166 if (reserve > allocated) {
168 name = ngx_http_map_uri_to_path(r, &path, &root, reserve);
173 allocated = path.data + path.len - name;
176 if (index[i].values == NULL) {
178 /* index[i].name.len includes the terminating '\0' */
180 ngx_memcpy(name, index[i].name.data, index[i].name.len);
182 path.len = (name + index[i].name.len - 1) - path.data;
185 e.ip = index[i].values->elts;
188 while (*(uintptr_t *) e.ip) {
189 code = *(ngx_http_script_code_pt *) e.ip;
190 code((ngx_http_script_engine_t *) &e);
196 return ngx_http_internal_redirect(r, &uri, &r->args);
199 path.len = e.pos - path.data;
204 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
205 "open index \"%V\"", &path);
207 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
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;
215 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
218 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, of.err,
219 ngx_open_file_n " \"%s\" failed", path.data);
222 return NGX_HTTP_INTERNAL_SERVER_ERROR;
225 if (of.err == NGX_ENOTDIR || of.err == NGX_EACCES) {
226 return ngx_http_index_error(r, clcf, path.data, of.err);
230 rc = ngx_http_index_test_dir(r, clcf, path.data, name - 1);
239 if (of.err == NGX_ENOENT) {
243 ngx_log_error(NGX_LOG_ERR, r->connection->log, of.err,
244 ngx_open_file_n " \"%s\" failed", path.data);
246 return NGX_HTTP_INTERNAL_SERVER_ERROR;
249 uri.len = r->uri.len + len - 1;
252 uri.data = path.data + root;
255 uri.data = ngx_pnalloc(r->pool, uri.len);
256 if (uri.data == NULL) {
257 return NGX_HTTP_INTERNAL_SERVER_ERROR;
260 p = ngx_copy(uri.data, r->uri.data, r->uri.len);
261 ngx_memcpy(p, name, len - 1);
264 return ngx_http_internal_redirect(r, &uri, &r->args);
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)
277 ngx_open_file_info_t of;
280 if (c != '/' || path == last) {
281 /* "alias" without trailing slash */
286 dir.len = last - path;
289 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
290 "http index check dir: \"%V\"", &dir);
292 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
295 of.valid = clcf->open_file_cache_valid;
296 of.errors = clcf->open_file_cache_errors;
298 if (ngx_open_cached_file(clcf->open_file_cache, &dir, &of, r->pool)
303 if (of.err == NGX_ENOENT) {
305 return ngx_http_index_error(r, clcf, dir.data, NGX_ENOENT);
308 if (of.err == NGX_EACCES) {
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.
321 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
322 ngx_open_file_n " \"%s\" failed", dir.data);
325 return NGX_HTTP_INTERNAL_SERVER_ERROR;
334 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
335 "\"%s\" is not a directory", dir.data);
337 return NGX_HTTP_INTERNAL_SERVER_ERROR;
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)
345 if (err == NGX_EACCES) {
346 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
347 "\"%s\" is forbidden", file);
349 return NGX_HTTP_FORBIDDEN;
352 if (clcf->log_not_found) {
353 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
354 "\"%s\" is not found", file);
357 return NGX_HTTP_NOT_FOUND;
362 ngx_http_index_create_loc_conf(ngx_conf_t *cf)
364 ngx_http_index_loc_conf_t *conf;
366 conf = ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t));
368 return NGX_CONF_ERROR;
371 conf->indices = NULL;
372 conf->max_index_len = 0;
379 ngx_http_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
381 ngx_http_index_loc_conf_t *prev = parent;
382 ngx_http_index_loc_conf_t *conf = child;
384 ngx_http_index_t *index;
386 if (conf->indices == NULL) {
387 conf->indices = prev->indices;
388 conf->max_index_len = prev->max_index_len;
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;
397 index = ngx_array_push(conf->indices);
399 return NGX_CONF_ERROR;
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;
407 conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX);
417 ngx_http_index_init(ngx_conf_t *cf)
419 ngx_http_handler_pt *h;
420 ngx_http_core_main_conf_t *cmcf;
422 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
424 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
429 *h = ngx_http_index_handler;
435 /* TODO: warn about duplicate indices */
438 ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
440 ngx_http_index_loc_conf_t *ilcf = conf;
444 ngx_http_index_t *index;
445 ngx_http_script_compile_t sc;
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;
454 value = cf->args->elts;
456 for (i = 1; i < cf->args->nelts; i++) {
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");
464 if (value[i].len == 0) {
465 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
466 "index \"%V\" in \"index\" directive is invalid",
468 return NGX_CONF_ERROR;
471 index = ngx_array_push(ilcf->indices);
473 return NGX_CONF_ERROR;
476 index->name.len = value[i].len;
477 index->name.data = value[i].data;
478 index->lengths = NULL;
479 index->values = NULL;
481 n = ngx_http_script_variables_count(&value[i]);
484 if (ilcf->max_index_len < index->name.len) {
485 ilcf->max_index_len = index->name.len;
488 if (index->name.data[0] == '/') {
492 /* include the terminating '\0' to the length to use ngx_memcpy() */
498 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
501 sc.source = &value[i];
502 sc.lengths = &index->lengths;
503 sc.values = &index->values;
505 sc.complete_lengths = 1;
506 sc.complete_values = 1;
508 if (ngx_http_script_compile(&sc) != NGX_OK) {
509 return NGX_CONF_ERROR;