8b8d9c474609fbf36540c69d8a0b03579d16ea7d
[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
11 local parse_url = require "socket.url".parse;
12 local server = require "net.http.server";
13
14 local function normalize_path(path)
15         if path:sub(1,1) ~= "/" then path = "/"..path; end
16         if path:sub(-1,-1) == "/" then path = path:sub(1, -2); end
17         return path;
18 end
19
20 local function get_http_event(host, app_path, key)
21         local method, path = key:match("^(%S+)%s+(.+)$");
22         if not method then
23                 if key:sub(1,1) ~= "/" then
24                         return nil;
25                 end
26                 method, path = "GET", key;
27         end
28         path = normalize_path(path);
29         return method:upper().." "..host..app_path..path;
30 end
31
32 local function get_base_path(host_module, app_name, default_app_path)
33         return host_module:get_option("http_paths", {})[app_name] -- Host
34                 or module:get_option("http_paths", {})[app_name] -- Global
35                 or default_app_path; -- Default
36 end
37
38 function module.add_host(module)
39         local host = module.host;
40         local apps = {};
41         module.environment.apps = apps;
42         local function http_app_added(event)
43                 local app_name = event.item.name;
44                 local default_app_path = event.item.default_path or "/"..app_name;
45                 local app_path = normalize_path(get_base_path(module, app_name, default_app_path));
46                 if not app_name then            
47                         -- TODO: Link to docs
48                         module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)");
49                         return;
50                 end
51                 apps[app_name] = apps[app_name] or {};
52                 local app_handlers = apps[app_name];
53                 for key, handler in pairs(event.item.route or {}) do
54                         local event_name = get_http_event(host, app_path, key);
55                         if event_name then
56                                 if event_name:sub(-2, -1) == "/*" then
57                                         local base_path = event_name:match("/(.+)/*$");
58                                         local _handler = handler;
59                                         handler = function (event)
60                                                 local path = event.request.path:sub(#base_path+1);
61                                                 return _handler(event, path);
62                                         end;
63                                 end
64                                 if not app_handlers[event_name] then
65                                         app_handlers[event_name] = handler;
66                                         module:hook_object_event(server, event_name, handler);
67                                 else
68                                         module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name);
69                                 end
70                         else
71                                 module:log("error", "Invalid route in %s: %q", app_name, key);
72                         end
73                 end
74         end
75         
76         local function http_app_removed(event)
77                 local app_handlers = apps[event.item.name];
78                 apps[event.item.name] = nil;
79                 for event, handler in pairs(app_handlers) do
80                         module:unhook_object_event(server, event, handler);
81                 end
82         end
83         
84         module:handle_items("http-provider", http_app_added, http_app_removed);
85 end
86
87 module:add_item("net-provider", {
88         name = "http";
89         listener = server.listener;
90         default_port = 5280;
91         multiplex = {
92                 pattern = "^[A-Z]";
93         };
94 });
95
96 module:add_item("net-provider", {
97         name = "https";
98         listener = server.listener;
99         default_port = 5281;
100         encryption = "ssl";
101         multiplex = {
102                 pattern = "^[A-Z]";
103         };
104 });