Mercurial > prosody-hg
comparison plugins/mod_bosh.lua @ 13961:ab7be38146bc
mod_bosh: Create sessions via sessionmanager
Normalizes things like session.id, .type and other properties set by
sessionmanager and util.session.
Fixes #658
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Tue, 01 Jul 2025 14:07:49 +0200 |
| parents | b1e9942ce471 |
| children | c5a2846c9cba |
comparison
equal
deleted
inserted
replaced
| 13960:b1e9942ce471 | 13961:ab7be38146bc |
|---|---|
| 8 | 8 |
| 9 module:set_global(); | 9 module:set_global(); |
| 10 | 10 |
| 11 local new_xmpp_stream = require "prosody.util.xmppstream".new; | 11 local new_xmpp_stream = require "prosody.util.xmppstream".new; |
| 12 local sm = require "prosody.core.sessionmanager"; | 12 local sm = require "prosody.core.sessionmanager"; |
| 13 local sm_new_session = sm.new_session; | |
| 13 local sm_destroy_session = sm.destroy_session; | 14 local sm_destroy_session = sm.destroy_session; |
| 14 local new_uuid = require "prosody.util.uuid".generate; | 15 local new_uuid = require "prosody.util.uuid".generate; |
| 15 local core_process_stanza = prosody.core_process_stanza; | 16 local core_process_stanza = prosody.core_process_stanza; |
| 16 local st = require "prosody.util.stanza"; | 17 local st = require "prosody.util.stanza"; |
| 17 local logger = require "prosody.util.logger"; | |
| 18 local log = module._log; | 18 local log = module._log; |
| 19 local initialize_filters = require "prosody.util.filters".initialize; | |
| 20 local math_min = math.min; | 19 local math_min = math.min; |
| 21 local tostring, type = tostring, type; | 20 local tostring, type = tostring, type; |
| 22 local traceback = debug.traceback; | 21 local traceback = debug.traceback; |
| 23 local runner = require"prosody.util.async".runner; | 22 local runner = require"prosody.util.async".runner; |
| 24 local nameprep = require "prosody.util.encodings".stringprep.nameprep; | 23 local nameprep = require "prosody.util.encodings".stringprep.nameprep; |
| 322 | 321 |
| 323 wait = math_min(wait, bosh_max_wait); | 322 wait = math_min(wait, bosh_max_wait); |
| 324 | 323 |
| 325 -- New session | 324 -- New session |
| 326 sid = new_uuid(); | 325 sid = new_uuid(); |
| 327 -- TODO use util.session | 326 local session = sm_new_session(request.conn); |
| 328 local session = { | 327 session.sid = sid; |
| 329 base_type = "c2s", type = "c2s_unauthed", conn = request.conn, sid = sid, host = attr.to, | 328 session.host = attr.to; |
| 330 rid = rid - 1, -- Hack for initial session setup, "previous" rid was $current_request - 1 | 329 session.rid = rid - 1; -- Hack for initial session setup, "previous" rid was $current_request - 1 |
| 331 bosh_version = attr.ver, bosh_wait = wait, streamid = sid, | 330 session.bosh_version = attr.ver; |
| 332 bosh_max_inactive = bosh_max_inactivity, bosh_responses = cache.new(BOSH_HOLD+1):table(); | 331 session.bosh_wait = wait; |
| 333 requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream, | 332 session.streamid = sid; |
| 334 close = bosh_close_stream, dispatch_stanza = core_process_stanza, notopen = true, | 333 session.bosh_max_inactive = bosh_max_inactivity; |
| 335 log = logger.init("bosh"..sid), secure = consider_bosh_secure or request.secure, | 334 session.bosh_responses = cache.new(BOSH_HOLD + 1):table(); |
| 336 ip = request.ip; | 335 session.requests = {}; |
| 337 }; | 336 session.send_buffer = {}; |
| 337 session.reset_stream = bosh_reset_stream; | |
| 338 session.close = bosh_close_stream; | |
| 339 session.dispatch_stanza = core_process_stanza; | |
| 340 session.notopen = true; | |
| 341 session.secure = consider_bosh_secure or request.secure; | |
| 338 sessions[sid] = session; | 342 sessions[sid] = session; |
| 339 | 343 |
| 340 session.thread = runner(function (stanza) | 344 session.thread = runner(function (stanza) |
| 341 session:dispatch_stanza(stanza); | 345 session:dispatch_stanza(stanza); |
| 342 end, runner_callbacks, session); | 346 end, runner_callbacks, session); |
| 343 | 347 |
| 344 local filter = initialize_filters(session); | |
| 345 | |
| 346 session.log("debug", "BOSH session created for request from %s", session.ip); | 348 session.log("debug", "BOSH session created for request from %s", session.ip); |
| 347 log("info", "New BOSH session, assigned it sid '%s'", sid); | 349 log("info", "New BOSH session, assigned it sid '%s'", sid); |
| 348 report_new_sid(); | 350 report_new_sid(); |
| 349 | 351 |
| 350 module:fire_event("bosh-session", { session = session, request = request }); | 352 module:fire_event("bosh-session", { session = session, request = request }); |
| 351 | 353 |
| 352 -- Send creation response | 354 -- Send creation response |
| 353 local creating_session = true; | 355 local creating_session = true; |
| 354 | 356 |
| 355 local r = session.requests; | 357 local r = session.requests; |
| 356 function session.send(s) | 358 function session.rawsend(s) |
| 357 -- We need to ensure that outgoing stanzas have the jabber:client xmlns | 359 -- We need to ensure that outgoing stanzas have the jabber:client xmlns |
| 358 if s.attr and not s.attr.xmlns then | 360 if s.attr and not s.attr.xmlns then |
| 359 s = st.clone(s); | 361 s = st.clone(s); |
| 360 s.attr.xmlns = "jabber:client"; | 362 s.attr.xmlns = "jabber:client"; |
| 361 end | 363 end |
| 362 s = filter("stanzas/out", s); | |
| 363 --log("debug", "Sending BOSH data: %s", s); | 364 --log("debug", "Sending BOSH data: %s", s); |
| 364 if not s then return true end | 365 if not s then return true end |
| 365 t_insert(session.send_buffer, tostring(s)); | 366 t_insert(session.send_buffer, tostring(s)); |
| 366 | 367 |
| 367 local oldest_request = r[1]; | 368 local oldest_request = r[1]; |
