plugins/mod_bosh.lua
changeset 10115 0f335815244f
parent 9994 f122972b77b2
child 10381 4c36bc28b99e
equal deleted inserted replaced
10114:3fa3872588a8 10115:0f335815244f
    76 	measure_inactive(inactive);
    76 	measure_inactive(inactive);
    77 end);
    77 end);
    78 
    78 
    79 -- Used to respond to idle sessions (those with waiting requests)
    79 -- Used to respond to idle sessions (those with waiting requests)
    80 function on_destroy_request(request)
    80 function on_destroy_request(request)
    81 	log("debug", "Request destroyed: %s", tostring(request));
    81 	log("debug", "Request destroyed: %s", request);
    82 	local session = sessions[request.context.sid];
    82 	local session = sessions[request.context.sid];
    83 	if session then
    83 	if session then
    84 		local requests = session.requests;
    84 		local requests = session.requests;
    85 		for i, r in ipairs(requests) do
    85 		for i, r in ipairs(requests) do
    86 			if r == request then
    86 			if r == request then
   113 		sm_destroy_session(session, reason);
   113 		sm_destroy_session(session, reason);
   114 	end
   114 	end
   115 end
   115 end
   116 
   116 
   117 function handle_POST(event)
   117 function handle_POST(event)
   118 	log("debug", "Handling new request %s: %s\n----------", tostring(event.request), tostring(event.request.body));
   118 	log("debug", "Handling new request %s: %s\n----------", event.request, event.request.body);
   119 
   119 
   120 	local request, response = event.request, event.response;
   120 	local request, response = event.request, event.response;
   121 	response.on_destroy = on_destroy_request;
   121 	response.on_destroy = on_destroy_request;
   122 	local body = request.body;
   122 	local body = request.body;
   123 
   123 
   222 
   222 
   223 local function bosh_reset_stream(session) session.notopen = true; end
   223 local function bosh_reset_stream(session) session.notopen = true; end
   224 
   224 
   225 local stream_xmlns_attr = { xmlns = "urn:ietf:params:xml:ns:xmpp-streams" };
   225 local stream_xmlns_attr = { xmlns = "urn:ietf:params:xml:ns:xmpp-streams" };
   226 local function bosh_close_stream(session, reason)
   226 local function bosh_close_stream(session, reason)
   227 	(session.log or log)("info", "BOSH client disconnected: %s", tostring((reason and reason.condition or reason) or "session close"));
   227 	(session.log or log)("info", "BOSH client disconnected: %s", (reason and reason.condition or reason) or "session close");
   228 
   228 
   229 	local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
   229 	local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
   230 		["xmlns:stream"] = xmlns_streams });
   230 		["xmlns:stream"] = xmlns_streams });
   231 
   231 
   232 
   232 
   247 				end
   247 				end
   248 			elseif reason.name then -- a stanza
   248 			elseif reason.name then -- a stanza
   249 				close_reply = reason;
   249 				close_reply = reason;
   250 			end
   250 			end
   251 		end
   251 		end
   252 		log("info", "Disconnecting client, <stream:error> is: %s", tostring(close_reply));
   252 		log("info", "Disconnecting client, <stream:error> is: %s", close_reply);
   253 	end
   253 	end
   254 
   254 
   255 	local response_body = tostring(close_reply);
   255 	local response_body = tostring(close_reply);
   256 	for _, held_request in ipairs(session.requests) do
   256 	for _, held_request in ipairs(session.requests) do
   257 		held_request:send(response_body);
   257 		held_request:send(response_body);
   273 		context.notopen = nil; -- Signals that we accept this opening tag
   273 		context.notopen = nil; -- Signals that we accept this opening tag
   274 
   274 
   275 		local to_host = nameprep(attr.to);
   275 		local to_host = nameprep(attr.to);
   276 		local wait = tonumber(attr.wait);
   276 		local wait = tonumber(attr.wait);
   277 		if not to_host then
   277 		if not to_host then
   278 			log("debug", "BOSH client tried to connect to invalid host: %s", tostring(attr.to));
   278 			log("debug", "BOSH client tried to connect to invalid host: %s", attr.to);
   279 			report_bad_host();
   279 			report_bad_host();
   280 			local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
   280 			local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
   281 				["xmlns:stream"] = xmlns_streams, condition = "improper-addressing" });
   281 				["xmlns:stream"] = xmlns_streams, condition = "improper-addressing" });
   282 			response:send(tostring(close_reply));
   282 			response:send(tostring(close_reply));
   283 			return;
   283 			return;
   284 		end
   284 		end
   285 		if not rid or (not attr.wait or not wait or wait < 0 or wait % 1 ~= 0) then
   285 		if not rid or (not attr.wait or not wait or wait < 0 or wait % 1 ~= 0) then
   286 			log("debug", "BOSH client sent invalid rid or wait attributes: rid=%s, wait=%s", tostring(attr.rid), tostring(attr.wait));
   286 			log("debug", "BOSH client sent invalid rid or wait attributes: rid=%s, wait=%s", attr.rid, attr.wait);
   287 			local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
   287 			local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
   288 				["xmlns:stream"] = xmlns_streams, condition = "bad-request" });
   288 				["xmlns:stream"] = xmlns_streams, condition = "bad-request" });
   289 			response:send(tostring(close_reply));
   289 			response:send(tostring(close_reply));
   290 			return;
   290 			return;
   291 		end
   291 		end
   327 			if s.attr and not s.attr.xmlns then
   327 			if s.attr and not s.attr.xmlns then
   328 				s = st.clone(s);
   328 				s = st.clone(s);
   329 				s.attr.xmlns = "jabber:client";
   329 				s.attr.xmlns = "jabber:client";
   330 			end
   330 			end
   331 			s = filter("stanzas/out", s);
   331 			s = filter("stanzas/out", s);
   332 			--log("debug", "Sending BOSH data: %s", tostring(s));
   332 			--log("debug", "Sending BOSH data: %s", s);
   333 			if not s then return true end
   333 			if not s then return true end
   334 			t_insert(session.send_buffer, tostring(s));
   334 			t_insert(session.send_buffer, tostring(s));
   335 
   335 
   336 			local oldest_request = r[1];
   336 			local oldest_request = r[1];
   337 			if oldest_request and not session.bosh_processing then
   337 			if oldest_request and not session.bosh_processing then
   430 		session.send(features);
   430 		session.send(features);
   431 		session.notopen = nil;
   431 		session.notopen = nil;
   432 	end
   432 	end
   433 end
   433 end
   434 
   434 
   435 local function handleerr(err) log("error", "Traceback[bosh]: %s", traceback(tostring(err), 2)); end
   435 local function handleerr(err) log("error", "Traceback[bosh]: %s", traceback(err, 2)); end
   436 
   436 
   437 function runner_callbacks:error(err) -- luacheck: ignore 212/self
   437 function runner_callbacks:error(err) -- luacheck: ignore 212/self
   438 	return handleerr(err);
   438 	return handleerr(err);
   439 end
   439 end
   440 
   440