Software / code / prosody
Comparison
plugins/mod_http.lua @ 4892:6c8074f47ca4
mod_http: Add module:http_url([app_name,][default_path]) for a module to get a guess at its external URL
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 18 May 2012 04:24:33 +0100 |
| parent | 4774:b2ed4e1bcb6e |
| child | 4911:4c8575b09ff6 |
comparison
equal
deleted
inserted
replaced
| 4891:189cfe565d03 | 4892:6c8074f47ca4 |
|---|---|
| 6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
| 7 -- | 7 -- |
| 8 | 8 |
| 9 module:set_global(); | 9 module:set_global(); |
| 10 module:depends("http_errors"); | 10 module:depends("http_errors"); |
| 11 | |
| 12 local moduleapi = require "core.moduleapi"; | |
| 13 local url_parse = require "socket.url".parse; | |
| 11 | 14 |
| 12 local server = require "net.http.server"; | 15 local server = require "net.http.server"; |
| 13 | 16 |
| 14 server.set_default_host(module:get_option_string("http_default_host")); | 17 server.set_default_host(module:get_option_string("http_default_host")); |
| 15 | 18 |
| 29 end | 32 end |
| 30 return method:upper().." "..host..app_path..path; | 33 return method:upper().." "..host..app_path..path; |
| 31 end | 34 end |
| 32 | 35 |
| 33 local function get_base_path(host_module, app_name, default_app_path) | 36 local function get_base_path(host_module, app_name, default_app_path) |
| 34 return host_module:get_option("http_paths", {})[app_name] -- Host | 37 return normalize_path(host_module:get_option("http_paths", {})[app_name] -- Host |
| 35 or module:get_option("http_paths", {})[app_name] -- Global | 38 or module:get_option("http_paths", {})[app_name] -- Global |
| 36 or default_app_path; -- Default | 39 or default_app_path); -- Default |
| 40 end | |
| 41 | |
| 42 -- Helper to deduce a module's external URL | |
| 43 function moduleapi.http_url(module, app_name, default_path) | |
| 44 app_name = app_name or (module.name:gsub("^http_", "")); | |
| 45 local ext = url_parse(module:get_option_string("http_external_url")) or {}; | |
| 46 local services = portmanager.get_active_services(); | |
| 47 local http_services = services:get("https") or services:get("http"); | |
| 48 for interface, ports in pairs(http_services) do | |
| 49 for port, services in pairs(ports) do | |
| 50 local path = get_base_path(module, app_name, default_path or "/"..app_name); | |
| 51 port = tonumber(ext.port) or port or 80; | |
| 52 if port == 80 then port = ""; else port = ":"..port; end | |
| 53 return (ext.scheme or services[1].service.name).."://" | |
| 54 ..(ext.host or module.host)..port | |
| 55 ..normalize_path(ext.path or "/")..(path:sub(2)); | |
| 56 end | |
| 57 end | |
| 37 end | 58 end |
| 38 | 59 |
| 39 function module.add_host(module) | 60 function module.add_host(module) |
| 40 local host = module.host; | 61 local host = module.host; |
| 41 local apps = {}; | 62 local apps = {}; |
| 42 module.environment.apps = apps; | 63 module.environment.apps = apps; |
| 43 local function http_app_added(event) | 64 local function http_app_added(event) |
| 44 local app_name = event.item.name; | 65 local app_name = event.item.name; |
| 45 local default_app_path = event.item.default_path or "/"..app_name; | 66 local default_app_path = event.item.default_path or "/"..app_name; |
| 46 local app_path = normalize_path(get_base_path(module, app_name, default_app_path)); | 67 local app_path = get_base_path(module, app_name, default_app_path); |
| 47 if not app_name then | 68 if not app_name then |
| 48 -- TODO: Link to docs | 69 -- TODO: Link to docs |
| 49 module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)"); | 70 module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)"); |
| 50 return; | 71 return; |
| 51 end | 72 end |
| 52 apps[app_name] = apps[app_name] or {}; | 73 apps[app_name] = apps[app_name] or {}; |