Annotate

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
parent 856:7eb54ed58856
child 895:1f4d77104da5
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;
677
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
21 local band = require "bit".band;
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
22 local bxor = require "bit".bxor;
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 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
25 local traceback = debug.traceback;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
26
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
27 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
28
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
29 local log = module._log;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
30
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
31 local c2s_timeout = module:get_option_number("c2s_timeout");
839
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
32 local stream_close_timeout = module:get_option_number("c2s_close_timeout", 5);
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
33 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
34
856
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
35 local cross_domain = module:get_option("cross_domain_websocket");
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
36 if cross_domain then
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
37 if cross_domain == true then
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
38 cross_domain = "*";
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
39 elseif type(cross_domain) == "table" then
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
40 cross_domain = table.concat(cross_domain, ", ");
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
41 end
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
42 if type(cross_domain) ~= "string" then
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
43 cross_domain = nil;
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
44 end
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
45 end
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
46
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
47 local sessions = module:shared("sessions");
839
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
48 local core_process_stanza = prosody.core_process_stanza;
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
49
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
50 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
51 local listener = {};
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
52
677
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
53 -- Websocket helpers
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
54 local function parse_frame(frame)
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
55 local result = {};
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
56 local pos = 1;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
57 local length_bytes = 0;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
58 local counter = 0;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
59 local tmp_byte;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
60
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
61 if #frame < 2 then return; end
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
62
677
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
63 tmp_byte = string.byte(frame, pos);
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
64 result.FIN = band(tmp_byte, 0x80) > 0;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
65 result.RSV1 = band(tmp_byte, 0x40) > 0;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
66 result.RSV2 = band(tmp_byte, 0x20) > 0;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
67 result.RSV3 = band(tmp_byte, 0x10) > 0;
690
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
68 result.opcode = band(tmp_byte, 0x0F);
677
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
69
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
70 pos = pos + 1;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
71 tmp_byte = string.byte(frame, pos);
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
72 result.MASK = band(tmp_byte, 0x80) > 0;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
73 result.length = band(tmp_byte, 0x7F);
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
74
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
75 if result.length == 126 then
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
76 length_bytes = 2;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
77 result.length = 0;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
78 elseif result.length == 127 then
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
79 length_bytes = 8;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
80 result.length = 0;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
81 end
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
82
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
83 if #frame < (2 + length_bytes) then return; end
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
84
677
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
85 for i = 1, length_bytes do
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
86 pos = pos + 1;
840
f568661c9d39 mod_websocket: Fix frame length calculation
Florian Zeitz <florob@babelmonkeys.de>
parents: 839
diff changeset
87 result.length = result.length * 256 + string.byte(frame, pos);
677
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
88 end
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
89
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
90 if #frame < (2 + length_bytes + (result.MASK and 4 or 0) + result.length) then return; end
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
91
677
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
92 if result.MASK then
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
93 result.key = {string.byte(frame, pos+1), string.byte(frame, pos+2),
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
94 string.byte(frame, pos+3), string.byte(frame, pos+4)}
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
95
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
96 pos = pos + 5;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
97 result.data = "";
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
98 for i = pos, pos + result.length - 1 do
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
99 result.data = result.data .. string.char(bxor(result.key[counter+1], string.byte(frame, i)));
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
100 counter = (counter + 1) % 4;
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
101 end
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
102 else
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
103 result.data = frame:sub(pos + 1, pos + result.length);
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
104 end
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
105
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
106 return result, 2 + length_bytes + (result.MASK and 4 or 0) + result.length;
677
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
107 end
eeb41cd5e9f3 mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents: 676
diff changeset
108
687
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
109 local function build_frame(desc)
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
110 local length;
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
111 local result = "";
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
112 local data = desc.data or "";
687
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
113
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
114 result = result .. string.char(0x80 * (desc.FIN and 1 or 0) + desc.opcode);
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
115
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
116 length = #data;
687
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
117 if length <= 125 then -- 7-bit length
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
118 result = result .. string.char(length);
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
119 elseif length <= 0xFFFF then -- 2-byte length
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
120 result = result .. string.char(126);
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
121 result = result .. string.char(length/0x100) .. string.char(length%0x100);
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
122 else -- 8-byte length
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
123 result = result .. string.char(127);
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
124 for i = 7, 0, -1 do
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
125 result = result .. string.char(( length / (2^(8*i)) ) % 0x100);
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
126 end
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
127 end
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
128
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
129 result = result .. data;
687
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
130
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
131 return result;
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
132 end
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
133
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
134 --- Stream events handlers
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
135 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
136 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
137
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
138 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
139 local send = session.send;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
140 session.host = nameprep(attr.to);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
141 if not session.host then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
142 session:close{ condition = "improper-addressing",
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
143 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
144 return;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
145 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
146 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
147 session.streamid = uuid_generate();
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
148 (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
149
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
150 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
151 -- 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
152 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
153 return;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
154 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
155
857
1393af36ec9c mod_websocket: Determine whether to use a self-closing stream tag, based on the client's initial data
Florian Zeitz <florob@babelmonkeys.de>
parents: 856
diff changeset
156 -- COMPAT: Some current client implementations need this to be self-closing
1393af36ec9c mod_websocket: Determine whether to use a self-closing stream tag, based on the client's initial data
Florian Zeitz <florob@babelmonkeys.de>
parents: 856
diff changeset
157 if session.self_closing_stream then
692
2de21fa40382 mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents: 691
diff changeset
158 send("<?xml version='1.0'?>"..tostring(st.stanza("stream:stream", {
2de21fa40382 mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents: 691
diff changeset
159 xmlns = 'jabber:client', ["xmlns:stream"] = 'http://etherx.jabber.org/streams';
2de21fa40382 mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents: 691
diff changeset
160 id = session.streamid, from = session.host, version = '1.0', ["xml:lang"] = 'en' })));
2de21fa40382 mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents: 691
diff changeset
161 else
2de21fa40382 mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents: 691
diff changeset
162 send("<?xml version='1.0'?>"..st.stanza("stream:stream", {
2de21fa40382 mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents: 691
diff changeset
163 xmlns = 'jabber:client', ["xmlns:stream"] = 'http://etherx.jabber.org/streams';
2de21fa40382 mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents: 691
diff changeset
164 id = session.streamid, from = session.host, version = '1.0', ["xml:lang"] = 'en' }):top_tag());
2de21fa40382 mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents: 691
diff changeset
165 end
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
166
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
167 (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
168 session.notopen = nil;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
169
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
170 -- 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
171 -- 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
172 if session.secure == false then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
173 session.secure = true;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
174 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
175
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
176 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
177 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
178 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
179
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
180 send(features);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
181 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
182
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
183 function stream_callbacks.streamclosed(session)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
184 session.log("debug", "Received </stream:stream>");
839
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
185 session:close(false);
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
186 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
187
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
188 function stream_callbacks.error(session, error, data)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
189 if error == "no-stream" then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
190 session.log("debug", "Invalid opening stream header");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
191 session:close("invalid-namespace");
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
192 elseif error == "parse-error" then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
193 (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
194 session:close("not-well-formed");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
195 elseif error == "stream-error" then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
196 local condition, text = "undefined-condition";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
197 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
198 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
199 if child.name ~= "text" then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
200 condition = child.name;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
201 else
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
202 text = child:get_text();
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
203 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
204 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
205 break;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
206 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
207 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
208 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
209 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
210 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
211 session:close(nil, text);
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
212 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
213 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
214
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
215 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
216 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
217 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
218 if stanza then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
219 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
220 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
221 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
222
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
223 --- Session methods
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
224 local function session_close(session, reason)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
225 local log = session.log or log;
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
226 if session.conn then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
227 if session.notopen then
857
1393af36ec9c mod_websocket: Determine whether to use a self-closing stream tag, based on the client's initial data
Florian Zeitz <florob@babelmonkeys.de>
parents: 856
diff changeset
228 -- COMPAT: Some current client implementations need this to be self-closing
1393af36ec9c mod_websocket: Determine whether to use a self-closing stream tag, based on the client's initial data
Florian Zeitz <florob@babelmonkeys.de>
parents: 856
diff changeset
229 if session.self_closing_stream then
692
2de21fa40382 mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents: 691
diff changeset
230 session.send("<?xml version='1.0'?>"..tostring(st.stanza("stream:stream", default_stream_attr)));
2de21fa40382 mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents: 691
diff changeset
231 else
2de21fa40382 mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents: 691
diff changeset
232 session.send("<?xml version='1.0'?>"..st.stanza("stream:stream", default_stream_attr):top_tag());
2de21fa40382 mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents: 691
diff changeset
233 end
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
234 end
839
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
235 if reason then -- nil == no err, initiated by us, false == initiated by client
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
236 if type(reason) == "string" then -- assume stream error
839
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
237 log("debug", "Disconnecting client, <stream:error> is: %s", reason);
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
238 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
239 elseif type(reason) == "table" then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
240 if reason.condition then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
241 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
242 if reason.text then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
243 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
244 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
245 if reason.extra then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
246 stanza:add_child(reason.extra);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
247 end
839
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
248 log("debug", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
249 session.send(stanza);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
250 elseif reason.name then -- a stanza
839
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
251 log("debug", "Disconnecting client, <stream:error> is: %s", tostring(reason));
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
252 session.send(reason);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
253 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
254 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
255 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
256 session.send("</stream:stream>");
839
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
257 function session.send() return false; end
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
258
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
259 local reason = (reason and (reason.text or reason.condition)) or reason;
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
260 session.log("info", "c2s stream for %s closed: %s", session.full_jid or ("<"..session.ip..">"), reason or "session closed");
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
261
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
262 -- Authenticated incoming stream may still be sending us stanzas, so wait for </stream:stream> from remote
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
263 local conn = session.conn;
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
264 if reason == nil and not session.notopen and session.type == "c2s" then
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
265 -- Grace time to process data from authenticated cleanly-closed stream
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
266 add_task(stream_close_timeout, function ()
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
267 if not session.destroyed then
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
268 session.log("warn", "Failed to receive a stream close response, closing connection anyway...");
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
269 sm_destroy_session(session, reason);
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
270 conn:close();
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
271 end
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
272 end);
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
273 else
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
274 sm_destroy_session(session, reason);
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
275 conn:close();
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
276 end
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
277 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
278 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
279
839
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
280 module:hook_global("user-deleted", function(event)
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
281 local username, host = event.username, event.host;
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
282 local user = hosts[host].sessions[username];
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
283 if user and user.sessions then
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
284 for jid, session in pairs(user.sessions) do
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
285 session:close{ condition = "not-authorized", text = "Account deleted" };
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
286 end
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
287 end
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
288 end, 200);
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
289
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
290 --- Port listener
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
291 function listener.onconnect(conn)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
292 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
293 sessions[conn] = session;
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
294
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
295 session.log("info", "Client connected");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
296
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
297 -- 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
298 if conn:ssl() then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
299 session.secure = true;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
300 end
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
301
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
302 if opt_keepalives then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
303 conn:setoption("keepalive", opt_keepalives);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
304 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
305
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
306 session.close = session_close;
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
307
853
4d27552ac443 mod_websocket: Don't offer starttls
Florian Zeitz <florob@babelmonkeys.de>
parents: 849
diff changeset
308 session.conn.starttls = nil;
4d27552ac443 mod_websocket: Don't offer starttls
Florian Zeitz <florob@babelmonkeys.de>
parents: 849
diff changeset
309
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
310 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
311 session.stream = stream;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
312 session.notopen = true;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
313
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
314 function session.reset_stream()
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
315 session.notopen = true;
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
316 session.stream:reset();
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
317 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
318
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
319 local function websocket_close(code, message)
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
320 local data = string.char(code/0x100) .. string.char(code%0x100) .. message;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
321 conn:write(build_frame({opcode = 0x8, FIN = true, data = data}));
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
322 conn:close();
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
323 end
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
324
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
325 local filter = session.filter;
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
326 local dataBuffer;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
327 local function handle_frame(frame)
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
328 module:log("debug", "Websocket received: %s (%i bytes)", frame.data, #frame.data);
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
329
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
330 -- Error cases
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
331 if frame.RSV1 or frame.RSV2 or frame.RSV3 then -- Reserved bits non zero
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
332 websocket_close(1002, "Reserved bits not zero");
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
333 return false;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
334 end
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
335
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
336 if frame.opcode >= 0x8 and frame.length > 125 then -- Control frame with too much payload
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
337 websocket_close(1002, "Payload too large");
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
338 return false;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
339 end
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
340
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
341 if frame.opcode >= 0x8 and not frame.FIN then -- Fragmented control frame
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
342 websocket_close(1002, "Fragmented control frame");
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
343 return false;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
344 end
690
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
345
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
346 if (frame.opcode > 0x2 and frame.opcode < 0x8) or (frame.opcode > 0xA) then
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
347 websocket_close(1002, "Reserved opcode");
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
348 return false;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
349 end
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
350
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
351 if frame.opcode == 0x0 and not dataBuffer then
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
352 websocket_close(1002, "Unexpected continuation frame");
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
353 return false;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
354 end
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
355
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
356 if (frame.opcode == 0x1 or frame.opcode == 0x2) and dataBuffer then
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
357 websocket_close(1002, "Continuation frame expected");
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
358 return false;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
359 end
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
360
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
361 -- Valid cases
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
362 if frame.opcode == 0x0 then -- Continuation frame
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
363 dataBuffer = dataBuffer .. frame.data;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
364 elseif frame.opcode == 0x1 then -- Text frame
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
365 dataBuffer = frame.data;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
366 elseif frame.opcode == 0x2 then -- Binary frame
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
367 websocket_close(1003, "Only text frames are supported");
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
368 return false;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
369 elseif frame.opcode == 0x8 then -- Close request
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
370 websocket_close(1000, "Goodbye");
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
371 return false;
691
04662cc35280 mod_websocket: Answer ping frames
Florian Zeitz <florob@babelmonkeys.de>
parents: 690
diff changeset
372 elseif frame.opcode == 0x9 then -- Ping frame
04662cc35280 mod_websocket: Answer ping frames
Florian Zeitz <florob@babelmonkeys.de>
parents: 690
diff changeset
373 frame.opcode = 0xA;
04662cc35280 mod_websocket: Answer ping frames
Florian Zeitz <florob@babelmonkeys.de>
parents: 690
diff changeset
374 conn:write(build_frame(frame));
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
375 return true;
690
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
376 else
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
377 log("warn", "Received frame with unsupported opcode %i", frame.opcode);
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
378 return true;
690
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
379 end
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
380
690
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
381 if frame.FIN then
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
382 data = dataBuffer;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
383 dataBuffer = nil;
690
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
384
857
1393af36ec9c mod_websocket: Determine whether to use a self-closing stream tag, based on the client's initial data
Florian Zeitz <florob@babelmonkeys.de>
parents: 856
diff changeset
385 -- COMPAT: Some current client implementations send a self-closing <stream:stream>
1393af36ec9c mod_websocket: Determine whether to use a self-closing stream tag, based on the client's initial data
Florian Zeitz <florob@babelmonkeys.de>
parents: 856
diff changeset
386 data, session.self_closing_stream = data:gsub("^(<stream:stream.*)/>$", "%1>");
1393af36ec9c mod_websocket: Determine whether to use a self-closing stream tag, based on the client's initial data
Florian Zeitz <florob@babelmonkeys.de>
parents: 856
diff changeset
387 session.self_closing_stream = (session.self_closing_stream == 1)
690
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
388
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
389 data = filter("bytes/in", data);
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
390 if data then
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
391 local ok, err = stream:feed(data);
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
392 if ok then return; end
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
393 log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
394 session:close("not-well-formed");
5acc203972f3 mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents: 689
diff changeset
395 end
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
396 end
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
397 return true;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
398 end
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
399
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
400 local frameBuffer = "";
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
401 function session.data(data)
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
402 frameBuffer = frameBuffer .. data;
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
403 local frame, length = parse_frame(frameBuffer);
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
404
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
405 while frame do
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
406 frameBuffer = frameBuffer:sub(length + 1);
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
407 if not handle_frame(frame) then return; end
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
408 frame, length = parse_frame(frameBuffer);
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
409 end
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
410 end
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
411
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
412 function session.send(s)
687
d141375ece4b mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents: 677
diff changeset
413 conn:write(build_frame({ FIN = true, opcode = 0x01, data = tostring(s)}));
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
414 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
415
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
416 if c2s_timeout then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
417 add_task(c2s_timeout, function ()
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
418 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
419 session:close("connection-timeout");
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
420 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
421 end);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
422 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
423
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
424 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
425 end
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
426
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
427 function listener.onincoming(conn, data)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
428 local session = sessions[conn];
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
429 if session then
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
430 session.data(data);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
431 else
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
432 listener.onconnect(conn, data);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
433 session = sessions[conn];
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
434 session.data(data);
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
435 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
436 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
437
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
438 function listener.ondisconnect(conn, err)
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
439 local session = sessions[conn];
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
440 if session then
839
d297d76010d4 mod_websocket: Merge with current mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents: 694
diff changeset
441 (session.log or log)("info", "Client disconnected: %s", err or "connection closed");
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
442 sm_destroy_session(session, err);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
443 sessions[conn] = nil;
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
444 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
445 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
446
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
447 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
448 sessions[conn] = session;
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
449 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
450
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
451 function handle_request(event, path)
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
452 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
453
689
4d21bd5dde49 mod_websocket: Be nice to non-websocket clients
Florian Zeitz <florob@babelmonkeys.de>
parents: 688
diff changeset
454 if not request.headers.sec_websocket_key then
4d21bd5dde49 mod_websocket: Be nice to non-websocket clients
Florian Zeitz <florob@babelmonkeys.de>
parents: 688
diff changeset
455 response.headers.content_type = "text/html";
4d21bd5dde49 mod_websocket: Be nice to non-websocket clients
Florian Zeitz <florob@babelmonkeys.de>
parents: 688
diff changeset
456 return [[<!DOCTYPE html><html><head><title>Websocket</title></head><body>
4d21bd5dde49 mod_websocket: Be nice to non-websocket clients
Florian Zeitz <florob@babelmonkeys.de>
parents: 688
diff changeset
457 <p>It works! Now point your WebSocket client to this URL to connect to Prosody.</p>
4d21bd5dde49 mod_websocket: Be nice to non-websocket clients
Florian Zeitz <florob@babelmonkeys.de>
parents: 688
diff changeset
458 </body></html>]];
4d21bd5dde49 mod_websocket: Be nice to non-websocket clients
Florian Zeitz <florob@babelmonkeys.de>
parents: 688
diff changeset
459 end
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
460
844
a0987c0e4e1d mod_websocket: Check whether the xmpp sub-protocol was requested
Florian Zeitz <florob@babelmonkeys.de>
parents: 842
diff changeset
461 local wants_xmpp = false;
a0987c0e4e1d mod_websocket: Check whether the xmpp sub-protocol was requested
Florian Zeitz <florob@babelmonkeys.de>
parents: 842
diff changeset
462 (request.headers.sec_websocket_protocol or ""):gsub("([^,]*),?", function (proto)
a0987c0e4e1d mod_websocket: Check whether the xmpp sub-protocol was requested
Florian Zeitz <florob@babelmonkeys.de>
parents: 842
diff changeset
463 if proto == "xmpp" then wants_xmpp = true; end
a0987c0e4e1d mod_websocket: Check whether the xmpp sub-protocol was requested
Florian Zeitz <florob@babelmonkeys.de>
parents: 842
diff changeset
464 end);
a0987c0e4e1d mod_websocket: Check whether the xmpp sub-protocol was requested
Florian Zeitz <florob@babelmonkeys.de>
parents: 842
diff changeset
465
a0987c0e4e1d mod_websocket: Check whether the xmpp sub-protocol was requested
Florian Zeitz <florob@babelmonkeys.de>
parents: 842
diff changeset
466 if not wants_xmpp then
a0987c0e4e1d mod_websocket: Check whether the xmpp sub-protocol was requested
Florian Zeitz <florob@babelmonkeys.de>
parents: 842
diff changeset
467 return 501;
a0987c0e4e1d mod_websocket: Check whether the xmpp sub-protocol was requested
Florian Zeitz <florob@babelmonkeys.de>
parents: 842
diff changeset
468 end
842
32df4d13f178 mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 840
diff changeset
469
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
470 response.conn:setlistener(listener);
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
471 response.status = "101 Switching Protocols";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
472 response.headers.Upgrade = "websocket";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
473 response.headers.Connection = "Upgrade";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
474 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
475 response.headers.Sec_WebSocket_Protocol = "xmpp";
856
7eb54ed58856 mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents: 853
diff changeset
476 response.headers.Access_Control_Allow_Origin = cross_domain;
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
477
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
478 return "";
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
479 end
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
480
688
e87678a52720 mod_websocket: Make this a shared module
Florian Zeitz <florob@babelmonkeys.de>
parents: 687
diff changeset
481 function module.add_host(module)
e87678a52720 mod_websocket: Make this a shared module
Florian Zeitz <florob@babelmonkeys.de>
parents: 687
diff changeset
482 module:depends("http");
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
483 module:provides("http", {
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
484 name = "xmpp-websocket";
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
485 route = {
849
6e2ec1825182 mod_websocket: Accept GET request without trailing /
Florian Zeitz <florob@babelmonkeys.de>
parents: 844
diff changeset
486 ["GET"] = handle_request;
6e2ec1825182 mod_websocket: Accept GET request without trailing /
Florian Zeitz <florob@babelmonkeys.de>
parents: 844
diff changeset
487 ["GET /"] = handle_request;
676
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
488 };
54fa9d6d7809 mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents: 129
diff changeset
489 });
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
490 end