【问题标题】:What NGINX autoindex_format JSON(P) response fields/format should I expect?我应该期待什么 NGINX autoindex_format JSON(P) 响应字段/格式?
【发布时间】:2018-04-07 07:54:01
【问题描述】:

NGINX ngx-http-autoindex-module 支持 autoindex_format 指令将自动索引响应从默认 html 页面更改为 xml、json 或 jsonp。

官方文档对该主题非常简短,没有提供有关 JSON(P) 响应的进一步解释或规则。此外,谷歌只提供了一些相关的搜索结果,没有一个提供进一步的见解,甚至没有简单的例子。

那么对于 json 和 jsonp,我应该在响应中期望哪些字段和值类型(甚至是固定值)?
在使用响应和生成 html 输出时,我还应该了解其他规则吗?

【问题讨论】:

    标签: json nginx format jsonp


    【解决方案1】:

    自 1.7.9 版(2014 年 12 月 23 日发布)起,NGINX 中包含的ngx_http_autoindex_module 模块(为以斜杠字符结尾的请求生成目录列表:/) 添加一个autoindex_format directive,它设置目录列表的格式

    语法: autoindex_formathtml | xml | json | jsonp;
    默认值: autoindex_format html
    上下文: http, server, location

    示例 启用此功能的配置(对于 JSON):

    location / {
        autoindex on;
        autoindex_format json;
    }
    

    当使用 JSONP 格式时,回调函数的名称使用 callback 请求参数设置。 如果该参数缺失或值为空,则返回 JSON 格式

    JSON(P) 响应数据结构(方案)定义为:

    callback(  // when format directive jsonp AND callback argument in request
      [ // a single one-dimensional Array (wrap) containing
        { // Objects representing directory entries, structured as:
          "name" :"*"                             //String
        , "type" :"directory"|"file"|"other"      //String, ONLY 1 OF THESE 3
        , "mtime":"Ddd, DD Mmm YYYY hh:mm:ss GMT" //String, RFC1123-date of HTTP-date defined by RFC2616
        , "size" :*   //Integer,  ONLY PRESENT IFF "type":"file" !!!!! 
        } /*
      , { // and repeating the above Object structure for each directory entry
        } */
      ]
    ); // end callbackFunction-call when jsonp
    

    "name""mtime""type":"directory""type":"file" 应该是不言自明的。
    重要要注意 "size" 字段仅存在于 IFF @ 987654343@,否则完全省略(参见下面的示例)!

    "type":"other" 值得进一步解释:
    假设您做了一些您在生产中永远不会做的事情,并在 linux 系统的配置中设置 'root /;',然后索引 /dev/,那么您将获得如下目录条目:

    [
    { "name":"floppy", "type":"other", "mtime":"Mon, 23 Oct 2017 15:16:56 GMT" },
    { "name":"loop0", "type":"other", "mtime":"Mon, 23 Oct 2017 15:16:56 GMT" }
    // and so forth
    ]
    

    "type":"other"可能还有其他例子,如果你知道一些,请发表评论!

    特殊点当前/点父(...点文件/点文件夹(.hidden)在响应中被过滤(不存在)!
    有趣的是:特殊的 dot-current/dot-parent (., ..) 可以由您的客户端代码添加,就像 ngx_http_autoindex_module 将它们硬编码到默认的 html 格式响应中一样。


    以下是原始“格式”中的两个“原始”响应示例
    注意:响应行结尾被硬编码为CRLF

    JSON(和没有回调的 JSONP):

    [
    { "name":"subdir", "type":"directory", "mtime":"Tue, 24 Oct 2017 16:16:16 GMT" },
    { "name":"image.jpg", "type":"file", "mtime":"Tue, 24 Oct 2017 16:16:39 GMT", "size":5 },
    { "name":"test.html", "type":"file", "mtime":"Tue, 24 Oct 2017 16:09:18 GMT", "size":5 }
    ]
    

    JSONP(带有 callback=foo):
    注意:前导块注释是 JSONP 响应的一部分。

    /* callback */
    foo([
    { "name":"subdir", "type":"directory", "mtime":"Tue, 24 Oct 2017 16:16:16 GMT" },
    { "name":"image.jpg", "type":"file", "mtime":"Tue, 24 Oct 2017 16:16:39 GMT", "size":5 },
    { "name":"test.html", "type":"file", "mtime":"Tue, 24 Oct 2017 16:09:18 GMT", "size":5 }
    ]);
    



    一个非常简单的 sn-p 消耗一个 JSONP 回调并构建一个 HTML 表,旨在明确地阐明格式:

    <script>
    function foo(d){
      var i= 0
      ,   L= d.length
      ,   r= '<table border="1" cellpadding="4px" style="border-collapse:collapse"><thead style="border-bottom:4px solid black">\n'
           + '<tr><th style="width:80%">Name</th><th>Type</th><th>Last Modified</th><th>Size</th></tr>\n'
           + '</thead><tbody>\n'
      ;
      for(;i<L;++i)
        r+= '<tr><td>'  + d[i].name
          + '</td><td>' + d[i].type
          + '</td><td style="white-space: nowrap">' + d[i].mtime
          + '</td><td>' + (d[i].type==='file' ? d[i].size : '-')
          + '</td></tr>\n';
    
      r+='</tbody></table>';
      document.body.innerHTML=r;
    }
    </script>
    
    <script> // JSONP        Use your own preferred JSON(P) fetchmethod
    /* callback */
    foo([
    { "name":"subdir", "type":"directory", "mtime":"Tue, 24 Oct 2017 16:16:16 GMT" },
    { "name":"image.jpg", "type":"file", "mtime":"Tue, 24 Oct 2017 16:16:39 GMT", "size":5 },
    { "name":"test.html", "type":"file", "mtime":"Tue, 24 Oct 2017 16:09:18 GMT", "size":5 },
    { "name":"????", "type":"other", "mtime":"Tue, 24 Oct 2017 16:17:52 GMT" }
    ]);
    </script>

    一些额外的灵感可能来自:

    • nginx-autoindex-js(Nginx JSON 自动索引格式的 JavaScript 前端)
    • pretty-autoindex
      (目前有一个'bug':它在 type=directory 时隐藏大小,而不是在 type!=file 时隐藏大小的正确行为)

    来源:

    【讨论】:

      猜你喜欢
      • 2018-06-11
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 2020-05-01
      • 2012-05-10
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多