Comparison

mod_admin_web/admin_web/mod_admin_web.lua @ 885:292ea8df7267

mod_admin_web: Let mod_http_files handle file serving
author Kim Alvefur <zash@zash.se>
date Sat, 22 Dec 2012 21:09:41 +0100
parent 770:a85d86a7f24e
child 901:e3ad5f3aa6d4
comparison
equal deleted inserted replaced
884:2ece37bf9cc6 885:292ea8df7267
20 local st = require "util.stanza"; 20 local st = require "util.stanza";
21 local uuid_generate = require "util.uuid".generate; 21 local uuid_generate = require "util.uuid".generate;
22 local is_admin = require "core.usermanager".is_admin; 22 local is_admin = require "core.usermanager".is_admin;
23 local pubsub = require "util.pubsub"; 23 local pubsub = require "util.pubsub";
24 local jid_bare = require "util.jid".bare; 24 local jid_bare = require "util.jid".bare;
25 local lfs = require "lfs";
26 local open = io.open;
27 local stat = lfs.attributes;
28 25
29 module:set_global(); 26 module:set_global();
30 27
31 local service = {}; 28 local service = {};
32
33 local http_base = module.path:gsub("/[^/]+$","") .. "/www_files/";
34 29
35 local xmlns_adminsub = "http://prosody.im/adminsub"; 30 local xmlns_adminsub = "http://prosody.im/adminsub";
36 local xmlns_c2s_session = "http://prosody.im/streams/c2s"; 31 local xmlns_c2s_session = "http://prosody.im/streams/c2s";
37 local xmlns_s2s_session = "http://prosody.im/streams/s2s"; 32 local xmlns_s2s_session = "http://prosody.im/streams/s2s";
38
39 local mime_map = {
40 html = "text/html";
41 xml = "text/xml";
42 js = "text/javascript";
43 css = "text/css";
44 };
45 33
46 local idmap = {}; 34 local idmap = {};
47 35
48 function add_client(session, host) 36 function add_client(session, host)
49 local name = session.full_jid; 37 local name = session.full_jid;
102 local notifier = st.stanza("retract", { id = id }); 90 local notifier = st.stanza("retract", { id = id });
103 service[host]:retract(xmlns_s2s_session, host, id, notifier); 91 service[host]:retract(xmlns_s2s_session, host, id, notifier);
104 end 92 end
105 end 93 end
106 94
107 function serve_file(event, path)
108 local full_path = http_base .. path;
109
110 if stat(full_path, "mode") == "directory" then
111 if stat(full_path.."/index.html", "mode") == "file" then
112 return serve_file(event, path.."/index.html");
113 end
114 return 403;
115 end
116
117 local f, err = open(full_path, "rb");
118 if not f then
119 return 404;
120 end
121
122 local data = f:read("*a");
123 f:close();
124 if not data then
125 return 403;
126 end
127
128 local ext = path:match("%.([^.]*)$");
129 event.response.headers.content_type = mime_map[ext]; -- Content-Type should be nil when not known
130 return data;
131 end
132
133 function module.add_host(module) 95 function module.add_host(module)
134 -- Dependencies 96 -- Dependencies
135 module:depends("bosh"); 97 module:depends("bosh");
136 module:depends("admin_adhoc"); 98 module:depends("admin_adhoc");
137 module:depends("http"); 99 module:depends("http");
100 local serve_file = module:depends("http_files").serve {
101 path = module:get_directory() .. "/www_files";
102 };
138 103
139 -- Setup HTTP server 104 -- Setup HTTP server
140 module:provides("http", { 105 module:provides("http", {
141 name = "admin"; 106 name = "admin";
142 route = { 107 route = {