Comparison

plugins/mod_http.lua @ 4664:7438b3c68576

mod_http: Revamp module for new API and config
author Matthew Wild <mwild1@gmail.com>
date Mon, 23 Apr 2012 14:16:59 +0100
parent 4636:41983ec223f0
child 4667:d0cfc49f3f2b
comparison
equal deleted inserted replaced
4663:24524d70a50a 4664:7438b3c68576
1 -- Prosody IM 1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild 2 -- Copyright (C) 2008-2012 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain 3 -- Copyright (C) 2008-2012 Waqas Hussain
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 module:set_global(); 9 module:set_global();
10 10
11 --local sessions = module:shared("sessions"); 11 local parse_url = require "socket.url".parse;
12 local server = require "net.http.server";
12 13
13 --[[function listener.associate_session(conn, session) 14 local function normalize_path(path)
14 sessions[conn] = session; 15 if path:sub(1,1) ~= "/" then path = "/"..path; end
15 end]] 16 if path:sub(-1,-1) == "/" then path = path:sub(1, -2); end
17 return path;
18 end
16 19
17 local NULL = {}; 20 local function get_http_event(http_module, app_name, key)
18 local handlers = {}; 21 local method, path = key:match("^(%S+)%s+(.+)$");
22 if not method and key:sub(1,1) == "/" then
23 method, path = "GET", key;
24 else
25 return nil;
26 end
27 path = normalize_path(path);
28 local app_path = normalize_path(http_module:get_option_string(app_name.."_http_path", "/"..app_name));
29 return method:upper().." "..http_module.host..app_path..path;
30 end
19 31
20 function build_handlers(host) 32 function module.add_host(module)
21 if not hosts[host] then return; end 33 local apps = {};
22 local h = {}; 34 module.environment.apps = apps;
23 handlers[host] = h; 35 local function http_app_added(event)
24 36 local app_name = event.item.name;
25 for mod_name, module in pairs(modulemanager.get_modules(host)) do 37 if not app_name then
26 module = module.module; 38 -- TODO: Link to docs
27 if module.items then 39 module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)");
28 for _, item in ipairs(module.items["http-handler"] or NULL) do 40 return;
29 local previous = handlers[item.path]; 41 end
30 if not previous and item.path then 42 apps[app_name] = apps[app_name] or {};
31 h[item.path] = item; 43 local app_handlers = apps[app_name];
44 for key, handler in pairs(event.item.route or {}) do
45 local event_name = get_http_event(module, app_name, key);
46 if event_name then
47 if not app_handlers[event_name] then
48 app_handlers[event_name] = handler;
49 server.add_handler(event_name, handler);
50 else
51 module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name);
32 end 52 end
53 else
54 module:log("error", "Invalid route in %s: %q", app_name, key);
33 end 55 end
34 end 56 end
35 end 57 end
36 58
37 return h; 59 local function http_app_removed(event)
60 local app_handlers = apps[event.item.name];
61 apps[event.item.name] = nil;
62 for event, handler in pairs(app_handlers) do
63 server.remove_handler(event, handler);
64 end
65 end
66
67 module:handle_items("http-provider", http_app_added, http_app_removed);
38 end 68 end
39 function clear_handlers(event)
40 handlers[event.source.host] = nil;
41 end
42 function get_handler(host, path)
43 local h = handlers[host] or build_handlers(host);
44 if h then
45 local item = h[path];
46 return item and item.handler;
47 end
48 end
49 module:handle_items("http-handler", clear_handlers, clear_handlers, false);
50
51 function http_handler(event)
52 local request, response = event.request, event.response;
53
54 local handler = get_handler(request.headers.host:match("[^:]*"):lower(), request.path:match("[^?]*"));
55 if handler then
56 handler(request, response);
57 return true;
58 end
59 end
60
61 local server = require "net.http.server";
62 local listener = server.listener;
63 server.add_handler("*", http_handler);
64 function module.unload()
65 server.remove_handler("*", http_handler);
66 end
67 --require "net.http.server".listen_on(8080);
68 69
69 module:add_item("net-provider", { 70 module:add_item("net-provider", {
70 name = "http"; 71 name = "http";
71 listener = listener; 72 listener = server.listener;
72 default_port = 5280; 73 default_port = 5280;
73 multiplex = { 74 multiplex = {
74 pattern = "^[A-Z]"; 75 pattern = "^[A-Z]";
75 }; 76 };
76 }); 77 });
77 78
78 module:add_item("net-provider", { 79 module:add_item("net-provider", {
79 name = "https"; 80 name = "https";
80 listener = listener; 81 listener = server.listener;
82 default_port = 5281;
81 encryption = "ssl"; 83 encryption = "ssl";
82 multiplex = { 84 multiplex = {
83 pattern = "^[A-Z]"; 85 pattern = "^[A-Z]";
84 }; 86 };
85 }); 87 });