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