comparison mod_http_altconnect/mod_http_altconnect.lua @ 1290:c0957b904487

mod_http_altconnect: Refactor to have module/connection method lookup in a common place
author Kim Alvefur <zash@zash.se>
date Sat, 25 Jan 2014 20:18:54 +0100
parents 1f15cc58bb56
children 1ac28a953e5f
comparison
equal deleted inserted replaced
1289:1f15cc58bb56 1290:c0957b904487
1 -- http://legastero.github.io/customxeps/extensions/xep-0156.html 1 -- mod_http_altconnect
2 -- XEP-0156: Discovering Alternative XMPP Connection Methods
2 3
3 module:depends"http"; 4 module:depends"http";
4 5
5 local json = require"util.json"; 6 local json = require"util.json";
6 local st = require"util.stanza"; 7 local st = require"util.stanza";
8 local array = require"util.array";
7 9
8 local host_modules = hosts[module.host].modules; 10 local host_modules = hosts[module.host].modules;
11
12 local function get_supported()
13 local uris = array();
14 if host_modules["bosh"] then
15 uris:push({ rel = "urn:xmpp:altconnect:bosh", href = module:http_url("bosh", "/http-bind") });
16 end
17 if host_modules["websocket"] then
18 uris:push({ rel = "urn:xmpp:altconnect:websocket", href = module:http_url("websocket", "xmpp-websocket"):gsub("^http", "ws") });
19 end
20 return uris;
21 end
22
9 23
10 local function GET_xml(event) 24 local function GET_xml(event)
11 local request, response = event.request, event.response; 25 local request, response = event.request, event.response;
12 local xrd = st.stanza("XRD", { xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0' }); 26 local xrd = st.stanza("XRD", { xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0' });
13 if host_modules["bosh"] then 27 local uris = get_supported();
14 xrd:tag("Link", { rel="urn:xmpp:altconnect:bosh", href = module:http_url("bosh", "/http-bind") }):up(); 28 for i, method in ipairs(uris) do
15 end 29 xrd:tag("Link", method):up();
16 if host_modules["websocket"] then
17 xrd:tag("Link", { rel="urn:xmpp:altconnect:websocket", href = module:http_url("websocket", "/xmpp-websocket"):gsub("^http", "ws") }):up();
18 end 30 end
19 response.headers.content_type = "application/xrd+xml" 31 response.headers.content_type = "application/xrd+xml"
20 response.headers.access_control_allow_origin = "*"; 32 response.headers.access_control_allow_origin = "*";
21 return tostring(xrd); 33 return tostring(xrd);
22 end 34 end
23 35
24 local function GET_json(event) 36 local function GET_json(event)
25 local request, response = event.request, event.response; 37 local request, response = event.request, event.response;
26 local jrd = { links = { } }; 38 local jrd = { links = get_supported() };
27 if host_modules["bosh"] then
28 jrd.links[#jrd.links+1] = { rel="urn:xmpp:altconnect:bosh", href = module:http_url("bosh", "/http-bind") };
29 end
30 if host_modules["websocket"] then
31 jrd.links[#jrd.links+1] = { rel="urn:xmpp:altconnect:websocket", href = module:http_url("websocket", "/xmpp-websocket"):gsub("^http", "ws") }
32 end
33 response.headers.content_type = "application/json" 39 response.headers.content_type = "application/json"
34 response.headers.access_control_allow_origin = "*"; 40 response.headers.access_control_allow_origin = "*";
35 return json.encode(jrd); 41 return json.encode(jrd);
36 end; 42 end;
37 43