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 return method:upper().." "..host..app_path..path;
36 local function get_base_path(host_module, app_name, default_app_path)
37 return normalize_path(host_module:get_option("http_paths", {})[app_name] -- Host
38 or module:get_option("http_paths", {})[app_name] -- Global
39 or default_app_path); -- Default
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") or {};
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));
60 function module.add_host(module)
61 local host = module.host;
63 module.environment.apps = apps;
64 local function http_app_added(event)
65 local app_name = event.item.name;
66 local default_app_path = event.item.default_path or "/"..app_name;
67 local app_path = get_base_path(module, app_name, default_app_path);
70 module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)");
73 apps[app_name] = apps[app_name] or {};
74 local app_handlers = apps[app_name];
75 for key, handler in pairs(event.item.route or {}) do
76 local event_name = get_http_event(host, app_path, key);
78 if type(handler) ~= "function" then
80 handler = function () return data; end
81 elseif event_name:sub(-2, -1) == "/*" then
82 local base_path = event_name:match("/(.+)/*$");
83 local _handler = handler;
84 handler = function (event)
85 local path = event.request.path:sub(#base_path+1);
86 return _handler(event, path);
89 if not app_handlers[event_name] then
90 app_handlers[event_name] = handler;
91 module:hook_object_event(server, event_name, handler);
93 module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name);
96 module:log("error", "Invalid route in %s, %q. See http://prosody.im/doc/developers/http#routes", app_name, key);
101 local function http_app_removed(event)
102 local app_handlers = apps[event.item.name];
103 apps[event.item.name] = nil;
104 for event, handler in pairs(app_handlers) do
105 module:unhook_object_event(server, event, handler);
109 module:handle_items("http-provider", http_app_added, http_app_removed);
111 server.add_host(host);
112 function module.unload()
113 server.remove_host(host);
117 module:add_item("net-provider", {
119 listener = server.listener;
126 module:add_item("net-provider", {
128 listener = server.listener;