Merge 0.10->trunk
authorKim Alvefur <zash@zash.se>
Tue, 19 Apr 2016 15:44:11 +0000 (17:44 +0200)
committerKim Alvefur <zash@zash.se>
Tue, 19 Apr 2016 15:44:11 +0000 (17:44 +0200)
1  2 
plugins/mod_bosh.lua

diff --combined plugins/mod_bosh.lua
index 6acdc23cb749345b1d1bb4d558a0ab6dc84b8512,34613cbb2837348749551770c968d560228607f0..b65e76854b9c355260a4e5cb67f46908a25742b1
@@@ -13,7 -13,6 +13,7 @@@ local new_xmpp_stream = require "util.x
  local sm = require "core.sessionmanager";
  local sm_destroy_session = sm.destroy_session;
  local new_uuid = require "util.uuid".generate;
 +local fire_event = prosody.events.fire_event;
  local core_process_stanza = prosody.core_process_stanza;
  local st = require "util.stanza";
  local logger = require "util.logger";
@@@ -22,6 -21,7 +22,6 @@@ local initialize_filters = require "uti
  local math_min = math.min;
  local xpcall, tostring, type = xpcall, tostring, type;
  local traceback = debug.traceback;
 -local nameprep = require "util.encodings".stringprep.nameprep;
  
  local xmlns_streams = "http://etherx.jabber.org/streams";
  local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
@@@ -142,14 -142,8 +142,14 @@@ function handle_POST(event
        if session then
                -- Session was marked as inactive, since we have
                -- a request open now, unmark it
 -              if inactive_sessions[session] and #session.requests > 0 then
 -                      inactive_sessions[session] = nil;
 +              if session.inactive_timer and #session.requests > 0 then
 +                      session.inactive_timer:stop();
 +                      session.inactive_timer = nil;
 +              end
 +
 +              if session.bosh_wait_timer then
 +                      session.bosh_wait_timer:stop();
 +                      session.bosh_wait_timer = nil;
                end
  
                local r = session.requests;
                        -- We're keeping this request open, to respond later
                        log("debug", "Have nothing to say, so leaving request unanswered for now");
                        if session.bosh_wait then
 -                              waiting_requests[response] = os_time() + session.bosh_wait;
 +                              session.bosh_wait_timer = module:add_timer(session.bosh_wait, after_bosh_wait, request, session)
                        end
                end
  
        return tostring(close_reply) .. "\n";
  end
  
 +function after_bosh_wait(now, request, session)
 +      if request.conn then
 +              session.send("");
 +      end
 +end
  
  local function bosh_reset_stream(session) session.notopen = true; end
  
@@@ -242,11 -231,10 +242,11 @@@ local function bosh_close_stream(sessio
                held_request:send(response_body);
        end
        sessions[session.sid] = nil;
 -      inactive_sessions[session] = nil;
        sm_destroy_session(session);
  end
  
 +local runner_callbacks = { };
 +
  -- Handle the <body> tag in the request payload.
  function stream_callbacks.streamopened(context, attr)
        local request, response = context.request, context.response;
                -- New session
                sid = new_uuid();
                local session = {
-                       type = "c2s_unauthed", conn = {}, sid = sid, rid = rid-1, host = attr.to,
+                       type = "c2s_unauthed", conn = {}, sid = sid, rid = rid, host = attr.to,
                        bosh_version = attr.ver, bosh_wait = wait, streamid = sid,
                        bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY,
                        requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream,
                };
                sessions[sid] = session;
  
 +              session.thread = runner(function (stanza)
 +                      session:dispatch_stanza(stanza);
 +              end, runner_callbacks, session);
 +
                local filter = initialize_filters(session);
  
                session.log("debug", "BOSH session created for request from %s", session.ip);
  end
  
  local function handleerr(err) log("error", "Traceback[bosh]: %s", traceback(tostring(err), 2)); end
 +
 +function runner_callbacks:error(err)
 +      return handleerr(err);
 +end
 +
  function stream_callbacks.handlestanza(context, stanza)
        if context.ignore then return; end
        log("debug", "BOSH stanza received: %s\n", stanza:top_tag());
                        stanza.attr.xmlns = nil;
                end
                stanza = session.filter("stanzas/in", stanza);
 -              if stanza then
 -                      return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
 -              end
 +              session.thread:run(stanza);
        end
  end
  
@@@ -445,13 -426,51 +445,13 @@@ function stream_callbacks.error(context
        end
  end
  
 -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
 -      local now = os_time() + 3;
 -      for request, reply_before in pairs(waiting_requests) do
 -              if reply_before <= now then
 -                      log("debug", "%s was soon to timeout (at %d, now %d), sending empty response", tostring(request), reply_before, now);
 -                      -- Send empty response to let the
 -                      -- client know we're still here
 -                      if request.conn then
 -                              sessions[request.context.sid].send("");
 -                      end
 -              end
 -      end
 -
 -      now = now - 3;
 -      local n_dead_sessions = 0;
 -      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
 -
 -      for i=1,n_dead_sessions do
 -              local session = dead_sessions[i];
 -              dead_sessions[i] = nil;
 -              sm_destroy_session(session, "BOSH client silent for over "..session.bosh_max_inactive.." seconds");
 -      end
 -      return 1;
 -end
 -module:add_timer(1, on_timer);
 -
 -
  local GET_response = {
        headers = {
                content_type = "text/html";
        };
        body = [[<html><body>
        <p>It works! Now point your BOSH client to this URL to connect to Prosody.</p>
 -      <p>For more information see <a href="http://prosody.im/doc/setting_up_bosh">Prosody: Setting up BOSH</a>.</p>
 +      <p>For more information see <a href="https://prosody.im/doc/setting_up_bosh">Prosody: Setting up BOSH</a>.</p>
        </body></html>]];
  };