[PATCH] autoindex_ignore directive

Derrick Petzold contact at derrickpetzold.com
Thu Sep 2 08:28:12 MSD 2010


Hello,

This patch adds support for ignoring files in the auto index. Its modeled somewhat 
after Apache's IndexIgnore

http://httpd.apache.org/docs/2.0/mod/mod_autoindex.html#indexignore

and its implementation is based off of gzip_disable. Here is a sample config.

|        autoindex on;
|
|        location /ubuntu/ {
|            autoindex_ignore (db|conf);  # with pcre
|            autoindex_ignore db conf;    # without pcre
|        }

Its supported with or without the pcre library. Without pcre it just does a simple string 
compare. Either way it can take multiple arguments. It is meant to be used with the existing 
acl framework so a more complete config would look something like this

|        autoindex on;
|        location /ubuntu/ {
|            autoindex_ignore (db|conf);
|        }
|        location ~ /ubuntu/(db|conf)/ {
|            deny all;
|        }

Questions and comments are welcome.

Regards,
  Derrick

----
diff --git a/src/http/modules/ngx_http_autoindex_module.c b/src/http/modules/ngx_http_autoindex_module.
c
index b679318..0792aba 100644
--- a/src/http/modules/ngx_http_autoindex_module.c
+++ b/src/http/modules/ngx_http_autoindex_module.c
@@ -39,6 +39,7 @@ typedef struct {
     ngx_flag_t     enable;
     ngx_flag_t     localtime;
     ngx_flag_t     exact_size;
+    ngx_array_t   *ignore;
 } ngx_http_autoindex_loc_conf_t;


@@ -55,7 +56,8 @@ static ngx_int_t ngx_http_autoindex_init(ngx_conf_t *cf);
 static void *ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf);
 static char *ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf,
     void *parent, void *child);
-
+static char *ngx_http_autoindex_ignore(ngx_conf_t *cf,
+    ngx_command_t *cmd, void *conf);

 static ngx_command_t  ngx_http_autoindex_commands[] = {

@@ -80,6 +82,13 @@ static ngx_command_t  ngx_http_autoindex_commands[] = {
       offsetof(ngx_http_autoindex_loc_conf_t, exact_size),
       NULL },

+    { ngx_string("autoindex_ignore"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
+      ngx_http_autoindex_ignore,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      0,
+      NULL },
+
       ngx_null_command
 };

@@ -132,6 +141,80 @@ static u_char tail[] =
 "</html>" CRLF
 ;
+static char *
+ngx_http_autoindex_ignore(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+    ngx_http_autoindex_loc_conf_t  *alcf = conf;
+    ngx_str_t            *value;
+
+#if (NGX_PCRE)
+    ngx_uint_t            i;
+    ngx_regex_elt_t      *re;
+    ngx_regex_compile_t   rc;
+    u_char                errstr[NGX_MAX_CONF_ERRSTR];
+
+    if (alcf->ignore == NGX_CONF_UNSET_PTR) {
+        alcf->ignore = ngx_array_create(cf->pool, 2, sizeof(ngx_regex_elt_t));
+        if (alcf->ignore == NULL) {
+            return NGX_CONF_ERROR;
+        }
+    }
+
+    value = cf->args->elts;
+
+    ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
+
+    rc.pool = cf->pool;
+    rc.err.len = NGX_MAX_CONF_ERRSTR;
+    rc.err.data = errstr;
+
+    for (i = 1; i < cf->args->nelts; i++) {
+        re = ngx_array_push(alcf->ignore);
+        if (re == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        rc.pattern = value[i];
+        rc.options = NGX_REGEX_CASELESS;
+
+        if (ngx_regex_compile(&rc) != NGX_OK) {
+            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc.err);
+            return NGX_CONF_ERROR;
+        }
+
+        re->regex = rc.regex;
+        re->name = value[i].data;
+    }
+
+    return NGX_CONF_OK;
+
+#else
+    ngx_uint_t            i;
+    ngx_str_t            *str;
+
+    if (alcf->ignore == NGX_CONF_UNSET_PTR) {
+        alcf->ignore = ngx_array_create(cf->pool, 2, sizeof(ngx_str_t));
+        if (alcf->ignore == NULL) {
+            return NGX_CONF_ERROR;
+        }
+    }
+
+    value = cf->args->elts;
+
+    for (i = 1; i < cf->args->nelts; i++) {
+        str = ngx_array_push(alcf->ignore);
+        if (str == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        str->data = value[i].data;
+        str->len = value[i].len;
+    }
+
+    return NGX_CONF_OK;
+#endif
+}
+

 static ngx_int_t
 ngx_http_autoindex_handler(ngx_http_request_t *r)
@@ -281,6 +364,40 @@ ngx_http_autoindex_handler(ngx_http_request_t *r)
             continue;
         }


+#if (NGX_PCRE)
+
+        {
+        ngx_str_t   str;
+        str.data = ngx_de_name(&dir);
+        str.len = len;
+
+        if (alcf->ignore && ngx_regex_exec_array(alcf->ignore,
+                                     &str,
+                                     r->connection->log)
+                != NGX_DECLINED)
+        {
+            continue;
+        }
+        }
+#else
+        {
+        u_int found_match = 0;
+        ngx_str_t   *s;
+        if (alcf->ignore) {
+            s = alcf->ignore->elts;
+            for (i = 0; i < alcf->ignore->nelts; i++) {
+                if (ngx_strcmp(ngx_de_name(&dir), s[i].data) == 0) {
+                    found_match = 1;
+                    break;
+                }
+            }
+            if (found_match) {
+                continue;
+            }
+        }
+        }
+#endif
+
         if (!dir.valid_info) {

             /* 1 byte for '/' and 1 byte for terminating '\0' */
@@ -633,6 +750,7 @@ ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf)
     conf->enable = NGX_CONF_UNSET;
     conf->localtime = NGX_CONF_UNSET;
     conf->exact_size = NGX_CONF_UNSET;
+    conf->ignore = NGX_CONF_UNSET_PTR;

     return conf;
 }
@@ -647,6 +765,7 @@ ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
     ngx_conf_merge_value(conf->enable, prev->enable, 0);
     ngx_conf_merge_value(conf->localtime, prev->localtime, 0);
     ngx_conf_merge_value(conf->exact_size, prev->exact_size, 1);
+    ngx_conf_merge_ptr_value(conf->ignore, prev->ignore, NULL);

     return NGX_CONF_OK;
 }




More information about the nginx-devel mailing list