Diff

plugins/mod_bosh.lua @ 684:b7d85c6a0002

Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
author Matthew Wild <mwild1@gmail.com>
date Fri, 09 Jan 2009 19:18:46 +0000
parent 683:7428244a82a6
child 692:4a218377f4e3
line wrap: on
line diff
--- a/plugins/mod_bosh.lua	Fri Jan 09 19:16:47 2009 +0000
+++ b/plugins/mod_bosh.lua	Fri Jan 09 19:18:46 2009 +0000
@@ -84,9 +84,21 @@
 	end
 end
 
-local session_close_reply = tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate" }));
+
 local function bosh_reset_stream(session) session.notopen = true; end
-local function bosh_close_stream(session, reason) end
+
+local session_close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate" });
+local function bosh_close_stream(session, reason)
+	(session.log or log)("info", "BOSH client disconnected");
+	session_close_reply.attr.condition = reason;
+	local session_close_reply = tostring(session_close_reply);
+	for _, held_request in ipairs(session.requests) do
+		held_request:send(session_close_reply);
+		held_request:destroy();
+	end
+	sessions[session.sid]  = nil;
+	sm_destroy_session(session);
+end
 
 function stream_callbacks.streamopened(request, attr)
 	print("Attr:")
@@ -95,8 +107,16 @@
 	local sid = attr.sid
 	if not sid then
 		-- New session request
+		request.notopen = nil; -- Signals that we accept this opening tag
+		
 		-- TODO: Sanity checks here (rid, to, known host, etc.)
-		request.notopen = nil; -- Signals that we accept this opening tag
+		if not hosts[attr.to] then
+			-- Unknown host
+			session_close_reply.attr.condition = "host-unknown";
+			request:send(tostring(session_close_reply));
+			request.notopen = nil
+			return;
+		end
 		
 		-- New session
 		sid = tostring(new_uuid());
@@ -169,13 +189,7 @@
 	
 	if attr.type == "terminate" then
 		-- Client wants to end this session
-		(session.log or log)("info", "BOSH client disconnected");
-		for _, held_request in ipairs(session.requests) do
-				held_request:send(session_close_reply);
-				held_request:destroy();
-		end
-		sm_destroy_session(session);
-		sessions[sid]  = nil;
+		session:close();
 		request.notopen = nil;
 		return;
 	end