comparison mod_websocket/mod_websocket.lua @ 857:1393af36ec9c

mod_websocket: Determine whether to use a self-closing stream tag, based on the client's initial data
author Florian Zeitz <florob@babelmonkeys.de>
date Thu, 08 Nov 2012 01:55:00 +0100
parents 7eb54ed58856
children 1f4d77104da5
comparison
equal deleted inserted replaced
856:7eb54ed58856 857:1393af36ec9c
29 local log = module._log; 29 local log = module._log;
30 30
31 local c2s_timeout = module:get_option_number("c2s_timeout"); 31 local c2s_timeout = module:get_option_number("c2s_timeout");
32 local stream_close_timeout = module:get_option_number("c2s_close_timeout", 5); 32 local stream_close_timeout = module:get_option_number("c2s_close_timeout", 5);
33 local opt_keepalives = module:get_option_boolean("tcp_keepalives", false); 33 local opt_keepalives = module:get_option_boolean("tcp_keepalives", false);
34 local self_closing_stream = module:get_option_boolean("websocket_self_closing_stream", true);
35 34
36 local cross_domain = module:get_option("cross_domain_websocket"); 35 local cross_domain = module:get_option("cross_domain_websocket");
37 if cross_domain then 36 if cross_domain then
38 if cross_domain == true then 37 if cross_domain == true then
39 cross_domain = "*"; 38 cross_domain = "*";
152 -- We don't serve this host... 151 -- We don't serve this host...
153 session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)}; 152 session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)};
154 return; 153 return;
155 end 154 end
156 155
157 -- COMPAT: Current client implementations need this to be self-closing 156 -- COMPAT: Some current client implementations need this to be self-closing
158 if self_closing_stream then 157 if session.self_closing_stream then
159 send("<?xml version='1.0'?>"..tostring(st.stanza("stream:stream", { 158 send("<?xml version='1.0'?>"..tostring(st.stanza("stream:stream", {
160 xmlns = 'jabber:client', ["xmlns:stream"] = 'http://etherx.jabber.org/streams'; 159 xmlns = 'jabber:client', ["xmlns:stream"] = 'http://etherx.jabber.org/streams';
161 id = session.streamid, from = session.host, version = '1.0', ["xml:lang"] = 'en' }))); 160 id = session.streamid, from = session.host, version = '1.0', ["xml:lang"] = 'en' })));
162 else 161 else
163 send("<?xml version='1.0'?>"..st.stanza("stream:stream", { 162 send("<?xml version='1.0'?>"..st.stanza("stream:stream", {
224 --- Session methods 223 --- Session methods
225 local function session_close(session, reason) 224 local function session_close(session, reason)
226 local log = session.log or log; 225 local log = session.log or log;
227 if session.conn then 226 if session.conn then
228 if session.notopen then 227 if session.notopen then
229 -- COMPAT: Current client implementations need this to be self-closing 228 -- COMPAT: Some current client implementations need this to be self-closing
230 if self_closing_stream then 229 if session.self_closing_stream then
231 session.send("<?xml version='1.0'?>"..tostring(st.stanza("stream:stream", default_stream_attr))); 230 session.send("<?xml version='1.0'?>"..tostring(st.stanza("stream:stream", default_stream_attr)));
232 else 231 else
233 session.send("<?xml version='1.0'?>"..st.stanza("stream:stream", default_stream_attr):top_tag()); 232 session.send("<?xml version='1.0'?>"..st.stanza("stream:stream", default_stream_attr):top_tag());
234 end 233 end
235 end 234 end
381 380
382 if frame.FIN then 381 if frame.FIN then
383 data = dataBuffer; 382 data = dataBuffer;
384 dataBuffer = nil; 383 dataBuffer = nil;
385 384
386 -- COMPAT: Current client implementations send a self-closing <stream:stream> 385 -- COMPAT: Some current client implementations send a self-closing <stream:stream>
387 if self_closing_stream then 386 data, session.self_closing_stream = data:gsub("^(<stream:stream.*)/>$", "%1>");
388 data = data:gsub("(<stream:stream.*)/>$", "%1>"); 387 session.self_closing_stream = (session.self_closing_stream == 1)
389 end
390 388
391 data = filter("bytes/in", data); 389 data = filter("bytes/in", data);
392 if data then 390 if data then
393 local ok, err = stream:feed(data); 391 local ok, err = stream:feed(data);
394 if ok then return; end 392 if ok then return; end