Automated merge with http://waqas.ath.cx:8000/

Thu, 25 Dec 2008 01:37:13 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 25 Dec 2008 01:37:13 +0000
changeset 644
e1b3a472ca00
parent 643
8ff454831f7d (current diff)
parent 639
38cb21431257 (diff)
child 645
d0a8ff9ba3e0

Automated merge with http://waqas.ath.cx:8000/

--- a/core/modulemanager.lua	Thu Dec 25 06:35:05 2008 +0500
+++ b/core/modulemanager.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -53,6 +53,24 @@
 
 -- 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
--- a/core/sessionmanager.lua	Thu Dec 25 06:35:05 2008 +0500
+++ b/core/sessionmanager.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -113,8 +113,6 @@
 	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
 	
--- a/core/xmlhandlers.lua	Thu Dec 25 06:35:05 2008 +0500
+++ b/core/xmlhandlers.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -47,29 +47,27 @@
 
 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 @@
 			
 			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 @@
 				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
--- a/net/connlisteners.lua	Thu Dec 25 06:35:05 2008 +0500
+++ b/net/connlisteners.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -47,7 +47,8 @@
 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;
--- a/net/http.lua	Thu Dec 25 06:35:05 2008 +0500
+++ b/net/http.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -81,7 +81,7 @@
 			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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/net/httpserver.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -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 = "<html><head><title>Page Not Found</title></head><body>Not here :(</body></html>" };
+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;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/net/httpserver_listener.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -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);
--- a/net/server.lua	Thu Dec 25 06:35:05 2008 +0500
+++ b/net/server.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -37,6 +37,7 @@
 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 @@
 
 	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 @@
 
 	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 @@
 		--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 @@
 			--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 @@
 
 	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 @@
 		--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 @@
 				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 @@
 
 	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 @@
 		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 @@
 			--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
--- a/net/xmppclient_listener.lua	Thu Dec 25 06:35:05 2008 +0500
+++ b/net/xmppclient_listener.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -36,7 +36,7 @@
 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
--- a/net/xmppserver_listener.lua	Thu Dec 25 06:35:05 2008 +0500
+++ b/net/xmppserver_listener.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -28,7 +28,7 @@
 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/mod_bosh.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -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 = "<html><body>You don't look like a BOSH client to me... what do you want?</body></html>" };
+		return "<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>";
+	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{"<body xmlns='http://jabber.org/protocol/httpbind' sid='", sid, "' xmlns:stream = 'http://etherx.jabber.org/streams'>", tostring(s), "</body>" };
+				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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/mod_httpserver.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -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 = "<h1>Page Not Found</h1>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
--- a/plugins/mod_tls.lua	Thu Dec 25 06:35:05 2008 +0500
+++ b/plugins/mod_tls.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -33,8 +33,6 @@
 		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...");
--- a/util/stanza.lua	Thu Dec 25 06:35:05 2008 +0500
+++ b/util/stanza.lua	Thu Dec 25 01:37:13 2008 +0000
@@ -19,6 +19,7 @@
 
 
 local t_insert      =  table.insert;
+local t_concat      =  table.concat;
 local t_remove      =  table.remove;
 local t_concat      =  table.concat;
 local s_format      = string.format;

mercurial