nginx-quic/src/http/modules/ngx_http_index_handler.c

363 lines
10 KiB
C
Raw Normal View History

2003-06-02 11:24:30 -04: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;
2003-10-12 13:49:16 -03:00
typedef struct {
int index;
unsigned tested:1;
} ngx_http_index_ctx_t;
2003-06-02 11:24:30 -04:00
#define NGX_HTTP_DEFAULT_INDEX "index.html"
2003-10-12 13:49:16 -03:00
static int ngx_http_index_test_dir(ngx_http_request_t *r,
ngx_http_index_ctx_t *ctx);
static int ngx_http_index_error(ngx_http_request_t *r,
ngx_http_index_ctx_t *ctx, ngx_err_t err);
2003-07-04 11:10:33 -04:00
static int ngx_http_index_init(ngx_cycle_t *cycle);
2003-07-20 17:15:59 -04:00
static void *ngx_http_index_create_conf(ngx_conf_t *cf);
static char *ngx_http_index_merge_conf(ngx_conf_t *cf,
void *parent, void *child);
2002-12-26 13:26:23 -03:00
static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
2003-07-20 17:15:59 -04:00
void *conf);
2002-09-11 11:18:33 -04:00
2002-12-15 03:25:09 -03:00
static ngx_command_t ngx_http_index_commands[] = {
2002-12-26 13:26:23 -03:00
{ngx_string("index"),
2003-06-02 11:24:30 -04:00
NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
2002-12-26 13:26:23 -03:00
ngx_http_index_set_index,
2003-01-10 14:45:47 -03:00
NGX_HTTP_LOC_CONF_OFFSET,
2003-05-15 11:42:53 -04:00
0,
NULL},
2002-12-15 03:25:09 -03:00
2003-05-27 08:18:54 -04:00
ngx_null_command
2002-12-15 03:25:09 -03:00
};
2002-09-11 11:18:33 -04:00
2002-12-26 13:26:23 -03:00
ngx_http_module_t ngx_http_index_module_ctx = {
2003-11-10 14:17:31 -03:00
NULL, /* pre conf */
2003-05-19 12:39:14 -04:00
NULL, /* create main configuration */
NULL, /* init main configuration */
2003-03-20 12:09:44 -04:00
2003-05-19 12:39:14 -04: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-26 13:26:23 -03:00
};
ngx_module_t ngx_http_index_module = {
2003-05-27 08:18:54 -04:00
NGX_MODULE,
2002-12-26 13:26:23 -03:00
&ngx_http_index_module_ctx, /* module context */
ngx_http_index_commands, /* module directives */
2003-05-27 08:18:54 -04:00
NGX_HTTP_MODULE, /* module type */
2003-07-02 14:51:41 -04:00
ngx_http_index_init, /* init module */
2003-07-04 11:10:33 -04:00
NULL /* init child */
2002-09-11 11:18:33 -04:00
};
2003-01-10 14:45:47 -03:00
/*
2003-10-12 13:49:16 -03: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-10 14:45:47 -03:00
int ngx_http_index_handler(ngx_http_request_t *r)
{
2003-10-16 17:19:16 -03:00
int rc;
2003-05-27 08:18:54 -04:00
char *name, *file;
2003-05-29 09:02:09 -04:00
ngx_str_t redirect, *index;
2003-05-27 08:18:54 -04:00
ngx_err_t err;
ngx_fd_t fd;
2003-10-12 13:49:16 -03:00
ngx_http_index_ctx_t *ctx;
2003-05-27 08:18:54 -04:00
ngx_http_index_conf_t *icf;
ngx_http_core_loc_conf_t *clcf;
2003-10-12 13:49:16 -03:00
if (r->uri.data[r->uri.len - 1] != '/') {
return NGX_DECLINED;
}
2003-01-09 02:36:00 -03:00
2003-10-12 13:49:16 -03: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-10-12 13:49:16 -03: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-10-12 13:49:16 -03:00
if (r->path.data == NULL) {
r->path_allocated = clcf->doc_root.len + r->uri.len
+ icf->max_index_len;
ngx_test_null(r->path.data,
ngx_palloc(r->pool, r->path_allocated),
NGX_HTTP_INTERNAL_SERVER_ERROR);
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);
r->path.len = file - r->path.data;
} else{
redirect.data = r->path.data + r->path.len;
file = redirect.data + r->uri.len;
}
2003-01-10 14:45:47 -03:00
2003-05-29 09:02:09 -04:00
index = icf->indices.elts;
2003-10-12 13:49:16 -03:00
for (/* void */; ctx->index < icf->indices.nelts; ctx->index++) {
2003-01-10 03:09:20 -03:00
2003-10-12 13:49:16 -03:00
if (index[ctx->index].data[0] == '/') {
name = index[ctx->index].data;
2003-01-10 03:09:20 -03:00
} else {
2003-10-12 13:49:16 -03:00
ngx_memcpy(file, index[ctx->index].data, index[ctx->index].len + 1);
name = r->path.data;
2003-01-10 03:09:20 -03:00
}
2003-06-02 11:24:30 -04:00
fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN);
2003-10-12 13:49:16 -03:00
2003-10-16 17:19:16 -03:00
if (fd == (ngx_fd_t) NGX_AGAIN) {
2003-10-12 13:49:16 -03:00
return NGX_AGAIN;
}
2002-12-15 03:25:09 -03:00
if (fd == NGX_INVALID_FILE) {
err = ngx_errno;
2003-01-10 14:45:47 -03:00
2003-01-15 04:02:27 -03:00
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, err,
"DEBUG: " ngx_open_file_n " %s failed", name);
2003-01-10 14:45:47 -03:00
2003-01-10 03:09:20 -03:00
if (err == NGX_ENOTDIR) {
2003-10-12 13:49:16 -03:00
return ngx_http_index_error(r, ctx, err);
2003-01-15 04:02:27 -03:00
} else if (err == NGX_EACCES) {
2003-10-12 13:49:16 -03:00
return ngx_http_index_error(r, ctx, err);
2003-01-10 14:45:47 -03:00
}
2003-10-12 13:49:16 -03:00
if (!ctx->tested) {
rc = ngx_http_index_test_dir(r, ctx);
2003-01-10 14:45:47 -03:00
if (rc != NGX_OK) {
return rc;
}
2003-10-12 13:49:16 -03:00
ctx->tested = 1;
2003-01-10 14:45:47 -03:00
}
if (err == NGX_ENOENT) {
2002-12-15 03:25:09 -03:00
continue;
2002-12-26 13:26:23 -03:00
}
2002-09-11 11:18:33 -04:00
ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
ngx_open_file_n " %s failed", name);
2002-09-11 11:18:33 -04:00
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
2003-05-27 08:18:54 -04:00
r->file.name.data = name;
r->file.fd = fd;
2003-10-12 13:49:16 -03:00
if (index[ctx->index].data[0] == '/') {
r->file.name.len = index[ctx->index].len;
redirect.len = index[ctx->index].len;
redirect.data = index[ctx->index].data;
2003-01-10 03:09:20 -03:00
} else {
2003-10-12 13:49:16 -03:00
redirect.len = r->uri.len + index[ctx->index].len;
r->file.name.len = clcf->doc_root.len + r->uri.len
+ index[ctx->index].len;
2003-01-10 03:09:20 -03:00
}
2003-05-29 09:02:09 -04:00
return ngx_http_internal_redirect(r, &redirect, NULL);
}
2002-09-11 11:18:33 -04:00
return NGX_DECLINED;
}
2002-12-10 15:05:12 -03:00
2003-10-12 13:49:16 -03:00
static int ngx_http_index_test_dir(ngx_http_request_t *r,
ngx_http_index_ctx_t *ctx)
2003-01-10 14:45:47 -03:00
{
2003-10-12 13:49:16 -03:00
ngx_err_t err;
2003-01-10 14:45:47 -03:00
r->path.data[r->path.len - 1] = '\0';
2003-01-15 04:02:27 -03:00
r->path.data[r->path.len] = '\0';
2003-01-10 14:45:47 -03:00
ngx_log_debug(r->connection->log, "IS_DIR: %s" _ r->path.data);
2003-11-16 18:49:42 -03:00
if (ngx_file_info(r->path.data, &r->file.info) == -1) {
2003-01-15 04:02:27 -03:00
2003-10-12 13:49:16 -03:00
err = ngx_errno;
2003-01-15 04:02:27 -03:00
2003-10-12 13:49:16 -03:00
if (err == NGX_ENOENT) {
2003-01-15 04:02:27 -03:00
r->path.data[r->path.len - 1] = '/';
2003-10-12 13:49:16 -03:00
return ngx_http_index_error(r, ctx, err);
2003-01-10 14:45:47 -03:00
}
2003-10-12 13:49:16 -03:00
ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
2003-11-16 18:49:42 -03:00
ngx_file_info_n " %s failed", r->path.data);
2003-01-10 14:45:47 -03:00
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
2003-01-15 04:02:27 -03:00
r->path.data[r->path.len - 1] = '/';
2003-11-16 18:49:42 -03:00
if (ngx_is_dir(&r->file.info)) {
2003-01-10 14:45:47 -03:00
return NGX_OK;
2003-10-12 13:49:16 -03:00
}
2003-01-10 14:45:47 -03:00
2003-10-12 13:49:16 -03:00
/* THINK: not reached ??? */
return ngx_http_index_error(r, ctx, 0);
}
static int ngx_http_index_error(ngx_http_request_t *r,
ngx_http_index_ctx_t *ctx, ngx_err_t err)
{
if (err == NGX_EACCES) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
"\"%s\" is forbidden", r->path.data);
return NGX_HTTP_FORBIDDEN;
2003-01-10 14:45:47 -03:00
}
2003-10-12 13:49:16 -03:00
ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
"\"%s\" is not found", r->path.data);
return NGX_HTTP_NOT_FOUND;
2003-01-10 14:45:47 -03:00
}
2003-07-04 11:10:33 -04:00
static int ngx_http_index_init(ngx_cycle_t *cycle)
2003-01-10 03:09:20 -03:00
{
2003-07-02 14:51:41 -04:00
ngx_http_handler_pt *h;
ngx_http_conf_ctx_t *ctx;
ngx_http_core_main_conf_t *cmcf;
2003-01-10 03:09:20 -03:00
2003-07-02 14:51:41 -04:00
ctx = (ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index];
cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
2003-10-12 13:49:16 -03:00
ngx_test_null(h, ngx_push_array(
&cmcf->phases[NGX_HTTP_TRANSLATE_PHASE].handlers),
NGX_ERROR);
2003-01-10 03:09:20 -03:00
*h = ngx_http_index_handler;
return NGX_OK;
}
2003-07-20 17:15:59 -04:00
static void *ngx_http_index_create_conf(ngx_conf_t *cf)
{
2002-09-11 11:18:33 -04:00
ngx_http_index_conf_t *conf;
2003-07-20 17:15:59 -04:00
ngx_test_null(conf, ngx_palloc(cf->pool, sizeof(ngx_http_index_conf_t)),
2003-01-09 02:36:00 -03:00
NGX_CONF_ERROR);
2003-07-20 17:15:59 -04:00
ngx_init_array(conf->indices, cf->pool, 3, sizeof(ngx_str_t),
NGX_CONF_ERROR);
2003-05-27 08:18:54 -04:00
conf->max_index_len = 0;
2002-09-11 11:18:33 -04:00
return conf;
}
2002-12-10 15:05:12 -03:00
2003-05-27 08:18:54 -04:00
/* TODO: remove duplicate indices */
2003-07-20 17:15:59 -04:00
static char *ngx_http_index_merge_conf(ngx_conf_t *cf,
void *parent, void *child)
2003-01-09 02:36:00 -03:00
{
2003-05-27 08:18:54 -04:00
ngx_http_index_conf_t *prev = parent;
ngx_http_index_conf_t *conf = child;
2003-01-09 02:36:00 -03:00
2003-05-27 08:18:54 -04:00
int i;
ngx_str_t *index, *prev_index;
2003-01-10 14:45:47 -03:00
2003-05-27 08:18:54 -04:00
if (conf->max_index_len == 0) {
if (prev->max_index_len != 0) {
2003-06-11 11:28:34 -04:00
ngx_memcpy(conf, prev, sizeof(ngx_http_index_conf_t));
2003-05-27 08:18:54 -04:00
return NGX_CONF_OK;
}
2003-01-09 02:36:00 -03:00
2003-05-27 08:18:54 -04: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 02:36:00 -03:00
2003-05-27 08:18:54 -04:00
return NGX_CONF_OK;
}
2003-01-09 02:36:00 -03:00
2003-05-27 08:18:54 -04:00
if (prev->max_index_len != 0) {
2003-05-27 08:18:54 -04: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-26 13:26:23 -03:00
}
2003-05-27 08:18:54 -04:00
}
2003-05-27 08:18:54 -04:00
if (conf->max_index_len < prev->max_index_len) {
conf->max_index_len = prev->max_index_len;
}
2002-09-11 11:18:33 -04:00
2003-05-27 08:18:54 -04:00
return NGX_CONF_OK;
}
2003-05-27 08:18:54 -04:00
2003-05-29 09:02:09 -04:00
/* TODO: warn about duplicate indices */
2002-12-10 15:05:12 -03:00
2002-12-26 13:26:23 -03:00
static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
2003-05-27 08:18:54 -04:00
void *conf)
{
2003-05-27 08:18:54 -04:00
ngx_http_index_conf_t *icf = conf;
int i;
2002-12-26 13:26:23 -03:00
ngx_str_t *index, *value;
2003-05-27 08:18:54 -04:00
value = cf->args->elts;
if (value[1].data[0] == '/' && icf->indices.nelts == 0) {
2003-07-18 10:44:05 -04:00
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"first index \"%s\" in \"%s\" directive "
"must not be absolute",
value[1].data, cmd->name.data);
return NGX_CONF_ERROR;
2003-05-27 08:18:54 -04:00
}
2002-12-26 13:26:23 -03:00
for (i = 1; i < cf->args->nelts; i++) {
2003-05-27 08:18:54 -04:00
if (value[i].len == 0) {
2003-07-18 10:44:05 -04:00
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"index \"%s\" in \"%s\" directive is invalid",
value[1].data, cmd->name.data);
return NGX_CONF_ERROR;
2003-05-27 08:18:54 -04:00
}
ngx_test_null(index, ngx_push_array(&icf->indices), NGX_CONF_ERROR);
2002-12-26 13:26:23 -03:00
index->len = value[i].len;
index->data = value[i].data;
2003-05-27 08:18:54 -04:00
if (icf->max_index_len < index->len + 1) {
icf->max_index_len = index->len + 1;
2002-12-26 13:26:23 -03:00
}
}
2003-05-29 09:02:09 -04:00
return NGX_CONF_OK;
}