2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
2002-09-11 23:18:33 +08:00
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_errno.h>
|
|
|
|
#include <ngx_string.h>
|
|
|
|
#include <ngx_files.h>
|
|
|
|
#include <ngx_config_command.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
#include <ngx_http_config.h>
|
|
|
|
#include <ngx_http_index_handler.h>
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
static void *ngx_http_index_create_conf(ngx_pool_t *pool);
|
2002-12-11 02:05:12 +08:00
|
|
|
static void *ngx_http_index_merge_conf(ngx_pool_t *p,
|
|
|
|
void *parent, void *child);
|
|
|
|
static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf,
|
|
|
|
ngx_str_t *value);
|
2002-09-11 23:18:33 +08:00
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
|
|
|
|
static ngx_command_t ngx_http_index_commands[] = {
|
|
|
|
|
|
|
|
{"index", ngx_http_index_set_index, NULL,
|
|
|
|
NGX_HTTP_LOC_CONF, NGX_CONF_ITERATE,
|
|
|
|
"set index files"},
|
|
|
|
|
|
|
|
{NULL}
|
|
|
|
|
|
|
|
};
|
2002-09-11 23:18:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
ngx_http_module_t ngx_http_index_module = {
|
|
|
|
NGX_HTTP_MODULE,
|
2002-12-11 02:05:12 +08:00
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
NULL, /* create server config */
|
|
|
|
ngx_http_index_create_conf, /* create location config */
|
|
|
|
ngx_http_index_commands, /* module directives */
|
2002-12-11 02:05:12 +08:00
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
NULL, /* init module */
|
2002-12-11 02:05:12 +08:00
|
|
|
NULL, /* translate handler */
|
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
NULL, /* init output body filter */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
int ngx_http_index_handler(ngx_http_request_t *r)
|
|
|
|
{
|
2002-12-11 02:05:12 +08:00
|
|
|
int i;
|
|
|
|
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
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
ngx_http_index_conf_t *cf;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
cf = (ngx_http_index_conf_t *)
|
|
|
|
ngx_get_module_loc_conf(r, ngx_http_index_module);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-08-16 01:20:26 +08:00
|
|
|
ngx_test_null(name,
|
2002-12-11 02:05:12 +08:00
|
|
|
ngx_palloc(r->pool,
|
|
|
|
r->server->doc_root_len + r->uri.len
|
|
|
|
+ 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
|
|
|
|
2002-12-11 02:05:12 +08:00
|
|
|
loc.data = ngx_cpystrn(name, r->server->doc_root, r->server->doc_root_len);
|
|
|
|
file = ngx_cpystrn(loc.data, r->uri.data, r->uri.len + 1);
|
2002-08-07 00:39:45 +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++) {
|
2002-12-11 02:05:12 +08:00
|
|
|
ngx_memcpy(file, index[i].data, index[i].len + 1);
|
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;
|
|
|
|
if (err == NGX_ENOENT)
|
2002-09-11 23:18:33 +08:00
|
|
|
continue;
|
2002-12-15 14:25:09 +08:00
|
|
|
#if (WIN32)
|
|
|
|
if (err == ERROR_PATH_NOT_FOUND)
|
|
|
|
continue;
|
|
|
|
#endif
|
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.len = r->server->doc_root_len + r->uri.len + index[i].len;
|
|
|
|
r->file.name.data = name;
|
|
|
|
r->file.fd = fd;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-12-11 02:05:12 +08:00
|
|
|
loc.len = r->uri.len + index[i].len;
|
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
|
|
|
|
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
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
ngx_test_null(conf, ngx_pcalloc(pool, sizeof(ngx_http_index_conf_t)), NULL);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
ngx_test_null(conf->indices,
|
2002-12-11 02:05:12 +08:00
|
|
|
ngx_create_array(pool, sizeof(ngx_str_t), 3),
|
2002-09-11 23:18:33 +08:00
|
|
|
NULL);
|
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
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
static void *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) {
|
|
|
|
if (prev->max_index_len != 0)
|
|
|
|
return prev;
|
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
|
|
|
}
|
|
|
|
|
2002-12-11 02:05:12 +08:00
|
|
|
|
|
|
|
static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf,
|
|
|
|
ngx_str_t *value)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2002-09-11 23:18:33 +08:00
|
|
|
ngx_http_index_conf_t *cf = (ngx_http_index_conf_t *) conf;
|
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
|
|
|
ngx_test_null(index, ngx_push_array(cf->indices), NULL);
|
2002-12-11 02:05:12 +08:00
|
|
|
index->len = value->len;
|
|
|
|
index->data = value->data;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-09-11 23:18:33 +08:00
|
|
|
if (cf->max_index_len < index->len)
|
|
|
|
cf->max_index_len = index->len;
|
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
|
|
|
}
|