2002-08-07 00:39:45 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-06-02 23:24:30 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2004-11-26 00:17:31 +08:00
|
|
|
ngx_array_t indices;
|
|
|
|
size_t max_index_len;
|
|
|
|
ngx_http_cache_hash_t *index_cache;
|
2003-12-01 04:03:18 +08:00
|
|
|
} ngx_http_index_loc_conf_t;
|
2003-06-02 23:24:30 +08:00
|
|
|
|
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
typedef struct {
|
2004-11-26 00:17:31 +08:00
|
|
|
ngx_uint_t index;
|
|
|
|
u_char *last;
|
|
|
|
ngx_str_t path;
|
|
|
|
ngx_str_t redirect;
|
|
|
|
ngx_http_cache_entry_t *cache;
|
|
|
|
ngx_uint_t tested; /* unsigned tested:1 */
|
2003-10-13 00:49:16 +08:00
|
|
|
} ngx_http_index_ctx_t;
|
|
|
|
|
|
|
|
|
2004-03-19 13:25:53 +08:00
|
|
|
#define NGX_HTTP_DEFAULT_INDEX "index.html"
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r,
|
|
|
|
ngx_http_index_ctx_t *ctx);
|
|
|
|
static ngx_int_t ngx_http_index_error(ngx_http_request_t *r,
|
|
|
|
ngx_http_index_ctx_t *ctx, ngx_err_t err);
|
2003-10-13 00:49:16 +08:00
|
|
|
|
2004-06-16 23:32:11 +08:00
|
|
|
static ngx_int_t ngx_http_index_init(ngx_cycle_t *cycle);
|
2003-12-01 04:03:18 +08:00
|
|
|
static void *ngx_http_index_create_loc_conf(ngx_conf_t *cf);
|
|
|
|
static char *ngx_http_index_merge_loc_conf(ngx_conf_t *cf,
|
2003-07-21 05:15:59 +08:00
|
|
|
void *parent, void *child);
|
2002-12-27 00:26:23 +08:00
|
|
|
static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
|
2003-07-21 05:15:59 +08:00
|
|
|
void *conf);
|
2002-09-11 23:18:33 +08:00
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
static ngx_command_t ngx_http_index_commands[] = {
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
{ ngx_string("index"),
|
|
|
|
NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
|
|
ngx_http_index_set_index,
|
|
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
|
|
|
0,
|
|
|
|
NULL },
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#if (NGX_HTTP_CACHE)
|
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
{ ngx_string("index_cache"),
|
2003-12-01 04:03:18 +08:00
|
|
|
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE3,
|
2003-11-28 16:40:40 +08:00
|
|
|
ngx_http_set_cache_slot,
|
|
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
2003-12-01 04:03:18 +08:00
|
|
|
offsetof(ngx_http_index_loc_conf_t, index_cache),
|
2003-11-28 16:40:40 +08:00
|
|
|
NULL },
|
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#endif
|
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
ngx_null_command
|
2002-12-15 14:25:09 +08:00
|
|
|
};
|
2002-09-11 23:18:33 +08:00
|
|
|
|
|
|
|
|
2002-12-27 00:26:23 +08:00
|
|
|
ngx_http_module_t ngx_http_index_module_ctx = {
|
2003-11-11 01:17:31 +08:00
|
|
|
NULL, /* pre conf */
|
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
2003-03-21 00:09:44 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_http_index_create_loc_conf, /* create location configration */
|
|
|
|
ngx_http_index_merge_loc_conf /* merge location configration */
|
2002-12-27 00:26:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_http_index_module = {
|
2003-05-27 20:18:54 +08:00
|
|
|
NGX_MODULE,
|
2002-12-27 00:26:23 +08:00
|
|
|
&ngx_http_index_module_ctx, /* module context */
|
|
|
|
ngx_http_index_commands, /* module directives */
|
2003-05-27 20:18:54 +08:00
|
|
|
NGX_HTTP_MODULE, /* module type */
|
2003-07-03 02:51:41 +08:00
|
|
|
ngx_http_index_init, /* init module */
|
2003-07-04 23:10:33 +08:00
|
|
|
NULL /* init child */
|
2002-09-11 23:18:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-01-11 01:45:47 +08:00
|
|
|
/*
|
2003-10-13 00:49:16 +08:00
|
|
|
* Try to open the first index file before the test of the directory existence
|
|
|
|
* because the valid requests should be many more than invalid ones.
|
|
|
|
* If open() failed then stat() should be more quickly because some data
|
|
|
|
* is already cached in the kernel.
|
|
|
|
* Besides Win32 has ERROR_PATH_NOT_FOUND (NGX_ENOTDIR).
|
|
|
|
* Unix has ENOTDIR error, although it less helpfull - it shows only
|
|
|
|
* that path contains the usual file in place of the directory.
|
|
|
|
*/
|
2003-01-11 01:45:47 +08:00
|
|
|
|
2004-10-25 23:29:23 +08:00
|
|
|
static ngx_int_t ngx_http_index_handler(ngx_http_request_t *r)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2004-03-16 15:10:12 +08:00
|
|
|
u_char *name;
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_fd_t fd;
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_str_t *index;
|
|
|
|
ngx_err_t err;
|
|
|
|
ngx_log_t *log;
|
|
|
|
ngx_http_index_ctx_t *ctx;
|
|
|
|
ngx_http_core_loc_conf_t *clcf;
|
|
|
|
ngx_http_index_loc_conf_t *ilcf;
|
2004-07-27 00:21:18 +08:00
|
|
|
#if (NGX_HTTP_CACHE0)
|
|
|
|
/* crc must be in ctx !! */
|
2004-03-15 04:46:25 +08:00
|
|
|
uint32_t crc;
|
|
|
|
#endif
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
if (r->uri.data[r->uri.len - 1] != '/') {
|
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
2003-01-09 13:36:00 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
log = r->connection->log;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we use context because the handler supports an async file opening
|
|
|
|
* and thus can be called several times
|
|
|
|
*/
|
|
|
|
|
|
|
|
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
2003-11-28 16:40:40 +08:00
|
|
|
ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ctx = ngx_http_get_module_ctx(r, ngx_http_index_module);
|
|
|
|
if (ctx == NULL) {
|
|
|
|
ngx_http_create_ctx(r, ctx, ngx_http_index_module,
|
|
|
|
sizeof(ngx_http_index_ctx_t),
|
|
|
|
NGX_HTTP_INTERNAL_SERVER_ERROR);
|
2003-11-28 16:40:40 +08:00
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#if (NGX_HTTP_CACHE)
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (ilcf->index_cache) {
|
|
|
|
ctx->cache = ngx_http_cache_get(ilcf->index_cache, NULL,
|
|
|
|
&r->uri, &crc);
|
2003-11-28 16:40:40 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
2004-11-11 22:07:14 +08:00
|
|
|
"http index cache get: %p", ctx->cache);
|
2003-11-28 16:40:40 +08:00
|
|
|
|
2003-12-02 00:28:14 +08:00
|
|
|
if (ctx->cache && !ctx->cache->expired) {
|
2003-11-28 16:40:40 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ctx->cache->accessed = ngx_cached_time;
|
2003-11-28 16:40:40 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ctx->redirect.len = ctx->cache->data.value.len;
|
|
|
|
ctx->redirect.data = ngx_palloc(r->pool, ctx->redirect.len + 1);
|
|
|
|
if (ctx->redirect.data == NULL) {
|
|
|
|
ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
2003-11-28 16:40:40 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_memcpy(ctx->redirect.data, ctx->cache->data.value.data,
|
|
|
|
ctx->redirect.len + 1);
|
|
|
|
ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
|
2003-11-28 16:40:40 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
return ngx_http_internal_redirect(r, &ctx->redirect, NULL);
|
|
|
|
}
|
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#endif
|
|
|
|
|
2004-07-19 03:11:20 +08:00
|
|
|
#if 0
|
2004-03-19 13:25:53 +08:00
|
|
|
ctx->path.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len
|
|
|
|
+ ilcf->max_index_len
|
|
|
|
- clcf->alias * clcf->name.len);
|
|
|
|
if (ctx->path.data == NULL) {
|
2003-12-01 04:03:18 +08:00
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
ctx->redirect.data = ngx_cpymem(ctx->path.data, clcf->root.data,
|
|
|
|
clcf->root.len);
|
2004-07-19 03:11:20 +08:00
|
|
|
#endif
|
2004-03-19 13:25:53 +08:00
|
|
|
|
|
|
|
if (clcf->alias) {
|
2004-07-27 00:21:18 +08:00
|
|
|
ctx->path.data = ngx_palloc(r->pool, clcf->root.len
|
|
|
|
+ r->uri.len + 1 - clcf->name.len
|
|
|
|
+ ilcf->max_index_len);
|
|
|
|
if (ctx->path.data == NULL) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
2004-07-19 03:11:20 +08:00
|
|
|
|
2004-07-27 00:21:18 +08:00
|
|
|
ctx->redirect.data = ngx_palloc(r->pool, r->uri.len
|
|
|
|
+ ilcf->max_index_len);
|
|
|
|
if (ctx->redirect.data == NULL) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
2004-07-19 03:11:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memcpy(ctx->path.data, clcf->root.data, clcf->root.len);
|
|
|
|
|
|
|
|
ctx->last = ngx_cpystrn(ctx->path.data + clcf->root.len,
|
2004-03-19 13:25:53 +08:00
|
|
|
r->uri.data + clcf->name.len,
|
|
|
|
r->uri.len + 1 - clcf->name.len);
|
|
|
|
|
2004-07-27 00:21:18 +08:00
|
|
|
#if 0
|
2004-03-19 13:25:53 +08:00
|
|
|
/*
|
|
|
|
* aliases usually have trailling "/",
|
|
|
|
* set it in the start of the possible redirect
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*ctx->redirect.data != '/') {
|
|
|
|
ctx->redirect.data--;
|
|
|
|
}
|
2004-07-27 00:21:18 +08:00
|
|
|
#endif
|
2004-03-19 13:25:53 +08:00
|
|
|
|
|
|
|
} else {
|
2004-07-19 03:11:20 +08:00
|
|
|
ctx->path.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len
|
|
|
|
+ ilcf->max_index_len);
|
|
|
|
if (ctx->path.data == NULL) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->redirect.data = ngx_cpymem(ctx->path.data, clcf->root.data,
|
|
|
|
clcf->root.len);
|
|
|
|
|
2004-03-19 13:25:53 +08:00
|
|
|
ctx->last = ngx_cpystrn(ctx->redirect.data, r->uri.data,
|
|
|
|
r->uri.len + 1);
|
|
|
|
}
|
2003-10-13 00:49:16 +08:00
|
|
|
}
|
2003-01-11 01:45:47 +08:00
|
|
|
|
2004-03-19 13:25:53 +08:00
|
|
|
ctx->path.len = ctx->last - ctx->path.data;
|
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
index = ilcf->indices.elts;
|
|
|
|
for (/* void */; ctx->index < ilcf->indices.nelts; ctx->index++) {
|
2003-01-10 14:09:20 +08:00
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
if (index[ctx->index].data[0] == '/') {
|
|
|
|
name = index[ctx->index].data;
|
2003-01-10 14:09:20 +08:00
|
|
|
|
|
|
|
} else {
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_memcpy(ctx->last, index[ctx->index].data,
|
|
|
|
index[ctx->index].len + 1);
|
|
|
|
name = ctx->path.data;
|
2003-01-10 14:09:20 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2004-07-27 00:21:18 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
|
|
"open index \"%s\"", name);
|
|
|
|
|
2003-06-02 23:24:30 +08:00
|
|
|
fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
2003-10-13 00:49:16 +08:00
|
|
|
|
2003-10-17 04:19:16 +08:00
|
|
|
if (fd == (ngx_fd_t) NGX_AGAIN) {
|
2003-10-13 00:49:16 +08:00
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
if (fd == NGX_INVALID_FILE) {
|
2002-08-07 00:39:45 +08:00
|
|
|
err = ngx_errno;
|
2003-01-11 01:45:47 +08:00
|
|
|
|
2004-07-27 00:21:18 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, err,
|
2004-11-11 22:07:14 +08:00
|
|
|
ngx_open_file_n " \"%s\" failed", name);
|
2003-01-11 01:45:47 +08:00
|
|
|
|
2003-01-10 14:09:20 +08:00
|
|
|
if (err == NGX_ENOTDIR) {
|
2003-10-13 00:49:16 +08:00
|
|
|
return ngx_http_index_error(r, ctx, err);
|
2003-01-15 15:02:27 +08:00
|
|
|
|
|
|
|
} else if (err == NGX_EACCES) {
|
2003-10-13 00:49:16 +08:00
|
|
|
return ngx_http_index_error(r, ctx, err);
|
2003-01-11 01:45:47 +08:00
|
|
|
}
|
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
if (!ctx->tested) {
|
|
|
|
rc = ngx_http_index_test_dir(r, ctx);
|
2003-01-11 01:45:47 +08:00
|
|
|
|
|
|
|
if (rc != NGX_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
ctx->tested = 1;
|
2003-01-11 01:45:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err == NGX_ENOENT) {
|
2002-12-15 14:25:09 +08:00
|
|
|
continue;
|
2002-12-27 00:26:23 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_log_error(NGX_LOG_ERR, log, err,
|
2004-11-11 22:07:14 +08:00
|
|
|
ngx_open_file_n " \"%s\" failed", name);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
|
|
|
|
/* STUB: open file cache */
|
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
r->file.name.data = name;
|
|
|
|
r->file.fd = fd;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
if (index[ctx->index].data[0] == '/') {
|
|
|
|
r->file.name.len = index[ctx->index].len;
|
2003-12-01 04:03:18 +08:00
|
|
|
ctx->redirect.len = index[ctx->index].len;
|
|
|
|
ctx->redirect.data = index[ctx->index].data;
|
2003-01-10 14:09:20 +08:00
|
|
|
|
|
|
|
} else {
|
2004-07-19 03:11:20 +08:00
|
|
|
if (clcf->alias) {
|
2004-07-27 00:21:18 +08:00
|
|
|
name = ngx_cpymem(ctx->redirect.data, r->uri.data, r->uri.len);
|
|
|
|
ngx_memcpy(name, index[ctx->index].data,
|
|
|
|
index[ctx->index].len + 1);
|
2004-07-19 03:11:20 +08:00
|
|
|
}
|
|
|
|
|
2004-07-27 00:21:18 +08:00
|
|
|
ctx->redirect.len = r->uri.len + index[ctx->index].len;
|
2004-03-16 15:10:12 +08:00
|
|
|
r->file.name.len = clcf->root.len + r->uri.len
|
2004-07-19 03:11:20 +08:00
|
|
|
- clcf->alias * clcf->name.len
|
2003-10-13 00:49:16 +08:00
|
|
|
+ index[ctx->index].len;
|
2003-01-10 14:09:20 +08:00
|
|
|
}
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
/**/
|
2003-11-28 16:40:40 +08:00
|
|
|
|
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#if (NGX_HTTP_CACHE)
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (ilcf->index_cache) {
|
2003-11-28 16:40:40 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (ctx->cache) {
|
|
|
|
if (ctx->redirect.len == ctx->cache->data.value.len
|
|
|
|
&& ngx_memcmp(ctx->cache->data.value.data,
|
|
|
|
ctx->redirect.data, ctx->redirect.len) == 0)
|
|
|
|
{
|
|
|
|
ctx->cache->accessed = ngx_cached_time;
|
|
|
|
ctx->cache->updated = ngx_cached_time;
|
|
|
|
ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
|
2003-11-28 16:40:40 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
return ngx_http_internal_redirect(r, &ctx->redirect, NULL);
|
|
|
|
}
|
2003-11-28 16:40:40 +08:00
|
|
|
}
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ctx->redirect.len++;
|
|
|
|
ctx->cache = ngx_http_cache_alloc(ilcf->index_cache, ctx->cache,
|
|
|
|
NULL, &r->uri, crc,
|
|
|
|
&ctx->redirect, log);
|
|
|
|
ctx->redirect.len--;
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
2004-11-11 22:07:14 +08:00
|
|
|
"http index cache alloc: %p", ctx->cache);
|
2003-12-01 04:03:18 +08:00
|
|
|
|
|
|
|
if (ctx->cache) {
|
|
|
|
ctx->cache->fd = NGX_INVALID_FILE;
|
|
|
|
ctx->cache->accessed = ngx_cached_time;
|
|
|
|
ctx->cache->last_modified = 0;
|
|
|
|
ctx->cache->updated = ngx_cached_time;
|
|
|
|
ctx->cache->memory = 1;
|
|
|
|
ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
|
|
|
|
}
|
2003-11-28 16:40:40 +08:00
|
|
|
}
|
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#endif
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
return ngx_http_internal_redirect(r, &ctx->redirect, NULL);
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
return NGX_DECLINED;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
2002-12-11 02:05:12 +08:00
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r,
|
|
|
|
ngx_http_index_ctx_t *ctx)
|
2003-01-11 01:45:47 +08:00
|
|
|
{
|
2003-10-13 00:49:16 +08:00
|
|
|
ngx_err_t err;
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ctx->path.data[ctx->path.len - 1] = '\0';
|
|
|
|
ctx->path.data[ctx->path.len] = '\0';
|
2003-01-11 01:45:47 +08:00
|
|
|
|
2004-02-12 01:08:49 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
|
|
"http check dir: \"%s\"", ctx->path.data);
|
2003-01-11 01:45:47 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (ngx_file_info(ctx->path.data, &r->file.info) == -1) {
|
2003-01-15 15:02:27 +08:00
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
err = ngx_errno;
|
2003-01-15 15:02:27 +08:00
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
if (err == NGX_ENOENT) {
|
2003-12-01 04:03:18 +08:00
|
|
|
ctx->path.data[ctx->path.len - 1] = '/';
|
2003-10-13 00:49:16 +08:00
|
|
|
return ngx_http_index_error(r, ctx, err);
|
2003-01-11 01:45:47 +08:00
|
|
|
}
|
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
|
2004-11-11 22:07:14 +08:00
|
|
|
ngx_file_info_n " \"%s\" failed", ctx->path.data);
|
2003-01-11 01:45:47 +08:00
|
|
|
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ctx->path.data[ctx->path.len - 1] = '/';
|
2003-01-15 15:02:27 +08:00
|
|
|
|
2003-11-17 05:49:42 +08:00
|
|
|
if (ngx_is_dir(&r->file.info)) {
|
2003-01-11 01:45:47 +08:00
|
|
|
return NGX_OK;
|
2003-10-13 00:49:16 +08:00
|
|
|
}
|
2003-01-11 01:45:47 +08:00
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
/* THINK: not reached ??? */
|
|
|
|
return ngx_http_index_error(r, ctx, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
static ngx_int_t ngx_http_index_error(ngx_http_request_t *r,
|
|
|
|
ngx_http_index_ctx_t *ctx, ngx_err_t err)
|
2003-10-13 00:49:16 +08:00
|
|
|
{
|
|
|
|
if (err == NGX_EACCES) {
|
|
|
|
ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
2003-12-01 04:03:18 +08:00
|
|
|
"\"%s\" is forbidden", ctx->path.data);
|
2003-10-13 00:49:16 +08:00
|
|
|
|
|
|
|
return NGX_HTTP_FORBIDDEN;
|
2003-01-11 01:45:47 +08:00
|
|
|
}
|
2003-10-13 00:49:16 +08:00
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
2003-12-01 04:03:18 +08:00
|
|
|
"\"%s\" is not found", ctx->path.data);
|
2003-10-13 00:49:16 +08:00
|
|
|
return NGX_HTTP_NOT_FOUND;
|
2003-01-11 01:45:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-16 23:32:11 +08:00
|
|
|
static ngx_int_t ngx_http_index_init(ngx_cycle_t *cycle)
|
2003-01-10 14:09:20 +08:00
|
|
|
{
|
2003-07-03 02:51:41 +08:00
|
|
|
ngx_http_handler_pt *h;
|
|
|
|
ngx_http_core_main_conf_t *cmcf;
|
2003-01-10 14:09:20 +08:00
|
|
|
|
2004-07-19 03:11:20 +08:00
|
|
|
cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
|
2003-07-03 02:51:41 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
h = ngx_push_array(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
|
|
if (h == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-01-10 14:09:20 +08:00
|
|
|
*h = ngx_http_index_handler;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
static void *ngx_http_index_create_loc_conf(ngx_conf_t *cf)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_http_index_loc_conf_t *conf;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_test_null(conf, ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t)),
|
2003-01-09 13:36:00 +08:00
|
|
|
NGX_CONF_ERROR);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-07-21 05:15:59 +08:00
|
|
|
ngx_init_array(conf->indices, cf->pool, 3, sizeof(ngx_str_t),
|
|
|
|
NGX_CONF_ERROR);
|
2003-05-27 20:18:54 +08:00
|
|
|
conf->max_index_len = 0;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
conf->index_cache = NULL;
|
2003-11-28 16:40:40 +08:00
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
return conf;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
2002-12-11 02:05:12 +08:00
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
/* TODO: remove duplicate indices */
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
static char *ngx_http_index_merge_loc_conf(ngx_conf_t *cf,
|
|
|
|
void *parent, void *child)
|
2003-01-09 13:36:00 +08:00
|
|
|
{
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_http_index_loc_conf_t *prev = parent;
|
|
|
|
ngx_http_index_loc_conf_t *conf = child;
|
2003-01-09 13:36:00 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
ngx_str_t *index;
|
2003-01-11 01:45:47 +08:00
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
if (conf->max_index_len == 0) {
|
|
|
|
if (prev->max_index_len != 0) {
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_memcpy(conf, prev, sizeof(ngx_http_index_loc_conf_t));
|
2003-05-27 20:18:54 +08:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
2003-01-09 13:36:00 +08:00
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
ngx_test_null(index, ngx_push_array(&conf->indices), NGX_CONF_ERROR);
|
|
|
|
index->len = sizeof(NGX_HTTP_DEFAULT_INDEX) - 1;
|
2004-03-19 13:25:53 +08:00
|
|
|
index->data = (u_char *) NGX_HTTP_DEFAULT_INDEX;
|
2003-05-27 20:18:54 +08:00
|
|
|
conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX);
|
2003-01-09 13:36:00 +08:00
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
2003-01-09 13:36:00 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
#if 0
|
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
if (prev->max_index_len != 0) {
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
prev_index = prev->indices.elts;
|
|
|
|
for (i = 0; i < prev->indices.nelts; i++) {
|
|
|
|
ngx_test_null(index, ngx_push_array(&conf->indices),
|
|
|
|
NGX_CONF_ERROR);
|
|
|
|
index->len = prev_index[i].len;
|
|
|
|
index->data = prev_index[i].data;
|
2002-12-27 00:26:23 +08:00
|
|
|
}
|
2003-05-27 20:18:54 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
if (conf->max_index_len < prev->max_index_len) {
|
|
|
|
conf->max_index_len = prev->max_index_len;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
2002-09-11 23:18:33 +08:00
|
|
|
|
2004-12-06 22:45:08 +08:00
|
|
|
#endif
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (conf->index_cache == NULL) {
|
|
|
|
conf->index_cache = prev->index_cache;
|
2003-11-28 16:40:40 +08:00
|
|
|
}
|
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
return NGX_CONF_OK;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
2003-05-27 20:18:54 +08:00
|
|
|
|
|
|
|
|
2003-05-29 21:02:09 +08:00
|
|
|
/* TODO: warn about duplicate indices */
|
2002-12-11 02:05:12 +08:00
|
|
|
|
2002-12-27 00:26:23 +08:00
|
|
|
static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
|
2003-05-27 20:18:54 +08:00
|
|
|
void *conf)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_http_index_loc_conf_t *ilcf = conf;
|
2003-05-27 20:18:54 +08:00
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
ngx_uint_t i;
|
2002-12-27 00:26:23 +08:00
|
|
|
ngx_str_t *index, *value;
|
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
value = cf->args->elts;
|
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
if (value[1].data[0] == '/' && ilcf->indices.nelts == 0) {
|
2003-07-18 22:44:05 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
2004-11-11 22:07:14 +08:00
|
|
|
"first index \"%V\" in \"%V\" directive "
|
2003-07-18 22:44:05 +08:00
|
|
|
"must not be absolute",
|
2004-11-11 22:07:14 +08:00
|
|
|
&value[1], &cmd->name);
|
2003-07-18 22:44:05 +08:00
|
|
|
return NGX_CONF_ERROR;
|
2003-05-27 20:18:54 +08:00
|
|
|
}
|
|
|
|
|
2002-12-27 00:26:23 +08:00
|
|
|
for (i = 1; i < cf->args->nelts; i++) {
|
2003-05-27 20:18:54 +08:00
|
|
|
if (value[i].len == 0) {
|
2003-07-18 22:44:05 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
2004-11-11 22:07:14 +08:00
|
|
|
"index \"%V\" in \"%V\" directive is invalid",
|
|
|
|
&value[1], &cmd->name);
|
2003-07-18 22:44:05 +08:00
|
|
|
return NGX_CONF_ERROR;
|
2003-05-27 20:18:54 +08:00
|
|
|
}
|
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
ngx_test_null(index, ngx_push_array(&ilcf->indices), NGX_CONF_ERROR);
|
2002-12-27 00:26:23 +08:00
|
|
|
index->len = value[i].len;
|
|
|
|
index->data = value[i].data;
|
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
if (ilcf->max_index_len < index->len + 1) {
|
|
|
|
ilcf->max_index_len = index->len + 1;
|
2002-12-27 00:26:23 +08:00
|
|
|
}
|
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-29 21:02:09 +08:00
|
|
|
return NGX_CONF_OK;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|