2 -- Copyright (C) 2008-2012 Matthew Wild
3 -- Copyright (C) 2008-2012 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 module:depends("http_errors");
12 local moduleapi = require "core.moduleapi";
13 local url_parse = require "socket.url".parse;
15 local server = require "net.http.server";
17 server.set_default_host(module:get_option_string("http_default_host"));
19 local function normalize_path(path)
20 if path:sub(-1,-1) == "/" then path = path:sub(1, -2); end
21 if path:sub(1,1) ~= "/" then path = "/"..path; end
25 local function get_http_event(host, app_path, key)
26 local method, path = key:match("^(%S+)%s+(.+)$");
27 if not method then -- No path specified, default to "" (base path)
28 method, path = key, "";
30 if method:sub(1,1) == "/" then
33 if app_path == "/" and path:sub(1,1) == "/" then
36 return method:upper().." "..host..app_path..path;
39 local function get_base_path(host_module, app_name, default_app_path)
40 return normalize_path(host_module:get_option("http_paths", {})[app_name] -- Host
41 or module:get_option("http_paths", {})[app_name] -- Global
42 or default_app_path); -- Default
45 -- Helper to deduce a module's external URL
46 function moduleapi.http_url(module, app_name, default_path)
47 app_name = app_name or (module.name:gsub("^http_", ""));
48 local ext = url_parse(module:get_option_string("http_external_url")) or {};
49 local services = portmanager.get_active_services();
50 local http_services = services:get("https") or services:get("http") or {};
51 for interface, ports in pairs(http_services) do
52 for port, services in pairs(ports) do
53 local path = get_base_path(module, app_name, default_path or "/"..app_name);
54 port = tonumber(ext.port) or port or 80;
55 if port == 80 then port = ""; else port = ":"..port; end
56 return (ext.scheme or services[1].service.name).."://"
57 ..(ext.host or module.host)..port
58 ..normalize_path(ext.path or "/")..(path:sub(2));
63 function module.add_host(module)
64 local host = module.host;
66 module.environment.apps = apps;
67 local function http_app_added(event)
68 local app_name = event.item.name;
69 local default_app_path = event.item.default_path or "/"..app_name;
70 local app_path = get_base_path(module, app_name, default_app_path);
73 module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)");
76 apps[app_name] = apps[app_name] or {};
77 local app_handlers = apps[app_name];
78 for key, handler in pairs(event.item.route or {}) do
79 local event_name = get_http_event(host, app_path, key);
81 if type(handler) ~= "function" then
83 handler = function () return data; end
84 elseif event_name:sub(-2, -1) == "/*" then
85 local base_path = event_name:match("/(.+)/*$");
86 local _handler = handler;
87 handler = function (event)
88 local path = event.request.path:sub(#base_path+1);
89 return _handler(event, path);
92 if not app_handlers[event_name] then
93 app_handlers[event_name] = handler;
94 module:hook_object_event(server, event_name, handler);
96 module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name);
99 module:log("error", "Invalid route in %s, %q. See http://prosody.im/doc/developers/http#routes", app_name, key);
104 local function http_app_removed(event)
105 local app_handlers = apps[event.item.name];
106 apps[event.item.name] = nil;
107 for event, handler in pairs(app_handlers) do
108 module:unhook_object_event(server, event, handler);
112 module:handle_items("http-provider", http_app_added, http_app_removed);
114 server.add_host(host);
115 function module.unload()
116 server.remove_host(host);
120 module:add_item("net-provider", {
122 listener = server.listener;
129 module:add_item("net-provider", {
131 listener = server.listener;