mod_csi_simple: Consider messages encrypted payload as important (fixes part of ...
[prosody.git] / plugins / mod_net_multiplex.lua
blob8ef778832624e2cd54ad5eb266d08a78cb79d02c
1 module:set_global();
3 local max_buffer_len = module:get_option_number("multiplex_buffer_size", 1024);
5 local portmanager = require "core.portmanager";
7 local available_services = {};
9 local function add_service(service)
10 local multiplex_pattern = service.multiplex and service.multiplex.pattern;
11 if multiplex_pattern then
12 module:log("debug", "Adding multiplex service %q with pattern %q", service.name, multiplex_pattern);
13 available_services[service] = multiplex_pattern;
14 else
15 module:log("debug", "Service %q is not multiplex-capable", service.name);
16 end
17 end
18 module:hook("service-added", function (event) add_service(event.service); end);
19 module:hook("service-removed", function (event) available_services[event.service] = nil; end);
21 for _, services in pairs(portmanager.get_registered_services()) do
22 for _, service in ipairs(services) do
23 add_service(service);
24 end
25 end
27 local buffers = {};
29 local listener = { default_mode = "*a" };
31 function listener.onconnect()
32 end
34 function listener.onincoming(conn, data)
35 if not data then return; end
36 local buf = buffers[conn];
37 buf = buf and buf..data or data;
38 for service, multiplex_pattern in pairs(available_services) do
39 if buf:match(multiplex_pattern) then
40 module:log("debug", "Routing incoming connection to %s", service.name);
41 local next_listener = service.listener;
42 conn:setlistener(next_listener);
43 local onconnect = next_listener.onconnect;
44 if onconnect then onconnect(conn) end
45 return next_listener.onincoming(conn, buf);
46 end
47 end
48 if #buf > max_buffer_len then -- Give up
49 conn:close();
50 else
51 buffers[conn] = buf;
52 end
53 end
55 function listener.ondisconnect(conn)
56 buffers[conn] = nil; -- warn if no buffer?
57 end
59 listener.ondetach = listener.ondisconnect;
61 module:provides("net", {
62 name = "multiplex";
63 config_prefix = "";
64 listener = listener;
65 });
67 module:provides("net", {
68 name = "multiplex_ssl";
69 config_prefix = "ssl";
70 encryption = "ssl";
71 listener = listener;
72 });