mod_{admin_telnet,c2s,component,http,net_multiplex,s2s}: Use module:provides() instea...
[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 end
45
46 local ports_by_scheme = { http = 80, https = 443, };
47
48 -- Helper to deduce a module's external URL
49 function moduleapi.http_url(module, app_name, default_path)
50         app_name = app_name or (module.name:gsub("^http_", ""));
51         local ext = url_parse(module:get_option_string("http_external_url")) or {};
52         local services = portmanager.get_active_services();
53         local http_services = services:get("https") or services:get("http") or {};
54         for interface, ports in pairs(http_services) do
55                 for port, services in pairs(ports) do
56                         local url = {
57                                 scheme = (ext.scheme or services[1].service.name);
58                                 host = (ext.host or module.host);
59                                 port = tonumber(ext.port) or port or 80;
60                                 path = normalize_path(ext.path or "/")..
61                                         (get_base_path(module, app_name, default_path or "/"..app_name):sub(2));
62                         }
63                         if ports_by_scheme[url.scheme] == url.port then url.port = nil end
64                         return url_build(url);
65                 end
66         end
67 end
68
69 function module.add_host(module)
70         local host = module.host;
71         local apps = {};
72         module.environment.apps = apps;
73         local function http_app_added(event)
74                 local app_name = event.item.name;
75                 local default_app_path = event.item.default_path or "/"..app_name;
76                 local app_path = get_base_path(module, app_name, default_app_path);
77                 if not app_name then
78                         -- TODO: Link to docs
79                         module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)");
80                         return;
81                 end
82                 apps[app_name] = apps[app_name] or {};
83                 local app_handlers = apps[app_name];
84                 for key, handler in pairs(event.item.route or {}) do
85                         local event_name = get_http_event(host, app_path, key);
86                         if event_name then
87                                 if type(handler) ~= "function" then
88                                         local data = handler;
89                                         handler = function () return data; end
90                                 elseif event_name:sub(-2, -1) == "/*" then
91                                         local base_path = event_name:match("/(.+)/*$");
92                                         local _handler = handler;
93                                         handler = function (event)
94                                                 local path = event.request.path:sub(#base_path+1);
95                                                 return _handler(event, path);
96                                         end;
97                                 end
98                                 if not app_handlers[event_name] then
99                                         app_handlers[event_name] = handler;
100                                         module:hook_object_event(server, event_name, handler);
101                                 else
102                                         module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name);
103                                 end
104                         else
105                                 module:log("error", "Invalid route in %s, %q. See http://prosody.im/doc/developers/http#routes", app_name, key);
106                         end
107                 end
108         end
109         
110         local function http_app_removed(event)
111                 local app_handlers = apps[event.item.name];
112                 apps[event.item.name] = nil;
113                 for event, handler in pairs(app_handlers) do
114                         module:unhook_object_event(server, event, handler);
115                 end
116         end
117         
118         module:handle_items("http-provider", http_app_added, http_app_removed);
119
120         server.add_host(host);
121         function module.unload()
122                 server.remove_host(host);
123         end
124 end
125
126 module:provides("net", {
127         name = "http";
128         listener = server.listener;
129         default_port = 5280;
130         multiplex = {
131                 pattern = "^[A-Z]";
132         };
133 });
134
135 module:provides("net", {
136         name = "https";
137         listener = server.listener;
138         default_port = 5281;
139         encryption = "ssl";
140         multiplex = {
141                 pattern = "^[A-Z]";
142         };
143 });