Skip to content

Instantly share code, notes, and snippets.

@aa65535
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aa65535/a49bd62c3f8139c9c000 to your computer and use it in GitHub Desktop.
Save aa65535/a49bd62c3f8139c9c000 to your computer and use it in GitHub Desktop.
Nginx 打开目录浏览功能后, 可设置文件名显示长度(宽度), 选项名: `autoindex_name_length`, 值为整数 默认: 50. https://github.com/aa65535/Private-Mixed/tree/master/nginx-patch
--- a/ngx_http_autoindex_module.c Tue Aug 5 19:18:36 2014
+++ b/ngx_http_autoindex_module.c Wed Aug 20 14:17:28 2014
@@ -40,13 +40,12 @@
ngx_flag_t enable;
ngx_flag_t localtime;
ngx_flag_t exact_size;
+ ngx_uint_t name_length;
} ngx_http_autoindex_loc_conf_t;
#define NGX_HTTP_AUTOINDEX_PREALLOCATE 50
-#define NGX_HTTP_AUTOINDEX_NAME_LEN 50
-
static int ngx_libc_cdecl ngx_http_autoindex_cmp_entries(const void *one,
const void *two);
@@ -81,6 +80,13 @@
offsetof(ngx_http_autoindex_loc_conf_t, exact_size),
NULL },
+ { ngx_string("autoindex_name_length"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_num_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_autoindex_loc_conf_t, name_length),
+ NULL },
+
ngx_null_command
};
@@ -125,7 +131,7 @@
static u_char header[] =
"</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
-"<h1>Index of "
+"<b>Index of "
;
static u_char tail[] =
@@ -138,12 +144,12 @@
ngx_http_autoindex_handler(ngx_http_request_t *r)
{
u_char *last, *filename, scale;
- off_t length;
size_t len, char_len, escape_html, allocated, root;
ngx_tm_t tm;
ngx_err_t err;
ngx_buf_t *b;
- ngx_int_t rc, size;
+ ngx_int_t rc;
+ float size, length;
ngx_str_t path;
ngx_dir_t dir;
ngx_uint_t i, level, utf8;
@@ -367,7 +373,7 @@
+ r->uri.len + escape_html
+ sizeof(header) - 1
+ r->uri.len + escape_html
- + sizeof("</h1>") - 1
+ + sizeof("</b>") - 1
+ sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1
+ sizeof("</pre><hr>") - 1
+ sizeof(tail) - 1;
@@ -380,7 +386,7 @@
+ sizeof("\">") - 1
+ entry[i].name.len - entry[i].utf_len
+ entry[i].escape_html
- + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof("&gt;") - 2
+ + alcf->name_length + sizeof("&gt;") - 2
+ sizeof("</a>") - 1
+ sizeof(" 28-Sep-1970 12:00 ") - 1
+ 20 /* the file size */
@@ -411,7 +417,7 @@
b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len);
}
- b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1);
+ b->last = ngx_cpymem(b->last, "</b>", sizeof("</b>") - 1);
b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF,
sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1);
@@ -442,11 +448,11 @@
len = entry[i].utf_len;
if (entry[i].name.len != len) {
- if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) {
- char_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3 + 1;
+ if (len > alcf->name_length) {
+ char_len = alcf->name_length - 3 + 1;
} else {
- char_len = NGX_HTTP_AUTOINDEX_NAME_LEN + 1;
+ char_len = alcf->name_length + 1;
}
last = b->last;
@@ -462,8 +468,8 @@
} else {
if (entry[i].escape_html) {
- if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) {
- char_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3;
+ if (len > alcf->name_length) {
+ char_len = alcf->name_length - 3;
} else {
char_len = len;
@@ -475,25 +481,25 @@
} else {
b->last = ngx_cpystrn(b->last, entry[i].name.data,
- NGX_HTTP_AUTOINDEX_NAME_LEN + 1);
+ alcf->name_length + 1);
last = b->last - 3;
}
}
- if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) {
+ if (len > alcf->name_length) {
b->last = ngx_cpymem(last, "..&gt;</a>", sizeof("..&gt;</a>") - 1);
} else {
- if (entry[i].dir && NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) {
+ if (entry[i].dir && alcf->name_length - len > 0) {
*b->last++ = '/';
len++;
}
b->last = ngx_cpymem(b->last, "</a>", sizeof("</a>") - 1);
- if (NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) {
- ngx_memset(b->last, ' ', NGX_HTTP_AUTOINDEX_NAME_LEN - len);
- b->last += NGX_HTTP_AUTOINDEX_NAME_LEN - len;
+ if (alcf->name_length - len > 0) {
+ ngx_memset(b->last, ' ', alcf->name_length - len);
+ b->last += alcf->name_length - len;
}
}
@@ -522,38 +528,27 @@
sizeof(" -") - 1);
} else {
- length = entry[i].size;
+ length = (float) entry[i].size;
if (length > 1024 * 1024 * 1024 - 1) {
- size = (ngx_int_t) (length / (1024 * 1024 * 1024));
- if ((length % (1024 * 1024 * 1024))
- > (1024 * 1024 * 1024 / 2 - 1))
- {
- size++;
- }
+ size = length / (1024 * 1024 * 1024);
scale = 'G';
} else if (length > 1024 * 1024 - 1) {
- size = (ngx_int_t) (length / (1024 * 1024));
- if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) {
- size++;
- }
+ size = length / (1024 * 1024);
scale = 'M';
- } else if (length > 9999) {
- size = (ngx_int_t) (length / 1024);
- if (length % 1024 > 511) {
- size++;
- }
+ } else if (length > 1023) {
+ size = length / 1024;
scale = 'K';
} else {
- size = (ngx_int_t) length;
- scale = '\0';
+ size = length;
+ scale = 'B';
}
if (scale) {
- b->last = ngx_sprintf(b->last, "%6i%c", size, scale);
+ b->last = ngx_sprintf(b->last, "%6.2f%c", size, scale);
} else {
b->last = ngx_sprintf(b->last, " %6i", size);
@@ -667,6 +662,7 @@
conf->enable = NGX_CONF_UNSET;
conf->localtime = NGX_CONF_UNSET;
conf->exact_size = NGX_CONF_UNSET;
+ conf->name_length = NGX_CONF_UNSET_UINT;
return conf;
}
@@ -681,6 +677,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_uint_value(conf->name_length, prev->name_length, 50);
return NGX_CONF_OK;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment