annotate mod_websocket/mod_websocket.lua @ 676:54fa9d6d7809

mod_websocket: New mod_c2s based version, still WIP
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 25 May 2012 17:20:41 +0200
parents 5fc00a3e47b5
children eeb41cd5e9f3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
1 -- Prosody IM
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
4 -- Copyright (C) 2012 Florian Zeitz
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
5 --
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
7 -- COPYING file in the source package for more information.
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
8 --
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
9
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
10 module:set_global();
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
11
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
12 local add_task = require "util.timer".add_task;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
13 local new_xmpp_stream = require "util.xmppstream".new;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
14 local nameprep = require "util.encodings".stringprep.nameprep;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
15 local sessionmanager = require "core.sessionmanager";
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
16 local st = require "util.stanza";
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
17 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
18 local uuid_generate = require "util.uuid".generate;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
19 local sha1 = require "util.hashes".sha1;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
20 local base64 = require "util.encodings".base64.encode;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
21 local bxor = require "bit".bxor;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
22 local tohex = require "bit".tohex;
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
23
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
24 module:depends("http")
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
25
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
26
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
27 local xpcall, tostring, type = xpcall, tostring, type;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
28 local traceback = debug.traceback;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
29
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
30 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
31
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
32 local log = module._log;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
33
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
34 local c2s_timeout = module:get_option_number("c2s_timeout");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
35 local opt_keepalives = module:get_option_boolean("tcp_keepalives", false);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
36
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
37 local sessions = module:shared("sessions");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
38
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
39 local stream_callbacks = { default_ns = "jabber:client", handlestanza = core_process_stanza };
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
40 local listener = {};
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
41
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
42 --- Stream events handlers
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
43 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
44 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
45
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
46 function stream_callbacks.streamopened(session, attr)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
47 local send = session.send;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
48 session.host = nameprep(attr.to);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
49 if not session.host then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
50 session:close{ condition = "improper-addressing",
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
51 text = "A valid 'to' attribute is required on stream headers" };
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
52 return;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
53 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
54 session.version = tonumber(attr.version) or 0;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
55 session.streamid = uuid_generate();
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
56 (session.log or session)("debug", "Client sent opening <stream:stream> to %s", session.host);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
57
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
58 if not hosts[session.host] then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
59 -- We don't serve this host...
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
60 session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)};
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
61 return;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
62 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
63
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
64 send("<?xml version='1.0'?>"..(tostring(st.stanza("stream:stream", {
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
65 xmlns = 'jabber:client', ["xmlns:stream"] = 'http://etherx.jabber.org/streams';
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
66 id = session.streamid, from = session.host, version = '1.0', ["xml:lang"] = 'en' }):top_tag()):gsub(">", "/>")));
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
67
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
68 (session.log or log)("debug", "Sent reply <stream:stream> to client");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
69 session.notopen = nil;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
70
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
71 -- If session.secure is *false* (not nil) then it means we /were/ encrypting
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
72 -- since we now have a new stream header, session is secured
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
73 if session.secure == false then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
74 session.secure = true;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
75 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
76
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
77 local features = st.stanza("stream:features");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
78 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
79 module:fire_event("stream-features", session, features);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
80
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
81 send(features);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
82 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
83
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
84 function stream_callbacks.streamclosed(session)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
85 session.log("debug", "Received </stream:stream>");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
86 session:close();
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
87 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
88
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
89 function stream_callbacks.error(session, error, data)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
90 if error == "no-stream" then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
91 session.log("debug", "Invalid opening stream header");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
92 session:close("invalid-namespace");
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
93 elseif error == "parse-error" then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
94 (session.log or log)("debug", "Client XML parse error: %s", tostring(data));
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
95 session:close("not-well-formed");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
96 elseif error == "stream-error" then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
97 local condition, text = "undefined-condition";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
98 for child in data:children() do
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
99 if child.attr.xmlns == xmlns_xmpp_streams then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
100 if child.name ~= "text" then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
101 condition = child.name;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
102 else
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
103 text = child:get_text();
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
104 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
105 if condition ~= "undefined-condition" and text then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
106 break;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
107 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
108 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
109 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
110 text = condition .. (text and (" ("..text..")") or "");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
111 session.log("info", "Session closed by remote with error: %s", text);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
112 session:close(nil, text);
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
113 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
114 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
115
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
116 local function handleerr(err) log("error", "Traceback[c2s]: %s: %s", tostring(err), traceback()); end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
117 function stream_callbacks.handlestanza(session, stanza)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
118 stanza = session.filter("stanzas/in", stanza);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
119 if stanza then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
120 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
121 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
122 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
123
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
124 --- Session methods
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
125 local function session_close(session, reason)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
126 local log = session.log or log;
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
127 if session.conn then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
128 if session.notopen then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
129 session.send("<?xml version='1.0'?>");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
130 session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
131 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
132 if reason then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
133 if type(reason) == "string" then -- assume stream error
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
134 log("info", "Disconnecting client, <stream:error> is: %s", reason);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
135 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
136 elseif type(reason) == "table" then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
137 if reason.condition then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
138 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
139 if reason.text then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
140 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
141 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
142 if reason.extra then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
143 stanza:add_child(reason.extra);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
144 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
145 log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
146 session.send(stanza);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
147 elseif reason.name then -- a stanza
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
148 log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
149 session.send(reason);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
150 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
151 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
152 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
153 session.send("</stream:stream>");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
154 session.conn:close();
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
155 listener.ondisconnect(session.conn, (reason and (reason.text or reason.condition)) or reason or "session closed");
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
156 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
157 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
158
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
159 --- Port listener
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
160 function listener.onconnect(conn)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
161 local session = sm_new_session(conn);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
162 sessions[conn] = session;
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
163
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
164 session.log("info", "Client connected");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
165
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
166 -- Client is using legacy SSL (otherwise mod_tls sets this flag)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
167 if conn:ssl() then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
168 session.secure = true;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
169 end
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
170
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
171 if opt_keepalives then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
172 conn:setoption("keepalive", opt_keepalives);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
173 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
174
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
175 session.close = session_close;
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
176
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
177 local stream = new_xmpp_stream(session, stream_callbacks);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
178 session.stream = stream;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
179 session.notopen = true;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
180
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
181 function session.reset_stream()
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
182 session.notopen = true;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
183 session.stream:reset();
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
184 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
185
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
186 local filter = session.filter;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
187 function session.data(data)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
188 local off = 0;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
189 local len = string.byte(data, 2) - 0x80;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
190 if len == 126 then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
191 off = 2;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
192 elseif len ==127 then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
193 off = 8;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
194 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
195 local key = {string.byte(data, off+3), string.byte(data, off+4), string.byte(data, off+5), string.byte(data, off+6)}
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
196 local decoded = "";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
197 local counter = 0;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
198 for i = off+7, #data do
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
199 decoded = decoded .. string.char(bxor(key[counter+1], string.byte(data, i)));
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
200 counter = (counter + 1) % 4;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
201 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
202 module:log("debug", "Websocket received: %s %i", decoded, #decoded)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
203 decoded = decoded:gsub("/>$", ">");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
204
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
205 data = filter("bytes/in", decoded);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
206 if data then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
207 local ok, err = stream:feed(data);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
208 if ok then return; end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
209 log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
210 session:close("not-well-formed");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
211 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
212 end
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
213
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
214 function session.send(s)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
215 s = tostring(s);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
216 local len = #s;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
217 if len < 126 then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
218 conn:write("\x81" .. string.char(len) .. s);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
219 elseif len <= 0xffff then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
220 conn:write("\x81" .. string.char(126) .. string.char(len/0x100) .. string.char(len%0x100) .. s);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
221 else
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
222 conn:write("\x81" .. string.char(127) .. string.char(len/0x100000000000000)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
223 .. string.char((len%0x100000000000000)/0x1000000000000) .. string.char((len%0x1000000000000)/0x10000000000)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
224 .. string.char((len%0x10000000000)/0x100000000) .. string.char((len%0x100000000)/0x1000000)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
225 .. string.char((len%0x1000000)/0x10000) .. string.char((len%0x10000)/0x100)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
226 .. string.char((len%0x100)))
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
227 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
228 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
229
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
230 if c2s_timeout then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
231 add_task(c2s_timeout, function ()
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
232 if session.type == "c2s_unauthed" then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
233 session:close("connection-timeout");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
234 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
235 end);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
236 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
237
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
238 session.dispatch_stanza = stream_callbacks.handlestanza;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
239 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
240
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
241 function listener.onincoming(conn, data)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
242 local session = sessions[conn];
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
243 if session then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
244 session.data(data);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
245 else
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
246 listener.onconnect(conn, data);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
247 session = sessions[conn];
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
248 session.data(data);
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
249 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
250 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
251
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
252 function listener.ondisconnect(conn, err)
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
253 local session = sessions[conn];
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
254 if session then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
255 (session.log or log)("info", "Client disconnected: %s", err);
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
256 sm_destroy_session(session, err);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
257 sessions[conn] = nil;
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
258 session = nil;
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
259 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
260 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
261
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
262 function listener.associate_session(conn, session)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
263 sessions[conn] = session;
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
264 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
265
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
266 function handle_request(event, path)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
267 local request, response = event.request, event.response;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
268
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
269 -- Add sanity checks
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
270
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
271 response.conn:setlistener(listener);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
272 response.status = "101 Switching Protocols";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
273 response.headers.Upgrade = "websocket";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
274 response.headers.Connection = "Upgrade";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
275 response.headers.Sec_WebSocket_Accept = base64(sha1(request.headers.sec_websocket_key .. "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
276 response.headers.Sec_WebSocket_Protocol = "xmpp";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
277
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
278 return "";
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
279 end
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
280
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
281 function module.load()
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
282 module:provides("http", {
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
283 name = "xmpp-websocket";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
284 route = {
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
285 ["GET /*"] = handle_request;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
286 };
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
287 });
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
288 end