prosodyctl: Make the 'restart' command start Prosody even if it wasn't already running
[prosody.git] / plugins / mod_bosh.lua
index 4e78cd63263a2d99491097607615d82b02481ba6..d346f247c428020d8fbeef02b9b9415c40036780 100644 (file)
@@ -10,8 +10,7 @@ module.host = "*" -- Global module
 
 local hosts = _G.hosts;
 local lxp = require "lxp";
-local init_xmlhandlers = require "core.xmlhandlers"
-local server = require "net.server";
+local new_xmpp_stream = require "util.xmppstream".new;
 local httpserver = require "net.httpserver";
 local sm = require "core.sessionmanager";
 local sm_destroy_session = sm.destroy_session;
@@ -21,6 +20,7 @@ local core_process_stanza = core_process_stanza;
 local st = require "util.stanza";
 local logger = require "util.logger";
 local log = logger.init("mod_bosh");
+local timer = require "util.timer";
 
 local xmlns_streams = "http://etherx.jabber.org/streams";
 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
@@ -102,7 +102,10 @@ end
 function handle_request(method, body, request)
        if (not body) or request.method ~= "POST" then
                if request.method == "OPTIONS" then
-                       return { headers = default_headers, body = "" };
+                       local headers = {};
+                       for k,v in pairs(default_headers) do headers[k] = v; end
+                       headers["Content-Type"] = nil;
+                       return { headers = headers, body = "" };
                else
                        return "<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>";
                end
@@ -116,9 +119,10 @@ function handle_request(method, body, request)
        request.log = log;
        request.on_destroy = on_destroy_request;
        
-       local parser = lxp.new(init_xmlhandlers(request, stream_callbacks), "\1");
-       
-       parser:parse(body);
+       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.
+       stream:feed(body);
        
        local session = sessions[request.sid];
        if session then
@@ -398,13 +402,14 @@ function on_timer()
                dead_sessions[i] = nil;
                sm_destroy_session(session, "BOSH client silent for over "..session.bosh_max_inactive.." seconds");
        end
+       return 1;
 end
 
 
 local function setup()
        local ports = module:get_option("bosh_ports") or { 5280 };
        httpserver.new_from_config(ports, handle_request, { base = "http-bind" });
-       server.addtimer(on_timer);
+       timer.add_task(1, on_timer);
 end
 if prosody.start_time then -- already started
        setup();