mod_bosh: Fixed use of a private HTTP request property.
[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 new_xmpp_stream = require "util.xmppstream".new;
14 local httpserver = require "net.httpserver";
15 local sm = require "core.sessionmanager";
16 local sm_destroy_session = sm.destroy_session;
17 local new_uuid = require "util.uuid".generate;
18 local fire_event = prosody.events.fire_event;
19 local core_process_stanza = core_process_stanza;
20 local st = require "util.stanza";
21 local logger = require "util.logger";
22 local log = logger.init("mod_bosh");
23 local timer = require "util.timer";
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 = module:get_option_number("bosh_default_hold", 1);
33 local BOSH_DEFAULT_INACTIVITY = module:get_option_number("bosh_max_inactivity", 60);
34 local BOSH_DEFAULT_POLLING = module:get_option_number("bosh_max_polling", 5);
35 local BOSH_DEFAULT_REQUESTS = module:get_option_number("bosh_max_requests", 2);
36
37 local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure");
38 local auto_cork = module:get_option_boolean("bosh_auto_cork", false);
39
40 local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8" };
41
42 local cross_domain = module:get_option("cross_domain_bosh", false);
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 trusted_proxies = module:get_option_set("trusted_proxies", {"127.0.0.1"})._items;
59
60 local function get_ip_from_request(request)
61         local ip = request.conn:ip();
62         local forwarded_for = request.headers["x-forwarded-for"];
63         if forwarded_for then
64                 forwarded_for = forwarded_for..", "..ip;
65                 for forwarded_ip in forwarded_for:gmatch("[^%s,]+") do
66                         if not trusted_proxies[forwarded_ip] then
67                                 ip = forwarded_ip;
68                         end
69                 end
70         end
71         return ip;
72 end
73
74 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
75 local os_time = os.time;
76
77 local sessions = {};
78 local inactive_sessions = {}; -- Sessions which have no open requests
79
80 -- Used to respond to idle sessions (those with waiting requests)
81 local waiting_requests = {};
82 function on_destroy_request(request)
83         waiting_requests[request] = nil;
84         local session = sessions[request.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_request(method, body, request)
104         if (not body) or request.method ~= "POST" then
105                 if request.method == "OPTIONS" then
106                         local headers = {};
107                         for k,v in pairs(default_headers) do headers[k] = v; end
108                         headers["Content-Type"] = nil;
109                         return { headers = headers, body = "" };
110                 else
111                         return "<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>";
112                 end
113         end
114         if not method then
115                 log("debug", "Request %s suffered error %s", tostring(request.id), body);
116                 return;
117         end
118         --log("debug", "Handling new request %s: %s\n----------", request.id, tostring(body));
119         request.notopen = true;
120         request.log = log;
121         request.on_destroy = on_destroy_request;
122         
123         local stream = new_xmpp_stream(request, stream_callbacks);
124         
125         -- stream:feed() calls the stream_callbacks, so all stanzas in
126         -- the body are processed in this next line before it returns.
127         -- In particular, the streamopened() stream callback is where
128         -- much of the session logic happens, because it's where we first
129         -- get to see the 'sid' of this request.
130         stream:feed(body);
131         
132         -- Stanzas (if any) in the request have now been processed, and
133         -- we take care of the high-level BOSH logic here, including
134         -- giving a response or putting the request "on hold".
135         local session = sessions[request.sid];
136         if session then
137                 -- Session was marked as inactive, since we have
138                 -- a request open now, unmark it
139                 if inactive_sessions[session] and #session.requests > 0 then
140                         inactive_sessions[session] = nil;
141                 end
142
143                 local r = session.requests;
144                 log("debug", "Session %s has %d out of %d requests open", request.sid, #r, session.bosh_hold);
145                 log("debug", "and there are %d things in the send_buffer", #session.send_buffer);
146                 if #r > session.bosh_hold then
147                         -- We are holding too many requests, send what's in the buffer,
148                         log("debug", "We are holding too many requests, so...");
149                         if #session.send_buffer > 0 then
150                                 log("debug", "...sending what is in the buffer")
151                                 session.send(t_concat(session.send_buffer));
152                                 session.send_buffer = {};
153                         else
154                                 -- or an empty response
155                                 log("debug", "...sending an empty response");
156                                 session.send("");
157                         end
158                 elseif #session.send_buffer > 0 then
159                         log("debug", "Session has data in the send buffer, will send now..");
160                         local resp = t_concat(session.send_buffer);
161                         session.send_buffer = {};
162                         session.send(resp);
163                 end
164                 
165                 if not request.destroyed then
166                         -- We're keeping this request open, to respond later
167                         log("debug", "Have nothing to say, so leaving request unanswered for now");
168                         if session.bosh_wait then
169                                 request.reply_before = os_time() + session.bosh_wait;
170                                 waiting_requests[request] = true;
171                         end
172                 end
173                 
174                 if session.bosh_terminate then
175                         session.log("debug", "Closing session with %d requests open", #session.requests);
176                         session:close();
177                         return nil;
178                 else
179                         return true; -- Inform httpserver we shall reply later
180                 end
181         end
182 end
183
184
185 local function bosh_reset_stream(session) session.notopen = true; end
186
187 local stream_xmlns_attr = { xmlns = "urn:ietf:params:xml:ns:xmpp-streams" };
188
189 local function bosh_close_stream(session, reason)
190         (session.log or log)("info", "BOSH client disconnected");
191         
192         local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
193                 ["xmlns:stream"] = xmlns_streams });
194         
195
196         if reason then
197                 close_reply.attr.condition = "remote-stream-error";
198                 if type(reason) == "string" then -- assume stream error
199                         close_reply:tag("stream:error")
200                                 :tag(reason, {xmlns = xmlns_xmpp_streams});
201                 elseif type(reason) == "table" then
202                         if reason.condition then
203                                 close_reply:tag("stream:error")
204                                         :tag(reason.condition, stream_xmlns_attr):up();
205                                 if reason.text then
206                                         close_reply:tag("text", stream_xmlns_attr):text(reason.text):up();
207                                 end
208                                 if reason.extra then
209                                         close_reply:add_child(reason.extra);
210                                 end
211                         elseif reason.name then -- a stanza
212                                 close_reply = reason;
213                         end
214                 end
215                 log("info", "Disconnecting client, <stream:error> is: %s", tostring(close_reply));
216         end
217
218         local session_close_response = { headers = default_headers, body = tostring(close_reply) };
219
220         for _, held_request in ipairs(session.requests) do
221                 held_request:send(session_close_response);
222                 held_request:destroy();
223         end
224         sessions[session.sid]  = nil;
225         inactive_sessions[session] = nil;
226         sm_destroy_session(session);
227 end
228
229 -- Handle the <body> tag in the request payload.
230 function stream_callbacks.streamopened(request, attr)
231         local sid = attr.sid;
232         log("debug", "BOSH body open (sid: %s)", sid or "<none>");
233         if not sid then
234                 -- New session request
235                 request.notopen = nil; -- Signals that we accept this opening tag
236                 
237                 -- TODO: Sanity checks here (rid, to, known host, etc.)
238                 if not hosts[attr.to] then
239                         -- Unknown host
240                         log("debug", "BOSH client tried to connect to unknown host: %s", tostring(attr.to));
241                         local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
242                                 ["xmlns:stream"] = xmlns_streams, condition = "host-unknown" });
243                         request:send(tostring(close_reply));
244                         return;
245                 end
246                 
247                 -- New session
248                 sid = new_uuid();
249                 local session = {
250                         type = "c2s_unauthed", conn = {}, sid = sid, rid = tonumber(attr.rid), host = attr.to,
251                         bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid,
252                         bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY,
253                         requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream,
254                         close = bosh_close_stream, dispatch_stanza = core_process_stanza,
255                         log = logger.init("bosh"..sid), secure = consider_bosh_secure or request.secure,
256                         ip = get_ip_from_request(request);
257                 };
258                 sessions[sid] = 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                 local r, send_buffer = session.requests, session.send_buffer;
263                 local response = { headers = default_headers }
264                 function session.send(s)
265                         -- We need to ensure that outgoing stanzas have the jabber:client xmlns
266                         if s.attr and not s.attr.xmlns then
267                                 s = st.clone(s);
268                                 s.attr.xmlns = "jabber:client";
269                         end
270                         --log("debug", "Sending BOSH data: %s", tostring(s));
271                         local oldest_request = r[1];
272                         if oldest_request and (not(auto_cork) or waiting_requests[oldest_request]) then
273                                 log("debug", "We have an open request, so sending on that");
274                                 response.body = t_concat({
275                                         "<body xmlns='http://jabber.org/protocol/httpbind' ",
276                                         session.bosh_terminate and "type='terminate' " or "",
277                                         "sid='", sid, "' xmlns:stream = 'http://etherx.jabber.org/streams'>",
278                                         tostring(s),
279                                         "</body>"
280                                 });
281                                 oldest_request:send(response);
282                                 --log("debug", "Sent");
283                                 if oldest_request.stayopen then
284                                         if #r>1 then
285                                                 -- Move front request to back
286                                                 t_insert(r, oldest_request);
287                                                 t_remove(r, 1);
288                                         end
289                                 else
290                                         log("debug", "Destroying the request now...");
291                                         oldest_request:destroy();
292                                 end
293                         elseif s ~= "" then
294                                 log("debug", "Saved to send buffer because there are %d open requests", #r);
295                                 -- Hmm, no requests are open :(
296                                 t_insert(session.send_buffer, tostring(s));
297                                 log("debug", "There are now %d things in the send_buffer", #session.send_buffer);
298                         end
299                         return true;
300                 end
301                 
302                 -- Send creation response
303                 
304                 local features = st.stanza("stream:features");
305                 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
306                 fire_event("stream-features", session, features);
307                 --xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh'
308                 local response = st.stanza("body", { xmlns = xmlns_bosh,
309                         wait = attr.wait,
310                         inactivity = tostring(BOSH_DEFAULT_INACTIVITY),
311                         polling = tostring(BOSH_DEFAULT_POLLING),
312                         requests = tostring(BOSH_DEFAULT_REQUESTS),
313                         hold = tostring(session.bosh_hold),
314                         sid = sid, authid = sid,
315                         ver  = '1.6', from = session.host,
316                         secure = 'true', ["xmpp:version"] = "1.0",
317                         ["xmlns:xmpp"] = "urn:xmpp:xbosh",
318                         ["xmlns:stream"] = "http://etherx.jabber.org/streams"
319                 }):add_child(features);
320                 request:send{ headers = default_headers, body = tostring(response) };
321                 
322                 request.sid = sid;
323                 return;
324         end
325         
326         local session = sessions[sid];
327         if not session then
328                 -- Unknown sid
329                 log("info", "Client tried to use sid '%s' which we don't know about", sid);
330                 request:send{ headers = default_headers, body = tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" })) };
331                 request.notopen = nil;
332                 return;
333         end
334         
335         if session.rid then
336                 local rid = tonumber(attr.rid);
337                 local diff = rid - session.rid;
338                 if diff > 1 then
339                         session.log("warn", "rid too large (means a request was lost). Last rid: %d New rid: %s", session.rid, attr.rid);
340                 elseif diff <= 0 then
341                         -- Repeated, ignore
342                         session.log("debug", "rid repeated (on request %s), ignoring: %s (diff %d)", request.id, session.rid, diff);
343                         request.notopen = nil;
344                         request.ignore = true;
345                         request.sid = sid;
346                         t_insert(session.requests, request);
347                         return;
348                 end
349                 session.rid = rid;
350         end
351         
352         if attr.type == "terminate" then
353                 -- Client wants to end this session, which we'll do
354                 -- after processing any stanzas in this request
355                 session.bosh_terminate = true;
356         end
357
358         request.notopen = nil; -- Signals that we accept this opening tag
359         t_insert(session.requests, request);
360         request.sid = sid;
361
362         if session.notopen then
363                 local features = st.stanza("stream:features");
364                 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
365                 fire_event("stream-features", session, features);
366                 session.send(features);
367                 session.notopen = nil;
368         end
369 end
370
371 function stream_callbacks.handlestanza(request, stanza)
372         if request.ignore then return; end
373         log("debug", "BOSH stanza received: %s\n", stanza:top_tag());
374         local session = sessions[request.sid];
375         if session then
376                 if stanza.attr.xmlns == xmlns_bosh then
377                         stanza.attr.xmlns = nil;
378                 end
379                 core_process_stanza(session, stanza);
380         end
381 end
382
383 function stream_callbacks.error(request, error)
384         log("debug", "Error parsing BOSH request payload; %s", error);
385         if not request.sid then
386                 request:send({ headers = default_headers, status = "400 Bad Request" });
387                 return;
388         end
389         
390         local session = sessions[request.sid];
391         if error == "stream-error" then -- Remote stream error, we close normally
392                 session:close();
393         else
394                 session:close({ condition = "bad-format", text = "Error processing stream" });
395         end
396 end
397
398 local dead_sessions = {};
399 function on_timer()
400         -- log("debug", "Checking for requests soon to timeout...");
401         -- Identify requests timing out within the next few seconds
402         local now = os_time() + 3;
403         for request in pairs(waiting_requests) do
404                 if request.reply_before <= now then
405                         log("debug", "%s was soon to timeout, sending empty response", request.id);
406                         -- Send empty response to let the
407                         -- client know we're still here
408                         if request.conn then
409                                 sessions[request.sid].send("");
410                         end
411                 end
412         end
413         
414         now = now - 3;
415         local n_dead_sessions = 0;
416         for session, close_after in pairs(inactive_sessions) do
417                 if close_after < now then
418                         (session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now);
419                         sessions[session.sid]  = nil;
420                         inactive_sessions[session] = nil;
421                         n_dead_sessions = n_dead_sessions + 1;
422                         dead_sessions[n_dead_sessions] = session;
423                 end
424         end
425
426         for i=1,n_dead_sessions do
427                 local session = dead_sessions[i];
428                 dead_sessions[i] = nil;
429                 sm_destroy_session(session, "BOSH client silent for over "..session.bosh_max_inactive.." seconds");
430         end
431         return 1;
432 end
433
434
435 local function setup()
436         local ports = module:get_option_array("bosh_ports") or { 5280 };
437         httpserver.new_from_config(ports, handle_request, { base = "http-bind" });
438         timer.add_task(1, on_timer);
439 end
440 if prosody.start_time then -- already started
441         setup();
442 else
443         prosody.events.add_handler("server-started", setup);
444 end