从零手写实现 nginx-06-文件夹内容的主动索引展示

打印 上一主题 下一主题

主题 882|帖子 882|积分 2646

前言

大家好,我是老马。很高兴遇到你。
我们希望实现最简朴的 http 服务信息,可以处理静态文件。
如果你想知道 servlet 如那边理的,可以参考我的另一个项目:
手写从零实现浅显版 tomcat minicat
手写 nginx 系列

如果你对 nginx 原理感兴趣,可以阅读:
从零手写实现 nginx-01-为什么不能有 java 版本的 nginx?
从零手写实现 nginx-02-nginx 的核心本领
从零手写实现 nginx-03-nginx 基于 Netty 实现
从零手写实现 nginx-04-基于 netty http 出入参优化处理
从零手写实现 nginx-05-MIME类型(Multipurpose Internet Mail Extensions,多用途互联网邮件扩展类型)
从零手写实现 nginx-06-文件夹主动索引
从零手写实现 nginx-07-大文件下载
从零手写实现 nginx-08-范围查询
从零手写实现 nginx-09-文件压缩
从零手写实现 nginx-10-sendfile 零拷贝
从零手写实现 nginx-11-file+range 合并
从零手写实现 nginx-12-keep-alive 连接复用
从零手写实现 nginx-13-nginx.conf 设置文件介绍
从零手写实现 nginx-14-nginx.conf 和 hocon 格式有关系吗?
从零手写实现 nginx-15-nginx.conf 如何通过 java 解析处理?
从零手写实现 nginx-16-nginx 支持设置多个 server
目标

这一节我们想实现如果访问的是文件夹,我们就把文件夹内容全部列出来。
然后用户可以自己点击跳转,实现文件夹的主动索引。
思绪

直接判断是否为文件夹,如果是则列出所有的文件信息。
然后构建一个 html 返回即可。
核心实现

文件夹相应
  1.     /**
  2.      * 构建文件夹结果
  3.      * @param targetFile 目标文件
  4.      * @param request 请求
  5.      * @param nginxConfig 配置
  6.      * @return 结果
  7.      * @since 0.5.0
  8.      * @author 老马啸西风
  9.      */
  10.     protected FullHttpResponse buildDirResp(File targetFile, final FullHttpRequest request, final NginxConfig nginxConfig) {
  11.         try {
  12.             String html = generateFileListHTML(targetFile, request, nginxConfig);
  13.             byte[] fileContent = html.getBytes(nginxConfig.getCharset());
  14.             FullHttpResponse response = buildCommentResp(fileContent, HttpResponseStatus.OK, request, nginxConfig);
  15.             setContentType(response, "text/html;");
  16.             return response;
  17.         } catch (Exception e) {
  18.             throw new Nginx4jException(e);
  19.         }
  20.     }
复制代码
文件夹对应的内容
  1. protected String generateFileListHTML(File directory, final FullHttpRequest request, final NginxConfig nginxConfig) {
  2.         // 确保传入的是一个目录
  3.         if (!directory.isDirectory()) {
  4.             return "Error: The specified path is not a directory.";
  5.         }
  6.         StringBuilder htmlBuilder = new StringBuilder();
  7.         htmlBuilder.append("<html><head><title>File List</title></head><body>");
  8.         htmlBuilder.append("<h1>File List</h1>");
  9.         htmlBuilder.append("<ul>");
  10.         File[] fileList = directory.listFiles();
  11.         for (File file : fileList) {
  12.             String fileName = file.getName();
  13.             String fileLink = getFileLink(file, request, nginxConfig);
  14.             htmlBuilder.append("<li><a target="_blank" href="").append(fileLink).append("">").append(fileName).append("</a></li>");
  15.         }
  16.         htmlBuilder.append("</ul></body></html>");
  17.         return htmlBuilder.toString();
  18. }
复制代码
其中链接的构建也很简朴:
  1. protected String getFileLink(File file, final FullHttpRequest request, final NginxConfig nginxConfig) {
  2.     String fileName = file.getName();
  3.     return FileUtil.buildFullPath(request.uri(), fileName);
  4. }
复制代码
小结

本节我们实现了一个文件夹的主动索引支持,整体而言非常简朴。
当然,也可以添加样式,或者是【..】之类的上级跳转,让用户体验更佳。我们暂时不做处理。
甚至做的更好一些,可以加上常见文件类型的 logo。
下一节,我们一起来看一下大文件分段请求的处理。
我是老马,期待与你的下次相逢。
开源地点

为了便于大家学习,已经将 nginx 开源
https://github.com/houbb/nginx4j

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

怀念夏天

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表