plugins/mod_bosh.lua

changeset 636
9c9c671ecc50
child 660
b2c4a7ec31c6
equal deleted inserted replaced
635:25f1117d7886 636:9c9c671ecc50
1
2 module.host = "*" -- Global module
3
4 local lxp = require "lxp";
5 local init_xmlhandlers = require "core.xmlhandlers"
6 local server = require "net.server";
7 local httpserver = require "net.httpserver";
8 local sm = require "core.sessionmanager";
9 local new_uuid = require "util.uuid".generate;
10 local fire_event = require "core.eventmanager".fire_event;
11 local core_process_stanza = core_process_stanza;
12 local st = require "util.stanza";
13 local log = require "util.logger".init("bosh");
14 local stream_callbacks = { stream_tag = "http://jabber.org/protocol/httpbind|body" };
15
16 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send)
17
18 local BOSH_DEFAULT_HOLD = 1;
19 local BOSH_DEFAULT_INACTIVITY = 30;
20 local BOSH_DEFAULT_POLLING = 5;
21 local BOSH_DEFAULT_REQUESTS = 2;
22 local BOSH_DEFAULT_MAXPAUSE = 120;
23
24 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
25 local os_time = os.time;
26
27 local sessions = {};
28
29 -- Used to respond to idle sessions
30 local waiting_requests = {};
31 function on_destroy_request(request)
32 waiting_requests[request] = nil;
33 end
34
35 function handle_request(method, body, request)
36 if (not body) or request.method ~= "POST" then
37 --return { status = "200 OK", headers = { ["Content-Type"] = "text/html" }, body = "<html><body>You don't look like a BOSH client to me... what do you want?</body></html>" };
38 return "<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>";
39 end
40 if not method then
41 log("debug", "Request %s suffered error %s", tostring(request.id), body);
42 return;
43 end
44 log("debug", "Handling new request %s: %s\n----------", request.id, tostring(body));
45 request.notopen = true;
46 request.log = log;
47 local parser = lxp.new(init_xmlhandlers(request, stream_callbacks), "|");
48
49 parser:parse(body);
50
51 local session = sessions[request.sid];
52 if session then
53 local r = session.requests;
54 log("debug", "Session %s has %d out of %d requests open", request.sid, #r, session.bosh_hold);
55 log("debug", "and there are %d things in the send_buffer", #session.send_buffer);
56 if #r > session.bosh_hold then
57 -- We are holding too many requests, send what's in the buffer,
58 log("debug", "We are holding too many requests, so...");
59 if #session.send_buffer > 0 then
60 log("debug", "...sending what is in the buffer")
61 session.send(t_concat(session.send_buffer));
62 session.send_buffer = {};
63 return;
64 else
65 -- or an empty response
66 log("debug", "...sending an empty response");
67 session.send("");
68 return;
69 end
70 elseif #session.send_buffer > 0 then
71 log("debug", "Session has data in the send buffer, will send now..");
72 local resp = t_concat(session.send_buffer);
73 session.send_buffer = {};
74 session.send(resp);
75 return;
76 end
77
78 if not request.destroyed and session.bosh_wait then
79 request.reply_before = os_time() + session.bosh_wait;
80 request.on_destroy = on_destroy_request;
81 waiting_requests[request] = true;
82 end
83
84 log("debug", "Had nothing to say, so leaving request unanswered for now");
85 return true;
86 end
87 end
88
89 local function bosh_reset_stream(session) session.notopen = true; end
90 local function bosh_close_stream(session, reason) end
91
92 function stream_callbacks.streamopened(request, attr)
93 print("Attr:")
94 for k,v in pairs(attr) do print("", k, v); end
95 log("debug", "BOSH body open (sid: %s)", attr.sid);
96 local sid = attr.sid
97 if not sid then
98 -- TODO: Sanity checks here (rid, to, known host, etc.)
99 request.notopen = nil; -- Signals that we accept this opening tag
100
101 -- New session
102 sid = tostring(new_uuid());
103 local session = { type = "c2s_unauthed", conn = {}, sid = sid, rid = attr.rid, host = attr.to, bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid,
104 bosh_hold = BOSH_DEFAULT_HOLD,
105 requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream, close = bosh_close_stream };
106 sessions[sid] = session;
107 log("info", "New BOSH session, assigned it sid '%s'", sid);
108 local r, send_buffer = session.requests, session.send_buffer;
109 local response = { }
110 function session.send(s)
111 log("debug", "Sending BOSH data: %s", tostring(s));
112 local oldest_request = r[1];
113 while oldest_request and oldest_request.destroyed do
114 t_remove(r, 1);
115 waiting_requests[oldest_request] = nil;
116 oldest_request = r[1];
117 end
118 if oldest_request then
119 log("debug", "We have an open request, so using that to send with");
120 response.body = t_concat{"<body xmlns='http://jabber.org/protocol/httpbind' sid='", sid, "' xmlns:stream = 'http://etherx.jabber.org/streams'>", tostring(s), "</body>" };
121 oldest_request:send(response);
122 log("debug", "Sent");
123 if oldest_request.stayopen then
124 if #r>1 then
125 -- Move front request to back
126 t_insert(r, oldest_request);
127 t_remove(r, 1);
128 end
129 else
130 log("debug", "Destroying the request now...");
131 oldest_request:destroy();
132 t_remove(r, 1);
133 end
134 elseif s ~= "" then
135 log("debug", "Saved to send buffer because there are %d open requests", #r);
136 -- Hmm, no requests are open :(
137 t_insert(session.send_buffer, tostring(s));
138 log("debug", "There are now %d things in the send_buffer", #session.send_buffer);
139 end
140 end
141
142 -- Send creation response
143
144 local features = st.stanza("stream:features");
145 fire_event("stream-features", session, features);
146 --xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh'
147 local response = st.stanza("body", { xmlns = xmlns_bosh,
148 inactivity = "30", polling = "5", requests = "2", hold = tostring(session.bosh_hold), maxpause = "120",
149 sid = sid, ver = '1.6', from = session.host, secure = 'true', ["xmpp:version"] = "1.0",
150 ["xmlns:xmpp"] = "urn:xmpp:xbosh", ["xmlns:stream"] = "http://etherx.jabber.org/streams" }):add_child(features);
151 request:send(tostring(response));
152
153 request.sid = sid;
154 return;
155 end
156
157 local session = sessions[sid];
158 if not session then
159 -- Unknown sid
160 log("info", "Client tried to use sid '%s' which we don't know about", sid);
161 request:send(tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" })));
162 request.notopen = nil;
163 return;
164 end
165
166 if session.notopen then
167 local features = st.stanza("stream:features");
168 fire_event("stream-features", session, features);
169 session.send(features);
170 session.notopen = nil;
171 end
172
173 request.notopen = nil; -- Signals that we accept this opening tag
174 t_insert(session.requests, request);
175 request.sid = sid;
176 end
177
178 function stream_callbacks.handlestanza(request, stanza)
179 log("debug", "BOSH stanza received: %s\n", stanza:pretty_print());
180 local session = sessions[request.sid];
181 if session then
182 if stanza.attr.xmlns == xmlns_bosh then
183 stanza.attr.xmlns = "jabber:client";
184 end
185 core_process_stanza(session, stanza);
186 end
187 end
188
189 function on_timer()
190 log("debug", "Checking for requests soon to timeout...");
191 -- Identify requests timing out within the next few seconds
192 local now = os_time() + 3;
193 for request in pairs(waiting_requests) do
194 if request.reply_before <= now then
195 log("debug", "%s was soon to timeout, sending empty response", request.id);
196 -- Send empty response to let the
197 -- client know we're still here
198 if request.conn then
199 sessions[request.sid].send("");
200 end
201 else
202 log("debug", "%s timing out in %ds [destroyed: %s]", request.id, request.reply_before - now, tostring(request.destroyed));
203 end
204 if not request.on_destroy then
205 log("warn", "%s has no on_destroy!", request.id);
206 end
207 end
208 end
209
210 httpserver.new{ port = 5280, base = "http-bind", handler = handle_request, ssl = false}
211 server.addtimer(on_timer);

mercurial