plugins/mod_bosh.lua
changeset 4438 7f51186ed28b
parent 4437 916681a9a7be
child 4442 bc0a68cae236
equal deleted inserted replaced
4437:916681a9a7be 4438:7f51186ed28b
   118 	request.notopen = true;
   118 	request.notopen = true;
   119 	request.log = log;
   119 	request.log = log;
   120 	request.on_destroy = on_destroy_request;
   120 	request.on_destroy = on_destroy_request;
   121 	
   121 	
   122 	local stream = new_xmpp_stream(request, stream_callbacks);
   122 	local stream = new_xmpp_stream(request, stream_callbacks);
       
   123 	
   123 	-- stream:feed() calls the stream_callbacks, so all stanzas in
   124 	-- stream:feed() calls the stream_callbacks, so all stanzas in
   124 	-- the body are processed in this next line before it returns.
   125 	-- the body are processed in this next line before it returns.
       
   126 	-- In particular, the streamopened() stream callback is where
       
   127 	-- much of the session logic happens, because it's where we first
       
   128 	-- get to see the 'sid' of this request.
   125 	stream:feed(body);
   129 	stream:feed(body);
   126 	
   130 	
       
   131 	-- Stanzas (if any) in the request have now been processed, and
       
   132 	-- we take care of the high-level BOSH logic here, including
       
   133 	-- giving a response or putting the request "on hold".
   127 	local session = sessions[request.sid];
   134 	local session = sessions[request.sid];
   128 	if session then
   135 	if session then
   129 		-- Session was marked as inactive, since we have
   136 		-- Session was marked as inactive, since we have
   130 		-- a request open now, unmark it
   137 		-- a request open now, unmark it
   131 		if inactive_sessions[session] and #session.requests > 0 then
   138 		if inactive_sessions[session] and #session.requests > 0 then
   216 	sessions[session.sid]  = nil;
   223 	sessions[session.sid]  = nil;
   217 	inactive_sessions[session] = nil;
   224 	inactive_sessions[session] = nil;
   218 	sm_destroy_session(session);
   225 	sm_destroy_session(session);
   219 end
   226 end
   220 
   227 
       
   228 -- Handle the <body> tag in the request payload.
   221 function stream_callbacks.streamopened(request, attr)
   229 function stream_callbacks.streamopened(request, attr)
   222 	local sid = attr.sid;
   230 	local sid = attr.sid;
   223 	log("debug", "BOSH body open (sid: %s)", sid or "<none>");
   231 	log("debug", "BOSH body open (sid: %s)", sid or "<none>");
   224 	if not sid then
   232 	if not sid then
   225 		-- New session request
   233 		-- New session request
   338 			return;
   346 			return;
   339 		end
   347 		end
   340 		session.rid = rid;
   348 		session.rid = rid;
   341 	end
   349 	end
   342 	
   350 	
       
   351 	if attr.type == "terminate" then
       
   352 		-- Client wants to end this session, which we'll do
       
   353 		-- after processing any stanzas in this request
       
   354 		session.bosh_terminate = true;
       
   355 	end
       
   356 
       
   357 	request.notopen = nil; -- Signals that we accept this opening tag
       
   358 	t_insert(session.requests, request);
       
   359 	request.sid = sid;
       
   360 
   343 	if session.notopen then
   361 	if session.notopen then
   344 		local features = st.stanza("stream:features");
   362 		local features = st.stanza("stream:features");
   345 		hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
   363 		hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
   346 		fire_event("stream-features", session, features);
   364 		fire_event("stream-features", session, features);
   347 		session.send(features);
   365 		session.send(features);
   348 		session.notopen = nil;
   366 		session.notopen = nil;
   349 	end
   367 	end
   350 	
       
   351 	if attr.type == "terminate" then
       
   352 		-- Client wants to end this session, which we'll do
       
   353 		-- after processing any stanzas in this request
       
   354 		session.bosh_terminate = true;
       
   355 	end
       
   356 
       
   357 	request.notopen = nil; -- Signals that we accept this opening tag
       
   358 	t_insert(session.requests, request);
       
   359 	request.sid = sid;
       
   360 end
   368 end
   361 
   369 
   362 function stream_callbacks.handlestanza(request, stanza)
   370 function stream_callbacks.handlestanza(request, stanza)
   363 	if request.ignore then return; end
   371 	if request.ignore then return; end
   364 	log("debug", "BOSH stanza received: %s\n", stanza:top_tag());
   372 	log("debug", "BOSH stanza received: %s\n", stanza:top_tag());