[PATCH] http: add autoindex_name_length

Maciej Krüger maciej at xeredo.it
Tue Dec 7 05:54:19 UTC 2021


# HG changeset patch
# User Maciej Krüger <maciej at xeredo.it>
# Date 1638856063 -3600
#      Tue Dec 07 06:47:43 2021 +0100
# Node ID 8c9f2abb0e4ee6302203e1fa3e63ad218d6251cb
# Parent  a7a77549265ef46f1f0fdb3897f4beabf9e09c40
http: add autoindex_name_length

This adds an option to change the name length from the hardcoded 50
characters to whatever the user desires

In my case specifically it is the long filenames openwrt produces
that I need displayed properly, but others might have similar
usecases where having this option makes sense

Can be used like so

autoindex on;
autoindex_name_length 100;

diff -r a7a77549265e -r 8c9f2abb0e4e src/http/modules/ngx_http_autoindex_module.c
--- a/src/http/modules/ngx_http_autoindex_module.c	Thu Nov 25 22:02:10 2021 +0300
+++ b/src/http/modules/ngx_http_autoindex_module.c	Tue Dec 07 06:47:43 2021 +0100
@@ -42,6 +42,7 @@
     ngx_uint_t     format;
     ngx_flag_t     localtime;
     ngx_flag_t     exact_size;
+    ngx_uint_t    name_length;
 } ngx_http_autoindex_loc_conf_t;
 
 
@@ -50,10 +51,6 @@
 #define NGX_HTTP_AUTOINDEX_JSONP        2
 #define NGX_HTTP_AUTOINDEX_XML          3
 
-#define NGX_HTTP_AUTOINDEX_PREALLOCATE  50
-
-#define NGX_HTTP_AUTOINDEX_NAME_LEN     50
-
 
 static ngx_buf_t *ngx_http_autoindex_html(ngx_http_request_t *r,
     ngx_array_t *entries);
@@ -114,6 +111,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
 };
 
@@ -186,8 +190,7 @@
         return rc;
     }
 
-    last = ngx_http_map_uri_to_path(r, &path, &root,
-                                    NGX_HTTP_AUTOINDEX_PREALLOCATE);
+    last = ngx_http_map_uri_to_path(r, &path, &root, alcf->name_length);
     if (last == NULL) {
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
@@ -324,7 +327,7 @@
 
             if (path.len + 1 + len + 1 > allocated) {
                 allocated = path.len + 1 + len + 1
-                                     + NGX_HTTP_AUTOINDEX_PREALLOCATE;
+                                     + alcf->name_length;
 
                 filename = ngx_pnalloc(pool, allocated);
                 if (filename == NULL) {
@@ -472,6 +475,8 @@
         utf8 = 0;
     }
 
+    alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module);
+
     escape_html = ngx_escape_html(NULL, r->uri.data, r->uri.len);
 
     len = sizeof(title) - 1
@@ -505,7 +510,7 @@
                   + sizeof("\">") - 1
                   + entry[i].name.len - entry[i].utf_len
                   + entry[i].escape_html
-                  + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof(">") - 2
+                  + alcf->name_length + sizeof(">") - 2
                   + sizeof("</a>") - 1
                   + sizeof(" 28-Sep-1970 12:00 ") - 1
                   + 20                                   /* the file size */
@@ -541,7 +546,6 @@
     b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF,
                          sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1);
 
-    alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module);
     tp = ngx_timeofday();
 
     for (i = 0; i < entries->nelts; i++) {
@@ -568,11 +572,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;
@@ -588,8 +592,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;
@@ -601,25 +605,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, "..></a>", sizeof("..></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;
             }
         }
 
@@ -1048,6 +1052,7 @@
                               NGX_HTTP_AUTOINDEX_HTML);
     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;
 }


More information about the nginx-devel mailing list