mod_bosh: Fix to properly ignore repeated requests
[prosody.git] / plugins / mod_bosh.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 module.host = "*" -- Global module
10
11 local hosts = _G.hosts;
12 local lxp = require "lxp";
13 local init_xmlhandlers = require "core.xmlhandlers"
14 local server = require "net.server";
15 local httpserver = require "net.httpserver";
16 local sm = require "core.sessionmanager";
17 local sm_destroy_session = sm.destroy_session;
18 local new_uuid = require "util.uuid".generate;
19 local fire_event = prosody.events.fire_event;
20 local core_process_stanza = core_process_stanza;
21 local st = require "util.stanza";
22 local logger = require "util.logger";
23 local log = logger.init("mod_bosh");
24
25 local xmlns_streams = "http://etherx.jabber.org/streams";
26 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
27 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send)
28
29 local stream_callbacks = {
30         stream_ns = xmlns_bosh, stream_tag = "body", default_ns = "jabber:client" };
31
32 local BOSH_DEFAULT_HOLD = tonumber(module:get_option("bosh_default_hold")) or 1;
33 local BOSH_DEFAULT_INACTIVITY = tonumber(module:get_option("bosh_max_inactivity")) or 60;
34 local BOSH_DEFAULT_POLLING = tonumber(module:get_option("bosh_max_polling")) or 5;
35 local BOSH_DEFAULT_REQUESTS = tonumber(module:get_option("bosh_max_requests")) or 2;
36 local BOSH_DEFAULT_MAXPAUSE = tonumber(module:get_option("bosh_max_pause")) or 300;
37
38 local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure");
39
40 local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8" };
41
42 local cross_domain = module:get_option("cross_domain_bosh");
43 if cross_domain then
44         default_headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
45         default_headers["Access-Control-Allow-Headers"] = "Content-Type";
46         default_headers["Access-Control-Max-Age"] = "7200";
47
48         if cross_domain == true then
49                 default_headers["Access-Control-Allow-Origin"] = "*";
50         elseif type(cross_domain) == "table" then
51                 cross_domain = table.concat(cross_domain, ", ");
52         end
53         if type(cross_domain) == "string" then
54                 default_headers["Access-Control-Allow-Origin"] = cross_domain;
55         end
56 end
57
58 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
59 local os_time = os.time;
60
61 local sessions = {};
62 local inactive_sessions = {}; -- Sessions which have no open requests
63
64 -- Used to respond to idle sessions (those with waiting requests)
65 local waiting_requests = {};
66 function on_destroy_request(request)
67         waiting_requests[request] = nil;
68         local session = sessions[request.sid];
69         if session then
70                 local requests = session.requests;
71                 for i,r in ipairs(requests) do
72                         if r == request then
73                                 t_remove(requests, i);
74                                 break;
75                         end
76                 end
77                 
78                 -- If this session now has no requests open, mark it as inactive
79                 if #requests == 0 and session.bosh_max_inactive and not inactive_sessions[session] then
80                         inactive_sessions[session] = os_time();
81                         (session.log or log)("debug", "BOSH session marked as inactive at %d", inactive_sessions[session]);
82                 end
83         end
84 end
85
86 function handle_request(method, body, request)
87         if (not body) or request.method ~= "POST" then
88                 if request.method == "OPTIONS" then
89                         return { headers = default_headers, body = "" };
90                 else
91                         return "<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>";
92                 end
93         end
94         if not method then
95                 log("debug", "Request %s suffered error %s", tostring(request.id), body);
96                 return;
97         end
98         --log("debug", "Handling new request %s: %s\n----------", request.id, tostring(body));
99         request.notopen = true;
100         request.log = log;
101         request.on_destroy = on_destroy_request;
102         
103         local parser = lxp.new(init_xmlhandlers(request, stream_callbacks), "\1");
104         
105         parser:parse(body);
106         
107         local session = sessions[request.sid];
108         if session then
109                 local r = session.requests;
110                 log("debug", "Session %s has %d out of %d requests open", request.sid, #r, session.bosh_hold);
111                 log("debug", "and there are %d things in the send_buffer", #session.send_buffer);
112                 if #r > session.bosh_hold then
113                         -- We are holding too many requests, send what's in the buffer,
114                         log("debug", "We are holding too many requests, so...");
115                         if #session.send_buffer > 0 then
116                                 log("debug", "...sending what is in the buffer")
117                                 session.send(t_concat(session.send_buffer));
118                                 session.send_buffer = {};
119                         else
120                                 -- or an empty response
121                                 log("debug", "...sending an empty response");
122                                 session.send("");
123                         end
124                 elseif #session.send_buffer > 0 then
125                         log("debug", "Session has data in the send buffer, will send now..");
126                         local resp = t_concat(session.send_buffer);
127                         session.send_buffer = {};
128                         session.send(resp);
129                 end
130                 
131                 if not request.destroyed then
132                         -- We're keeping this request open, to respond later
133                         log("debug", "Have nothing to say, so leaving request unanswered for now");
134                         if session.bosh_wait then
135                                 request.reply_before = os_time() + session.bosh_wait;
136                                 waiting_requests[request] = true;
137                         end
138                         if inactive_sessions[session] then
139                                 -- Session was marked as inactive, since we have
140                                 -- a request open now, unmark it
141                                 inactive_sessions[session] = nil;
142                         end
143                 end
144                 
145                 return true; -- Inform httpserver we shall reply later
146         end
147 end
148
149
150 local function bosh_reset_stream(session) session.notopen = true; end
151
152 local stream_xmlns_attr = { xmlns = "urn:ietf:params:xml:ns:xmpp-streams" };
153
154 local function bosh_close_stream(session, reason)
155         (session.log or log)("info", "BOSH client disconnected");
156         
157         local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
158                 ["xmlns:streams"] = xmlns_streams });
159         
160
161         if reason then
162                 close_reply.attr.condition = "remote-stream-error";
163                 if type(reason) == "string" then -- assume stream error
164                         close_reply:tag("stream:error")
165                                 :tag(reason, {xmlns = xmlns_xmpp_streams});
166                 elseif type(reason) == "table" then
167                         if reason.condition then
168                                 close_reply:tag("stream:error")
169                                         :tag(reason.condition, stream_xmlns_attr):up();
170                                 if reason.text then
171                                         close_reply:tag("text", stream_xmlns_attr):text(reason.text):up();
172                                 end
173                                 if reason.extra then
174                                         close_reply:add_child(reason.extra);
175                                 end
176                         elseif reason.name then -- a stanza
177                                 close_reply = reason;
178                         end
179                 end
180                 log("info", "Disconnecting client, <stream:error> is: %s", tostring(close_reply));
181         end
182
183         local session_close_response = { headers = default_headers, body = tostring(close_reply) };
184
185         --FIXME: Quite sure we shouldn't reply to all requests with the error
186         for _, held_request in ipairs(session.requests) do
187                 held_request:send(session_close_response);
188                 held_request:destroy();
189         end
190         sessions[session.sid]  = nil;
191         sm_destroy_session(session);
192 end
193
194 function stream_callbacks.streamopened(request, attr)
195         log("debug", "BOSH body open (sid: %s)", attr.sid);
196         local sid = attr.sid
197         if not sid then
198                 -- New session request
199                 request.notopen = nil; -- Signals that we accept this opening tag
200                 
201                 -- TODO: Sanity checks here (rid, to, known host, etc.)
202                 if not hosts[attr.to] then
203                         -- Unknown host
204                         log("debug", "BOSH client tried to connect to unknown host: %s", tostring(attr.to));
205                         session_close_reply.body.attr.condition = "host-unknown";
206                         request:send(session_close_reply);
207                         request.notopen = nil
208                         return;
209                 end
210                 
211                 -- New session
212                 sid = new_uuid();
213                 local session = {
214                         type = "c2s_unauthed", conn = {}, sid = sid, rid = tonumber(attr.rid)-1, host = attr.to,
215                         bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid,
216                         bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY,
217                         requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream,
218                         close = bosh_close_stream, dispatch_stanza = core_process_stanza,
219                         log = logger.init("bosh"..sid), secure = consider_bosh_secure or request.secure
220                 };
221                 sessions[sid] = session;
222                 
223                 log("info", "New BOSH session, assigned it sid '%s'", sid);
224                 local r, send_buffer = session.requests, session.send_buffer;
225                 local response = { headers = default_headers }
226                 function session.send(s)
227                         -- We need to ensure that outgoing stanzas have the jabber:client xmlns
228                         if s.attr and not s.attr.xmlns then
229                                 s = st.clone(s);
230                                 s.attr.xmlns = "jabber:client";
231                         end
232                         --log("debug", "Sending BOSH data: %s", tostring(s));
233                         local oldest_request = r[1];
234                         if oldest_request then
235                                 log("debug", "We have an open request, so sending on that");
236                                 response.body = t_concat{"<body xmlns='http://jabber.org/protocol/httpbind' sid='", sid, "' xmlns:stream = 'http://etherx.jabber.org/streams'>", tostring(s), "</body>" };
237                                 oldest_request:send(response);
238                                 --log("debug", "Sent");
239                                 if oldest_request.stayopen then
240                                         if #r>1 then
241                                                 -- Move front request to back
242                                                 t_insert(r, oldest_request);
243                                                 t_remove(r, 1);
244                                         end
245                                 else
246                                         log("debug", "Destroying the request now...");
247                                         oldest_request:destroy();
248                                 end
249                         elseif s ~= "" then
250                                 log("debug", "Saved to send buffer because there are %d open requests", #r);
251                                 -- Hmm, no requests are open :(
252                                 t_insert(session.send_buffer, tostring(s));
253                                 log("debug", "There are now %d things in the send_buffer", #session.send_buffer);
254                         end
255                 end
256                 
257                 -- Send creation response
258                 
259                 local features = st.stanza("stream:features");
260                 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
261                 fire_event("stream-features", session, features);
262                 --xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh'
263                 local response = st.stanza("body", { xmlns = xmlns_bosh,
264                                                                         inactivity = tostring(BOSH_DEFAULT_INACTIVITY), polling = tostring(BOSH_DEFAULT_POLLING), requests = tostring(BOSH_DEFAULT_REQUESTS), hold = tostring(session.bosh_hold), maxpause = "120",
265                                                                         sid = sid, authid = sid, ver  = '1.6', from = session.host, secure = 'true', ["xmpp:version"] = "1.0",
266                                                                         ["xmlns:xmpp"] = "urn:xmpp:xbosh", ["xmlns:stream"] = "http://etherx.jabber.org/streams" }):add_child(features);
267                 request:send{ headers = default_headers, body = tostring(response) };
268                 
269                 request.sid = sid;
270                 return;
271         end
272         
273         local session = sessions[sid];
274         if not session then
275                 -- Unknown sid
276                 log("info", "Client tried to use sid '%s' which we don't know about", sid);
277                 request:send{ headers = default_headers, body = tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" })) };
278                 request.notopen = nil;
279                 return;
280         end
281         
282         if session.rid then
283                 local rid = tonumber(attr.rid);
284                 local diff = rid - session.rid;
285                 if diff > 1 then
286                         session.log("warn", "rid too large (means a request was lost). Last rid: %d New rid: %s", session.rid, attr.rid);
287                 elseif diff <= 0 then
288                         -- Repeated, ignore
289                         session.log("debug", "rid repeated (on request %s), ignoring: %s (diff %d)", request.id, session.rid, diff);
290                         request.notopen = nil;
291                         request.ignore = true;
292                         request.sid = sid;
293                         t_insert(session.requests, request);
294                         return;
295                 end
296                 session.rid = rid;
297         end
298         
299         if attr.type == "terminate" then
300                 -- Client wants to end this session
301                 session:close();
302                 request.notopen = nil;
303                 return;
304         end
305         
306         if session.notopen then
307                 local features = st.stanza("stream:features");
308                 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
309                 fire_event("stream-features", session, features);
310                 session.send(features);
311                 session.notopen = nil;
312         end
313         
314         request.notopen = nil; -- Signals that we accept this opening tag
315         t_insert(session.requests, request);
316         request.sid = sid;
317 end
318
319 function stream_callbacks.handlestanza(request, stanza)
320         if request.ignore then return; end
321         log("debug", "BOSH stanza received: %s\n", stanza:top_tag());
322         local session = sessions[request.sid];
323         if session then
324                 if stanza.attr.xmlns == xmlns_bosh then
325                         stanza.attr.xmlns = nil;
326                 end
327                 session.ip = request.handler:ip();
328                 core_process_stanza(session, stanza);
329         end
330 end
331
332 function stream_callbacks.error(request, error)
333         log("debug", "Error parsing BOSH request payload; %s", error);
334         if not request.sid then
335                 request:send({ headers = default_headers, status = "400 Bad Request" });
336                 return;
337         end
338         
339         local session = sessions[request.sid];
340         if error == "stream-error" then -- Remote stream error, we close normally
341                 session:close();
342         else
343                 session:close({ condition = "bad-format", text = "Error processing stream" });
344         end
345 end
346
347 local dead_sessions = {};
348 function on_timer()
349         -- log("debug", "Checking for requests soon to timeout...");
350         -- Identify requests timing out within the next few seconds
351         local now = os_time() + 3;
352         for request in pairs(waiting_requests) do
353                 if request.reply_before <= now then
354                         log("debug", "%s was soon to timeout, sending empty response", request.id);
355                         -- Send empty response to let the
356                         -- client know we're still here
357                         if request.conn then
358                                 sessions[request.sid].send("");
359                         end
360                 end
361         end
362         
363         now = now - 3;
364         local n_dead_sessions = 0;
365         for session, inactive_since in pairs(inactive_sessions) do
366                 if session.bosh_max_inactive then
367                         if now - inactive_since > session.bosh_max_inactive then
368                                 (session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now);
369                                 sessions[session.sid]  = nil;
370                                 inactive_sessions[session] = nil;
371                                 n_dead_sessions = n_dead_sessions + 1;
372                                 dead_sessions[n_dead_sessions] = session;
373                         end
374                 else
375                         inactive_sessions[session] = nil;
376                 end
377         end
378
379         for i=1,n_dead_sessions do
380                 local session = dead_sessions[i];
381                 dead_sessions[i] = nil;
382                 sm_destroy_session(session, "BOSH client silent for over "..session.bosh_max_inactive.." seconds");
383         end
384 end
385
386
387 local function setup()
388         local ports = module:get_option("bosh_ports") or { 5280 };
389         httpserver.new_from_config(ports, handle_request, { base = "http-bind" });
390         server.addtimer(on_timer);
391 end
392 if prosody.start_time then -- already started
393         setup();
394 else
395         prosody.events.add_handler("server-started", setup);
396 end