tools/ejabberd2prosody: Disable generating a config, as the format it generates is...
[prosody.git] / plugins / mod_http.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2012 Matthew Wild
3 -- Copyright (C) 2008-2012 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 module:set_global();
10 module:depends("http_errors");
11
12 local portmanager = require "core.portmanager";
13 local moduleapi = require "core.moduleapi";
14 local url_parse = require "socket.url".parse;
15 local url_build = require "socket.url".build;
16
17 local server = require "net.http.server";
18
19 server.set_default_host(module:get_option_string("http_default_host"));
20
21 local function normalize_path(path)
22         if path:sub(-1,-1) == "/" then path = path:sub(1, -2); end
23         if path:sub(1,1) ~= "/" then path = "/"..path; end
24         return path;
25 end
26
27 local function get_http_event(host, app_path, key)
28         local method, path = key:match("^(%S+)%s+(.+)$");
29         if not method then -- No path specified, default to "" (base path)
30                 method, path = key, "";
31         end
32         if method:sub(1,1) == "/" then
33                 return nil;
34         end
35         if app_path == "/" and path:sub(1,1) == "/" then
36                 app_path = "";
37         end
38         return method:upper().." "..host..app_path..path;
39 end
40
41 local function get_base_path(host_module, app_name, default_app_path)
42         return (normalize_path(host_module:get_option("http_paths", {})[app_name] -- Host
43                 or module:get_option("http_paths", {})[app_name] -- Global
44                 or default_app_path)) -- Default
45                 :gsub("%$(%w+)", { host = module.host });
46 end
47
48 local ports_by_scheme = { http = 80, https = 443, };
49
50 -- Helper to deduce a module's external URL
51 function moduleapi.http_url(module, app_name, default_path)
52         app_name = app_name or (module.name:gsub("^http_", ""));
53         local external_url = url_parse(module:get_option_string("http_external_url")) or {};
54         local services = portmanager.get_active_services();
55         local http_services = services:get("https") or services:get("http") or {};
56         for interface, ports in pairs(http_services) do
57                 for port, services in pairs(ports) do
58                         local url = {
59                                 scheme = (external_url.scheme or services[1].service.name);
60                                 host = (external_url.host or module:get_option_string("http_host", module.host));
61                                 port = tonumber(external_url.port) or port or 80;
62                                 path = normalize_path(external_url.path or "/")..
63                                         (get_base_path(module, app_name, default_path or "/"..app_name):sub(2));
64                         }
65                         if ports_by_scheme[url.scheme] == url.port then url.port = nil end
66                         return url_build(url);
67                 end
68         end
69 end
70
71 function module.add_host(module)
72         local host = module:get_option_string("http_host", module.host);
73         local apps = {};
74         module.environment.apps = apps;
75         local function http_app_added(event)
76                 local app_name = event.item.name;
77                 local default_app_path = event.item.default_path or "/"..app_name;
78                 local app_path = get_base_path(module, app_name, default_app_path);
79                 if not app_name then
80                         -- TODO: Link to docs
81                         module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)");
82                         return;
83                 end
84                 apps[app_name] = apps[app_name] or {};
85                 local app_handlers = apps[app_name];
86                 for key, handler in pairs(event.item.route or {}) do
87                         local event_name = get_http_event(host, app_path, key);
88                         if event_name then
89                                 if type(handler) ~= "function" then
90                                         local data = handler;
91                                         handler = function () return data; end
92                                 elseif event_name:sub(-2, -1) == "/*" then
93                                         local base_path_len = #event_name:match("/.+$");
94                                         local _handler = handler;
95                                         handler = function (event)
96                                                 local path = event.request.path:sub(base_path_len);
97                                                 return _handler(event, path);
98                                         end;
99                                 end
100                                 if not app_handlers[event_name] then
101                                         app_handlers[event_name] = handler;
102                                         module:hook_object_event(server, event_name, handler);
103                                 else
104                                         module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name);
105                                 end
106                         else
107                                 module:log("error", "Invalid route in %s, %q. See http://prosody.im/doc/developers/http#routes", app_name, key);
108                         end
109                 end
110         end
111         
112         local function http_app_removed(event)
113                 local app_handlers = apps[event.item.name];
114                 apps[event.item.name] = nil;
115                 for event, handler in pairs(app_handlers) do
116                         module:unhook_object_event(server, event, handler);
117                 end
118         end
119         
120         module:handle_items("http-provider", http_app_added, http_app_removed);
121
122         server.add_host(host);
123         function module.unload()
124                 server.remove_host(host);
125         end
126 end
127
128 module:provides("net", {
129         name = "http";
130         listener = server.listener;
131         default_port = 5280;
132         multiplex = {
133                 pattern = "^[A-Z]";
134         };
135 });
136
137 module:provides("net", {
138         name = "https";
139         listener = server.listener;
140         default_port = 5281;
141         encryption = "ssl";
142         ssl_config = { verify = "none" };
143         multiplex = {
144                 pattern = "^[A-Z]";
145         };
146 });