[PATCH] Add autoindex_css_file option

Clément Bœsch ubitux at gmail.com
Sun Feb 20 20:03:45 MSK 2011


Hi,

I sent a while ago a patch on nginx mailing list, and recently realized
it was the wrong place. So I took the time to update and fix the old patch
for nginx current development version.

This patch basically adds an autoindex_css_file option in order to allow
some customizations in the index listing.

One thing I'm not sure about is the ngx_alloc call; I didn't find any
ngx_asprintf or such, but maybe there is some quicker/better way to do it.

Also, I didn't add the prototype declaration since it's a static function;
should I?

Regards,

-- 
Clément B.
-------------- next part --------------
From d3c3318d570091ca0920363c0178e53b2f4eaa76 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
Date: Sun, 20 Feb 2011 17:57:03 +0100
Subject: [PATCH] Add autoindex_css_file option.

---
 src/http/modules/ngx_http_autoindex_module.c |   48 +++++++++++++++++++++++--
 1 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/src/http/modules/ngx_http_autoindex_module.c b/src/http/modules/ngx_http_autoindex_module.c
index b679318..499d595 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_str_t      css_file;
 } ngx_http_autoindex_loc_conf_t;
 
 
@@ -80,6 +81,13 @@ static ngx_command_t  ngx_http_autoindex_commands[] = {
       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,37 @@ static u_char title[] =
 "<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 void
+ngx_http_autoindex_get_hdr(ngx_log_t *log, ngx_str_t *dst, ngx_str_t *css_file)
+{
+    if (!css_file->len)
+        goto no_custom_css;
+
+    dst->len = sizeof(title_pre_css) - 1
+               + css_file->len
+               + sizeof(title_post_css) - 1;
+    dst->data = ngx_alloc(dst->len + 1, log);
+    if (!dst->data)
+        goto no_custom_css;
+
+    ngx_sprintf(dst->data, "%s%s%s", title_pre_css, css_file->data,
+                title_post_css);
+    return;
+
+no_custom_css:
+    dst->data = title;
+    dst->len = sizeof(title) - 1;
+}
 
 static u_char header[] =
 "</title></head>" CRLF
-"<body bgcolor=\"white\">" CRLF
 "<h1>Index of "
 ;
 
@@ -143,7 +178,7 @@ ngx_http_autoindex_handler(ngx_http_request_t *r)
     ngx_err_t                       err;
     ngx_buf_t                      *b;
     ngx_int_t                       rc, size;
-    ngx_str_t                       path;
+    ngx_str_t                       path, html_hdr;
     ngx_dir_t                       dir;
     ngx_uint_t                      i, level, utf8;
     ngx_pool_t                     *pool;
@@ -358,7 +393,9 @@ ngx_http_autoindex_handler(ngx_http_request_t *r)
                       ngx_close_dir_n " \"%s\" failed", &path);
     }
 
-    len = sizeof(title) - 1
+    ngx_http_autoindex_get_hdr(r->connection->log, &html_hdr, &alcf->css_file);
+
+    len = html_hdr.len
           + r->uri.len
           + sizeof(header) - 1
           + r->uri.len
@@ -392,7 +429,7 @@ ngx_http_autoindex_handler(ngx_http_request_t *r)
                   ngx_http_autoindex_cmp_entries);
     }
 
-    b->last = ngx_cpymem(b->last, title, sizeof(title) - 1);
+    b->last = ngx_cpymem(b->last, html_hdr.data, html_hdr.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);
@@ -633,6 +670,8 @@ 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->css_file.len = 0;
+    conf->css_file.data = NULL;
 
     return conf;
 }
@@ -647,6 +686,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_str_value(conf->css_file, prev->css_file, "");
 
     return NGX_CONF_OK;
 }
-- 
1.7.4.1

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://nginx.org/pipermail/nginx-devel/attachments/20110220/60d2816c/attachment.pgp>


More information about the nginx-devel mailing list