plugins/mod_bosh.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 09 Jan 2009 19:18:46 +0000
changeset 684 b7d85c6a0002
parent 683 7428244a82a6
child 692 4a218377f4e3
permissions -rw-r--r--
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
module.host = "*" -- Global module
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
local lxp = require "lxp";
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
local init_xmlhandlers = require "core.xmlhandlers"
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
local server = require "net.server";
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
local httpserver = require "net.httpserver";
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
local sm = require "core.sessionmanager";
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
     9
local sm_destroy_session = sm.destroy_session;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local new_uuid = require "util.uuid".generate;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local fire_event = require "core.eventmanager".fire_event;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local core_process_stanza = core_process_stanza;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
local st = require "util.stanza";
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
local log = require "util.logger".init("bosh");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
local stream_callbacks = { stream_tag = "http://jabber.org/protocol/httpbind|body" };
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
    16
local config = require "core.configmanager";
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send)
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
    19
local BOSH_DEFAULT_HOLD = tonumber(config.get("*", "core", "bosh_default_hold")) or 1;
683
7428244a82a6 Change default maximum inactivity period to 60s from 30s
Matthew Wild <mwild1@gmail.com>
parents: 679
diff changeset
    20
local BOSH_DEFAULT_INACTIVITY = tonumber(config.get("*", "core", "bosh_max_inactivity")) or 60;
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
    21
local BOSH_DEFAULT_POLLING = tonumber(config.get("*", "core", "bosh_max_polling")) or 5;
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
    22
local BOSH_DEFAULT_REQUESTS = tonumber(config.get("*", "core", "bosh_max_requests")) or 2;
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
    23
local BOSH_DEFAULT_MAXPAUSE = tonumber(config.get("*", "core", "bosh_max_pause")) or 300;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
local os_time = os.time;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
local sessions = {};
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
    29
local inactive_sessions = {}; -- Sessions which have no open requests
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
    31
-- Used to respond to idle sessions (those with waiting requests)
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
local waiting_requests = {};
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
function on_destroy_request(request)
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
	waiting_requests[request] = nil;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
