mod_http_altconnect/mod_http_altconnect.lua
author Kim Alvefur <zash@zash.se>
Sat, 25 Jan 2014 20:08:10 +0100
changeset 1289 1f15cc58bb56
parent 1211 27de4109b7e9
child 1290 c0957b904487
permissions -rw-r--r--
mod_http_altconnect: Correct module name in HTTP path lookup for websockets
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1211
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
-- http://legastero.github.io/customxeps/extensions/xep-0156.html
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
module:depends"http";
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
local json = require"util.json";
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
local st = require"util.stanza";
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
local host_modules = hosts[module.host].modules;
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
local function GET_xml(event)
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
	local request, response = event.request, event.response;
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
	local xrd = st.stanza("XRD", { xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0' });
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
	if host_modules["bosh"] then
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
		xrd:tag("Link", { rel="urn:xmpp:altconnect:bosh", href = module:http_url("bosh", "/http-bind") }):up();
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
	end
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
	if host_modules["websocket"] then
1289
1f15cc58bb56 mod_http_altconnect: Correct module name in HTTP path lookup for websockets
Kim Alvefur <zash@zash.se>
parents: 1211
diff changeset
    17
		xrd:tag("Link", { rel="urn:xmpp:altconnect:websocket", href = module:http_url("websocket", "/xmpp-websocket"):gsub("^http", "ws") }):up();
1211
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
	end
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
	response.headers.content_type = "application/xrd+xml"
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
	response.headers.access_control_allow_origin = "*";
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
	return tostring(xrd);
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
end
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
local function GET_json(event)
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
	local request, response = event.request, event.response;
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
	local jrd = { links = { } };
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
	if host_modules["bosh"] then
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
		jrd.links[#jrd.links+1] = { rel="urn:xmpp:altconnect:bosh", href = module:http_url("bosh", "/http-bind") };
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
	end
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
	if host_modules["websocket"] then
1289
1f15cc58bb56 mod_http_altconnect: Correct module name in HTTP path lookup for websockets
Kim Alvefur <zash@zash.se>
parents: 1211
diff changeset
    31
		jrd.links[#jrd.links+1] = { rel="urn:xmpp:altconnect:websocket", href = module:http_url("websocket", "/xmpp-websocket"):gsub("^http", "ws") }
1211
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
	end
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
	response.headers.content_type = "application/json"
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
	response.headers.access_control_allow_origin = "*";
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    35
	return json.encode(jrd);
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    36
end;
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    37
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    38
local function GET_either(event)
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    39
	local accept_type = event.request.headers.accept or "";
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    40
	if ( accept_type:find("xml") or #accept_type ) < ( accept_type:find("json") or #accept_type+1 ) then
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    41
		return GET_xml(event);
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    42
	else
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    43
		return GET_json(event);
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    44
	end
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    45
end;
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    46
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    47
module:provides("http", {
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    48
	default_path = "/.well-known";
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    49
	route = {
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    50
		["GET /host-meta"] = GET_either;
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    51
		-- ["GET /host-meta.xml"] = GET_xml; -- Hmmm
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    52
		["GET /host-meta.json"] = GET_json;
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    53
	};
27de4109b7e9 mod_http_altconnect: Exposes BOSH and WebSocket endpoints over HTTP per http://legastero.github.io/customxeps/extensions/xep-0156.html#http
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    54
});