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