function handle_request(method, body, request)
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
	if (not body) or request.method ~= "POST" then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
		return "<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>";
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
	if not method then 
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
		log("debug", "Request %s suffered error %s", tostring(request.id), body);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
		return;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
	log("debug", "Handling new request %s: %s\n----------", request.id, tostring(body));
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
	request.notopen = true;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
	request.log = log;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
	local parser = lxp.new(init_xmlhandlers(request, stream_callbacks), "|");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
	
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
	parser:parse(body);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
	
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
	local session = sessions[request.sid];
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
	if session then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
		local r = session.requests;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
		log("debug", "Session %s has %d out of %d requests open", request.sid, #r, session.bosh_hold);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
		log("debug", "and there are %d things in the send_buffer", #session.send_buffer);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
		if #r > session.bosh_hold then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
			-- We are holding too many requests, send what's in the buffer,
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
			log("debug", "We are holding too many requests, so...");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
			if #session.send_buffer > 0 then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
				log("debug", "...sending what is in the buffer")
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
				session.send(t_concat(session.send_buffer));
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
				session.send_buffer = {};
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
			else
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
				-- or an empty response
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
				log("debug", "...sending an empty response");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
				session.send("");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
			end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
		elseif #session.send_buffer > 0 then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
			log("debug", "Session has data in the send buffer, will send now..");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
			local resp = t_concat(session.send_buffer);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
			session.send_buffer = {};
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
			session.send(resp);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
		end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
		
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
		if not request.destroyed and session.bosh_wait then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
			request.reply_before = os_time() + session.bosh_wait;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
			request.on_destroy = on_destroy_request;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
			waiting_requests[request] = true;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
		end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
		
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
		log("debug", "Had nothing to say, so leaving request unanswered for now");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
		return true;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
684
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
    87
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
local function bosh_reset_stream(session) session.notopen = true; end
684
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
    89
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
    90
local session_close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate" });
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
    91
local function bosh_close_stream(session, reason)
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
    92
	(session.log or log)("info", "BOSH client disconnected");
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
    93
	session_close_reply.attr.condition = reason;
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
    94
	local session_close_reply = tostring(session_close_reply);
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
    95
	for _, held_request in ipairs(session.requests) do
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
    96
		held_request:send(session_close_reply);
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
    97
		held_request:destroy();
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
    98
	end
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
    99
	sessions[session.sid]  = nil;
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   100
	sm_destroy_session(session);
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   101
end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
function stream_callbacks.streamopened(request, attr)
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
	print("Attr:")
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
	for k,v in pairs(attr) do print("", k, v); end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
	log("debug", "BOSH body open (sid: %s)", attr.sid);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
	local sid = attr.sid
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
	if not sid then
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   109
		-- New session request
684
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   110
		request.notopen = nil; -- Signals that we accept this opening tag
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   111
		
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
		-- TODO: Sanity checks here (rid, to, known host, etc.)
684
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   113
		if not hosts[attr.to] then
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   114
			-- Unknown host
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   115
			session_close_reply.attr.condition = "host-unknown";
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   116
			request:send(tostring(session_close_reply));
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   117
			request.notopen = nil
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   118
			return;
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   119
		end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
		
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
		-- New session
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
		sid = tostring(new_uuid());
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
		local session = { type = "c2s_unauthed", conn = {}, sid = sid, rid = attr.rid, host = attr.to, bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid, 
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   124
						bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY,
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   125
						requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream, close = bosh_close_stream, dispatch_stanza = core_process_stanza };
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
		sessions[sid] = session;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
		log("info", "New BOSH session, assigned it sid '%s'", sid);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
		local r, send_buffer = session.requests, session.send_buffer;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
		local response = { }
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
		function session.send(s)
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
			log("debug", "Sending BOSH data: %s", tostring(s));
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
			local oldest_request = r[1];
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
			while oldest_request and oldest_request.destroyed do
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
				t_remove(r, 1);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
				waiting_requests[oldest_request] = nil;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
				oldest_request = r[1];
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
			end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
			if oldest_request then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
				log("debug", "We have an open request, so using that to send with");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
				response.body = t_concat{"<body xmlns='http://jabber.org/protocol/httpbind' sid='", sid, "' xmlns:stream = 'http://etherx.jabber.org/streams'>", tostring(s), "</body>" };
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
				oldest_request:send(response);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
				log("debug", "Sent");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
				if oldest_request.stayopen then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
					if #r>1 then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
						-- Move front request to back
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
						t_insert(r, oldest_request);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   147
						t_remove(r, 1);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
					end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
				else
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   150
					log("debug", "Destroying the request now...");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
					oldest_request:destroy();
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
					t_remove(r, 1);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
				end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
			elseif s ~= "" then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
				log("debug", "Saved to send buffer because there are %d open requests", #r);
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   156
				if session.bosh_max_inactive and not inactive_sessions[session] then
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   157
					inactive_sessions[session] = os_time();
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   158
					(session.log or log)("debug", "BOSH session marked as inactive at %d", inactive_sessions[session]);
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   159
				end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   160
				-- Hmm, no requests are open :(
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
				t_insert(session.send_buffer, tostring(s));
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
				log("debug", "There are now %d things in the send_buffer", #session.send_buffer);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
			end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
		end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
		
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
		-- Send creation response
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
		
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
		local features = st.stanza("stream:features");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
		fire_event("stream-features", session, features);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
		--xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh'
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
		local response = st.stanza("body", { xmlns = xmlns_bosh, 
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   172
									inactivity = tostring(BOSH_DEFAULT_INACTIVITY), polling = tostring(BOSH_DEFAULT_POLLING), requests = tostring(BOSH_DEFAULT_REQUESTS), hold = tostring(session.bosh_hold), maxpause = "120", 
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
									sid = sid, ver  = '1.6', from = session.host, secure = 'true', ["xmpp:version"] = "1.0", 
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   174
									["xmlns:xmpp"] = "urn:xmpp:xbosh", ["xmlns:stream"] = "http://etherx.jabber.org/streams" }):add_child(features);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   175
		request:send(tostring(response));
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
				
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   177
		request.sid = sid;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   178
		return;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   179
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   180
	
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   181
	local session = sessions[sid];
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   182
	if not session then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   183
		-- Unknown sid
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
		log("info", "Client tried to use sid '%s' which we don't know about", sid);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   185
		request:send(tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" })));
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   186
		request.notopen = nil;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
		return;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   188
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   189
	
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   190
	if attr.type == "terminate" then
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   191
		-- Client wants to end this session
684
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   192
		session:close();
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   193
		request.notopen = nil;
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   194
		return;
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   195
	end
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   196
	
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   197
	-- If session was inactive, make sure it is now marked as not
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   198
	if #session.requests == 0 then
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   199
		(session.log or log)("debug", "BOSH client now active again at %d", os_time());
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   200
		inactive_sessions[session] = nil;
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   201
	end
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   202
	
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   203
	if session.notopen then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   204
		local features = st.stanza("stream:features");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   205
		fire_event("stream-features", session, features);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   206
		session.send(features);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   207
		session.notopen = nil;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   208
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   209
	
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   210
	request.notopen = nil; -- Signals that we accept this opening tag
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   211
	t_insert(session.requests, request);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   212
	request.sid = sid;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   213
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   214
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   215
function stream_callbacks.handlestanza(request, stanza)
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   216
	log("debug", "BOSH stanza received: %s\n", stanza:pretty_print());
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   217
	local session = sessions[request.sid];
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   218
	if session then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   219
		if stanza.attr.xmlns == xmlns_bosh then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   220
			stanza.attr.xmlns = "jabber:client";
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   221
		end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   222
		core_process_stanza(session, stanza);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   223
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   225
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   226
function on_timer()
660
b2c4a7ec31c6 Remove some old debugging code from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 636
diff changeset
   227
	-- log("debug", "Checking for requests soon to timeout...");
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   228
	-- Identify requests timing out within the next few seconds
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   229
	local now = os_time() + 3;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   230
	for request in pairs(waiting_requests) do
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   231
		if request.reply_before <= now then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   232
			log("debug", "%s was soon to timeout, sending empty response", request.id);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   233
			-- Send empty response to let the
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   234
			-- client know we're still here
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   235
			if request.conn then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   236
				sessions[request.sid].send("");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   237
			end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   238
		end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   239
	end
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   240
	
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   241
	now = now - 3;
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   242
	for session, inactive_since in pairs(inactive_sessions) do
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   243
		if now - inactive_since > session.bosh_max_inactive then
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   244
			(session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now);
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   245
			sessions[session.sid]  = nil;
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   246
			inactive_sessions[session] = nil;
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   247
			sm_destroy_session(session, "BOSH client silent for over "..session.bosh_max_inactive.." seconds");
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   248
		end
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   249
	end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   250
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   251
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   252
httpserver.new{ port = 5280, base = "http-bind", handler = handle_request, ssl = false}
660
b2c4a7ec31c6 Remove some old debugging code from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 636
diff changeset
   253
server.addtimer(on_timer);