aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWaqas Hussain <waqas20@gmail.com>2009-12-10 05:04:09 +0500
committerWaqas Hussain <waqas20@gmail.com>2009-12-10 05:04:09 +0500
commit005e186fc8839b231b593a1cb98526009d57d62f (patch)
treef6ebdf90fb663f22836ae1f1c579ac64a5688697
parentf742f936091ba45358a3a73d73b24d911f1d2987 (diff)
downloadprosody-005e186fc8839b231b593a1cb98526009d57d62f.tar.gz
prosody-005e186fc8839b231b593a1cb98526009d57d62f.zip
multiplex_listener: Initial commit. Multiple protocols on a single port.
-rw-r--r--net/multiplex_listener.lua46
1 files changed, 46 insertions, 0 deletions
diff --git a/net/multiplex_listener.lua b/net/multiplex_listener.lua
new file mode 100644
index 00000000..bf193ad8
--- /dev/null
+++ b/net/multiplex_listener.lua
@@ -0,0 +1,46 @@
+
+local connlisteners_register = require "net.connlisteners".register;
+local connlisteners_get = require "net.connlisteners".get;
+
+local httpserver_listener = connlisteners_get("httpserver");
+local xmppserver_listener = connlisteners_get("xmppserver");
+local xmppclient_listener = connlisteners_get("xmppclient");
+local xmppcomponent_listener = connlisteners_get("xmppcomponent");
+
+local server = { default_mode = "*a" };
+
+local buffer = {};
+
+function server.onincoming(conn, data)
+ if not data then return; end
+ local buf = buffer[conn];
+ buffer[conn] = nil;
+ buf = buf and buf..data or data;
+ if buf:match("^[a-zA-Z]") then
+ local listener = httpserver_listener;
+ conn:setlistener(listener);
+ listener.onincoming(conn, buf);
+ elseif buf:match(">") then
+ local listener;
+ local xmlns = buf:match("%sxmlns%s*=%s*['\"]([^'\"]*)");
+ if xmlns == "jabber:server" then
+ listener = xmppserver_listener;
+ elseif xmlns == "jabber:component:accept" then
+ listener = xmppcomponent_listener;
+ else
+ listener = xmppclient_listener;
+ end
+ conn:setlistener(listener);
+ listener.onincoming(conn, buf);
+ elseif #buf > 1024 then
+ conn:close();
+ else
+ buffer[conn] = buf;
+ end
+end
+
+function server.ondisconnect(conn, err)
+ buffer[conn] = nil; -- warn if no buffer?
+end
+
+connlisteners_register("multiplex", server);