Comparison

plugins/mod_bosh.lua @ 5189:7f6cc8ed2247

Merge 0.9->trunk
author Matthew Wild <mwild1@gmail.com>
date Thu, 22 Nov 2012 19:38:42 +0000
parent 5188:6689605f8591
child 5634:7298c9bbb30f
child 5639:7a0e19e649b7
comparison
equal deleted inserted replaced
5184:e71cf524893c 5189:7f6cc8ed2247
16 local fire_event = prosody.events.fire_event; 16 local fire_event = prosody.events.fire_event;
17 local core_process_stanza = prosody.core_process_stanza; 17 local core_process_stanza = prosody.core_process_stanza;
18 local st = require "util.stanza"; 18 local st = require "util.stanza";
19 local logger = require "util.logger"; 19 local logger = require "util.logger";
20 local log = logger.init("mod_bosh"); 20 local log = logger.init("mod_bosh");
21 local initialize_filters = require "util.filters".initialize;
22 local math_min = math.min;
21 23
22 local xmlns_streams = "http://etherx.jabber.org/streams"; 24 local xmlns_streams = "http://etherx.jabber.org/streams";
23 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams"; 25 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
24 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send) 26 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send)
25 27
28 30
29 local BOSH_DEFAULT_HOLD = module:get_option_number("bosh_default_hold", 1); 31 local BOSH_DEFAULT_HOLD = module:get_option_number("bosh_default_hold", 1);
30 local BOSH_DEFAULT_INACTIVITY = module:get_option_number("bosh_max_inactivity", 60); 32 local BOSH_DEFAULT_INACTIVITY = module:get_option_number("bosh_max_inactivity", 60);
31 local BOSH_DEFAULT_POLLING = module:get_option_number("bosh_max_polling", 5); 33 local BOSH_DEFAULT_POLLING = module:get_option_number("bosh_max_polling", 5);
32 local BOSH_DEFAULT_REQUESTS = module:get_option_number("bosh_max_requests", 2); 34 local BOSH_DEFAULT_REQUESTS = module:get_option_number("bosh_max_requests", 2);
35 local bosh_max_wait = module:get_option_number("bosh_max_wait", 120);
33 36
34 local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure"); 37 local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure");
35 38
36 local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8" }; 39 local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8", ["Connection"] = "keep-alive" };
37 40
38 local cross_domain = module:get_option("cross_domain_bosh", false); 41 local cross_domain = module:get_option("cross_domain_bosh", false);
39 if cross_domain then 42 if cross_domain then
40 default_headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"; 43 default_headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
41 default_headers["Access-Control-Allow-Headers"] = "Content-Type"; 44 default_headers["Access-Control-Allow-Headers"] = "Content-Type";
241 244
242 -- New session 245 -- New session
243 sid = new_uuid(); 246 sid = new_uuid();
244 local session = { 247 local session = {
245 type = "c2s_unauthed", conn = {}, sid = sid, rid = tonumber(attr.rid)-1, host = attr.to, 248 type = "c2s_unauthed", conn = {}, sid = sid, rid = tonumber(attr.rid)-1, host = attr.to,
246 bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid, 249 bosh_version = attr.ver, bosh_wait = math_min(attr.wait, bosh_max_wait), streamid = sid,
247 bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY, 250 bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY,
248 requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream, 251 requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream,
249 close = bosh_close_stream, dispatch_stanza = core_process_stanza, notopen = true, 252 close = bosh_close_stream, dispatch_stanza = core_process_stanza, notopen = true,
250 log = logger.init("bosh"..sid), secure = consider_bosh_secure or request.secure, 253 log = logger.init("bosh"..sid), secure = consider_bosh_secure or request.secure,
251 ip = get_ip_from_request(request); 254 ip = get_ip_from_request(request);
252 }; 255 };
253 sessions[sid] = session; 256 sessions[sid] = session;
254 257
258 local filter = initialize_filters(session);
259
255 session.log("debug", "BOSH session created for request from %s", session.ip); 260 session.log("debug", "BOSH session created for request from %s", session.ip);
256 log("info", "New BOSH session, assigned it sid '%s'", sid); 261 log("info", "New BOSH session, assigned it sid '%s'", sid);
257 262
258 -- Send creation response 263 -- Send creation response
259 local creating_session = true; 264 local creating_session = true;
263 -- We need to ensure that outgoing stanzas have the jabber:client xmlns 268 -- We need to ensure that outgoing stanzas have the jabber:client xmlns
264 if s.attr and not s.attr.xmlns then 269 if s.attr and not s.attr.xmlns then
265 s = st.clone(s); 270 s = st.clone(s);
266 s.attr.xmlns = "jabber:client"; 271 s.attr.xmlns = "jabber:client";
267 end 272 end
273 s = filter("stanzas/out", s);
268 --log("debug", "Sending BOSH data: %s", tostring(s)); 274 --log("debug", "Sending BOSH data: %s", tostring(s));
269 t_insert(session.send_buffer, tostring(s)); 275 t_insert(session.send_buffer, tostring(s));
270 276
271 local oldest_request = r[1]; 277 local oldest_request = r[1];
272 if oldest_request and not session.bosh_processing then 278 if oldest_request and not session.bosh_processing then
276 ["xmlns:stream"] = "http://etherx.jabber.org/streams"; 282 ["xmlns:stream"] = "http://etherx.jabber.org/streams";
277 type = session.bosh_terminate and "terminate" or nil; 283 type = session.bosh_terminate and "terminate" or nil;
278 sid = sid; 284 sid = sid;
279 }; 285 };
280 if creating_session then 286 if creating_session then
281 body_attr.wait = attr.wait;
282 body_attr.inactivity = tostring(BOSH_DEFAULT_INACTIVITY); 287 body_attr.inactivity = tostring(BOSH_DEFAULT_INACTIVITY);
283 body_attr.polling = tostring(BOSH_DEFAULT_POLLING); 288 body_attr.polling = tostring(BOSH_DEFAULT_POLLING);
284 body_attr.requests = tostring(BOSH_DEFAULT_REQUESTS); 289 body_attr.requests = tostring(BOSH_DEFAULT_REQUESTS);
290 body_attr.wait = tostring(session.bosh_wait);
285 body_attr.hold = tostring(session.bosh_hold); 291 body_attr.hold = tostring(session.bosh_hold);
286 body_attr.authid = sid; 292 body_attr.authid = sid;
287 body_attr.secure = "true"; 293 body_attr.secure = "true";
288 body_attr.ver = '1.6'; from = session.host; 294 body_attr.ver = '1.6'; from = session.host;
289 body_attr["xmlns:xmpp"] = "urn:xmpp:xbosh"; 295 body_attr["xmlns:xmpp"] = "urn:xmpp:xbosh";
350 local session = sessions[context.sid]; 356 local session = sessions[context.sid];
351 if session then 357 if session then
352 if stanza.attr.xmlns == xmlns_bosh then 358 if stanza.attr.xmlns == xmlns_bosh then
353 stanza.attr.xmlns = nil; 359 stanza.attr.xmlns = nil;
354 end 360 end
361 stanza = session.filter("stanzas/in", stanza);
355 core_process_stanza(session, stanza); 362 core_process_stanza(session, stanza);
356 end 363 end
357 end 364 end
358 365
359 function stream_callbacks.streamclosed(request) 366 function stream_callbacks.streamclosed(request)