From 7677430ce2f2b807ed3b193f1afa831f740eb372 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Wed, 10 Dec 2008 06:58:56 +0500 Subject: Optimized stanza_mt.__tostring (called when doing tostring(stanza)) --- util/stanza.lua | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/util/stanza.lua b/util/stanza.lua index dceb0e73..a4a89191 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -19,6 +19,7 @@ local t_insert = table.insert; +local t_concat = table.concat; local t_remove = table.remove; local s_format = string.format; local tostring = tostring; @@ -27,7 +28,7 @@ local pairs = pairs; local ipairs = ipairs; local type = type; local next = next; -local print = print; +local print = print; local unpack = unpack; local s_gsub = string.gsub; local os = os; @@ -116,21 +117,35 @@ end local xml_escape = xml_escape; -function stanza_mt.__tostring(t) - local children_text = ""; +local function dostring(t, buf, self) + t_insert(buf, "<"); + t_insert(buf, t.name); + if t.attr then + for k, v in pairs(t.attr) do if type(k) == "string" then + t_insert(buf, " "); + t_insert(buf, k); + t_insert(buf, "='"); + t_insert(buf, (xml_escape(tostring(v)))); + t_insert(buf, "'"); + end end + end + t_insert(buf, ">"); for n, child in ipairs(t) do - if type(child) == "string" then - children_text = children_text .. xml_escape(child); + if child.name then + self(child, buf, self); else - children_text = children_text .. tostring(child); + t_insert(buf, (xml_escape(child))); end end + t_insert(buf, ""); +end - local attr_string = ""; - if t.attr then - for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(" %s='%s'", k, xml_escape(tostring(v))); end end - end - return s_format("<%s%s>%s", t.name, attr_string, children_text, t.name); +function stanza_mt.__tostring(t) + local buf = {}; + dostring(t, buf, dostring); + return t_concat(buf); end function stanza_mt.top_tag(t) -- cgit v1.2.3 From b1bf7f567a722afb642370edf1e1400fb2bbbf8b Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 15 Dec 2008 18:06:59 +0000 Subject: Remove a FIXME from mod_tls --- plugins/mod_tls.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/mod_tls.lua b/plugins/mod_tls.lua index 771d251a..fc816ad1 100644 --- a/plugins/mod_tls.lua +++ b/plugins/mod_tls.lua @@ -33,8 +33,6 @@ module:add_handler("c2s_unauthed", "starttls", xmlns_starttls, function (session, stanza) if session.conn.starttls then session.send(st.stanza("proceed", { xmlns = xmlns_starttls })); - -- FIXME: I'm commenting the below, not sure why it was necessary - -- sessions[session.conn] = nil; session:reset_stream(); session.conn.starttls(); session.log("info", "TLS negotiation started..."); -- cgit v1.2.3 From 450c6ef7fc79aeca30f65a2e3f2c242e26353257 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 16 Dec 2008 02:30:24 +0000 Subject: Remove old debugging line from sessionmanager --- core/sessionmanager.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/sessionmanager.lua b/core/sessionmanager.lua index 169a6db2..963de7ce 100644 --- a/core/sessionmanager.lua +++ b/core/sessionmanager.lua @@ -113,8 +113,6 @@ function bind_resource(session, resource) if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end -- We don't support binding multiple resources - session.conntimetotal = gettime()-session.conntime; - resource = resource or uuid_generate(); --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing -- cgit v1.2.3 From 1fb0452d0cd7d6a9b534c58477447bd672cc76cd Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 16 Dec 2008 02:32:01 +0000 Subject: Protect loading of connlisteners, to catch errors --- net/connlisteners.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/net/connlisteners.lua b/net/connlisteners.lua index 8c9b163f..f027dfeb 100644 --- a/net/connlisteners.lua +++ b/net/connlisteners.lua @@ -47,7 +47,8 @@ end function get(name) local h = listeners[name]; if not h then - pcall(dofile, listeners_dir..name:gsub("[^%w%-]", "_").."_listener.lua"); + local ok, ret = pcall(dofile, listeners_dir..name:gsub("[^%w%-]", "_").."_listener.lua"); + if not ok then return nil, ret; end h = listeners[name]; end return h; -- cgit v1.2.3 From 85e074d8b858f72fb7d22079d0d47720e20b40fe Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 16 Dec 2008 02:33:08 +0000 Subject: Change xmlhandlers to match stream opening tag with ns+tag --- core/xmlhandlers.lua | 18 ++++++++---------- net/xmppclient_listener.lua | 2 +- net/xmppserver_listener.lua | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/core/xmlhandlers.lua b/core/xmlhandlers.lua index 38a0dd05..d4d9e141 100644 --- a/core/xmlhandlers.lua +++ b/core/xmlhandlers.lua @@ -47,29 +47,27 @@ local ns_prefixes = { function init_xmlhandlers(session, stream_callbacks) local ns_stack = { "" }; - local curr_ns = ""; + local curr_ns, name = ""; local curr_tag; local chardata = {}; local xml_handlers = {}; local log = session.log or default_log; - - local send = session.send; local cb_streamopened = stream_callbacks.streamopened; local cb_streamclosed = stream_callbacks.streamclosed; local cb_error = stream_callbacks.error or function (session, e) error("XML stream error: "..tostring(e)); end; local cb_handlestanza = stream_callbacks.handlestanza; - local stream_ns = stream_callbacks.ns; + local stream_tag = stream_callbacks.stream_tag; local stanza - function xml_handlers:StartElement(name, attr) + function xml_handlers:StartElement(tagname, attr) if stanza and #chardata > 0 then -- We have some character data in the buffer stanza:text(t_concat(chardata)); chardata = {}; end - curr_ns,name = name:match("^(.+)|([%w%-]+)$"); + local curr_ns,name = tagname:match("^(.+)|([%w%-]+)$"); if curr_ns ~= "jabber:server" then attr.xmlns = curr_ns; end @@ -91,7 +89,7 @@ function init_xmlhandlers(session, stream_callbacks) if not stanza then --if we are not currently inside a stanza if session.notopen then - if name == "stream" and curr_ns == stream_ns then + if tagname == stream_tag then if cb_streamopened then cb_streamopened(session, attr); end @@ -120,10 +118,10 @@ function init_xmlhandlers(session, stream_callbacks) t_insert(chardata, data); end end - function xml_handlers:EndElement(name) - curr_ns,name = name:match("^(.+)|([%w%-]+)$"); + function xml_handlers:EndElement(tagname) + curr_ns,name = tagname:match("^(.+)|([%w%-]+)$"); if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then - if name == "stream" then + if tagname == stream_tag then if cb_streamclosed then cb_streamclosed(session); end diff --git a/net/xmppclient_listener.lua b/net/xmppclient_listener.lua index 50b8aa93..357516e9 100644 --- a/net/xmppclient_listener.lua +++ b/net/xmppclient_listener.lua @@ -36,7 +36,7 @@ local sm_streamopened = sessionmanager.streamopened; local sm_streamclosed = sessionmanager.streamclosed; local st = stanza; -local stream_callbacks = { ns = "http://etherx.jabber.org/streams", streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza }; +local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza }; function stream_callbacks.error(session, error, data) if error == "no-stream" then diff --git a/net/xmppserver_listener.lua b/net/xmppserver_listener.lua index 4d4349aa..36b4c476 100644 --- a/net/xmppserver_listener.lua +++ b/net/xmppserver_listener.lua @@ -28,7 +28,7 @@ local s2s_streamopened = require "core.s2smanager".streamopened; local s2s_streamclosed = require "core.s2smanager".streamclosed; local s2s_destroy_session = require "core.s2smanager".destroy_session; local s2s_attempt_connect = require "core.s2smanager".attempt_connection; -local stream_callbacks = { ns = "http://etherx.jabber.org/streams", streamopened = s2s_streamopened, streamclosed = s2s_streamclosed, handlestanza = core_process_stanza }; +local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", streamopened = s2s_streamopened, streamclosed = s2s_streamclosed, handlestanza = core_process_stanza }; function stream_callbacks.error(session, error, data) if error == "no-stream" then -- cgit v1.2.3 From b922b642ef7b91179e616b0bf1415573204d21b1 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 18 Dec 2008 20:01:09 +0000 Subject: Fix data loss when closing connection with a large write queue. Thanks albert :) --- net/server.lua | 54 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/net/server.lua b/net/server.lua index 4e5ec366..68fa5be3 100644 --- a/net/server.lua +++ b/net/server.lua @@ -37,6 +37,7 @@ local coroutine_wrap = coroutine.wrap local coroutine_yield = coroutine.yield local print = print; local out_put = function () end --print; +local out_put = print; local out_error = print; --// extern libs //-- @@ -100,6 +101,8 @@ wrapserver = function( listener, socket, ip, serverport, mode, sslctx ) -- th local wrapclient, err + out_put("Starting a new server on "..tostring(serverport).." with ssl: "..tostring(sslctx)); + if sslctx then if not ssl_newcontext then return nil, "luasec not found" @@ -188,7 +191,7 @@ wrapsslclient = function( listener, socket, ip, serverport, clientport, mode, ss local writequeue = { } -- buffer for messages to send - local eol, fatal_send_error -- end of buffer + local eol, fatal_send_error, wants_closing local sstat, rstat = 0, 0 @@ -223,7 +226,17 @@ wrapsslclient = function( listener, socket, ip, serverport, clientport, mode, ss --return shutdown( socket, pattern ) end handler.close = function( closed ) - if eol and not fatal_send_error then handler._dispatchdata(); end + if eol and not fatal_send_error then + -- There is data in the buffer, and we haven't experienced + -- an error trying to send yet, so we'll flush the buffer now + handler._dispatchdata(); + if eol then + -- and there is *still* data in the buffer + -- we'll give up for now, and close later + wants_closing = true; + return; + end + end close( socket ) writelen = ( eol and removesocket( writelist, socket, writelen ) ) or writelen readlen = removesocket( readlist, socket, readlen ) @@ -287,6 +300,9 @@ wrapsslclient = function( listener, socket, ip, serverport, clientport, mode, ss --writequeue = { } eol = nil writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist + if wants_closing then + handler.close(); + end return true elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer @@ -368,7 +384,7 @@ wraptlsclient = function( listener, socket, ip, serverport, clientport, mode, ss local writequeue = { } -- buffer for messages to send - local eol, fatal_send_error -- end of buffer + local eol, fatal_send_error, wants_closing local sstat, rstat = 0, 0 @@ -403,7 +419,17 @@ wraptlsclient = function( listener, socket, ip, serverport, clientport, mode, ss --return shutdown( socket, pattern ) end handler.close = function( closed ) - if eol and not fatal_send_error then handler._dispatchdata(); end + if eol and not fatal_send_error then + -- There is data in the buffer, and we haven't experienced + -- an error trying to send yet, so we'll flush the buffer now + handler._dispatchdata(); + if eol then + -- and there is *still* data in the buffer + -- we'll give up for now, and close later + wants_closing = true; + return; + end + end close( socket ) writelen = ( eol and removesocket( writelist, socket, writelen ) ) or writelen readlen = removesocket( readlist, socket, readlen ) @@ -471,6 +497,9 @@ wraptlsclient = function( listener, socket, ip, serverport, clientport, mode, ss out_put("server.lua: connection is ready for tls handshake"); handler.starttls(true); end + if wants_closing then + handler.close(); + end return true elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer @@ -585,7 +614,7 @@ wraptcpclient = function( listener, socket, ip, serverport, clientport, mode ) local writequeue = { } -- list for messages to send - local eol, fatal_send_error + local eol, fatal_send_error, wants_closing socket:settimeout(0); @@ -622,7 +651,17 @@ wraptcpclient = function( listener, socket, ip, serverport, clientport, mode ) return shutdown( socket, pattern ) end handler.close = function( closed ) - if eol and not fatal_send_error then handler.dispatchdata(); end + if eol and not fatal_send_error then + -- There is data in the buffer, and we haven't experienced + -- an error trying to send yet, so we'll flush the buffer now + handler.dispatchdata(); + if eol then + -- and there is *still* data in the buffer + -- we'll give up for now, and close later + wants_closing = true; + return; + end + end _ = not closed and shutdown( socket ) _ = not closed and close( socket ) writelen = ( eol and removesocket( writelist, socket, writelen ) ) or writelen @@ -688,6 +727,9 @@ wraptcpclient = function( listener, socket, ip, serverport, clientport, mode ) --writequeue = { } eol = nil writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist + if wants_closing then + handler.close(); + end return true elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer -- cgit v1.2.3 From 97c4dc41bd6579d9aa216037263c1792f6f1e0c7 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 22 Dec 2008 22:01:49 +0000 Subject: Fix nil status code for http request callbacks --- net/http.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/http.lua b/net/http.lua index a661bb52..00949901 100644 --- a/net/http.lua +++ b/net/http.lua @@ -81,7 +81,7 @@ local function request_reader(request, data, startpos) return request.callback(0, "invalid-status-line", request); end - request.responsecode, request.responseversion = code, http; + request.code, request.responseversion = code, http; if request.onlystatus or not expectbody(request, tonumber(code)) then if request.callback then -- cgit v1.2.3 From 6f71936ac346872a130df634323e6f7ceb7240ea Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 22 Dec 2008 22:12:11 +0000 Subject: Adding initial net.httpserver (lots of work to do on it) --- net/httpserver.lua | 250 ++++++++++++++++++++++++++++++++++++++++++++ net/httpserver_listener.lua | 33 ++++++ 2 files changed, 283 insertions(+) create mode 100644 net/httpserver.lua create mode 100644 net/httpserver_listener.lua diff --git a/net/httpserver.lua b/net/httpserver.lua new file mode 100644 index 00000000..3a3c34b4 --- /dev/null +++ b/net/httpserver.lua @@ -0,0 +1,250 @@ + +local socket = require "socket" +local server = require "net.server" +local url_parse = require "socket.url".parse; + +local connlisteners_start = require "net.connlisteners".start; +local connlisteners_get = require "net.connlisteners".get; +local listener; + +local t_insert, t_concat = table.insert, table.concat; +local s_match, s_gmatch = string.match, string.gmatch; +local tonumber, tostring, pairs = tonumber, tostring, pairs; + +local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end }); +local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end + +local log = require "util.logger".init("httpserver"); + +local http_servers = {}; + +module "httpserver" + +local default_handler; + +local function expectbody(reqt) + return reqt.method == "POST"; +end + +local function send_response(request, response) + -- Write status line + local resp; + if response.body then + log("debug", "Sending response to %s: %s", request.id, response.body); + resp = { "HTTP/1.0 ", response.status or "200 OK", "\r\n"}; + local h = response.headers; + if h then + for k, v in pairs(h) do + t_insert(resp, k); + t_insert(resp, ": "); + t_insert(resp, v); + t_insert(resp, "\r\n"); + end + end + if response.body and not (h and h["Content-Length"]) then + t_insert(resp, "Content-Length: "); + t_insert(resp, #response.body); + t_insert(resp, "\r\n"); + end + t_insert(resp, "\r\n"); + + if response.body and request.method ~= "HEAD" then + t_insert(resp, response.body); + end + else + -- Response we have is just a string (the body) + log("debug", "Sending response to %s: %s", request.id, response); + + resp = { "HTTP/1.0 200 OK\r\n" }; + t_insert(resp, "Connection: close\r\n"); + t_insert(resp, "Content-Length: "); + t_insert(resp, #response); + t_insert(resp, "\r\n\r\n"); + + t_insert(resp, response); + end + request.write(t_concat(resp)); + if not request.stayopen then + request:destroy(); + end +end + +local function call_callback(request, err) + if request.handled then return; end + request.handled = true; + local callback = request.callback; + if not callback and request.path then + local path = request.url.path; + local base = path:match("^/([^/?]+)"); + if not base then + base = path:match("^http://[^/?]+/([^/?]+)"); + end + + callback = (request.server and request.server.handlers[base]) or default_handler; + if callback == default_handler then + log("debug", "Default callback for this request (base: "..tostring(base)..")") + end + end + if callback then + if err then + log("debug", "Request error: "..err); + if not callback(nil, err, request) then + destroy_request(request); + end + return; + end + + local response = callback(request.method, request.body and t_concat(request.body), request); + if response then + if response == true then + -- Keep connection open, we will reply later + log("warn", "Request %s left open, on_destroy is %s", request.id, tostring(request.on_destroy)); + else + -- Assume response + send_response(request, response); + destroy_request(request); + end + else + log("debug", "Request handler provided no response, destroying request..."); + -- No response, close connection + destroy_request(request); + end + end +end + +local function request_reader(request, data, startpos) + if not data then + if request.body then + call_callback(request); + else + -- Error.. connection was closed prematurely + call_callback(request, "connection-closed"); + end + -- Here we force a destroy... the connection is gone, so we can't reply later + destroy_request(request); + return; + end + if request.state == "body" then + log("debug", "Reading body...") + if not request.body then request.body = {}; request.havebodylength, request.bodylength = 0, tonumber(request.responseheaders["content-length"]); end + if startpos then + data = data:sub(startpos, -1) + end + t_insert(request.body, data); + if request.bodylength then + request.havebodylength = request.havebodylength + #data; + if request.havebodylength >= request.bodylength then + -- We have the body + call_callback(request); + end + end + elseif request.state == "headers" then + log("debug", "Reading headers...") + local pos = startpos; + local headers = request.responseheaders or {}; + for line in data:gmatch("(.-)\r\n") do + startpos = (startpos or 1) + #line + 2; + local k, v = line:match("(%S+): (.+)"); + if k and v then + headers[k:lower()] = v; +-- log("debug", "Header: "..k:lower().." = "..v); + elseif #line == 0 then + request.responseheaders = headers; + break; + else + log("debug", "Unhandled header line: "..line); + end + end + + if not expectbody(request) then + call_callback(request); + return; + end + + -- Reached the end of the headers + request.state = "body"; + if #data > startpos then + return request_reader(request, data:sub(startpos, -1)); + end + elseif request.state == "request" then + log("debug", "Reading request line...") + local method, path, http, linelen = data:match("^(%S+) (%S+) HTTP/(%S+)\r\n()", startpos); + if not method then + return call_callback(request, "invalid-status-line"); + end + + request.method, request.path, request.httpversion = method, path, http; + + request.url = url_parse(request.path); + + log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler.serverport()); + + if request.onlystatus then + if not call_callback(request) then + return; + end + end + + request.state = "headers"; + + if #data > linelen then + return request_reader(request, data:sub(linelen, -1)); + end + end +end + +-- The default handler for requests +default_handler = function (method, body, request) + log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler.serverport()); + return { status = "404 Not Found", + headers = { ["Content-Type"] = "text/html" }, + body = "Page Not FoundNot here :(" }; +end + + +function new_request(handler) + return { handler = handler, conn = handler.socket, + write = handler.write, state = "request", + server = http_servers[handler.serverport()], + send = send_response, + destroy = destroy_request, + id = tostring{}:match("%x+$") + }; +end + +function destroy_request(request) + log("debug", "Destroying request %s", request.id); + listener = listener or connlisteners_get("httpserver"); + if not request.destroyed then + request.destroyed = true; + if request.on_destroy then + log("debug", "Request has destroy callback"); + request.on_destroy(request); + else + log("debug", "Request has no destroy callback"); + end + request.handler.close() + if request.conn then + listener.disconnect(request.conn, "closed"); + end + end +end + +function new(params) + local http_server = http_servers[params.port]; + if not http_server then + http_server = { handlers = {} }; + http_servers[params.port] = http_server; + -- We weren't already listening on this port, so start now + connlisteners_start("httpserver", params); + end + if params.base then + http_server.handlers[params.base] = params.handler; + end +end + +_M.request_reader = request_reader; +_M.send_response = send_response; +_M.urlencode = urlencode; + +return _M; diff --git a/net/httpserver_listener.lua b/net/httpserver_listener.lua new file mode 100644 index 00000000..f7fb7f4e --- /dev/null +++ b/net/httpserver_listener.lua @@ -0,0 +1,33 @@ + + +local connlisteners_register = require "net.connlisteners".register; +local new_request = require "net.httpserver".new_request; +local request_reader = require "net.httpserver".request_reader; + +local requests = {}; -- Open requests + +local httpserver = { default_port = 80, default_mode = "*a" }; + +function httpserver.listener(conn, data) + local request = requests[conn]; + + if not request then + request = new_request(conn); + requests[conn] = request; + end + + if data then + request_reader(request, data); + end +end + +function httpserver.disconnect(conn, err) + local request = requests[conn]; + if request and not request.destroyed then + request.conn = nil; + request_reader(request, nil); + end + requests[conn] = nil; +end + +connlisteners_register("httpserver", httpserver); -- cgit v1.2.3 From 8ef6e94079631975a8443638db11e9e833e53e47 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 22 Dec 2008 22:13:22 +0000 Subject: Add initial mod_httpserver for serving static content --- plugins/mod_httpserver.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 plugins/mod_httpserver.lua diff --git a/plugins/mod_httpserver.lua b/plugins/mod_httpserver.lua new file mode 100644 index 00000000..02a9fd78 --- /dev/null +++ b/plugins/mod_httpserver.lua @@ -0,0 +1,20 @@ + +local open = io.open; +local t_concat = table.concat; + +local http_base = "www_files"; + +local response_404 = { status = "404 Not Found", body = "

Page Not Found

Sorry, we couldn't find what you were looking for :(" }; + +local http_path = { http_base }; +local function handle_request(method, body, request) + local path = request.url.path:gsub("%.%.%/", ""):gsub("^/[^/]+", ""); + http_path[2] = path; + local f, err = open(t_concat(http_path), "r"); + if not f then return response_404; end + local data = f:read("*a"); + f:close(); + return data; +end + +httpserver.new{ port = 5280, base = "files", handler = handle_request, ssl = false} \ No newline at end of file -- cgit v1.2.3 From e0cb7cec734ca29f49870f5633cfc738e320f232 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 22 Dec 2008 22:19:42 +0000 Subject: Initial mod_bosh, works, kind of, but quite incomplete --- plugins/mod_bosh.lua | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 plugins/mod_bosh.lua diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua new file mode 100644 index 00000000..776c3145 --- /dev/null +++ b/plugins/mod_bosh.lua @@ -0,0 +1,211 @@ + +module.host = "*" -- Global module + +local lxp = require "lxp"; +local init_xmlhandlers = require "core.xmlhandlers" +local server = require "net.server"; +local httpserver = require "net.httpserver"; +local sm = require "core.sessionmanager"; +local new_uuid = require "util.uuid".generate; +local fire_event = require "core.eventmanager".fire_event; +local core_process_stanza = core_process_stanza; +local st = require "util.stanza"; +local log = require "util.logger".init("bosh"); +local stream_callbacks = { stream_tag = "http://jabber.org/protocol/httpbind|body" }; + +local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send) + +local BOSH_DEFAULT_HOLD = 1; +local BOSH_DEFAULT_INACTIVITY = 30; +local BOSH_DEFAULT_POLLING = 5; +local BOSH_DEFAULT_REQUESTS = 2; +local BOSH_DEFAULT_MAXPAUSE = 120; + +local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; +local os_time = os.time; + +local sessions = {}; + +-- Used to respond to idle sessions +local waiting_requests = {}; +function on_destroy_request(request) + waiting_requests[request] = nil; +end + +function handle_request(method, body, request) + if (not body) or request.method ~= "POST" then + --return { status = "200 OK", headers = { ["Content-Type"] = "text/html" }, body = "You don't look like a BOSH client to me... what do you want?" }; + return "You really don't look like a BOSH client to me... what do you want?"; + end + if not method then + log("debug", "Request %s suffered error %s", tostring(request.id), body); + return; + end + log("debug", "Handling new request %s: %s\n----------", request.id, tostring(body)); + request.notopen = true; + request.log = log; + local parser = lxp.new(init_xmlhandlers(request, stream_callbacks), "|"); + + parser:parse(body); + + local session = sessions[request.sid]; + if session then + local r = session.requests; + log("debug", "Session %s has %d out of %d requests open", request.sid, #r, session.bosh_hold); + log("debug", "and there are %d things in the send_buffer", #session.send_buffer); + if #r > session.bosh_hold then + -- We are holding too many requests, send what's in the buffer, + log("debug", "We are holding too many requests, so..."); + if #session.send_buffer > 0 then + log("debug", "...sending what is in the buffer") + session.send(t_concat(session.send_buffer)); + session.send_buffer = {}; + return; + else + -- or an empty response + log("debug", "...sending an empty response"); + session.send(""); + return; + end + elseif #session.send_buffer > 0 then + log("debug", "Session has data in the send buffer, will send now.."); + local resp = t_concat(session.send_buffer); + session.send_buffer = {}; + session.send(resp); + return; + end + + if not request.destroyed and session.bosh_wait then + request.reply_before = os_time() + session.bosh_wait; + request.on_destroy = on_destroy_request; + waiting_requests[request] = true; + end + + log("debug", "Had nothing to say, so leaving request unanswered for now"); + return true; + end +end + +local function bosh_reset_stream(session) session.notopen = true; end +local function bosh_close_stream(session, reason) end + +function stream_callbacks.streamopened(request, attr) + print("Attr:") + for k,v in pairs(attr) do print("", k, v); end + log("debug", "BOSH body open (sid: %s)", attr.sid); + local sid = attr.sid + if not sid then + -- TODO: Sanity checks here (rid, to, known host, etc.) + request.notopen = nil; -- Signals that we accept this opening tag + + -- New session + sid = tostring(new_uuid()); + local session = { type = "c2s_unauthed", conn = {}, sid = sid, rid = attr.rid, host = attr.to, bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid, + bosh_hold = BOSH_DEFAULT_HOLD, + requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream, close = bosh_close_stream }; + sessions[sid] = session; + log("info", "New BOSH session, assigned it sid '%s'", sid); + local r, send_buffer = session.requests, session.send_buffer; + local response = { } + function session.send(s) + log("debug", "Sending BOSH data: %s", tostring(s)); + local oldest_request = r[1]; + while oldest_request and oldest_request.destroyed do + t_remove(r, 1); + waiting_requests[oldest_request] = nil; + oldest_request = r[1]; + end + if oldest_request then + log("debug", "We have an open request, so using that to send with"); + response.body = t_concat{"", tostring(s), "" }; + oldest_request:send(response); + log("debug", "Sent"); + if oldest_request.stayopen then + if #r>1 then + -- Move front request to back + t_insert(r, oldest_request); + t_remove(r, 1); + end + else + log("debug", "Destroying the request now..."); + oldest_request:destroy(); + t_remove(r, 1); + end + elseif s ~= "" then + log("debug", "Saved to send buffer because there are %d open requests", #r); + -- Hmm, no requests are open :( + t_insert(session.send_buffer, tostring(s)); + log("debug", "There are now %d things in the send_buffer", #session.send_buffer); + end + end + + -- Send creation response + + local features = st.stanza("stream:features"); + fire_event("stream-features", session, features); + --xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh' + local response = st.stanza("body", { xmlns = xmlns_bosh, + inactivity = "30", polling = "5", requests = "2", hold = tostring(session.bosh_hold), maxpause = "120", + sid = sid, ver = '1.6', from = session.host, secure = 'true', ["xmpp:version"] = "1.0", + ["xmlns:xmpp"] = "urn:xmpp:xbosh", ["xmlns:stream"] = "http://etherx.jabber.org/streams" }):add_child(features); + request:send(tostring(response)); + + request.sid = sid; + return; + end + + local session = sessions[sid]; + if not session then + -- Unknown sid + log("info", "Client tried to use sid '%s' which we don't know about", sid); + request:send(tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" }))); + request.notopen = nil; + return; + end + + if session.notopen then + local features = st.stanza("stream:features"); + fire_event("stream-features", session, features); + session.send(features); + session.notopen = nil; + end + + request.notopen = nil; -- Signals that we accept this opening tag + t_insert(session.requests, request); + request.sid = sid; +end + +function stream_callbacks.handlestanza(request, stanza) + log("debug", "BOSH stanza received: %s\n", stanza:pretty_print()); + local session = sessions[request.sid]; + if session then + if stanza.attr.xmlns == xmlns_bosh then + stanza.attr.xmlns = "jabber:client"; + end + core_process_stanza(session, stanza); + end +end + +function on_timer() + log("debug", "Checking for requests soon to timeout..."); + -- Identify requests timing out within the next few seconds + local now = os_time() + 3; + for request in pairs(waiting_requests) do + if request.reply_before <= now then + log("debug", "%s was soon to timeout, sending empty response", request.id); + -- Send empty response to let the + -- client know we're still here + if request.conn then + sessions[request.sid].send(""); + end + else + log("debug", "%s timing out in %ds [destroyed: %s]", request.id, request.reply_before - now, tostring(request.destroyed)); + end + if not request.on_destroy then + log("warn", "%s has no on_destroy!", request.id); + end + end +end + +httpserver.new{ port = 5280, base = "http-bind", handler = handle_request, ssl = false} +server.addtimer(on_timer); \ No newline at end of file -- cgit v1.2.3 From 8661b3a502e85126500cb4a05d509ec85bc3c24d Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 24 Dec 2008 18:48:09 +0000 Subject: Fix for not loading global modules when host-specific modules are specified in config --- core/modulemanager.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/modulemanager.lua b/core/modulemanager.lua index da5945ac..2617c7a6 100644 --- a/core/modulemanager.lua +++ b/core/modulemanager.lua @@ -53,6 +53,24 @@ local modulehelpers = setmetatable({}, { __index = _G }); -- Load modules when a host is activated function load_modules_for_host(host) + -- Load modules from global section + local modules_enabled = config.get("*", "core", "modules_enabled"); + local modules_disabled = config.get(host, "core", "modules_disabled"); + local disabled_set = {}; + if modules_enabled then + if modules_disabled then + for _, module in pairs(modules_disabled) do + disabled_set[module] = true; + end + end + for _, module in pairs(modules_enabled) do + if not disabled_set[module] then + load(host, module); + end + end + end + + -- Load modules from just this host local modules_enabled = config.get(host, "core", "modules_enabled"); if modules_enabled then for _, module in pairs(modules_enabled) do -- cgit v1.2.3