Add auto Option to autoindex module
Aaron U'Ren
aauren at gmail.com
Wed Nov 30 03:36:27 UTC 2016
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.
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.
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.
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.
Let me know what you guys think.
------------------------------------------------------------------------------------------------------------------------
# HG changeset patch
# User Aaron U'Ren <aauren at gmail.com>
# Date 1480475469 21600
# Tue Nov 29 21:11:09 2016 -0600
# Node ID a34bccf9c164fe2bca85c3f9d6fa1bea59fcea42
# Parent 52bd8cc17f34bbebcb4d2ce3651b40af7d572d55
add auto format option to autoindex module
Currently, you can only specify a single format (json, html, javascript,
xml) for autoindex to return to the client.
This change adds an "auto" option that obeys the client's Accept header (if
one is present) to return a specific index
format based on the clients request.
diff -r 52bd8cc17f34 -r a34bccf9c164
src/http/modules/ngx_http_autoindex_module.c
--- a/src/http/modules/ngx_http_autoindex_module.c Mon Nov 28 19:19:21
2016 +0300
+++ b/src/http/modules/ngx_http_autoindex_module.c Tue Nov 29 21:11:09
2016 -0600
@@ -49,6 +49,7 @@
#define NGX_HTTP_AUTOINDEX_JSON 1
#define NGX_HTTP_AUTOINDEX_JSONP 2
#define NGX_HTTP_AUTOINDEX_XML 3
+#define NGX_HTTP_AUTOINDEX_AUTO 4
#define NGX_HTTP_AUTOINDEX_PREALLOCATE 50
@@ -80,6 +81,7 @@
{ ngx_string("json"), NGX_HTTP_AUTOINDEX_JSON },
{ ngx_string("jsonp"), NGX_HTTP_AUTOINDEX_JSONP },
{ ngx_string("xml"), NGX_HTTP_AUTOINDEX_XML },
+ { ngx_string("auto"), NGX_HTTP_AUTOINDEX_AUTO },
{ ngx_null_string, 0 }
};
@@ -152,7 +154,7 @@
static ngx_int_t
ngx_http_autoindex_handler(ngx_http_request_t *r)
{
- u_char *last, *filename;
+ u_char *last, *filename, *accept_header;
size_t len, allocated, root;
ngx_err_t err;
ngx_buf_t *b;
@@ -200,6 +202,19 @@
format = alcf->format;
+#if (NGX_HTTP_HEADERS)
+ if (format == NGX_HTTP_AUTOINDEX_AUTO) {
+ accept_header = r->headers_in.accept->value.data;
+ if (ngx_strncmp(accept_header, "application/javascript", 22) == 0)
{
+ format = NGX_HTTP_AUTOINDEX_JSONP;
+ } else if (ngx_strncmp(accept_header, "application/json", 16) ==
0) {
+ format = NGX_HTTP_AUTOINDEX_JSON;
+ } else if (ngx_strncmp(accept_header, "text/xml", 8) == 0) {
+ format = NGX_HTTP_AUTOINDEX_XML;
+ }
+ }
+#endif
+
if (format == NGX_HTTP_AUTOINDEX_JSONP) {
if (ngx_http_autoindex_jsonp_callback(r, &callback) != NGX_OK) {
return NGX_HTTP_BAD_REQUEST;
------------------------------------------------------------------------------------------------------------------------
-Aaron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20161129/8b83b87f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: a34bccf9c164fe2bca85c3f9d6fa1bea59fcea42.patch
Type: text/x-patch
Size: 2396 bytes
Desc: not available
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20161129/8b83b87f/attachment.bin>
More information about the nginx-devel
mailing list