aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mod_http.lua
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/mod_http.lua')
-rw-r--r--plugins/mod_http.lua21
1 files changed, 15 insertions, 6 deletions
diff --git a/plugins/mod_http.lua b/plugins/mod_http.lua
index 53993f29..090b522b 100644
--- a/plugins/mod_http.lua
+++ b/plugins/mod_http.lua
@@ -11,6 +11,7 @@ module:depends("http_errors");
local moduleapi = require "core.moduleapi";
local url_parse = require "socket.url".parse;
+local url_build = require "socket.url".build;
local server = require "net.http.server";
@@ -30,6 +31,9 @@ local function get_http_event(host, app_path, key)
if method:sub(1,1) == "/" then
return nil;
end
+ if app_path == "/" and path:sub(1,1) == "/" then
+ app_path = "";
+ end
return method:upper().." "..host..app_path..path;
end
@@ -39,6 +43,8 @@ local function get_base_path(host_module, app_name, default_app_path)
or default_app_path); -- Default
end
+local ports_by_scheme = { http = 80, https = 443, };
+
-- Helper to deduce a module's external URL
function moduleapi.http_url(module, app_name, default_path)
app_name = app_name or (module.name:gsub("^http_", ""));
@@ -47,12 +53,15 @@ function moduleapi.http_url(module, app_name, default_path)
local http_services = services:get("https") or services:get("http") or {};
for interface, ports in pairs(http_services) do
for port, services in pairs(ports) do
- local path = get_base_path(module, app_name, default_path or "/"..app_name);
- port = tonumber(ext.port) or port or 80;
- if port == 80 then port = ""; else port = ":"..port; end
- return (ext.scheme or services[1].service.name).."://"
- ..(ext.host or module.host)..port
- ..normalize_path(ext.path or "/")..(path:sub(2));
+ local url = {
+ scheme = (ext.scheme or services[1].service.name);
+ host = (ext.host or module.host);
+ port = tonumber(ext.port) or port or 80;
+ path = normalize_path(ext.path or "/")..
+ (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