plugins/mod_bosh.lua

changeset 679
9506bf204b1a
parent 660
b2c4a7ec31c6
child 683
7428244a82a6
equal deleted inserted replaced
678:1859edec2237 679:9506bf204b1a
4 local lxp = require "lxp"; 4 local lxp = require "lxp";
5 local init_xmlhandlers = require "core.xmlhandlers" 5 local init_xmlhandlers = require "core.xmlhandlers"
6 local server = require "net.server"; 6 local server = require "net.server";
7 local httpserver = require "net.httpserver"; 7 local httpserver = require "net.httpserver";
8 local sm = require "core.sessionmanager"; 8 local sm = require "core.sessionmanager";
9 local sm_destroy_session = sm.destroy_session;
9 local new_uuid = require "util.uuid".generate; 10 local new_uuid = require "util.uuid".generate;
10 local fire_event = require "core.eventmanager".fire_event; 11 local fire_event = require "core.eventmanager".fire_event;
11 local core_process_stanza = core_process_stanza; 12 local core_process_stanza = core_process_stanza;
12 local st = require "util.stanza"; 13 local st = require "util.stanza";
13 local log = require "util.logger".init("bosh"); 14 local log = require "util.logger".init("bosh");
14 local stream_callbacks = { stream_tag = "http://jabber.org/protocol/httpbind|body" }; 15 local stream_callbacks = { stream_tag = "http://jabber.org/protocol/httpbind|body" };
15 16 local config = require "core.configmanager";
16 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send) 17 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send)
17 18
18 local BOSH_DEFAULT_HOLD = 1; 19 local BOSH_DEFAULT_HOLD = tonumber(config.get("*", "core", "bosh_default_hold")) or 1;
19 local BOSH_DEFAULT_INACTIVITY = 30; 20 local BOSH_DEFAULT_INACTIVITY = tonumber(config.get("*", "core", "bosh_max_inactivity")) or 30;
20 local BOSH_DEFAULT_POLLING = 5; 21 local BOSH_DEFAULT_POLLING = tonumber(config.get("*", "core", "bosh_max_polling")) or 5;
21 local BOSH_DEFAULT_REQUESTS = 2; 22 local BOSH_DEFAULT_REQUESTS = tonumber(config.get("*", "core", "bosh_max_requests")) or 2;
22 local BOSH_DEFAULT_MAXPAUSE = 120; 23 local BOSH_DEFAULT_MAXPAUSE = tonumber(config.get("*", "core", "bosh_max_pause")) or 300;
23 24
24 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; 25 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
25 local os_time = os.time; 26 local os_time = os.time;
26 27
27 local sessions = {}; 28 local sessions = {};
28 29 local inactive_sessions = {}; -- Sessions which have no open requests
29 -- Used to respond to idle sessions 30
31 -- Used to respond to idle sessions (those with waiting requests)
30 local waiting_requests = {}; 32 local waiting_requests = {};
31 function on_destroy_request(request) 33 function on_destroy_request(request)
32 waiting_requests[request] = nil; 34 waiting_requests[request] = nil;
33 end 35 end
34 36
35 function handle_request(method, body, request) 37 function handle_request(method, body, request)
36 if (not body) or request.method ~= "POST" then 38 if (not body) or request.method ~= "POST" then
37 --return { status = "200 OK", headers = { ["Content-Type"] = "text/html" }, body = "<html><body>You don't look like a BOSH client to me... what do you want?</body></html>" };
38 return "<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>"; 39 return "<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>";
39 end 40 end
40 if not method then 41 if not method then
41 log("debug", "Request %s suffered error %s", tostring(request.id), body); 42 log("debug", "Request %s suffered error %s", tostring(request.id), body);
42 return; 43 return;
58 log("debug", "We are holding too many requests, so..."); 59 log("debug", "We are holding too many requests, so...");
59 if #session.send_buffer > 0 then 60 if #session.send_buffer > 0 then
60 log("debug", "...sending what is in the buffer") 61 log("debug", "...sending what is in the buffer")
61 session.send(t_concat(session.send_buffer)); 62 session.send(t_concat(session.send_buffer));
62 session.send_buffer = {}; 63 session.send_buffer = {};
63 return;
64 else 64 else
65 -- or an empty response 65 -- or an empty response
66 log("debug", "...sending an empty response"); 66 log("debug", "...sending an empty response");
67 session.send(""); 67 session.send("");
68 return;
69 end 68 end
70 elseif #session.send_buffer > 0 then 69 elseif #session.send_buffer > 0 then
71 log("debug", "Session has data in the send buffer, will send now.."); 70 log("debug", "Session has data in the send buffer, will send now..");
72 local resp = t_concat(session.send_buffer); 71 local resp = t_concat(session.send_buffer);
73 session.send_buffer = {}; 72 session.send_buffer = {};
74 session.send(resp); 73 session.send(resp);
75 return;
76 end 74 end
77 75
78 if not request.destroyed and session.bosh_wait then 76 if not request.destroyed and session.bosh_wait then
79 request.reply_before = os_time() + session.bosh_wait; 77 request.reply_before = os_time() + session.bosh_wait;
80 request.on_destroy = on_destroy_request; 78 request.on_destroy = on_destroy_request;
84 log("debug", "Had nothing to say, so leaving request unanswered for now"); 82 log("debug", "Had nothing to say, so leaving request unanswered for now");
85 return true; 83 return true;
86 end 84 end
87 end 85 end
88 86
87 local session_close_reply = tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate" }));
89 local function bosh_reset_stream(session) session.notopen = true; end 88 local function bosh_reset_stream(session) session.notopen = true; end
90 local function bosh_close_stream(session, reason) end 89 local function bosh_close_stream(session, reason) end
91 90
92 function stream_callbacks.streamopened(request, attr) 91 function stream_callbacks.streamopened(request, attr)
93 print("Attr:") 92 print("Attr:")
94 for k,v in pairs(attr) do print("", k, v); end 93 for k,v in pairs(attr) do print("", k, v); end
95 log("debug", "BOSH body open (sid: %s)", attr.sid); 94 log("debug", "BOSH body open (sid: %s)", attr.sid);
96 local sid = attr.sid 95 local sid = attr.sid
97 if not sid then 96 if not sid then
97 -- New session request
98 -- TODO: Sanity checks here (rid, to, known host, etc.) 98 -- TODO: Sanity checks here (rid, to, known host, etc.)
99 request.notopen = nil; -- Signals that we accept this opening tag 99 request.notopen = nil; -- Signals that we accept this opening tag
100 100
101 -- New session 101 -- New session
102 sid = tostring(new_uuid()); 102 sid = tostring(new_uuid());
103 local session = { type = "c2s_unauthed", conn = {}, sid = sid, rid = attr.rid, host = attr.to, bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid, 103 local session = { type = "c2s_unauthed", conn = {}, sid = sid, rid = attr.rid, host = attr.to, bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid,
104 bosh_hold = BOSH_DEFAULT_HOLD, 104 bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY,
105 requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream, close = bosh_close_stream }; 105 requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream, close = bosh_close_stream, dispatch_stanza = core_process_stanza };
106 sessions[sid] = session; 106 sessions[sid] = session;
107 log("info", "New BOSH session, assigned it sid '%s'", sid); 107 log("info", "New BOSH session, assigned it sid '%s'", sid);
108 local r, send_buffer = session.requests, session.send_buffer; 108 local r, send_buffer = session.requests, session.send_buffer;
109 local response = { } 109 local response = { }
110 function session.send(s) 110 function session.send(s)
131 oldest_request:destroy(); 131 oldest_request:destroy();
132 t_remove(r, 1); 132 t_remove(r, 1);
133 end 133 end
134 elseif s ~= "" then 134 elseif s ~= "" then
135 log("debug", "Saved to send buffer because there are %d open requests", #r); 135 log("debug", "Saved to send buffer because there are %d open requests", #r);
136 if session.bosh_max_inactive and not inactive_sessions[session] then
137 inactive_sessions[session] = os_time();
138 (session.log or log)("debug", "BOSH session marked as inactive at %d", inactive_sessions[session]);
139 end
136 -- Hmm, no requests are open :( 140 -- Hmm, no requests are open :(
137 t_insert(session.send_buffer, tostring(s)); 141 t_insert(session.send_buffer, tostring(s));
138 log("debug", "There are now %d things in the send_buffer", #session.send_buffer); 142 log("debug", "There are now %d things in the send_buffer", #session.send_buffer);
139 end 143 end
140 end 144 end
143 147
144 local features = st.stanza("stream:features"); 148 local features = st.stanza("stream:features");
145 fire_event("stream-features", session, features); 149 fire_event("stream-features", session, features);
146 --xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh' 150 --xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh'
147 local response = st.stanza("body", { xmlns = xmlns_bosh, 151 local response = st.stanza("body", { xmlns = xmlns_bosh,
148 inactivity = "30", polling = "5", requests = "2", hold = tostring(session.bosh_hold), maxpause = "120", 152 inactivity = tostring(BOSH_DEFAULT_INACTIVITY), polling = tostring(BOSH_DEFAULT_POLLING), requests = tostring(BOSH_DEFAULT_REQUESTS), hold = tostring(session.bosh_hold), maxpause = "120",
149 sid = sid, ver = '1.6', from = session.host, secure = 'true', ["xmpp:version"] = "1.0", 153 sid = sid, ver = '1.6', from = session.host, secure = 'true', ["xmpp:version"] = "1.0",
150 ["xmlns:xmpp"] = "urn:xmpp:xbosh", ["xmlns:stream"] = "http://etherx.jabber.org/streams" }):add_child(features); 154 ["xmlns:xmpp"] = "urn:xmpp:xbosh", ["xmlns:stream"] = "http://etherx.jabber.org/streams" }):add_child(features);
151 request:send(tostring(response)); 155 request:send(tostring(response));
152 156
153 request.sid = sid; 157 request.sid = sid;
159 -- Unknown sid 163 -- Unknown sid
160 log("info", "Client tried to use sid '%s' which we don't know about", sid); 164 log("info", "Client tried to use sid '%s' which we don't know about", sid);
161 request:send(tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" }))); 165 request:send(tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" })));
162 request.notopen = nil; 166 request.notopen = nil;
163 return; 167 return;
168 end
169
170 if attr.type == "terminate" then
171 -- Client wants to end this session
172 (session.log or log)("info", "BOSH client disconnected");
173 for _, held_request in ipairs(session.requests) do
174 held_request:send(session_close_reply);
175 held_request:destroy();
176 end
177 sm_destroy_session(session);
178 sessions[sid] = nil;
179 request.notopen = nil;
180 return;
181 end
182
183 -- If session was inactive, make sure it is now marked as not
184 if #session.requests == 0 then
185 (session.log or log)("debug", "BOSH client now active again at %d", os_time());
186 inactive_sessions[session] = nil;
164 end 187 end
165 188
166 if session.notopen then 189 if session.notopen then
167 local features = st.stanza("stream:features"); 190 local features = st.stanza("stream:features");
168 fire_event("stream-features", session, features); 191 fire_event("stream-features", session, features);
198 if request.conn then 221 if request.conn then
199 sessions[request.sid].send(""); 222 sessions[request.sid].send("");
200 end 223 end
201 end 224 end
202 end 225 end
226
227 now = now - 3;
228 for session, inactive_since in pairs(inactive_sessions) do
229 if now - inactive_since > session.bosh_max_inactive then
230 (session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now);
231 sessions[session.sid] = nil;
232 inactive_sessions[session] = nil;
233 sm_destroy_session(session, "BOSH client silent for over "..session.bosh_max_inactive.." seconds");
234 end
235 end
203 end 236 end
204 237
205 httpserver.new{ port = 5280, base = "http-bind", handler = handle_request, ssl = false} 238 httpserver.new{ port = 5280, base = "http-bind", handler = handle_request, ssl = false}
206 server.addtimer(on_timer); 239 server.addtimer(on_timer);

mercurial