Software / code / prosody
Comparison
plugins/mod_http.lua @ 13249:5884d58707fa
mod_http: Generate URL from configuration in prosodyctl
This removes the need to configure e.g. http_external_url or similar
settings in order to get correct URLs out of prosodyctl, as the API
depends on portmanager to know the actual ports that are used.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Wed, 26 Jul 2023 14:39:36 +0200 |
| parent | 13214:5022525364f6 |
| child | 13834:61df1404dd7a |
comparison
equal
deleted
inserted
replaced
| 13248:db433ed3135c | 13249:5884d58707fa |
|---|---|
| 80 function moduleapi.http_url(module, app_name, default_path, mode) | 80 function moduleapi.http_url(module, app_name, default_path, mode) |
| 81 app_name = app_name or (module.name:gsub("^http_", "")); | 81 app_name = app_name or (module.name:gsub("^http_", "")); |
| 82 | 82 |
| 83 local external_url = url_parse(module:get_option_string("http_external_url")); | 83 local external_url = url_parse(module:get_option_string("http_external_url")); |
| 84 if external_url and mode ~= "internal" then | 84 if external_url and mode ~= "internal" then |
| 85 -- Current URL does not depend on knowing which ports are used, only configuration. | |
| 85 local url = { | 86 local url = { |
| 86 scheme = external_url.scheme; | 87 scheme = external_url.scheme; |
| 87 host = external_url.host; | 88 host = external_url.host; |
| 88 port = tonumber(external_url.port) or ports_by_scheme[external_url.scheme]; | 89 port = tonumber(external_url.port) or ports_by_scheme[external_url.scheme]; |
| 89 path = normalize_path(external_url.path or "/", true) | 90 path = normalize_path(external_url.path or "/", true) |
| 91 } | 92 } |
| 92 if ports_by_scheme[url.scheme] == url.port then url.port = nil end | 93 if ports_by_scheme[url.scheme] == url.port then url.port = nil end |
| 93 return url_build(url); | 94 return url_build(url); |
| 94 end | 95 end |
| 95 | 96 |
| 97 if prosody.process_type ~= "prosody" then | |
| 98 -- We generally don't open ports outside of Prosody, so we can't rely on | |
| 99 -- portmanager to tell us which ports and services are used and derive the | |
| 100 -- URL from that, so instead we derive it entirely from configuration. | |
| 101 local https_ports = module:get_option_array("https_ports", { 5281 }); | |
| 102 local scheme = "https"; | |
| 103 local port = tonumber(https_ports[1]); | |
| 104 if not port then | |
| 105 -- https is disabled and no http_external_url set | |
| 106 scheme = "http"; | |
| 107 local http_ports = module:get_option_array("http_ports", { 5280 }); | |
| 108 port = tonumber(http_ports[1]); | |
| 109 if not port then | |
| 110 return "http://disabled.invalid/"; | |
| 111 end | |
| 112 end | |
| 113 | |
| 114 local url = { | |
| 115 scheme = scheme; | |
| 116 host = module:get_option_string("http_host", module.global and module:get_option_string("http_default_host") or module.host); | |
| 117 port = port; | |
| 118 path = get_base_path(module, app_name, default_path or "/" .. app_name); | |
| 119 } | |
| 120 if ports_by_scheme[url.scheme] == url.port then | |
| 121 url.port = nil | |
| 122 end | |
| 123 return url_build(url); | |
| 124 end | |
| 125 | |
| 126 -- Use portmanager to find the actual port of https or http services | |
| 96 local services = portmanager.get_active_services(); | 127 local services = portmanager.get_active_services(); |
| 97 local http_services = services:get("https") or services:get("http") or {}; | 128 local http_services = services:get("https") or services:get("http") or {}; |
| 98 for interface, ports in pairs(http_services) do -- luacheck: ignore 213/interface | 129 for interface, ports in pairs(http_services) do -- luacheck: ignore 213/interface |
| 99 for port, service in pairs(ports) do -- luacheck: ignore 512 | 130 for port, service in pairs(ports) do -- luacheck: ignore 512 |
| 100 local url = { | 131 local url = { |