Diff

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
parent 1289:1f15cc58bb56
child 1291:1ac28a953e5f
line wrap: on
line diff
--- a/mod_http_altconnect/mod_http_altconnect.lua	Sat Jan 25 20:08:10 2014 +0100
+++ b/mod_http_altconnect/mod_http_altconnect.lua	Sat Jan 25 20:18:54 2014 +0100
@@ -1,20 +1,32 @@
--- http://legastero.github.io/customxeps/extensions/xep-0156.html
+-- mod_http_altconnect
+-- XEP-0156: Discovering Alternative XMPP Connection Methods
 
 module:depends"http";
 
 local json = require"util.json";
 local st = require"util.stanza";
+local array = require"util.array";
 
 local host_modules = hosts[module.host].modules;
 
+local function get_supported()
+	local uris = array();
+	if host_modules["bosh"] then
+		uris:push({ rel = "urn:xmpp:altconnect:bosh", href = module:http_url("bosh", "/http-bind") });
+	end
+	if host_modules["websocket"] then
+		uris:push({ rel = "urn:xmpp:altconnect:websocket", href = module:http_url("websocket", "xmpp-websocket"):gsub("^http", "ws") });
+	end
+	return uris;
+end
+
+
 local function GET_xml(event)
 	local request, response = event.request, event.response;
 	local xrd = st.stanza("XRD", { xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0' });
-	if host_modules["bosh"] then
-		xrd:tag("Link", { rel="urn:xmpp:altconnect:bosh", href = module:http_url("bosh", "/http-bind") }):up();
-	end
-	if host_modules["websocket"] then
-		xrd:tag("Link", { rel="urn:xmpp:altconnect:websocket", href = module:http_url("websocket", "/xmpp-websocket"):gsub("^http", "ws") }):up();
+	local uris = get_supported();
+	for i, method in ipairs(uris) do
+		xrd:tag("Link", method):up();
 	end
 	response.headers.content_type = "application/xrd+xml"
 	response.headers.access_control_allow_origin = "*";
@@ -23,13 +35,7 @@
 
 local function GET_json(event)
 	local request, response = event.request, event.response;
-	local jrd = { links = { } };
-	if host_modules["bosh"] then
-		jrd.links[#jrd.links+1] = { rel="urn:xmpp:altconnect:bosh", href = module:http_url("bosh", "/http-bind") };
-	end
-	if host_modules["websocket"] then
-		jrd.links[#jrd.links+1] = { rel="urn:xmpp:altconnect:websocket", href = module:http_url("websocket", "/xmpp-websocket"):gsub("^http", "ws") }
-	end
+	local jrd = { links = get_supported() };
 	response.headers.content_type = "application/json"
 	response.headers.access_control_allow_origin = "*";
 	return json.encode(jrd);