annotate mod_http_index/mod_http_index.lua @ 2538:a1b6a6b0aabb

mod_firewall: Reinstate the ability to set a default for stanza expressions
author Matthew Wild <mwild1@gmail.com>
date Tue, 21 Feb 2017 10:37:53 +0000
parents 9376e870f0e1
children 7953b7dde6e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local url = require"socket.url";
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
2 local render = require"util.interpolation".new("%b{}", require"util.stanza".xml_escape);
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 module:depends"http";
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
1827
9376e870f0e1 mod_http_index: Move template out into a file and make it configurable
Kim Alvefur <zash@zash.se>
parents: 1826
diff changeset
6 local base_template;
9376e870f0e1 mod_http_index: Move template out into a file and make it configurable
Kim Alvefur <zash@zash.se>
parents: 1826
diff changeset
7 do
9376e870f0e1 mod_http_index: Move template out into a file and make it configurable
Kim Alvefur <zash@zash.se>
parents: 1826
diff changeset
8 local template_file = module:get_option_string(module.name .. "_template", module.name .. ".html");
9376e870f0e1 mod_http_index: Move template out into a file and make it configurable
Kim Alvefur <zash@zash.se>
parents: 1826
diff changeset
9 template_file = assert(module:load_resource(template_file));
9376e870f0e1 mod_http_index: Move template out into a file and make it configurable
Kim Alvefur <zash@zash.se>
parents: 1826
diff changeset
10 base_template = template_file:read("*a");
9376e870f0e1 mod_http_index: Move template out into a file and make it configurable
Kim Alvefur <zash@zash.se>
parents: 1826
diff changeset
11 template_file:close();
9376e870f0e1 mod_http_index: Move template out into a file and make it configurable
Kim Alvefur <zash@zash.se>
parents: 1826
diff changeset
12 end
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local canonical = module:http_url(nil, "/");
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local function relative(base, link)
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 base = url.parse(base);
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 link = url.parse(link);
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 for k,v in pairs(base) do
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 if link[k] == v then
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 link[k] = nil;
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 end
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 return url.build(link);
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 end
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local function handler(event)
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
28 local host_items = module:get_host_items("http-provider");
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
29 local http_apps = {}
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
30 for _, item in ipairs(host_items) do
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 if module.name ~= item._provided_by then
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
32 table.insert(http_apps, {
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 name = item.name;
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 module = "mod_" .. item._provided_by;
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 url = relative(canonical, module:http_url(item.name, item.default_path));
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
36 });
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 event.response.headers.content_type = "text/html";
1825
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
40 return render(base_template, {
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
41 title = "HTTP Apps";
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
42 items = http_apps;
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
43 prosody_version = prosody.version;
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
44 mod_name = module.name;
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
45 canonical = canonical;
1b5c817cb642 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+)
Kim Alvefur <zash@zash.se>
parents: 1573
diff changeset
46 });
1573
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 end
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 module:provides("http", {
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 route = {
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 ["GET /"] = handler;
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 };
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 default_path = "/";
0d8cc6971cdb mod_http_index: Generates an index of local HTTP apps
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 });