Software /
code /
prosody
Annotate
plugins/mod_http_altconnect.lua @ 13718:569fae28a2f3 13.0
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Changes from community version:
- Add options to allow explicit control over whether BOSH/WS is advertised
- Always serve XML at /host-meta (no guessing based on Accept), least surprising
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 16 Feb 2025 13:19:05 +0000 |
rev | line source |
---|---|
13718
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 -- mod_http_altconnect |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 -- XEP-0156: Discovering Alternative XMPP Connection Methods |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 module:depends"http"; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local mm = require "prosody.core.modulemanager"; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local json = require"prosody.util.json"; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local st = require"prosody.util.stanza"; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local array = require"prosody.util.array"; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local advertise_bosh = module:get_option_boolean("advertise_bosh", true); |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local advertise_websocket = module:get_option_boolean("advertise_websocket", true); |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local function get_supported() |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local uris = array(); |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 if advertise_bosh and (mm.is_loaded(module.host, "bosh") or mm.is_loaded("*", "bosh")) then |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 uris:push({ rel = "urn:xmpp:alt-connections:xbosh", href = module:http_url("bosh", "/http-bind") }); |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 end |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 if advertise_websocket and (mm.is_loaded(module.host, "websocket") or mm.is_loaded("*", "websocket")) then |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 uris:push({ rel = "urn:xmpp:alt-connections:websocket", href = module:http_url("websocket", "xmpp-websocket"):gsub("^http", "ws") }); |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 return uris; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 end |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local function GET_xml(event) |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local response = event.response; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local xrd = st.stanza("XRD", { xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0' }); |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 local uris = get_supported(); |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 for _, method in ipairs(uris) do |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 xrd:tag("Link", method):up(); |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 end |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 response.headers.content_type = "application/xrd+xml" |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 response.headers.access_control_allow_origin = "*"; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 return '<?xml version="1.0" encoding="UTF-8"?>' .. tostring(xrd); |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 end |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 local function GET_json(event) |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 local response = event.response; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 local jrd = { links = get_supported() }; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 response.headers.content_type = "application/json" |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 response.headers.access_control_allow_origin = "*"; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 return json.encode(jrd); |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 end; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 module:provides("http", { |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 default_path = "/.well-known"; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 route = { |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 ["GET /host-meta"] = GET_xml; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 ["GET /host-meta.json"] = GET_json; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 }; |
569fae28a2f3
mod_http_altconnect: Imported from prosody-modules 6d5a19bdd718 w/changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 }); |