net.http.server: Properly handle persistent connections
[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:set_global(); -- Global module
10
11 local hosts = _G.hosts;
12 local new_xmpp_stream = require "util.xmppstream".new;
13 local sm = require "core.sessionmanager";
14 local sm_destroy_session = sm.destroy_session;
15 local new_uuid = require "util.uuid".generate;
16 local fire_event = prosody.events.fire_event;
17 local core_process_stanza = prosody.core_process_stanza;
18 local st = require "util.stanza";
19 local logger = require "util.logger";
20 local log = logger.init("mod_bosh");
21 local initialize_filters = require "util.filters".initialize;
22 local math_min = math.min;
23
24 local xmlns_streams = "http://etherx.jabber.org/streams";
25 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
26 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send)
27
28 local stream_callbacks = {
29         stream_ns = xmlns_bosh, stream_tag = "body", default_ns = "jabber:client" };
30
31 local BOSH_DEFAULT_HOLD = module:get_option_number("bosh_default_hold", 1);
32 local BOSH_DEFAULT_INACTIVITY = module:get_option_number("bosh_max_inactivity", 60);
33 local BOSH_DEFAULT_POLLING = module:get_option_number("bosh_max_polling", 5);
34 local BOSH_DEFAULT_REQUESTS = module:get_option_number("bosh_max_requests", 2);
35 local bosh_max_wait = module:get_option_number("bosh_max_wait", 120);
36
37 local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure");
38
39 local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8", ["Connection"] = "keep-alive" };
40
41 local cross_domain = module:get_option("cross_domain_bosh", false);
42 if cross_domain then
43         default_headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
44         default_headers["Access-Control-Allow-Headers"] = "Content-Type";
45         default_headers["Access-Control-Max-Age"] = "7200";
46
47         if cross_domain == true then
48                 default_headers["Access-Control-Allow-Origin"] = "*";
49         elseif type(cross_domain) == "table" then
50                 cross_domain = table.concat(cross_domain, ", ");
51         end
52         if type(cross_domain) == "string" then
53                 default_headers["Access-Control-Allow-Origin"] = cross_domain;
54         end
55 end
56
57 local trusted_proxies = module:get_option_set("trusted_proxies", {"127.0.0.1"})._items;
58
59 local function get_ip_from_request(request)
60         local ip = request.conn:ip();
61         local forwarded_for = request.headers.x_forwarded_for;
62         if forwarded_for then
63                 forwarded_for = forwarded_for..", "..ip;
64                 for forwarded_ip in forwarded_for:gmatch("[^%s,]+") do
65                         if not trusted_proxies[forwarded_ip] then
66                                 ip = forwarded_ip;
67                         end
68                 end
69         end
70         return ip;
71 end
72
73 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
74 local os_time = os.time;
75
76 -- All sessions, and sessions that have no requests open
77 local sessions, inactive_sessions = module:shared("sessions", "inactive_sessions");
78
79 -- Used to respond to idle sessions (those with waiting requests)
80 local waiting_requests = {};
81 function on_destroy_request(request)
82         log("debug", "Request destroyed: %s", tostring(request));
83         waiting_requests[request] = nil;
84         local session = sessions[request.context.sid];
85         if session then
86                 local requests = session.requests;
87                 for i, r in ipairs(requests) do
88                         if r == request then
89                                 t_remove(requests, i);
90                                 break;
91                         end
92                 end
93                 
94                 -- If this session now has no requests open, mark it as inactive
95                 local max_inactive = session.bosh_max_inactive;
96                 if max_inactive and #requests == 0 then
97                         inactive_sessions[session] = os_time() + max_inactive;
98                         (session.log or log)("debug", "BOSH session marked as inactive (for %ds)", max_inactive);
99                 end
100         end
101 end
102
103 function handle_OPTIONS(request)
104         local headers = {};
105         for k,v in pairs(default_headers) do headers[k] = v; end
106         headers["Content-Type"] = nil;
107         return { headers = headers, body = "" };
108 end
109
110 function handle_POST(event)
111         log("debug", "Handling new request %s: %s\n----------", tostring(event.request), tostring(event.request.body));
112
113         local request, response = event.request, event.response;
114         response.on_destroy = on_destroy_request;
115         local body = request.body;
116
117         local context = { request = request, response = response, notopen = true };
118         local stream = new_xmpp_stream(context, stream_callbacks);
119         response.context = context;
120         
121         -- stream:feed() calls the stream_callbacks, so all stanzas in
122         -- the body are processed in this next line before it returns.
123         -- In particular, the streamopened() stream callback is where
124         -- much of the session logic happens, because it's where we first
125         -- get to see the 'sid' of this request.
126         stream:feed(body);
127         
128         -- Stanzas (if any) in the request have now been processed, and
129         -- we take care of the high-level BOSH logic here, including
130         -- giving a response or putting the request "on hold".
131         local session = sessions[context.sid];
132         if session then
133                 -- Session was marked as inactive, since we have
134                 -- a request open now, unmark it
135                 if inactive_sessions[session] and #session.requests > 0 then
136                         inactive_sessions[session] = nil;
137                 end
138
139                 local r = session.requests;
140                 log("debug", "Session %s has %d out of %d requests open", context.sid, #r, session.bosh_hold);
141                 log("debug", "and there are %d things in the send_buffer:", #session.send_buffer);
142                 for i, thing in ipairs(session.send_buffer) do
143                         log("debug", "    %s", tostring(thing));
144                 end
145                 if #r > session.bosh_hold then
146                         -- We are holding too many requests, send what's in the buffer,
147                         log("debug", "We are holding too many requests, so...");
148                         if #session.send_buffer > 0 then
149                                 log("debug", "...sending what is in the buffer")
150                                 session.send(t_concat(session.send_buffer));
151                                 session.send_buffer = {};
152                         else
153                                 -- or an empty response
154                                 log("debug", "...sending an empty response");
155                                 session.send("");
156                         end
157                 elseif #session.send_buffer > 0 then
158                         log("debug", "Session has data in the send buffer, will send now..");
159                         local resp = t_concat(session.send_buffer);
160                         session.send_buffer = {};
161                         session.send(resp);
162                 end
163                 
164                 if not response.finished then
165                         -- We're keeping this request open, to respond later
166                         log("debug", "Have nothing to say, so leaving request unanswered for now");
167                         if session.bosh_wait then
168                                 waiting_requests[response] = os_time() + session.bosh_wait;
169                         end
170                 end
171                 
172                 if session.bosh_terminate then
173                         session.log("debug", "Closing session with %d requests open", #session.requests);
174                         session:close();
175                         return nil;
176                 else
177                         return true; -- Inform http server we shall reply later
178                 end
179         end
180 end
181
182
183 local function bosh_reset_stream(session) session.notopen = true; end
184
185 local stream_xmlns_attr = { xmlns = "urn:ietf:params:xml:ns:xmpp-streams" };
186
187 local function bosh_close_stream(session, reason)
188         (session.log or log)("info", "BOSH client disconnected");
189         
190         local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
191                 ["xmlns:stream"] = xmlns_streams });
192         
193
194         if reason then
195                 close_reply.attr.condition = "remote-stream-error";
196                 if type(reason) == "string" then -- assume stream error
197                         close_reply:tag("stream:error")
198                                 :tag(reason, {xmlns = xmlns_xmpp_streams});
199                 elseif type(reason) == "table" then
200                         if reason.condition then
201                                 close_reply:tag("stream:error")
202                                         :tag(reason.condition, stream_xmlns_attr):up();
203                                 if reason.text then
204                                         close_reply:tag("text", stream_xmlns_attr):text(reason.text):up();
205                                 end
206                                 if reason.extra then
207                                         close_reply:add_child(reason.extra);
208                                 end
209                         elseif reason.name then -- a stanza
210                                 close_reply = reason;
211                         end
212                 end
213                 log("info", "Disconnecting client, <stream:error> is: %s", tostring(close_reply));
214         end
215
216         local response_body = tostring(close_reply);
217         for _, held_request in ipairs(session.requests) do
218                 held_request.headers = default_headers;
219                 held_request:send(response_body);
220         end
221         sessions[session.sid]  = nil;
222         inactive_sessions[session] = nil;
223         sm_destroy_session(session);
224 end
225
226 -- Handle the <body> tag in the request payload.
227 function stream_callbacks.streamopened(context, attr)
228         local request, response = context.request, context.response;
229         local sid = attr.sid;
230         log("debug", "BOSH body open (sid: %s)", sid or "<none>");
231         if not sid then
232                 -- New session request
233                 context.notopen = nil; -- Signals that we accept this opening tag
234                 
235                 -- TODO: Sanity checks here (rid, to, known host, etc.)
236                 if not hosts[attr.to] then
237                         -- Unknown host
238                         log("debug", "BOSH client tried to connect to unknown host: %s", tostring(attr.to));
239                         local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
240                                 ["xmlns:stream"] = xmlns_streams, condition = "host-unknown" });
241                         response:send(tostring(close_reply));
242                         return;
243                 end
244                 
245                 -- New session
246                 sid = new_uuid();
247                 local session = {
248                         type = "c2s_unauthed", conn = {}, sid = sid, rid = tonumber(attr.rid)-1, host = attr.to,
249                         bosh_version = attr.ver, bosh_wait = math_min(attr.wait, bosh_max_wait), streamid = sid,
250                         bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY,
251                         requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream,
252                         close = bosh_close_stream, dispatch_stanza = core_process_stanza, notopen = true,
253                         log = logger.init("bosh"..sid), secure = consider_bosh_secure or request.secure,
254                         ip = get_ip_from_request(request);
255                 };
256                 sessions[sid] = session;
257                 
258                 local filter = initialize_filters(session);
259                 
260                 session.log("debug", "BOSH session created for request from %s", session.ip);
261                 log("info", "New BOSH session, assigned it sid '%s'", sid);
262
263                 -- Send creation response
264                 local creating_session = true;
265
266                 local r = session.requests;
267                 function session.send(s)
268                         -- We need to ensure that outgoing stanzas have the jabber:client xmlns
269                         if s.attr and not s.attr.xmlns then
270                                 s = st.clone(s);
271                                 s.attr.xmlns = "jabber:client";
272                         end
273                         s = filter("stanzas/out", s);
274                         --log("debug", "Sending BOSH data: %s", tostring(s));
275                         t_insert(session.send_buffer, tostring(s));
276
277                         local oldest_request = r[1];
278                         if oldest_request and not session.bosh_processing then
279                                 log("debug", "We have an open request, so sending on that");
280                                 oldest_request.headers = default_headers;
281                                 local body_attr = { xmlns = "http://jabber.org/protocol/httpbind",
282                                         ["xmlns:stream"] = "http://etherx.jabber.org/streams";
283                                         type = session.bosh_terminate and "terminate" or nil;
284                                         sid = sid;
285                                 };
286                                 if creating_session then
287                                         body_attr.inactivity = tostring(BOSH_DEFAULT_INACTIVITY);
288                                         body_attr.polling = tostring(BOSH_DEFAULT_POLLING);
289                                         body_attr.requests = tostring(BOSH_DEFAULT_REQUESTS);
290                                         body_attr.wait = tostring(session.bosh_wait);
291                                         body_attr.hold = tostring(session.bosh_hold);
292                                         body_attr.authid = sid;
293                                         body_attr.secure = "true";
294                                         body_attr.ver  = '1.6'; from = session.host;
295                                         body_attr["xmlns:xmpp"] = "urn:xmpp:xbosh";
296                                         body_attr["xmpp:version"] = "1.0";
297                                 end
298                                 oldest_request:send(st.stanza("body", body_attr):top_tag()..t_concat(session.send_buffer).."</body>");
299                                 session.send_buffer = {};
300                         end
301                         return true;
302                 end
303                 request.sid = sid;
304         end
305         
306         local session = sessions[sid];
307         if not session then
308                 -- Unknown sid
309                 log("info", "Client tried to use sid '%s' which we don't know about", sid);
310                 response.headers = default_headers;
311                 response:send(tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" })));
312                 context.notopen = nil;
313                 return;
314         end
315         
316         if session.rid then
317                 local rid = tonumber(attr.rid);
318                 local diff = rid - session.rid;
319                 if diff > 1 then
320                         session.log("warn", "rid too large (means a request was lost). Last rid: %d New rid: %s", session.rid, attr.rid);
321                 elseif diff <= 0 then
322                         -- Repeated, ignore
323                         session.log("debug", "rid repeated (on request %s), ignoring: %s (diff %d)", request.id, session.rid, diff);
324                         context.notopen = nil;
325                         context.ignore = true;
326                         context.sid = sid;
327                         t_insert(session.requests, response);
328                         return;
329                 end
330                 session.rid = rid;
331         end
332         
333         if attr.type == "terminate" then
334                 -- Client wants to end this session, which we'll do
335                 -- after processing any stanzas in this request
336                 session.bosh_terminate = true;
337         end
338
339         context.notopen = nil; -- Signals that we accept this opening tag
340         t_insert(session.requests, response);
341         context.sid = sid;
342         session.bosh_processing = true; -- Used to suppress replies until processing of this request is done
343
344         if session.notopen then
345                 local features = st.stanza("stream:features");
346                 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
347                 fire_event("stream-features", session, features);
348                 table.insert(session.send_buffer, tostring(features));
349                 session.notopen = nil;
350         end
351 end
352
353 function stream_callbacks.handlestanza(context, stanza)
354         if context.ignore then return; end
355         log("debug", "BOSH stanza received: %s\n", stanza:top_tag());
356         local session = sessions[context.sid];
357         if session then
358                 if stanza.attr.xmlns == xmlns_bosh then
359                         stanza.attr.xmlns = nil;
360                 end
361                 stanza = session.filter("stanzas/in", stanza);
362                 core_process_stanza(session, stanza);
363         end
364 end
365
366 function stream_callbacks.streamclosed(request)
367         local session = sessions[request.sid];
368         if session then
369                 session.bosh_processing = false;
370                 if #session.send_buffer > 0 then
371                         session.send("");
372                 end
373         end
374 end
375
376 function stream_callbacks.error(context, error)
377         log("debug", "Error parsing BOSH request payload; %s", error);
378         if not context.sid then
379                 local response = context.response;
380                 response.headers = default_headers;
381                 response.status_code = 400;
382                 response:send();
383                 return;
384         end
385         
386         local session = sessions[context.sid];
387         if error == "stream-error" then -- Remote stream error, we close normally
388                 session:close();
389         else
390                 session:close({ condition = "bad-format", text = "Error processing stream" });
391         end
392 end
393
394 local dead_sessions = {};
395 function on_timer()
396         -- log("debug", "Checking for requests soon to timeout...");
397         -- Identify requests timing out within the next few seconds
398         local now = os_time() + 3;
399         for request, reply_before in pairs(waiting_requests) do
400                 if reply_before <= now then
401                         log("debug", "%s was soon to timeout (at %d, now %d), sending empty response", tostring(request), reply_before, now);
402                         -- Send empty response to let the
403                         -- client know we're still here
404                         if request.conn then
405                                 sessions[request.context.sid].send("");
406                         end
407                 end
408         end
409         
410         now = now - 3;
411         local n_dead_sessions = 0;
412         for session, close_after in pairs(inactive_sessions) do
413                 if close_after < now then
414                         (session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now);
415                         sessions[session.sid]  = nil;
416                         inactive_sessions[session] = nil;
417                         n_dead_sessions = n_dead_sessions + 1;
418                         dead_sessions[n_dead_sessions] = session;
419                 end
420         end
421
422         for i=1,n_dead_sessions do
423                 local session = dead_sessions[i];
424                 dead_sessions[i] = nil;
425                 sm_destroy_session(session, "BOSH client silent for over "..session.bosh_max_inactive.." seconds");
426         end
427         return 1;
428 end
429 module:add_timer(1, on_timer);
430
431
432 local GET_response = {
433         headers = {
434                 content_type = "text/html";
435         };
436         body = [[<html><body>
437         <p>It works! Now point your BOSH client to this URL to connect to Prosody.</p>
438         <p>For more information see <a href="http://prosody.im/doc/setting_up_bosh">Prosody: Setting up BOSH</a>.</p>
439         </body></html>]];
440 };
441
442 function module.add_host(module)
443         module:depends("http");
444         module:provides("http", {
445                 default_path = "/http-bind";
446                 route = {
447                         ["GET"] = GET_response;
448                         ["GET /"] = GET_response;
449                         ["OPTIONS"] = handle_OPTIONS;
450                         ["OPTIONS /"] = handle_OPTIONS;
451                         ["POST"] = handle_POST;
452                         ["POST /"] = handle_POST;
453                 };
454         });
455 end