annotate mod_http_dir_index/http_dir_index/mod_http_dir_index.lua @ 886:1647b4fac445

mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
author Kim Alvefur <zash@zash.se>
date Sun, 23 Dec 2012 15:36:33 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
886
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- Prosody IM
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- Copyright (C) 2012 Kim Alvefur
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 --
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- This project is MIT/X11 licensed. Please see the
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 --
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 module:set_global();
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local server = require"net.http.server";
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local lfs = require "lfs";
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local stat = lfs.attributes;
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local build_path = require"socket.url".build_path;
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local base64_encode = require"util.encodings".base64.encode;
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local tag = require"util.stanza".stanza;
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local template = require"util.template";
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local function get_resource(resource)
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local fh = assert(module:load_resource(resource));
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local data = fh:read"*a";
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 fh:close();
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 return data;
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local dir_index_template = template(get_resource("resources/template.html"));
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local style = get_resource("resources/style.css"):gsub("url%((.-)%)", function(url)
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 --module:log("debug", "Inlineing %s", url);
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 return "url(data:image/png;base64,"..base64_encode(get_resource("resources/"..url))..")";
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end);
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local function generate_directory_index(path, full_path)
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local filelist = tag("ul", { class = "filelist" } ):text"\n";
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 if path ~= "/" then
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 filelist:tag("li", { class = "parent directory" })
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 :tag("a", { href = "..", rel = "up" }):text("Parent Directory"):up():up():text"\n"
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 end
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 for file in lfs.dir(full_path) do
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 if file:sub(1,1) ~= "." then
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 local attr = stat(full_path..file) or {};
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 local path = { file };
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 path.is_directory = attr.mode == "directory";
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 filelist:tag("li", { class = attr.mode })
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 :tag("a", { href = build_path(path) }):text(file):up()
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 :up():text"\n";
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 end
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 return "<!DOCTYPE html>\n"..tostring(dir_index_template.apply{
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 path = path,
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 style = style,
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 filelist = filelist,
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 footer = "Prosody "..prosody.version,
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 });
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 module:hook_object_event(server, "directory-index", function (event)
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 local ok, data = pcall(generate_directory_index, event.path, event.full_path);
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 if ok then return data end
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 module:log("warn", data);
1647b4fac445 mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end);