2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 module.host = "*" -- Global module
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";
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)
29 local stream_callbacks = {
30 stream_ns = xmlns_bosh, stream_tag = "body", default_ns = "jabber:client" };
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);
37 local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure");
38 local auto_cork = module:get_option_boolean("bosh_auto_cork", false);
40 local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8" };
42 local cross_domain = module:get_option("cross_domain_bosh", false);
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";
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, ", ");
53 if type(cross_domain) == "string" then
54 default_headers["Access-Control-Allow-Origin"] = cross_domain;
58 local trusted_proxies = module:get_option_set("trusted_proxies", {"127.0.0.1"})._items;
60 local function get_ip_from_request(request)
61 local ip = request.conn:ip();
62 local forwarded_for = request.headers["x-forwarded-for"];
64 forwarded_for = forwarded_for..", "..ip;
65 for forwarded_ip in forwarded_for:gmatch("[^%s,]+") do
66 if not trusted_proxies[forwarded_ip] then
74 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
75 local os_time = os.time;
78 local inactive_sessions = {}; -- Sessions which have no open requests
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];
86 local requests = session.requests;
87 for i,r in ipairs(requests) do
89 t_remove(requests, i);
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);
103 function handle_request(method, body, request)
104 if (not body) or request.method ~= "POST" then
105 if request.method == "OPTIONS" then
107 for k,v in pairs(default_headers) do headers[k] = v; end
108 headers["Content-Type"] = nil;
109 return { headers = headers, body = "" };
111 return "<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>";
115 log("debug", "Request %s suffered error %s", tostring(request.id), body);
118 --log("debug", "Handling new request %s: %s\n----------", request.id, tostring(body));
119 request.notopen = true;
121 request.on_destroy = on_destroy_request;
123 local stream = new_xmpp_stream(request, stream_callbacks);
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.
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];
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;
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 = {};
154 -- or an empty response
155 log("debug", "...sending an empty response");
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 = {};
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;
174 if session.bosh_terminate then
175 session.log("debug", "Closing session with %d requests open", #session.requests);
179 return true; -- Inform httpserver we shall reply later
185 local function bosh_reset_stream(session) session.notopen = true; end
187 local stream_xmlns_attr = { xmlns = "urn:ietf:params:xml:ns:xmpp-streams" };
189 local function bosh_close_stream(session, reason)
190 (session.log or log)("info", "BOSH client disconnected");
192 local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
193 ["xmlns:stream"] = xmlns_streams });
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();
206 close_reply:tag("text", stream_xmlns_attr):text(reason.text):up();
209 close_reply:add_child(reason.extra);
211 elseif reason.name then -- a stanza
212 close_reply = reason;
215 log("info", "Disconnecting client, <stream:error> is: %s", tostring(close_reply));
218 local session_close_response = { headers = default_headers, body = tostring(close_reply) };
220 for _, held_request in ipairs(session.requests) do
221 held_request:send(session_close_response);
222 held_request:destroy();
224 sessions[session.sid] = nil;
225 inactive_sessions[session] = nil;
226 sm_destroy_session(session);
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>");
234 -- New session request
235 request.notopen = nil; -- Signals that we accept this opening tag
237 -- TODO: Sanity checks here (rid, to, known host, etc.)
238 if not hosts[attr.to] then
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));
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);
258 sessions[sid] = session;
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
268 s.attr.xmlns = "jabber:client";
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'>",
281 oldest_request:send(response);
282 --log("debug", "Sent");
283 if oldest_request.stayopen then
285 -- Move front request to back
286 t_insert(r, oldest_request);
290 log("debug", "Destroying the request now...");
291 oldest_request:destroy();
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);
302 -- Send creation response
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,
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) };
326 local session = sessions[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;
336 local rid = tonumber(attr.rid);
337 local diff = rid - session.rid;
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
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;
346 t_insert(session.requests, request);
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;
358 request.notopen = nil; -- Signals that we accept this opening tag
359 t_insert(session.requests, request);
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;
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];
376 if stanza.attr.xmlns == xmlns_bosh then
377 stanza.attr.xmlns = nil;
379 core_process_stanza(session, stanza);
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" });
390 local session = sessions[request.sid];
391 if error == "stream-error" then -- Remote stream error, we close normally
394 session:close({ condition = "bad-format", text = "Error processing stream" });
398 local dead_sessions = {};
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
409 sessions[request.sid].send("");
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;
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");
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);
440 if prosody.start_time then -- already started
443 prosody.events.add_handler("server-started", setup);