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