Software /
code /
prosody-modules
Annotate
mod_http_altconnect/mod_http_altconnect.lua @ 1324:853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 28 Feb 2014 15:36:06 +0100 |
parent | 1323:c84ff82658cb |
child | 1325:b21236b6b8d8 |
rev | line source |
---|---|
1290
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
1 -- mod_http_altconnect |
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
2 -- XEP-0156: Discovering Alternative XMPP Connection Methods |
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
|
3 |
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 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
|
5 |
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 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
|
7 local st = require"util.stanza"; |
1290
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
8 local array = require"util.array"; |
1324
853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents:
1323
diff
changeset
|
9 local it = require"util.iterators"; |
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
|
10 |
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 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
|
12 |
1290
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
13 local function get_supported() |
1324
853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents:
1323
diff
changeset
|
14 local uris = array(it.values(module:get_host_items("alt-conn-method"))); |
853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents:
1323
diff
changeset
|
15 if #uris == 0 then |
853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents:
1323
diff
changeset
|
16 -- COMPAT for with before item array was added |
853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents:
1323
diff
changeset
|
17 if host_modules["bosh"] then |
853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents:
1323
diff
changeset
|
18 uris:push({ rel = "urn:xmpp:alt-connections:xbosh", href = module:http_url("bosh", "/http-bind") }); |
853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents:
1323
diff
changeset
|
19 end |
853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents:
1323
diff
changeset
|
20 if host_modules["websocket"] then |
853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents:
1323
diff
changeset
|
21 uris:push({ rel = "urn:xmpp:alt-connections:websocket", href = module:http_url("websocket", "xmpp-websocket"):gsub("^http", "ws") }); |
853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents:
1323
diff
changeset
|
22 end |
1290
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
23 end |
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
24 return uris; |
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
25 end |
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
26 |
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
27 |
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
|
28 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
|
29 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
|
30 local xrd = st.stanza("XRD", { xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0' }); |
1290
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
31 local uris = get_supported(); |
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
32 for i, method in ipairs(uris) do |
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
33 xrd:tag("Link", method):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
|
34 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
|
35 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
|
36 response.headers.access_control_allow_origin = "*"; |
1291
1ac28a953e5f
mod_http_altconnect: Send XML declaration
Kim Alvefur <zash@zash.se>
parents:
1290
diff
changeset
|
37 return '<?xml version="1.0" encoding="UTF-8"?>' .. tostring(xrd); |
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
|
38 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
|
39 |
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 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
|
41 local request, response = event.request, event.response; |
1290
c0957b904487
mod_http_altconnect: Refactor to have module/connection method lookup in a common place
Kim Alvefur <zash@zash.se>
parents:
1289
diff
changeset
|
42 local jrd = { links = get_supported() }; |
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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 |
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 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 |
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
|
57 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
|
58 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
|
59 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
|
60 ["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
|
61 -- ["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
|
62 ["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
|
63 }; |
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
|
64 }); |