Merge 0.10->trunk
[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 = host_module.host });
46 end
47
48 local function redir_handler(event)
49         event.response.headers.location = event.request.path.."/";
50  if event.request.url.query then
51  event.response.headers.location = event.response.headers.location .. "?" .. event.request.url.query
52  end
53         return 301;
54 end
55
56 local ports_by_scheme = { http = 80, https = 443, };
57
58 -- Helper to deduce a module's external URL
59 function moduleapi.http_url(module, app_name, default_path)
60         app_name = app_name or (module.name:gsub("^http_", ""));
61         local external_url = url_parse(module:get_option_string("http_external_url")) or {};
62         if external_url.scheme and external_url.port == nil then
63                 external_url.port = ports_by_scheme[external_url.scheme];
64         end
65         local services = portmanager.get_active_services();
66         local http_services = services:get("https") or services:get("http") or {};
67         for interface, ports in pairs(http_services) do
68                 for port, services in pairs(ports) do
69                         local url = {
70                                 scheme = (external_url.scheme or services[1].service.name);
71                                 host = (external_url.host or module:get_option_string("http_host", module.host));
72                                 port = tonumber(external_url.port) or port or 80;
73                                 path = normalize_path(external_url.path or "/")..
74                                         (get_base_path(module, app_name, default_path or "/"..app_name):sub(2));
75                         }
76                         if ports_by_scheme[url.scheme] == url.port then url.port = nil end
77                         return url_build(url);
78                 end
79         end
80         module:log("warn", "No http ports enabled, can't generate an external URL");
81         return "http://disabled.invalid/";
82 end
83
84 function module.add_host(module)
85         local host = module:get_option_string("http_host", module.host);
86         local apps = {};
87         module.environment.apps = apps;
88         local function http_app_added(event)
89                 local app_name = event.item.name;
90                 local default_app_path = event.item.default_path or "/"..app_name;
91                 local app_path = get_base_path(module, app_name, default_app_path);
92                 if not app_name then
93                         -- TODO: Link to docs
94                         module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)");
95                         return;
96                 end
97                 apps[app_name] = apps[app_name] or {};
98                 local app_handlers = apps[app_name];
99                 for key, handler in pairs(event.item.route or {}) do
100                         local event_name = get_http_event(host, app_path, key);
101                         if event_name then
102                                 if type(handler) ~= "function" then
103                                         local data = handler;
104                                         handler = function () return data; end
105                                 elseif event_name:sub(-2, -1) == "/*" then
106                                         local base_path_len = #event_name:match("/.+$");
107                                         local _handler = handler;
108                                         handler = function (event)
109                                                 local path = event.request.path:sub(base_path_len);
110                                                 return _handler(event, path);
111                                         end;
112                                         module:hook_object_event(server, event_name:sub(1, -3), redir_handler, -1);
113                                 elseif event_name:sub(-1, -1) == "/" then
114                                         module:hook_object_event(server, event_name:sub(1, -2), redir_handler, -1);
115                                 end
116                                 if not app_handlers[event_name] then
117                                         app_handlers[event_name] = handler;
118                                         module:hook_object_event(server, event_name, handler);
119                                 else
120                                         module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name);
121                                 end
122                         else
123                                 module:log("error", "Invalid route in %s, %q. See https://prosody.im/doc/developers/http#routes", app_name, key);
124                         end
125                 end
126                 local services = portmanager.get_active_services();
127                 if services:get("https") or services:get("http") then
128                         module:log("debug", "Serving '%s' at %s", app_name, module:http_url(app_name, app_path));
129                 else
130                         module:log("warn", "Not listening on any ports, '%s' will be unreachable", app_name);
131                 end
132         end
133
134         local function http_app_removed(event)
135                 local app_handlers = apps[event.item.name];
136                 apps[event.item.name] = nil;
137                 for event, handler in pairs(app_handlers) do
138                         module:unhook_object_event(server, event, handler);
139                 end
140         end
141
142         module:handle_items("http-provider", http_app_added, http_app_removed);
143
144         server.add_host(host);
145         function module.unload()
146                 server.remove_host(host);
147         end
148 end
149
150 module:provides("net", {
151         name = "http";
152         listener = server.listener;
153         default_port = 5280;
154         multiplex = {
155                 pattern = "^[A-Z]";
156         };
157 });
158
159 module:provides("net", {
160         name = "https";
161         listener = server.listener;
162         default_port = 5281;
163         encryption = "ssl";
164         ssl_config = {
165                 verify = {
166                         peer = false,
167                         client_once = false,
168                         "none",
169                 }
170         };
171         multiplex = {
172                 pattern = "^[A-Z]";
173         };
174 });