mod_storage_sql2: Add archive store with append and find methods
[prosody.git] / plugins / mod_bosh.lua
index 04d85e60fbd4e86fd8cec8e61b95daf029d2ca29..d8717d18a9075982bd21ebd21d56af3f40f49571 100644 (file)
@@ -20,6 +20,8 @@ local logger = require "util.logger";
 local log = logger.init("mod_bosh");
 local initialize_filters = require "util.filters".initialize;
 local math_min = math.min;
+local xpcall, tostring, type = xpcall, tostring, type;
+local traceback = debug.traceback;
 
 local xmlns_streams = "http://etherx.jabber.org/streams";
 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
@@ -37,6 +39,7 @@ local bosh_max_wait = module:get_option_number("bosh_max_wait", 120);
 local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure");
 local cross_domain = module:get_option("cross_domain_bosh", false);
 
+if cross_domain == true then cross_domain = "*"; end
 if type(cross_domain) == "table" then cross_domain = table.concat(cross_domain, ", "); end
 
 local trusted_proxies = module:get_option_set("trusted_proxies", {"127.0.0.1"})._items;
@@ -62,7 +65,7 @@ local os_time = os.time;
 local sessions, inactive_sessions = module:shared("sessions", "inactive_sessions");
 
 -- Used to respond to idle sessions (those with waiting requests)
-local waiting_requests = {};
+local waiting_requests = module:shared("waiting_requests");
 function on_destroy_request(request)
        log("debug", "Request destroyed: %s", tostring(request));
        waiting_requests[request] = nil;
@@ -90,12 +93,7 @@ local function set_cross_domain_headers(response)
        headers.access_control_allow_methods = "GET, POST, OPTIONS";
        headers.access_control_allow_headers = "Content-Type";
        headers.access_control_max_age = "7200";
-
-       if cross_domain == true then
-               headers.access_control_allow_origin = "*";
-       else
-               headers.access_control_allow_origin = cross_domain;
-       end
+       headers.access_control_allow_origin = cross_domain;
        return response;
 end
 
@@ -352,11 +350,12 @@ function stream_callbacks.streamopened(context, attr)
                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(tostring(features));
+               session.send(features);
                session.notopen = nil;
        end
 end
 
+local function handleerr(err) log("error", "Traceback[bosh]: %s", traceback(tostring(err), 2)); end
 function stream_callbacks.handlestanza(context, stanza)
        if context.ignore then return; end
        log("debug", "BOSH stanza received: %s\n", stanza:top_tag());
@@ -366,7 +365,9 @@ function stream_callbacks.handlestanza(context, stanza)
                        stanza.attr.xmlns = nil;
                end
                stanza = session.filter("stanzas/in", stanza);
-               core_process_stanza(session, stanza);
+               if stanza then
+                       return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
+               end
        end
 end
 
@@ -397,7 +398,7 @@ function stream_callbacks.error(context, error)
        end
 end
 
-local dead_sessions = {};
+local dead_sessions = module:shared("dead_sessions");
 function on_timer()
        -- log("debug", "Checking for requests soon to timeout...");
        -- Identify requests timing out within the next few seconds