Comparison

plugins/mod_http_files.lua @ 5263:736e7ec8cd2e

mod_http_files: Replace file listing with an event, allowing a different plugin to generate it
author Kim Alvefur <zash@zash.se>
date Fri, 21 Dec 2012 20:08:33 +0100
parent 5262:4e58fde55594
child 5264:700ff21a0451
comparison
equal deleted inserted replaced
5262:4e58fde55594 5263:736e7ec8cd2e
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 module:depends("http"); 9 module:depends("http");
10 local server = require"net.http.server";
10 local lfs = require "lfs"; 11 local lfs = require "lfs";
11 12
12 local os_date = os.date; 13 local os_date = os.date;
13 local open = io.open; 14 local open = io.open;
14 local stat = lfs.attributes; 15 local stat = lfs.attributes;
92 if stat(full_path..dir_indices[i], "mode") == "file" then 93 if stat(full_path..dir_indices[i], "mode") == "file" then
93 return serve_file(event, path..dir_indices[i]); 94 return serve_file(event, path..dir_indices[i]);
94 end 95 end
95 end 96 end
96 97
97 if not directory_index then 98 if directory_index then
99 data = server._events.fire_event("directory-index", { path = request.path, full_path = full_path });
100 end
101 if not data then
98 return 403; 102 return 403;
99 else
100 local html = require"util.stanza".stanza("html")
101 :tag("head"):tag("title"):text(path):up()
102 :tag("meta", { charset="utf-8" }):up()
103 :up()
104 :tag("body"):tag("h1"):text(path):up()
105 :tag("ul");
106 for file in lfs.dir(full_path) do
107 if file:sub(1,1) ~= "." then
108 local attr = stat(full_path..file) or {};
109 html:tag("li", { class = attr.mode })
110 :tag("a", { href = file }):text(file)
111 :up():up();
112 end
113 end
114 data = "<!DOCTYPE html>\n"..tostring(html);
115 cache[path] = { data = data, content_type = mime_map.html; etag = etag; };
116 response_headers.content_type = mime_map.html;
117 end 103 end
104 cache[path] = { data = data, content_type = mime_map.html; etag = etag; };
105 response_headers.content_type = mime_map.html;
118 106
119 else 107 else
120 local f, err = open(full_path, "rb"); 108 local f, err = open(full_path, "rb");
121 if f then 109 if f then
122 data, err = f:read("*a"); 110 data, err = f:read("*a");