From 6f147fde17da5bd5037247fac6b653cb439a4774 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 7 Dec 2011 02:58:22 +0000 Subject: mod_bosh: Store time to destroy session in inactive_sessions, removing dependency on session.bosh_max_inactive in cleanup timer --- plugins/mod_bosh.lua | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'plugins/mod_bosh.lua') diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index 834b128a..ffba5c11 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -91,9 +91,10 @@ function on_destroy_request(request) end -- If this session now has no requests open, mark it as inactive - if #requests == 0 and session.bosh_max_inactive and not inactive_sessions[session] then - inactive_sessions[session] = os_time(); - (session.log or log)("debug", "BOSH session marked as inactive at %d", inactive_sessions[session]); + local max_inactive = session.bosh_max_inactive; + if max_inactive and #requests == 0 then + inactive_sessions[session] = os_time() + max_inactive; + (session.log or log)("debug", "BOSH session marked as inactive (for %ds)", max_inactive); end end end @@ -402,17 +403,13 @@ function on_timer() now = now - 3; local n_dead_sessions = 0; - for session, inactive_since in pairs(inactive_sessions) do - if session.bosh_max_inactive then - if now - inactive_since > session.bosh_max_inactive then - (session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now); - sessions[session.sid] = nil; - inactive_sessions[session] = nil; - n_dead_sessions = n_dead_sessions + 1; - dead_sessions[n_dead_sessions] = session; - end - else + for session, close_after in pairs(inactive_sessions) do + if close_after < now then + (session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now); + sessions[session.sid] = nil; inactive_sessions[session] = nil; + n_dead_sessions = n_dead_sessions + 1; + dead_sessions[n_dead_sessions] = session; end end -- cgit v1.2.3 From d968e72bfbc364561846d88f07a6ad191cabf332 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 7 Dec 2011 03:54:28 +0000 Subject: mod_bosh: Remove a session from inactive_sessions before destroying it --- plugins/mod_bosh.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'plugins/mod_bosh.lua') diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index ffba5c11..3541f614 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -214,6 +214,7 @@ local function bosh_close_stream(session, reason) held_request:destroy(); end sessions[session.sid] = nil; + inactive_sessions[session] = nil; sm_destroy_session(session); end -- cgit v1.2.3 From 65e0b32a7e546136672837aba6acc8f9b1b53be7 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 7 Dec 2011 04:57:51 +0000 Subject: mod_bosh: Move stream:features sending until after the current request has been added to session.requests. Ensures correct inactivity logic. --- plugins/mod_bosh.lua | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'plugins/mod_bosh.lua') diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index 3541f614..b8759f60 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -120,10 +120,17 @@ function handle_request(method, body, request) request.on_destroy = on_destroy_request; local stream = new_xmpp_stream(request, stream_callbacks); + -- stream:feed() calls the stream_callbacks, so all stanzas in -- the body are processed in this next line before it returns. + -- In particular, the streamopened() stream callback is where + -- much of the session logic happens, because it's where we first + -- get to see the 'sid' of this request. stream:feed(body); + -- Stanzas (if any) in the request have now been processed, and + -- we take care of the high-level BOSH logic here, including + -- giving a response or putting the request "on hold". local session = sessions[request.sid]; if session then -- Session was marked as inactive, since we have @@ -218,6 +225,7 @@ local function bosh_close_stream(session, reason) sm_destroy_session(session); end +-- Handle the tag in the request payload. function stream_callbacks.streamopened(request, attr) local sid = attr.sid; log("debug", "BOSH body open (sid: %s)", sid or ""); @@ -340,14 +348,6 @@ function stream_callbacks.streamopened(request, attr) session.rid = rid; end - if session.notopen then - local features = st.stanza("stream:features"); - hosts[session.host].events.fire_event("stream-features", { origin = session, features = features }); - fire_event("stream-features", session, features); - session.send(features); - session.notopen = nil; - end - if attr.type == "terminate" then -- Client wants to end this session, which we'll do -- after processing any stanzas in this request @@ -357,6 +357,14 @@ function stream_callbacks.streamopened(request, attr) request.notopen = nil; -- Signals that we accept this opening tag t_insert(session.requests, request); request.sid = sid; + + if session.notopen then + local features = st.stanza("stream:features"); + hosts[session.host].events.fire_event("stream-features", { origin = session, features = features }); + fire_event("stream-features", session, features); + session.send(features); + session.notopen = nil; + end end function stream_callbacks.handlestanza(request, stanza) -- cgit v1.2.3 From b212e28356f112a0d36311fa6909db82d2ab2c15 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 7 Dec 2011 05:54:17 +0000 Subject: mod_bosh: Experimental option 'bosh_auto_cork' which witholds any response to a request until all stanzas in it have been processed. --- plugins/mod_bosh.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'plugins/mod_bosh.lua') diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index b8759f60..e45ebeb4 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -35,6 +35,7 @@ local BOSH_DEFAULT_POLLING = module:get_option_number("bosh_max_polling", 5); local BOSH_DEFAULT_REQUESTS = module:get_option_number("bosh_max_requests", 2); local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure"); +local auto_cork = module:get_option_boolean("bosh_auto_cork", false); local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8" }; @@ -268,7 +269,7 @@ function stream_callbacks.streamopened(request, attr) end --log("debug", "Sending BOSH data: %s", tostring(s)); local oldest_request = r[1]; - if oldest_request then + if oldest_request and (not(auto_cork) or waiting_requests[oldest_request]) then log("debug", "We have an open request, so sending on that"); response.body = t_concat({ " Date: Fri, 9 Dec 2011 11:44:00 +0500 Subject: mod_bosh: Fixed use of a private HTTP request property. --- plugins/mod_bosh.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/mod_bosh.lua') diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index e45ebeb4..f4b38bfd 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -58,7 +58,7 @@ end local trusted_proxies = module:get_option_set("trusted_proxies", {"127.0.0.1"})._items; local function get_ip_from_request(request) - local ip = request.handler:ip(); + local ip = request.conn:ip(); local forwarded_for = request.headers["x-forwarded-for"]; if forwarded_for then forwarded_for = forwarded_for..", "..ip; -- cgit v1.2.3 From e246f6a8d7d5526c16ea307719f7c0febd340103 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Wed, 14 Dec 2011 06:42:23 +0500 Subject: mod_bosh: Remove unused reference to lxp --- plugins/mod_bosh.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'plugins/mod_bosh.lua') diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index f4b38bfd..9de27b4b 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -9,7 +9,6 @@ module.host = "*" -- Global module local hosts = _G.hosts; -local lxp = require "lxp"; local new_xmpp_stream = require "util.xmppstream".new; local httpserver = require "net.httpserver"; local sm = require "core.sessionmanager"; -- cgit v1.2.3