mod_http: Rework how module:http_url() builds the url.

This commit is contained in:
Kim Alvefur 2012-08-21 21:10:54 +02:00
parent 76c09d2ad1
commit df886d2043

View file

@ -11,6 +11,7 @@ module:depends("http_errors");
local moduleapi = require "core.moduleapi"; local moduleapi = require "core.moduleapi";
local url_parse = require "socket.url".parse; local url_parse = require "socket.url".parse;
local url_build = require "socket.url".build;
local server = require "net.http.server"; local server = require "net.http.server";
@ -42,6 +43,8 @@ local function get_base_path(host_module, app_name, default_app_path)
or default_app_path); -- Default or default_app_path); -- Default
end end
local ports_by_scheme = { http = 80, https = 443, };
-- Helper to deduce a module's external URL -- Helper to deduce a module's external URL
function moduleapi.http_url(module, app_name, default_path) function moduleapi.http_url(module, app_name, default_path)
app_name = app_name or (module.name:gsub("^http_", "")); app_name = app_name or (module.name:gsub("^http_", ""));
@ -50,12 +53,15 @@ function moduleapi.http_url(module, app_name, default_path)
local http_services = services:get("https") or services:get("http") or {}; local http_services = services:get("https") or services:get("http") or {};
for interface, ports in pairs(http_services) do for interface, ports in pairs(http_services) do
for port, services in pairs(ports) do for port, services in pairs(ports) do
local path = get_base_path(module, app_name, default_path or "/"..app_name); local url = {
port = tonumber(ext.port) or port or 80; scheme = (ext.scheme or services[1].service.name);
if port == 80 then port = ""; else port = ":"..port; end host = (ext.host or module.host);
return (ext.scheme or services[1].service.name).."://" port = tonumber(ext.port) or port or 80;
..(ext.host or module.host)..port path = normalize_path(ext.path or "/")..
..normalize_path(ext.path or "/")..(path:sub(2)); (get_base_path(module, app_name, default_path or "/"..app_name):sub(2));
}
if ports_by_scheme[url.scheme] == url.port then url.port = nil end
return url_build(url);
end end
end end
end end