2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
2002-12-28 00:22:50 +08:00
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_errno.h>
|
|
|
|
#include <ngx_string.h>
|
|
|
|
#include <ngx_files.h>
|
2002-12-28 00:22:50 +08:00
|
|
|
#include <ngx_conf_file.h>
|
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
#include <ngx_http.h>
|
|
|
|
#include <ngx_http_config.h>
|
2003-01-09 13:36:00 +08:00
|
|
|
#include <ngx_http_core_module.h>
|
2002-09-11 23:18:33 +08:00
|
|
|
#include <ngx_http_index_handler.h>
|
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-01-10 14:09:20 +08:00
|
|
|
static int ngx_http_index_init(ngx_pool_t *pool);
|
2002-09-11 23:18:33 +08:00
|
|
|
static void *ngx_http_index_create_conf(ngx_pool_t *pool);
|
2003-01-09 13:36:00 +08:00
|
|
|
static char *ngx_http_index_merge_conf(ngx_pool_t *p,
|
2002-12-11 02:05:12 +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,
|
|
|
|
char *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-01-28 23:56:37 +08:00
|
|
|
NGX_HTTP_LOC_CONF|NGX_CONF_ANY,
|
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-15 23:42:53 +08:00
|
|
|
{ngx_string(""), 0, NULL, 0, 0, NULL}
|
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-04-08 23:40:10 +08:00
|
|
|
NGX_HTTP_MODULE,
|
|
|
|
|
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 = {
|
|
|
|
&ngx_http_index_module_ctx, /* module context */
|
2003-05-16 23:27:48 +08:00
|
|
|
0, /* module index */
|
2002-12-27 00:26:23 +08:00
|
|
|
ngx_http_index_commands, /* module directives */
|
|
|
|
NGX_HTTP_MODULE_TYPE, /* module type */
|
2003-01-10 14:09:20 +08:00
|
|
|
ngx_http_index_init /* init module */
|
2002-09-11 23:18:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-01-11 01:45:47 +08:00
|
|
|
/*
|
2003-01-15 15:02:27 +08:00
|
|
|
Try to open first index file before the test of the directory existence
|
|
|
|
because the valid requests should be many more then 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.
|
|
|
|
Besides Win32 has ERROR_PATH_NOT_FOUND (NGX_ENOTDIR) and
|
|
|
|
Unix has ENOTDIR error (although it less helpfull).
|
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-01-11 01:45:47 +08:00
|
|
|
int i, rc, test_dir;
|
2002-12-11 02:05:12 +08:00
|
|
|
char *name, *file;
|
|
|
|
ngx_str_t loc, *index;
|
2002-09-11 23:18:33 +08:00
|
|
|
ngx_err_t err;
|
|
|
|
ngx_fd_t fd;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-01-09 13:36:00 +08:00
|
|
|
ngx_http_index_conf_t *cf;
|
|
|
|
ngx_http_core_loc_conf_t *core_cf;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
cf = (ngx_http_index_conf_t *)
|
2003-04-08 23:40:10 +08:00
|
|
|
ngx_http_get_module_loc_conf(r, ngx_http_index_module_ctx);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-01-09 13:36:00 +08:00
|
|
|
core_cf = (ngx_http_core_loc_conf_t *)
|
2003-04-08 23:40:10 +08:00
|
|
|
ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
|
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-01-09 13:36:00 +08:00
|
|
|
core_cf->doc_root.len + r->uri.len
|
2002-12-11 02:05:12 +08:00
|
|
|
+ cf->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-01-10 14:09:20 +08:00
|
|
|
loc.data = ngx_cpystrn(r->path.data, core_cf->doc_root.data,
|
2003-01-09 13:36:00 +08:00
|
|
|
core_cf->doc_root.len + 1);
|
2002-12-11 02:05:12 +08:00
|
|
|
file = ngx_cpystrn(loc.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-01-11 01:45:47 +08:00
|
|
|
|
2002-12-11 02:05:12 +08:00
|
|
|
index = (ngx_str_t *) cf->indices->elts;
|
2002-09-11 23:18:33 +08:00
|
|
|
for (i = 0; i < cf->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
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
fd = ngx_open_file(name, NGX_FILE_RDONLY);
|
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) {
|
|
|
|
r->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) {
|
|
|
|
if (r->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
|
|
|
|
2002-12-15 14:25:09 +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;
|
|
|
|
loc.len = index[i].len;
|
|
|
|
loc.data = index[i].data;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
loc.len = r->uri.len + index[i].len;
|
|
|
|
r->file.name.len = core_cf->doc_root.len + r->uri.len
|
|
|
|
+ index[i].len;
|
|
|
|
}
|
|
|
|
|
2003-05-15 01:13:13 +08:00
|
|
|
/* STUB */ r->exten.len = 4; r->exten.data = "html";
|
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
return ngx_http_internal_redirect(r, loc);
|
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
|
|
|
|
if (r->path_err == NGX_EACCES) {
|
|
|
|
return NGX_HTTP_FORBIDDEN;
|
2003-01-11 01:45:47 +08:00
|
|
|
}
|
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_http_index_test_dir: "
|
|
|
|
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-01-10 14:09:20 +08:00
|
|
|
static int ngx_http_index_init(ngx_pool_t *pool)
|
|
|
|
{
|
|
|
|
ngx_http_handler_pt *h;
|
|
|
|
|
|
|
|
ngx_test_null(h, ngx_push_array(&ngx_http_index_handlers), NGX_ERROR);
|
|
|
|
|
|
|
|
*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-01-09 13:36:00 +08:00
|
|
|
ngx_test_null(conf, ngx_pcalloc(pool, sizeof(ngx_http_index_conf_t)),
|
|
|
|
NGX_CONF_ERROR);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
ngx_test_null(conf->indices,
|
2003-01-11 01:45:47 +08:00
|
|
|
ngx_create_array(pool, 3, sizeof(ngx_str_t)),
|
2003-01-09 13:36:00 +08:00
|
|
|
NGX_CONF_ERROR);
|
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-01-11 01:45:47 +08:00
|
|
|
/* STUB */
|
2003-01-09 13:36:00 +08:00
|
|
|
static char *ngx_http_index_merge_conf(ngx_pool_t *p, void *parent, void *child)
|
|
|
|
{
|
2003-02-07 01:21:13 +08:00
|
|
|
#if 0
|
2003-01-09 13:36:00 +08:00
|
|
|
ngx_http_index_conf_t *prev = (ngx_http_index_conf_t *) parent;
|
2003-02-07 01:21:13 +08:00
|
|
|
#endif
|
2003-01-09 13:36:00 +08:00
|
|
|
ngx_http_index_conf_t *conf = (ngx_http_index_conf_t *) child;
|
|
|
|
ngx_str_t *index;
|
|
|
|
|
2003-01-11 01:45:47 +08:00
|
|
|
if (conf->max_index_len == 0) {
|
|
|
|
ngx_test_null(index, ngx_push_array(conf->indices), NGX_CONF_ERROR);
|
|
|
|
index->len = sizeof(NGX_HTTP_INDEX) - 1;
|
|
|
|
index->data = NGX_HTTP_INDEX;
|
|
|
|
conf->max_index_len = sizeof(NGX_HTTP_INDEX);
|
|
|
|
}
|
|
|
|
|
2003-01-15 15:02:27 +08:00
|
|
|
/* FAIL: if first index is started with '/' */
|
2003-01-09 13:36:00 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static char *ngx_http_index_merge_conf(ngx_pool_t *p, void *parent, void *child)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2002-09-11 23:18:33 +08:00
|
|
|
ngx_http_index_conf_t *prev = (ngx_http_index_conf_t *) parent;
|
|
|
|
ngx_http_index_conf_t *conf = (ngx_http_index_conf_t *) child;
|
2002-12-11 02:05:12 +08:00
|
|
|
ngx_str_t *index;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
if (conf->max_index_len == 0) {
|
2002-12-27 00:26:23 +08:00
|
|
|
if (prev->max_index_len != 0) {
|
2002-09-11 23:18:33 +08:00
|
|
|
return prev;
|
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_test_null(index, ngx_push_array(conf->indices), NULL);
|
2002-12-11 02:05:12 +08:00
|
|
|
index->len = sizeof(NGX_HTTP_INDEX) - 1;
|
|
|
|
index->data = NGX_HTTP_INDEX;
|
|
|
|
conf->max_index_len = sizeof(NGX_HTTP_INDEX);
|
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
|
|
|
}
|
2003-01-09 13:36:00 +08:00
|
|
|
#endif
|
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,
|
|
|
|
char *conf)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2003-03-21 00:09:44 +08:00
|
|
|
ngx_http_index_conf_t *lcf = (ngx_http_index_conf_t *) conf;
|
2002-12-27 00:26:23 +08:00
|
|
|
int i;
|
|
|
|
ngx_str_t *index, *value;
|
|
|
|
|
|
|
|
value = (ngx_str_t *) cf->args->elts;
|
|
|
|
for (i = 1; i < cf->args->nelts; i++) {
|
2003-03-21 00:09:44 +08:00
|
|
|
ngx_test_null(index, ngx_push_array(lcf->indices), NGX_CONF_ERROR);
|
2002-12-27 00:26:23 +08:00
|
|
|
index->len = value[i].len;
|
|
|
|
index->data = value[i].data;
|
|
|
|
|
2003-03-21 00:09:44 +08:00
|
|
|
if (lcf->max_index_len < index->len) {
|
|
|
|
lcf->max_index_len = index->len;
|
2002-12-27 00:26:23 +08:00
|
|
|
}
|
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
return NULL;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|