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