test_util_multitable: make mt variable local [luacheck]
[prosody.git] / plugins / mod_http.lua
index 49529ea28b1109c264acc8cdc1ad56d487e3ea17..086887fbcc9a9b63ccd078c3a65804481d12a419 100644 (file)
@@ -45,6 +45,11 @@ local function get_base_path(host_module, app_name, default_app_path)
                :gsub("%$(%w+)", { host = host_module.host });
 end
 
+local function redir_handler(event)
+       event.response.headers.location = event.request.path.."/";
+       return 301;
+end
+
 local ports_by_scheme = { http = 80, https = 443, };
 
 -- Helper to deduce a module's external URL
@@ -69,6 +74,8 @@ function moduleapi.http_url(module, app_name, default_path)
                        return url_build(url);
                end
        end
+       module:log("warn", "No http ports enabled, can't generate an external URL");
+       return "http://disabled.invalid/";
 end
 
 function module.add_host(module)
@@ -99,6 +106,9 @@ function module.add_host(module)
                                                local path = event.request.path:sub(base_path_len);
                                                return _handler(event, path);
                                        end;
+                                       module:hook_object_event(server, event_name:sub(1, -3), redir_handler, -1);
+                               elseif event_name:sub(-1, -1) == "/" then
+                                       module:hook_object_event(server, event_name:sub(1, -2), redir_handler, -1);
                                end
                                if not app_handlers[event_name] then
                                        app_handlers[event_name] = handler;
@@ -110,6 +120,12 @@ function module.add_host(module)
                                module:log("error", "Invalid route in %s, %q. See http://prosody.im/doc/developers/http#routes", app_name, key);
                        end
                end
+               local services = portmanager.get_active_services();
+               if services:get("https") or services:get("http") then
+                       module:log("debug", "Serving '%s' at %s", app_name, module:http_url(app_name, app_path));
+               else
+                       module:log("warn", "Not listening on any ports, '%s' will be unreachable", app_name);
+               end
        end
 
        local function http_app_removed(event)