Welcome! Log In Create A New Profile

Advanced

[PATCH] Add autoindex_css_file option

Clément Bœsch
February 20, 2011 12:04PM
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.
From d3c3318d570091ca0920363c0178e53b2f4eaa76 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux@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

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[PATCH] Add autoindex_css_file option

Clément Bœsch 2102 February 20, 2011 12:04PM

Re: [PATCH] Add autoindex_css_file option

Clément Bœsch 657 February 20, 2011 12:10PM

Re: [PATCH] Add autoindex_css_file option

Maxim Dounin 653 February 20, 2011 01:26PM

Re: [PATCH] Add autoindex_css_file option

Clément Bœsch 615 February 20, 2011 03:48PM

Re: [PATCH] Add autoindex_css_file option

Maxim Dounin 621 February 20, 2011 07:10PM

Re: [PATCH] Add autoindex_css_file option

Clément Bœsch 681 February 20, 2011 07:22PM

Re: [PATCH] Add autoindex_css_file option

Maxim Dounin 792 February 21, 2011 06:02AM

Re: [PATCH] Add autoindex_css_file option

Adrian Perez 652 February 21, 2011 07:40AM

Re: [PATCH] Add autoindex_css_file option

Clément Bœsch 647 February 23, 2011 04:22AM

Re: [PATCH] Add autoindex_css_file option

Clément Bœsch 792 March 03, 2011 07:58PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 106
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready