comparison mod_http_altconnect/mod_http_altconnect.lua @ 1211:27de4109b7e9

mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
author Kim Alvefur <zash@zash.se>
date Sun, 13 Oct 2013 04:47:51 +0200
parents
children 1f15cc58bb56
comparison
equal deleted inserted replaced
1210:be5334e3f6ca 1211:27de4109b7e9
1 -- http://legastero.github.io/customxeps/extensions/xep-0156.html
2
3 module:depends"http";
4
5 local json = require"util.json";
6 local st = require"util.stanza";
7
8 local host_modules = hosts[module.host].modules;
9
10 local function GET_xml(event)
11 local request, response = event.request, event.response;
12 local xrd = st.stanza("XRD", { xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0' });
13 if host_modules["bosh"] then
14 xrd:tag("Link", { rel="urn:xmpp:altconnect:bosh", href = module:http_url("bosh", "/http-bind") }):up();
15 end
16 if host_modules["websocket"] then
17 xrd:tag("Link", { rel="urn:xmpp:altconnect:websocket", href = module:http_url("bosh", "/xmpp-websocket"):gsub("^http", "ws") }):up();
18 end
19 response.headers.content_type = "application/xrd+xml"
20 response.headers.access_control_allow_origin = "*";
21 return tostring(xrd);
22 end
23
24 local function GET_json(event)
25 local request, response = event.request, event.response;
26 local jrd = { links = { } };
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("bosh", "/xmpp-websocket"):gsub("^http", "ws") }
32 end
33 response.headers.content_type = "application/json"
34 response.headers.access_control_allow_origin = "*";
35 return json.encode(jrd);
36 end;
37
38 local function GET_either(event)
39 local accept_type = event.request.headers.accept or "";
40 if ( accept_type:find("xml") or #accept_type ) < ( accept_type:find("json") or #accept_type+1 ) then
41 return GET_xml(event);
42 else
43 return GET_json(event);
44 end
45 end;
46
47 module:provides("http", {
48 default_path = "/.well-known";
49 route = {
50 ["GET /host-meta"] = GET_either;
51 -- ["GET /host-meta.xml"] = GET_xml; -- Hmmm
52 ["GET /host-meta.json"] = GET_json;
53 };
54 });