Software /
code /
prosody
Annotate
plugins/mod_bosh.lua @ 1136:506012db54e8
mod_pep: Initial commit (extremely basic implementation)
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 08 May 2009 01:07:35 +0500 |
parent | 1112:e8a00a2ea910 |
child | 1522:569d58d21612 |
rev | line source |
---|---|
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 module.host = "*" -- Global module |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
1042
a3d77353c18a
mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents:
947
diff
changeset
|
4 local hosts = _G.hosts; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local lxp = require "lxp"; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local init_xmlhandlers = require "core.xmlhandlers" |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local server = require "net.server"; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local httpserver = require "net.httpserver"; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local sm = require "core.sessionmanager"; |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
10 local sm_destroy_session = sm.destroy_session; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local new_uuid = require "util.uuid".generate; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local fire_event = require "core.eventmanager".fire_event; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local core_process_stanza = core_process_stanza; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local st = require "util.stanza"; |
1109
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
15 local logger = require "util.logger"; |
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
16 local log = logger.init("mod_bosh"); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local stream_callbacks = { stream_tag = "http://jabber.org/protocol/httpbind|body" }; |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
18 local config = require "core.configmanager"; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send) |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
21 local BOSH_DEFAULT_HOLD = tonumber(config.get("*", "core", "bosh_default_hold")) or 1; |
683
7428244a82a6
Change default maximum inactivity period to 60s from 30s
Matthew Wild <mwild1@gmail.com>
parents:
679
diff
changeset
|
22 local BOSH_DEFAULT_INACTIVITY = tonumber(config.get("*", "core", "bosh_max_inactivity")) or 60; |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
23 local BOSH_DEFAULT_POLLING = tonumber(config.get("*", "core", "bosh_max_polling")) or 5; |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
24 local BOSH_DEFAULT_REQUESTS = tonumber(config.get("*", "core", "bosh_max_requests")) or 2; |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
25 local BOSH_DEFAULT_MAXPAUSE = tonumber(config.get("*", "core", "bosh_max_pause")) or 300; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
866
8958fe4b2391
mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents:
816
diff
changeset
|
27 local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8" }; |
8958fe4b2391
mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents:
816
diff
changeset
|
28 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 local os_time = os.time; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 local sessions = {}; |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
33 local inactive_sessions = {}; -- Sessions which have no open requests |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
35 -- Used to respond to idle sessions (those with waiting requests) |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 local waiting_requests = {}; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 function on_destroy_request(request) |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 waiting_requests[request] = nil; |
947
84202314ab7f
mod_bosh: Fix to correctly timeout idle sessions
Matthew Wild <mwild1@gmail.com>
parents:
866
diff
changeset
|
39 local session = sessions[request.sid]; |
816
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
40 if session then |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
41 local requests = session.requests; |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
42 for i,r in pairs(requests) do |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
43 if r == request then requests[i] = nil; break; end |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
44 end |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
45 |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
46 -- If this session now has no requests open, mark it as inactive |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
47 if #requests == 0 and session.bosh_max_inactive and not inactive_sessions[session] then |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
48 inactive_sessions[session] = os_time(); |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
49 (session.log or log)("debug", "BOSH session marked as inactive at %d", inactive_sessions[session]); |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
50 end |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
51 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 function handle_request(method, body, request) |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 if (not body) or request.method ~= "POST" then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 return "<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>"; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 if not method then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 log("debug", "Request %s suffered error %s", tostring(request.id), body); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 return; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
771
ecdf72f9b085
Remove redundant logging and debug printing from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
763
diff
changeset
|
62 --log("debug", "Handling new request %s: %s\n----------", request.id, tostring(body)); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 request.notopen = true; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 request.log = log; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 local parser = lxp.new(init_xmlhandlers(request, stream_callbacks), "|"); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 parser:parse(body); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 local session = sessions[request.sid]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 if session then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 local r = session.requests; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 log("debug", "Session %s has %d out of %d requests open", request.sid, #r, session.bosh_hold); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 log("debug", "and there are %d things in the send_buffer", #session.send_buffer); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 if #r > session.bosh_hold then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 -- We are holding too many requests, send what's in the buffer, |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 log("debug", "We are holding too many requests, so..."); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 if #session.send_buffer > 0 then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 log("debug", "...sending what is in the buffer") |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 session.send(t_concat(session.send_buffer)); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 session.send_buffer = {}; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 else |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 -- or an empty response |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 log("debug", "...sending an empty response"); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 session.send(""); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 elseif #session.send_buffer > 0 then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 log("debug", "Session has data in the send buffer, will send now.."); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 local resp = t_concat(session.send_buffer); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 session.send_buffer = {}; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 session.send(resp); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 if not request.destroyed and session.bosh_wait then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 request.reply_before = os_time() + session.bosh_wait; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 request.on_destroy = on_destroy_request; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 waiting_requests[request] = true; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 |
771
ecdf72f9b085
Remove redundant logging and debug printing from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
763
diff
changeset
|
99 log("debug", "Have nothing to say, so leaving request unanswered for now"); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 return true; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
104 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 local function bosh_reset_stream(session) session.notopen = true; end |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
106 |
1047
8c2d88cda1dd
mod_bosh: Fix nil indexing when client connects to unknown host
Matthew Wild <mwild1@gmail.com>
parents:
947
diff
changeset
|
107 local session_close_reply = { headers = default_headers, body = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate" }), attr = {} }; |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
108 local function bosh_close_stream(session, reason) |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
109 (session.log or log)("info", "BOSH client disconnected"); |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
110 session_close_reply.attr.condition = reason; |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
111 local session_close_reply = tostring(session_close_reply); |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
112 for _, held_request in ipairs(session.requests) do |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
113 held_request:send(session_close_reply); |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
114 held_request:destroy(); |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
115 end |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
116 sessions[session.sid] = nil; |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
117 sm_destroy_session(session); |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
118 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 function stream_callbacks.streamopened(request, attr) |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 log("debug", "BOSH body open (sid: %s)", attr.sid); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 local sid = attr.sid |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 if not sid then |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
124 -- New session request |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
125 request.notopen = nil; -- Signals that we accept this opening tag |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
126 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 -- TODO: Sanity checks here (rid, to, known host, etc.) |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
128 if not hosts[attr.to] then |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
129 -- Unknown host |
1048
45fc590539cd
mod_bosh: Add log message for clients connecting to unknown host
Matthew Wild <mwild1@gmail.com>
parents:
1047
diff
changeset
|
130 log("debug", "BOSH client tried to connect to unknown host: %s", tostring(attr.to)); |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
131 session_close_reply.attr.condition = "host-unknown"; |
866
8958fe4b2391
mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents:
816
diff
changeset
|
132 request:send{ headers = default_headers, body = tostring(session_close_reply) }; |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
133 request.notopen = nil |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
134 return; |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
135 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 -- New session |
763
8e77a39826c2
mod_bosh: No need to tostring() uuids now
Matthew Wild <mwild1@gmail.com>
parents:
725
diff
changeset
|
138 sid = new_uuid(); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 local session = { type = "c2s_unauthed", conn = {}, sid = sid, rid = attr.rid, host = attr.to, bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid, |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
140 bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY, |
1109
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
141 requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream, close = bosh_close_stream, |
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
142 dispatch_stanza = core_process_stanza, log = logger.init("bosh"..sid) }; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 sessions[sid] = session; |
1109
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
144 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 log("info", "New BOSH session, assigned it sid '%s'", sid); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 local r, send_buffer = session.requests, session.send_buffer; |
866
8958fe4b2391
mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents:
816
diff
changeset
|
147 local response = { headers = default_headers } |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 function session.send(s) |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 log("debug", "Sending BOSH data: %s", tostring(s)); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 local oldest_request = r[1]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 while oldest_request and oldest_request.destroyed do |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 t_remove(r, 1); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 waiting_requests[oldest_request] = nil; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 oldest_request = r[1]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 if oldest_request then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 log("debug", "We have an open request, so using that to send with"); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 response.body = t_concat{"<body xmlns='http://jabber.org/protocol/httpbind' sid='", sid, "' xmlns:stream = 'http://etherx.jabber.org/streams'>", tostring(s), "</body>" }; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 oldest_request:send(response); |
771
ecdf72f9b085
Remove redundant logging and debug printing from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
763
diff
changeset
|
160 --log("debug", "Sent"); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 if oldest_request.stayopen then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 if #r>1 then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 -- Move front request to back |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 t_insert(r, oldest_request); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 t_remove(r, 1); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 else |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 log("debug", "Destroying the request now..."); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 oldest_request:destroy(); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 t_remove(r, 1); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 elseif s ~= "" then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 log("debug", "Saved to send buffer because there are %d open requests", #r); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 -- Hmm, no requests are open :( |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 t_insert(session.send_buffer, tostring(s)); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 log("debug", "There are now %d things in the send_buffer", #session.send_buffer); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 -- Send creation response |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 local features = st.stanza("stream:features"); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 fire_event("stream-features", session, features); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 --xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh' |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 local response = st.stanza("body", { xmlns = xmlns_bosh, |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
186 inactivity = tostring(BOSH_DEFAULT_INACTIVITY), polling = tostring(BOSH_DEFAULT_POLLING), requests = tostring(BOSH_DEFAULT_REQUESTS), hold = tostring(session.bosh_hold), maxpause = "120", |
1049
c476bceaf2db
mod_bosh: Put dummy authid in session creation response to keep JSJaC happy
Matthew Wild <mwild1@gmail.com>
parents:
1048
diff
changeset
|
187 sid = sid, authid = sid, ver = '1.6', from = session.host, secure = 'true', ["xmpp:version"] = "1.0", |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 ["xmlns:xmpp"] = "urn:xmpp:xbosh", ["xmlns:stream"] = "http://etherx.jabber.org/streams" }):add_child(features); |
866
8958fe4b2391
mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents:
816
diff
changeset
|
189 request:send{ headers = default_headers, body = tostring(response) }; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 request.sid = sid; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 return; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 local session = sessions[sid]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 if not session then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 -- Unknown sid |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 log("info", "Client tried to use sid '%s' which we don't know about", sid); |
866
8958fe4b2391
mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents:
816
diff
changeset
|
199 request:send{ headers = default_headers, body = tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" })) }; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 request.notopen = nil; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 return; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
204 if attr.type == "terminate" then |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
205 -- Client wants to end this session |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
206 session:close(); |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
207 request.notopen = nil; |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
208 return; |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
209 end |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
210 |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
211 -- If session was inactive, make sure it is now marked as not |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
212 if #session.requests == 0 then |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
213 (session.log or log)("debug", "BOSH client now active again at %d", os_time()); |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
214 inactive_sessions[session] = nil; |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
215 end |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
216 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 if session.notopen then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 local features = st.stanza("stream:features"); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 fire_event("stream-features", session, features); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 session.send(features); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 session.notopen = nil; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 request.notopen = nil; -- Signals that we accept this opening tag |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 t_insert(session.requests, request); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 request.sid = sid; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 function stream_callbacks.handlestanza(request, stanza) |
725
96110075288b
Replacing pretty_print() with top_tag() for logging
Matthew Wild <mwild1@gmail.com>
parents:
701
diff
changeset
|
230 log("debug", "BOSH stanza received: %s\n", stanza:top_tag()); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 local session = sessions[request.sid]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
232 if session then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 if stanza.attr.xmlns == xmlns_bosh then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 stanza.attr.xmlns = "jabber:client"; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
236 core_process_stanza(session, stanza); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
238 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
239 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
240 function on_timer() |
660
b2c4a7ec31c6
Remove some old debugging code from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
636
diff
changeset
|
241 -- log("debug", "Checking for requests soon to timeout..."); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 -- Identify requests timing out within the next few seconds |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
243 local now = os_time() + 3; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 for request in pairs(waiting_requests) do |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
245 if request.reply_before <= now then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
246 log("debug", "%s was soon to timeout, sending empty response", request.id); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
247 -- Send empty response to let the |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
248 -- client know we're still here |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
249 if request.conn then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
250 sessions[request.sid].send(""); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
252 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
253 end |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
254 |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
255 now = now - 3; |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
256 for session, inactive_since in pairs(inactive_sessions) do |
692
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
257 if session.bosh_max_inactive then |
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
258 if now - inactive_since > session.bosh_max_inactive then |
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
259 (session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now); |
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
260 sessions[session.sid] = nil; |
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
261 inactive_sessions[session] = nil; |
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
262 sm_destroy_session(session, "BOSH client silent for over "..session.bosh_max_inactive.." seconds"); |
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
263 end |
693
d8b793e612a9
BOSH: Make previous fix a bit more efficient
Matthew Wild <mwild1@gmail.com>
parents:
692
diff
changeset
|
264 else |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
265 inactive_sessions[session] = nil; |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
266 end |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
267 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
268 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
269 |
701
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
270 local ports = config.get(module.host, "core", "bosh_ports") or { 5280 }; |
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
271 for _, options in ipairs(ports) do |
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
272 local port, base, ssl, interface = 5280, "http-bind", false, nil; |
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
273 if type(options) == "number" then |
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
274 port = options; |
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
275 elseif type(options) == "table" then |
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
276 port, base, ssl, interface = options.port or 5280, options.path or "http-bind", options.ssl or false, options.interface; |
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
277 elseif type(options) == "string" then |
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
278 base = options; |
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
279 end |
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
280 httpserver.new{ port = port, base = base, handler = handle_request, ssl = ssl } |
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
281 end |
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
282 |
660
b2c4a7ec31c6
Remove some old debugging code from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
636
diff
changeset
|
283 server.addtimer(on_timer); |