diff src/http/modules/ngx_http_index_handler.c @ 10:4f3879d9b6f6

nginx-0.0.1-2002-09-11-19:18:33 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 11 Sep 2002 15:18:33 +0000
parents d220029ac7f3
children f323b4f74e4a
line wrap: on
line diff
--- a/src/http/modules/ngx_http_index_handler.c
+++ b/src/http/modules/ngx_http_index_handler.c
@@ -1,23 +1,53 @@
 
 #include <ngx_config.h>
+#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>
 
-#include <ngx_strings.h>
-#include <ngx_open.h>
-#include <ngx_stat.h>
+
+static void *ngx_http_index_create_conf(ngx_pool_t *pool);
+static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf, char *value);
+
+static ngx_command_t ngx_http_index_commands[];
+
 
-#include <ngx_http.h>
+ngx_http_module_t  ngx_http_index_module = {
+    NGX_HTTP_MODULE,
+    NULL,                                  /* create server config */
+    ngx_http_index_create_conf,            /* create location config */
+    ngx_http_index_commands,               /* module directives */
+    NULL,                                  /* init module */
+    NULL,                                  /* init output body filter */
+};
+
+
+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}
+
+};
 
 int ngx_http_index_handler(ngx_http_request_t *r)
 {
-    int          index_len, err, i;
-    char        *name, *loc, *file
-    ngx_file_t   fd;
+    int          index_len, i;
+    char        *name, *loc, *file;
+    ngx_err_t    err;
+    ngx_fd_t     fd;
 
-    ngx_http_index_t  *index;
-    ngx_http_index_handler_loc_conf_t  *cf;
+    ngx_http_index_file_t  *index;
+    ngx_http_index_conf_t  *cf;
 
-    cf = (ngx_http_index_handler_loc_conf_t *)
-                    ngx_get_module_loc_conf(r, &ngx_http_index_handler_module);
+    cf = (ngx_http_index_conf_t *)
+                            ngx_get_module_loc_conf(r, ngx_http_index_module);
 
     index_len = (*(r->uri_end - 1) == '/') ? cf->max_index_len : 0;
 
@@ -29,96 +59,73 @@ int ngx_http_index_handler(ngx_http_requ
     loc = ngx_cpystrn(name, r->server->doc_root, r->server->doc_root_len);
     file = ngx_cpystrn(loc, r->uri_start, r->uri_end - r->uri_start + 1);
 
-    /* URI without / on the end - check directory */
-    if (index_len == 0) {
+    index = (ngx_http_index_file_t *) cf->indices->elts;
+    for (i = 0; i < cf->indices->nelts; i++) {
+        ngx_memcpy(file, index[i].name, index[i].len);
 
-        if (ngx_stat(name, &r->stat) == -1) {
+        fd = ngx_open_file(name, NGX_FILE_RDONLY);
+        if (fd == -1) {
             err = ngx_errno;
-            ngx_log_error(GX_LOG_ERR, r->connection->log, err,
-                         "ngx_http_handler: " ngx_stat_n " %s failed", name);
-
             if (err == NGX_ENOENT)
-                return NGX_HTTP_NOT_FOUND;
-            else
-                return NGX_HTTP_INTERNAL_SERVER_ERROR;
-        }
+                continue;
 
-        if (ngx_is_dir(r->stat)) {
-            *file++ = '/';
-            *file = '\0';
-            r->headers_out->location = loc;
-            return NGX_HTTP_MOVED_PERMANENTLY;
+            ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
+                          ngx_open_file_n " %s failed", name);
+
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
 
-        r->file = name;
-        r->stat_valid = 1;
+        r->filename = name; 
+        r->fd = fd; 
 
-        return NGX_OK;
+        return ngx_http_internal_redirect(r, loc);
     }
 
-    /* look for index file */
-    index = (ngx_http_index_t *) cf->indices->elts;
-    for (i = 0; i < cf->indices->nelts; i++) {
-        ngx_memcpy(file, index[i].name; index[i].len);
-
-        fd = ngx_open(name, O_RDONLY);
-        if (fd != -1) {
-            r->file = name; 
-            r->fd = fd; 
-            return NGX_OK;
-        }
-    }
-
-    return NGX_HTTP_FORBIDDEN;
+    return NGX_DECLINED;
 }
 
-/*
+static void *ngx_http_index_create_conf(ngx_pool_t *pool)
+{
+    ngx_http_index_conf_t  *conf;
 
-static void *ngx_create_index_config()
-{
-    ngx_http_index_handler_loc_conf_t  *cf;
+    ngx_test_null(conf, ngx_pcalloc(pool, sizeof(ngx_http_index_conf_t)), NULL);
 
-    ngx_check_null(cf, ngx_alloc(p, sizeof(ngx_http_index_handler_loc_conf)),
-                   NULL);
+    ngx_test_null(conf->indices,
+                  ngx_create_array(pool, sizeof(ngx_http_index_file_t), 3),
+                  NULL);
 
-    cf->indices = ngx_create_array(p, sizeof(ngx_http_index_t), 5);
-    if (cf->indices == NULL)
-        return NULL;
-
-    cf->max_index_len = 0;
-
-    return cf;
+    return conf;
 }
 
-static void *ngx_merge_index_config()
+static void *ngx_http_index_merge_conf(ngx_pool_t *p, void *parent, void *child)
 {
-    if (p->indices->nelts > 0) {
+    ngx_http_index_conf_t *prev = (ngx_http_index_conf_t *) parent;
+    ngx_http_index_conf_t *conf = (ngx_http_index_conf_t *) child;
+    ngx_http_index_file_t *index;
 
-        copy and check dups
+    if (conf->max_index_len == 0) {
+        if (prev->max_index_len != 0)
+            return prev;
 
-        if (c->max_index_len < c->max_index_len)
-            c->max_index_len < c->max_index_len);
+        ngx_test_null(index, ngx_push_array(conf->indices), NULL);
+        index->name = NGX_HTTP_INDEX;
+        conf->max_index_len = index->len = sizeof(NGX_HTTP_INDEX);
     }
+
+    return conf;
 }
 
-static void *ngx_set_index()
+static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf, char *value)
 {
-    if (*conf == NULL) {
-        cf = ngx_create_index_conf();
-        if (cf == NULL)
-            return "can not create config";
-    }
+    ngx_http_index_conf_t *cf = (ngx_http_index_conf_t *) conf;
+    ngx_http_index_file_t *index;
 
-    while (args) {
-       index = ngx_push_array(cf->indices);
-       index->name = arg;
-       index->len = ngx_strlen(arg) + 1;
+    ngx_test_null(index, ngx_push_array(cf->indices), NULL);
+    index->name = value;
+    index->len = strlen(value);
 
-       if (cf->max_index_len < index->len)
-           cf->max_index_len = index->len;
-    }
+    if (cf->max_index_len < index->len)
+        cf->max_index_len = index->len;
 
-    *conf = cf;
+    return NULL;
 }
-
-*/