<div dir="ltr">I recently came across the need to have multiple formats returned from the autoindex module based on the client. When a human hits the index page I needed it returned in HTML format. However, when an application fetched the index page it was much simpler for it to digest and parse JSON or XML. With the current autoindex module you could only specify a single format in the configuration.<div><br></div><div>It occurred to me that it might sometimes be better to have an option to allow the user agent to choose what format it wanted from the autoindex module rather than it being defined statically by the server's configuration. So I made a small patch that introduces a 5th "auto" option in addition to html, json, jsonp, and xml.</div><div><br></div><div>With this option enabled the nginx server reads the "Accept" header from the user agent's request and if it is able to satisfy the format it will send the response in that format. Otherwise it will gracefully fall back to serving the response in the default html format.</div><div><br></div><div>Fair warning, my C is pretty rusty and I'm not at all that familiar with the nginx code base, but I was able to put together and successfully test the following patch on my machine.</div><div><br></div><div>Let me know what you guys think.</div><div><br></div><div><div><font face="monospace, monospace" size="1">------------------------------------------------------------------------------------------------------------------------</font></div><div><font face="monospace, monospace" size="1"><br></font></div><div><font face="monospace, monospace" size="1"># HG changeset patch</font></div><div><font face="monospace, monospace" size="1"># User Aaron U'Ren <<a href="mailto:aauren@gmail.com">aauren@gmail.com</a>></font></div><div><font face="monospace, monospace" size="1"># Date 1480475469 21600</font></div><div><font face="monospace, monospace" size="1">#      Tue Nov 29 21:11:09 2016 -0600</font></div><div><font face="monospace, monospace" size="1"># Node ID a34bccf9c164fe2bca85c3f9d6fa1bea59fcea42</font></div><div><font face="monospace, monospace" size="1"># Parent  52bd8cc17f34bbebcb4d2ce3651b40af7d572d55</font></div><div><font face="monospace, monospace" size="1">add auto format option to autoindex module</font></div><div><font face="monospace, monospace" size="1"><br></font></div><div><font face="monospace, monospace" size="1">Currently, you can only specify a single format (json, html, javascript, xml) for autoindex to return to the client.</font></div><div><font face="monospace, monospace" size="1">This change adds an "auto" option that obeys the client's Accept header (if one is present) to return a specific index</font></div><div><font face="monospace, monospace" size="1">format based on the clients request.</font></div><div><font face="monospace, monospace" size="1"><br></font></div><div><font face="monospace, monospace" size="1">diff -r 52bd8cc17f34 -r a34bccf9c164 src/http/modules/ngx_http_autoindex_module.c</font></div><div><font face="monospace, monospace" size="1">--- a/src/http/modules/ngx_http_autoindex_module.c      Mon Nov 28 19:19:21 2016 +0300</font></div><div><font face="monospace, monospace" size="1">+++ b/src/http/modules/ngx_http_autoindex_module.c      Tue Nov 29 21:11:09 2016 -0600</font></div><div><font face="monospace, monospace" size="1">@@ -49,6 +49,7 @@</font></div><div><font face="monospace, monospace" size="1"> #define NGX_HTTP_AUTOINDEX_JSON         1</font></div><div><font face="monospace, monospace" size="1"> #define NGX_HTTP_AUTOINDEX_JSONP        2</font></div><div><font face="monospace, monospace" size="1"> #define NGX_HTTP_AUTOINDEX_XML          3</font></div><div><font face="monospace, monospace" size="1">+#define NGX_HTTP_AUTOINDEX_AUTO         4</font></div><div><font face="monospace, monospace" size="1"><br></font></div><div><font face="monospace, monospace" size="1"> #define NGX_HTTP_AUTOINDEX_PREALLOCATE  50</font></div><div><font face="monospace, monospace" size="1"><br></font></div><div><font face="monospace, monospace" size="1">@@ -80,6 +81,7 @@</font></div><div><font face="monospace, monospace" size="1">     { ngx_string("json"), NGX_HTTP_AUTOINDEX_JSON },</font></div><div><font face="monospace, monospace" size="1">     { ngx_string("jsonp"), NGX_HTTP_AUTOINDEX_JSONP },</font></div><div><font face="monospace, monospace" size="1">     { ngx_string("xml"), NGX_HTTP_AUTOINDEX_XML },</font></div><div><font face="monospace, monospace" size="1">+    { ngx_string("auto"), NGX_HTTP_AUTOINDEX_AUTO },</font></div><div><font face="monospace, monospace" size="1">     { ngx_null_string, 0 }</font></div><div><font face="monospace, monospace" size="1"> };</font></div><div><font face="monospace, monospace" size="1"><br></font></div><div><font face="monospace, monospace" size="1">@@ -152,7 +154,7 @@</font></div><div><font face="monospace, monospace" size="1"> static ngx_int_t</font></div><div><font face="monospace, monospace" size="1"> ngx_http_autoindex_handler(ngx_http_request_t *r)</font></div><div><font face="monospace, monospace" size="1"> {</font></div><div><font face="monospace, monospace" size="1">-    u_char                         *last, *filename;</font></div><div><font face="monospace, monospace" size="1">+    u_char                         *last, *filename, *accept_header;</font></div><div><font face="monospace, monospace" size="1">     size_t                          len, allocated, root;</font></div><div><font face="monospace, monospace" size="1">     ngx_err_t                       err;</font></div><div><font face="monospace, monospace" size="1">     ngx_buf_t                      *b;</font></div><div><font face="monospace, monospace" size="1">@@ -200,6 +202,19 @@</font></div><div><font face="monospace, monospace" size="1"><br></font></div><div><font face="monospace, monospace" size="1">     format = alcf->format;</font></div><div><font face="monospace, monospace" size="1"><br></font></div><div><font face="monospace, monospace" size="1">+#if (NGX_HTTP_HEADERS)</font></div><div><font face="monospace, monospace" size="1">+    if (format == NGX_HTTP_AUTOINDEX_AUTO) {</font></div><div><font face="monospace, monospace" size="1">+        accept_header = r->headers_in.accept->value.data;</font></div><div><font face="monospace, monospace" size="1">+        if (ngx_strncmp(accept_header, "application/javascript", 22) == 0) {</font></div><div><font face="monospace, monospace" size="1">+            format = NGX_HTTP_AUTOINDEX_JSONP;</font></div><div><font face="monospace, monospace" size="1">+        } else if (ngx_strncmp(accept_header, "application/json", 16) == 0) {</font></div><div><font face="monospace, monospace" size="1">+            format = NGX_HTTP_AUTOINDEX_JSON;</font></div><div><font face="monospace, monospace" size="1">+        } else if (ngx_strncmp(accept_header, "text/xml", 8) == 0) {</font></div><div><font face="monospace, monospace" size="1">+            format = NGX_HTTP_AUTOINDEX_XML;</font></div><div><font face="monospace, monospace" size="1">+        }</font></div><div><font face="monospace, monospace" size="1">+    }</font></div><div><font face="monospace, monospace" size="1">+#endif</font></div><div><font face="monospace, monospace" size="1">+</font></div><div><font face="monospace, monospace" size="1">     if (format == NGX_HTTP_AUTOINDEX_JSONP) {</font></div><div><font face="monospace, monospace" size="1">         if (ngx_http_autoindex_jsonp_callback(r, &callback) != NGX_OK) {</font></div><div><font face="monospace, monospace" size="1">             return NGX_HTTP_BAD_REQUEST;</font></div><div><font face="monospace, monospace" size="1"><br></font></div><div><span style="font-family:monospace,monospace;font-size:x-small">------------------------------------------------------------------------------------------------------------------------</span><font face="monospace, monospace" size="1"><br></font></div><div><br></div><div><div class="gmail_signature"><div><br></div>-Aaron</div></div>
</div></div>