2002-08-07 00:39:45 +08:00
|
|
|
|
2003-06-02 23:24:30 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ngx_array_t indices;
|
|
|
|
size_t max_index_len;
|
|
|
|
} ngx_http_index_conf_t;
|
|
|
|
|
|
|
|
|
|
|
|
#define NGX_HTTP_DEFAULT_INDEX "index.html"
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
|
2003-01-11 01:45:47 +08:00
|
|
|
static int ngx_http_index_test_dir(ngx_http_request_t *r);
|
2003-07-04 23:10:33 +08:00
|
|
|
static int ngx_http_index_init(ngx_cycle_t *cycle);
|
2002-09-11 23:18:33 +08:00
|
|
|
static void *ngx_http_index_create_conf(ngx_pool_t *pool);
|
2003-05-27 20:18:54 +08:00
|
|
|
static char *ngx_http_index_merge_conf(ngx_pool_t *p, 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-05-27 20:18:54 +08:00
|
|
|
void *conf);
|
2002-09-11 23:18:33 +08:00
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
|
|
|
|
static ngx_command_t ngx_http_index_commands[] = {
|
|
|
|
|
2002-12-27 00:26:23 +08:00
|
|
|
{ngx_string("index"),
|
2003-06-02 23:24:30 +08:00
|
|
|
NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
2002-12-27 00:26:23 +08:00
|
|
|
ngx_http_index_set_index,
|
2003-01-11 01:45:47 +08:00
|
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
2003-05-15 23:42:53 +08:00
|
|
|
0,
|
|
|
|
NULL},
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2003-05-27 20:18:54 +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-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 */
|
|
|
|
|
|
|
|
ngx_http_index_create_conf, /* create location configration */
|
|
|
|
ngx_http_index_merge_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-06-02 23:24:30 +08:00
|
|
|
Try to open the first index file before the directory existence test
|
|
|
|
because the valid requests should be many more than invalid ones.
|
2003-01-20 01:53:14 +08:00
|
|
|
If open() failed then stat() should be more quickly because some data
|
2003-04-12 00:01:14 +08:00
|
|
|
is already cached in the kernel.
|
2003-05-27 20:18:54 +08:00
|
|
|
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
|
|
|
*/
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
int ngx_http_index_handler(ngx_http_request_t *r)
|
|
|
|
{
|
2003-06-02 23:24:30 +08:00
|
|
|
int i, rc, test_dir, path_not_found;
|
2003-05-27 20:18:54 +08:00
|
|
|
char *name, *file;
|
2003-05-29 21:02:09 +08:00
|
|
|
ngx_str_t redirect, *index;
|
2003-05-27 20:18:54 +08:00
|
|
|
ngx_err_t err;
|
|
|
|
ngx_fd_t fd;
|
|
|
|
ngx_http_index_conf_t *icf;
|
|
|
|
ngx_http_core_loc_conf_t *clcf;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
icf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
|
|
|
|
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
2003-01-09 13:36:00 +08:00
|
|
|
|
2003-01-10 14:09:20 +08:00
|
|
|
ngx_test_null(r->path.data,
|
2002-12-11 02:05:12 +08:00
|
|
|
ngx_palloc(r->pool,
|
2003-05-27 20:18:54 +08:00
|
|
|
clcf->doc_root.len + r->uri.len
|
|
|
|
+ icf->max_index_len),
|
2002-08-16 01:20:26 +08:00
|
|
|
NGX_HTTP_INTERNAL_SERVER_ERROR);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-29 21:02:09 +08:00
|
|
|
redirect.data = ngx_cpymem(r->path.data, clcf->doc_root.data,
|
|
|
|
clcf->doc_root.len);
|
|
|
|
file = ngx_cpystrn(redirect.data, r->uri.data, r->uri.len + 1);
|
2003-01-10 14:09:20 +08:00
|
|
|
r->path.len = file - r->path.data;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-01-15 15:02:27 +08:00
|
|
|
test_dir = 1;
|
2003-06-02 23:24:30 +08:00
|
|
|
path_not_found = 1;
|
2003-01-11 01:45:47 +08:00
|
|
|
|
2003-05-29 21:02:09 +08:00
|
|
|
index = icf->indices.elts;
|
2003-05-27 20:18:54 +08:00
|
|
|
for (i = 0; i < icf->indices.nelts; i++) {
|
2003-01-10 14:09:20 +08:00
|
|
|
|
|
|
|
if (index[i].data[0] != '/') {
|
|
|
|
ngx_memcpy(file, index[i].data, index[i].len + 1);
|
|
|
|
name = r->path.data;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
name = index[i].data;
|
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-06-02 23:24:30 +08:00
|
|
|
fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
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
|
|
|
|
2003-01-15 15:02:27 +08:00
|
|
|
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, err,
|
|
|
|
"DEBUG: " 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-06-02 23:24:30 +08:00
|
|
|
path_not_found = 1;
|
2003-01-15 15:02:27 +08:00
|
|
|
|
|
|
|
} else if (err == NGX_EACCES) {
|
|
|
|
r->path_err = err;
|
|
|
|
return NGX_HTTP_FORBIDDEN;
|
2003-01-11 01:45:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (test_dir) {
|
2003-06-02 23:24:30 +08:00
|
|
|
if (path_not_found) {
|
2003-01-15 15:02:27 +08:00
|
|
|
r->path_err = err;
|
2003-01-11 01:45:47 +08:00
|
|
|
return NGX_HTTP_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ngx_http_index_test_dir(r);
|
|
|
|
if (rc != NGX_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
test_dir = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
|
|
|
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-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-01-10 14:09:20 +08:00
|
|
|
if (index[i].data[0] == '/') {
|
|
|
|
r->file.name.len = index[i].len;
|
2003-05-29 21:02:09 +08:00
|
|
|
redirect.len = index[i].len;
|
|
|
|
redirect.data = index[i].data;
|
2003-01-10 14:09:20 +08:00
|
|
|
|
|
|
|
} else {
|
2003-05-29 21:02:09 +08:00
|
|
|
redirect.len = r->uri.len + index[i].len;
|
|
|
|
r->file.name.len = clcf->doc_root.len + r->uri.len + index[i].len;
|
2003-01-10 14:09:20 +08:00
|
|
|
}
|
|
|
|
|
2003-05-29 21:02:09 +08:00
|
|
|
return ngx_http_internal_redirect(r, &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-01-11 01:45:47 +08:00
|
|
|
static int ngx_http_index_test_dir(ngx_http_request_t *r)
|
|
|
|
{
|
|
|
|
r->path.data[r->path.len - 1] = '\0';
|
2003-01-15 15:02:27 +08:00
|
|
|
r->path.data[r->path.len] = '\0';
|
2003-01-11 01:45:47 +08:00
|
|
|
|
|
|
|
ngx_log_debug(r->connection->log, "IS_DIR: %s" _ r->path.data);
|
|
|
|
|
2003-01-15 15:02:27 +08:00
|
|
|
#if 0
|
2003-05-27 20:18:54 +08:00
|
|
|
if (r->path_err == NGX_EACCES) {
|
|
|
|
return NGX_HTTP_FORBIDDEN;
|
|
|
|
}
|
2003-01-15 15:02:27 +08:00
|
|
|
#endif
|
2003-01-11 01:45:47 +08:00
|
|
|
|
2003-01-15 15:02:27 +08:00
|
|
|
if (ngx_file_type(r->path.data, &r->file.info) == -1) {
|
|
|
|
|
|
|
|
r->path_err = ngx_errno;
|
|
|
|
|
|
|
|
if (r->path_err == NGX_ENOENT) {
|
|
|
|
r->path.data[r->path.len - 1] = '/';
|
|
|
|
return NGX_HTTP_NOT_FOUND;
|
2003-01-11 01:45:47 +08:00
|
|
|
}
|
|
|
|
|
2003-01-15 15:02:27 +08:00
|
|
|
ngx_log_error(NGX_LOG_CRIT, r->connection->log, r->path_err,
|
|
|
|
ngx_file_type_n " %s failed", r->path.data);
|
2003-01-11 01:45:47 +08:00
|
|
|
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-01-15 15:02:27 +08:00
|
|
|
r->path.data[r->path.len - 1] = '/';
|
|
|
|
|
2003-01-11 01:45:47 +08:00
|
|
|
if (ngx_is_dir(r->file.info)) {
|
|
|
|
return NGX_OK;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return NGX_HTTP_NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-04 23:10:33 +08:00
|
|
|
static int 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_conf_ctx_t *ctx;
|
|
|
|
ngx_http_core_main_conf_t *cmcf;
|
2003-01-10 14:09:20 +08:00
|
|
|
|
2003-07-03 02:51:41 +08:00
|
|
|
ctx = (ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index];
|
|
|
|
cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
|
|
|
|
|
|
|
|
ngx_test_null(h, ngx_push_array(&cmcf->index_handlers), NGX_ERROR);
|
2003-01-10 14:09:20 +08:00
|
|
|
|
|
|
|
*h = ngx_http_index_handler;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
static void *ngx_http_index_create_conf(ngx_pool_t *pool)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2002-09-11 23:18:33 +08:00
|
|
|
ngx_http_index_conf_t *conf;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
ngx_test_null(conf, ngx_palloc(pool, sizeof(ngx_http_index_conf_t)),
|
2003-01-09 13:36:00 +08:00
|
|
|
NGX_CONF_ERROR);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
ngx_init_array(conf->indices, pool, 3, sizeof(ngx_str_t), NGX_CONF_ERROR);
|
|
|
|
conf->max_index_len = 0;
|
2002-08-07 00:39:45 +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-01-09 13:36:00 +08:00
|
|
|
static char *ngx_http_index_merge_conf(ngx_pool_t *p, void *parent, void *child)
|
|
|
|
{
|
2003-05-27 20:18:54 +08:00
|
|
|
ngx_http_index_conf_t *prev = parent;
|
|
|
|
ngx_http_index_conf_t *conf = child;
|
2003-01-09 13:36:00 +08:00
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
int i;
|
|
|
|
ngx_str_t *index, *prev_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-06-11 23:28:34 +08:00
|
|
|
ngx_memcpy(conf, prev, sizeof(ngx_http_index_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;
|
|
|
|
index->data = NGX_HTTP_DEFAULT_INDEX;
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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-05-27 20:18:54 +08:00
|
|
|
ngx_http_index_conf_t *icf = conf;
|
|
|
|
|
|
|
|
int 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;
|
|
|
|
|
|
|
|
if (value[1].data[0] == '/' && icf->indices.nelts == 0) {
|
|
|
|
ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1,
|
|
|
|
"first index \"%s\" must not be absolute", value[1].data);
|
|
|
|
return ngx_conf_errstr;
|
|
|
|
}
|
|
|
|
|
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-05-29 21:02:09 +08:00
|
|
|
ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1,
|
2003-05-30 22:27:59 +08:00
|
|
|
"index \"%s\" is invalid", value[i].data);
|
2003-05-29 21:02:09 +08:00
|
|
|
return ngx_conf_errstr;
|
2003-05-27 20:18:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ngx_test_null(index, ngx_push_array(&icf->indices), NGX_CONF_ERROR);
|
2002-12-27 00:26:23 +08:00
|
|
|
index->len = value[i].len;
|
|
|
|
index->data = value[i].data;
|
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
if (icf->max_index_len < index->len + 1) {
|
|
|
|
icf->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
|
|
|
}
|