Mercurial > prosody-modules
annotate mod_http_altconnect/mod_http_altconnect.lua @ 2452:7d86018a6394
mod_candy/README: Link to HTTP configuration info
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 20 Jan 2017 01:20:48 +0100 |
parents | b21236b6b8d8 |
children | 0a0bf87ccda6 |
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"; |
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
|
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 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
|
11 |
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
|
12 local function get_supported() |
1325
b21236b6b8d8
Backed out changeset 853a382c9bd6
Kim Alvefur <zash@zash.se>
parents:
1324
diff
changeset
|
13 local uris = array(); |
b21236b6b8d8
Backed out changeset 853a382c9bd6
Kim Alvefur <zash@zash.se>
parents:
1324
diff
changeset
|
14 if host_modules["bosh"] then |
b21236b6b8d8
Backed out changeset 853a382c9bd6
Kim Alvefur <zash@zash.se>
parents:
1324
diff
changeset
|
15 uris:push({ rel = "urn:xmpp:alt-connections:xbosh", href = module:http_url("bosh", "/http-bind") }); |
b21236b6b8d8
Backed out changeset 853a382c9bd6
Kim Alvefur <zash@zash.se>
parents:
1324
diff
changeset
|
16 end |
b21236b6b8d8
Backed out changeset 853a382c9bd6
Kim Alvefur <zash@zash.se>
parents:
1324
diff
changeset
|
17 if host_modules["websocket"] then |
b21236b6b8d8
Backed out changeset 853a382c9bd6
Kim Alvefur <zash@zash.se>
parents:
1324
diff
changeset
|
18 uris:push({ rel = "urn:xmpp:alt-connections:websocket", href = module:http_url("websocket", "xmpp-websocket"):gsub("^http", "ws") }); |
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
|
19 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
|
20 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
|
21 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
|
22 |
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 |
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
|
24 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
|
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 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
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 response.headers.access_control_allow_origin = "*"; |
1291
1ac28a953e5f
mod_http_altconnect: Send XML declaration
Kim Alvefur <zash@zash.se>
parents:
1290
diff
changeset
|
33 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
|
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 |
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 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 |
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 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 |
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 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
|
54 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
|
55 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
|
56 ["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
|
57 -- ["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
|
58 ["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
|
59 }; |
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 }); |