Comparison

plugins/mod_bosh.lua @ 3448:0ca7c3864431

mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
author Matthew Wild <mwild1@gmail.com>
date Fri, 06 Aug 2010 01:59:43 +0100
parent 3447:cc2dc55e66f9
child 3449:0a74ce129a06
comparison
equal deleted inserted replaced
3447:cc2dc55e66f9 3448:0ca7c3864431
20 local core_process_stanza = core_process_stanza; 20 local core_process_stanza = core_process_stanza;
21 local st = require "util.stanza"; 21 local st = require "util.stanza";
22 local logger = require "util.logger"; 22 local logger = require "util.logger";
23 local log = logger.init("mod_bosh"); 23 local log = logger.init("mod_bosh");
24 24
25 local xmlns_streams = "http://etherx.jabber.org/streams";
26 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
25 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send) 27 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send)
26 local stream_callbacks = { stream_ns = "http://jabber.org/protocol/httpbind", stream_tag = "body", default_ns = "jabber:client" }; 28 local stream_callbacks = { stream_ns = "http://jabber.org/protocol/httpbind", stream_tag = "body", default_ns = "jabber:client" };
27 29
28 local BOSH_DEFAULT_HOLD = tonumber(module:get_option("bosh_default_hold")) or 1; 30 local BOSH_DEFAULT_HOLD = tonumber(module:get_option("bosh_default_hold")) or 1;
29 local BOSH_DEFAULT_INACTIVITY = tonumber(module:get_option("bosh_max_inactivity")) or 60; 31 local BOSH_DEFAULT_INACTIVITY = tonumber(module:get_option("bosh_max_inactivity")) or 60;
32 local BOSH_DEFAULT_MAXPAUSE = tonumber(module:get_option("bosh_max_pause")) or 300; 34 local BOSH_DEFAULT_MAXPAUSE = tonumber(module:get_option("bosh_max_pause")) or 300;
33 35
34 local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure"); 36 local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure");
35 37
36 local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8" }; 38 local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8" };
37 local session_close_reply = { headers = default_headers, body = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate" }), attr = {} };
38 39
39 local cross_domain = module:get_option("cross_domain_bosh"); 40 local cross_domain = module:get_option("cross_domain_bosh");
40 if cross_domain then 41 if cross_domain then
41 default_headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"; 42 default_headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
42 default_headers["Access-Control-Allow-Headers"] = "Content-Type"; 43 default_headers["Access-Control-Allow-Headers"] = "Content-Type";
144 end 145 end
145 146
146 147
147 local function bosh_reset_stream(session) session.notopen = true; end 148 local function bosh_reset_stream(session) session.notopen = true; end
148 149
150 local stream_xmlns_attr = { xmlns = "urn:ietf:params:xml:ns:xmpp-streams" };
151
149 local function bosh_close_stream(session, reason) 152 local function bosh_close_stream(session, reason)
150 (session.log or log)("info", "BOSH client disconnected"); 153 (session.log or log)("info", "BOSH client disconnected");
151 session_close_reply.attr.condition = reason; 154
155 local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
156 ["xmlns:streams"] = xmlns_streams });
157
158
159 if reason then
160 close_reply.attr.condition = "remote-stream-error";
161 if type(reason) == "string" then -- assume stream error
162 close_reply:tag("stream:error")
163 :tag(reason, {xmlns = xmlns_xmpp_streams});
164 elseif type(reason) == "table" then
165 if reason.condition then
166 close_reply:tag("stream:error")
167 :tag(reason.condition, stream_xmlns_attr):up();
168 if reason.text then
169 close_reply:tag("text", stream_xmlns_attr):text(reason.text):up();
170 end
171 if reason.extra then
172 close_reply:add_child(reason.extra);
173 end
174 elseif reason.name then -- a stanza
175 close_reply = reason;
176 end
177 end
178 log("info", "Disconnecting client, <stream:error> is: %s", tostring(close_reply));
179 end
180
181 local session_close_response = { headers = default_headers, body = tostring(close_reply) };
182
183 --FIXME: Quite sure we shouldn't reply to all requests with the error
152 for _, held_request in ipairs(session.requests) do 184 for _, held_request in ipairs(session.requests) do
153 held_request:send(session_close_reply); 185 held_request:send(session_close_response);
154 held_request:destroy(); 186 held_request:destroy();
155 end 187 end
156 sessions[session.sid] = nil; 188 sessions[session.sid] = nil;
157 sm_destroy_session(session); 189 sm_destroy_session(session);
158 end 190 end