util.filters: Fix inserting items so that higher priority filters run first
[prosody.git] / plugins / mod_bosh.lua
index 19f191c8bf6335a924d2de2876885d62453b184e..d9c8defd90ead6d891c5948ede45d90e04ce98a0 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";
@@ -36,7 +38,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 default_headers = { ["Content-Type"] = "text/xml; charset=utf-8", ["Connection"] = "keep-alive" };
+local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8" };
 
 local cross_domain = module:get_option("cross_domain_bosh", false);
 if cross_domain then
@@ -284,6 +286,7 @@ function stream_callbacks.streamopened(context, attr)
                                        sid = sid;
                                };
                                if creating_session then
+                                       creating_session = nil;
                                        body_attr.inactivity = tostring(BOSH_DEFAULT_INACTIVITY);
                                        body_attr.polling = tostring(BOSH_DEFAULT_POLLING);
                                        body_attr.requests = tostring(BOSH_DEFAULT_REQUESTS);
@@ -291,7 +294,8 @@ function stream_callbacks.streamopened(context, attr)
                                        body_attr.hold = tostring(session.bosh_hold);
                                        body_attr.authid = sid;
                                        body_attr.secure = "true";
-                                       body_attr.ver  = '1.6'; from = session.host;
+                                       body_attr.ver  = '1.6';
+                                       body_attr.from = session.host;
                                        body_attr["xmlns:xmpp"] = "urn:xmpp:xbosh";
                                        body_attr["xmpp:version"] = "1.0";
                                end
@@ -320,7 +324,7 @@ function stream_callbacks.streamopened(context, attr)
                        session.log("warn", "rid too large (means a request was lost). Last rid: %d New rid: %s", session.rid, attr.rid);
                elseif diff <= 0 then
                        -- Repeated, ignore
-                       session.log("debug", "rid repeated (on request %s), ignoring: %s (diff %d)", request.id, session.rid, diff);
+                       session.log("debug", "rid repeated, ignoring: %s (diff %d)", session.rid, diff);
                        context.notopen = nil;
                        context.ignore = true;
                        context.sid = sid;
@@ -345,11 +349,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);
-               table.insert(session.send_buffer, tostring(features));
+               session.send(tostring(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());
@@ -359,7 +364,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