Patch for autoindex module: css file

ubitux ubitux at gmail.com
Thu May 13 02:32:53 MSD 2010


Hi,

I was trying to customize a little the dir listing html output, but I
didn't find any way to do that instead of hardcoding html/css in the
module source. So here is a patch which allows you to specify a "css file"
that will be appended in the header if set.

I didn't find any repository or special devel mailing list, so the patch
is based on the stable version, with a diff -u, 'hope it will be fine.

Also, as I just opened for the first time the nginx source code a few
hours ago, I may not use as well as expected the string helpers & co...

Please let me know what you think about this patch,

Regards,

-- 
ubitux
-------------- next part --------------
--- nginx-0.8.36/src/http/modules/ngx_http_autoindex_module.c	2009-06-02 18:09:44.000000000 +0200
+++ nginx-0.8.36/src/http/modules/ngx_http_autoindex_module.c	2010-05-13 00:22:06.132103745 +0200
@@ -39,6 +39,7 @@
     ngx_flag_t     enable;
     ngx_flag_t     localtime;
     ngx_flag_t     exact_size;
+    ngx_str_t      css_file;
 } ngx_http_autoindex_loc_conf_t;
 
 
@@ -80,6 +81,13 @@
       offsetof(ngx_http_autoindex_loc_conf_t, exact_size),
       NULL },
 
+    { ngx_string("autoindex_css_file"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_str_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_autoindex_loc_conf_t, css_file),
+      NULL },
+
       ngx_null_command
 };
 
@@ -120,10 +128,34 @@
 "<head><title>Index of "
 ;
 
+static u_char title_pre_css[] =
+"<html>" CRLF
+"<head><link rel=\"stylesheet\" type=\"text/css\" href=\""
+;
+
+static u_char title_post_css[] =
+"\" /><title>Index of "
+;
+
+static ngx_str_t
+get_title_with_css(ngx_str_t *css_file)
+{
+    ngx_str_t title;
+
+    title.len = sizeof(title_pre_css) + css_file->len + sizeof(title_post_css);
+    title.data = ngx_alloc(title.len + 1, ngx_cycle->log);
+
+    if (title.data == NULL) {
+        title.len = 0;
+        return title;
+    }
+
+    ngx_sprintf(title.data, "%s%s%s", title_pre_css, css_file->data, title_post_css);
+    return title;
+}
 
 static u_char header[] =
 "</title></head>" CRLF
-"<body bgcolor=\"white\">" CRLF
 "<h1>Index of "
 ;
 
@@ -132,7 +164,6 @@
 "</html>" CRLF
 ;
 
-
 static ngx_int_t
 ngx_http_autoindex_handler(ngx_http_request_t *r)
 {
@@ -143,7 +174,7 @@
     ngx_err_t                       err;
     ngx_buf_t                      *b;
     ngx_int_t                       rc, size;
-    ngx_str_t                       path;
+    ngx_str_t                       path, title_to_use;
     ngx_dir_t                       dir;
     ngx_uint_t                      i, level, utf8;
     ngx_pool_t                     *pool;
@@ -363,7 +394,18 @@
                       ngx_close_dir_n " \"%s\" failed", &path);
     }
 
-    len = sizeof(title) - 1
+    title_to_use.len = 0;
+    title_to_use.data = NULL;
+
+    if (alcf->css_file.len)
+        title_to_use = get_title_with_css(&alcf->css_file);
+
+    if (title_to_use.len == 0) {    /* No css_file specified or alloc failure */
+        title_to_use.data = title;
+        title_to_use.len = sizeof(title) - 1;
+    }
+
+    len = title_to_use.len
           + r->uri.len
           + sizeof(header) - 1
           + r->uri.len
@@ -397,7 +439,7 @@
                   ngx_http_autoindex_cmp_entries);
     }
 
-    b->last = ngx_cpymem(b->last, title, sizeof(title) - 1);
+    b->last = ngx_cpymem(b->last, title_to_use.data, title_to_use.len);
     b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len);
     b->last = ngx_cpymem(b->last, header, sizeof(header) - 1);
     b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len);
@@ -638,6 +680,8 @@
     conf->enable = NGX_CONF_UNSET;
     conf->localtime = NGX_CONF_UNSET;
     conf->exact_size = NGX_CONF_UNSET;
+    conf->css_file.len = 0;
+    conf->css_file.data = NULL;
 
     return conf;
 }
@@ -652,6 +696,7 @@
     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_str_value(conf->css_file, prev->css_file, "");
 
     return NGX_CONF_OK;
 }


More information about the nginx mailing list