Software /
code /
prosody-modules
Annotate
mod_websocket/mod_websocket.lua @ 693:7d165cd6168c
mod_websocket: Only un-self-close <stream:stream>s
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Mon, 28 May 2012 02:18:04 +0200 |
parent | 692:2de21fa40382 |
child | 694:02fcb102b9aa |
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 | 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 | 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; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
23 local tohex = require "bit".tohex; |
129 | 24 |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
25 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
|
26 local traceback = debug.traceback; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
27 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
28 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
|
29 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
30 local log = module._log; |
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 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
|
33 local opt_keepalives = module:get_option_boolean("tcp_keepalives", false); |
692
2de21fa40382
mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents:
691
diff
changeset
|
34 local self_closing_stream = module:get_option_boolean("websocket_self_closing_stream", true); |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
35 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
36 local sessions = module:shared("sessions"); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
37 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
38 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
|
39 local listener = {}; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
40 |
677
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
41 -- Websocket helpers |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
42 local function parse_frame(frame) |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
43 local result = {}; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
44 local pos = 1; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
45 local length_bytes = 0; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
46 local counter = 0; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
47 local tmp_byte; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
48 |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
49 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
|
50 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
|
51 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
|
52 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
|
53 result.RSV3 = band(tmp_byte, 0x10) > 0; |
690
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
54 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
|
55 |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
56 pos = pos + 1; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
61 if result.length == 126 then |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
62 length_bytes = 2; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
63 result.length = 0; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
64 elseif result.length == 127 then |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
65 length_bytes = 8; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
66 result.length = 0; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
67 end |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
68 |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
69 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
|
70 pos = pos + 1; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
71 result.length = result.length * 255 + string.byte(frame, pos); |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
72 end |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
73 |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
74 if result.MASK then |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
75 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
|
76 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
|
77 |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
78 pos = pos + 5; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
79 result.data = ""; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
80 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
|
81 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
|
82 counter = (counter + 1) % 4; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
83 end |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
84 else |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
85 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
|
86 end |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
87 |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
88 return result; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
89 end |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
90 |
687
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
91 local function build_frame(desc) |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
92 local length; |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
93 local result = ""; |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
94 |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
95 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
|
96 |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
97 length = #desc.data; |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
98 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
|
99 result = result .. string.char(length); |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
100 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
|
101 result = result .. string.char(126); |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
102 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
|
103 else -- 8-byte length |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
104 result = result .. string.char(127); |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
105 for i = 7, 0, -1 do |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
106 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
|
107 end |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
108 end |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
109 |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
110 result = result .. desc.data; |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
111 |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
112 return result; |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
113 end |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
114 |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
115 --- Stream events handlers |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
116 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
|
117 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
|
118 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
119 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
|
120 local send = session.send; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
121 session.host = nameprep(attr.to); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
122 if not session.host then |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
123 session:close{ condition = "improper-addressing", |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
124 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
|
125 return; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
126 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
127 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
|
128 session.streamid = uuid_generate(); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
129 (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
|
130 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
131 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
|
132 -- 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
|
133 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
|
134 return; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
135 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
136 |
677
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
137 -- COMPAT: Current client implementations need this to be self-closing |
692
2de21fa40382
mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents:
691
diff
changeset
|
138 if self_closing_stream then |
2de21fa40382
mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents:
691
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 else |
2de21fa40382
mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents:
691
diff
changeset
|
143 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
|
144 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
|
145 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
|
146 end |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
147 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
148 (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
|
149 session.notopen = nil; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
150 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
151 -- 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
|
152 -- 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
|
153 if session.secure == false then |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
154 session.secure = true; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
155 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
156 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
157 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
|
158 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
|
159 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
|
160 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
161 send(features); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
162 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
163 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
164 function stream_callbacks.streamclosed(session) |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
165 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
|
166 session:close(); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
167 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
168 |
129 | 169 function stream_callbacks.error(session, error, data) |
170 if error == "no-stream" then | |
171 session.log("debug", "Invalid opening stream header"); | |
172 session:close("invalid-namespace"); | |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
173 elseif error == "parse-error" then |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
174 (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
|
175 session:close("not-well-formed"); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
176 elseif error == "stream-error" then |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
177 local condition, text = "undefined-condition"; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
178 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
|
179 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
|
180 if child.name ~= "text" then |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
181 condition = child.name; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
182 else |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
183 text = child:get_text(); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
184 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
185 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
|
186 break; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
187 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
188 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
189 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
190 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
|
191 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
|
192 session:close(nil, text); |
129 | 193 end |
194 end | |
195 | |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
196 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
|
197 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
|
198 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
|
199 if stanza then |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
200 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr); |
129 | 201 end |
202 end | |
203 | |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
204 --- Session methods |
129 | 205 local function session_close(session, reason) |
206 local log = session.log or log; | |
207 if session.conn then | |
208 if session.notopen then | |
692
2de21fa40382
mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents:
691
diff
changeset
|
209 -- COMPAT: Current client implementations need this to be self-closing |
2de21fa40382
mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents:
691
diff
changeset
|
210 if self_closing_stream then |
2de21fa40382
mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents:
691
diff
changeset
|
211 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
|
212 else |
2de21fa40382
mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents:
691
diff
changeset
|
213 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
|
214 end |
129 | 215 end |
216 if reason then | |
217 if type(reason) == "string" then -- assume stream error | |
218 log("info", "Disconnecting client, <stream:error> is: %s", reason); | |
219 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' })); | |
220 elseif type(reason) == "table" then | |
221 if reason.condition then | |
222 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up(); | |
223 if reason.text then | |
224 stanza:tag("text", stream_xmlns_attr):text(reason.text):up(); | |
225 end | |
226 if reason.extra then | |
227 stanza:add_child(reason.extra); | |
228 end | |
229 log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza)); | |
230 session.send(stanza); | |
231 elseif reason.name then -- a stanza | |
232 log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason)); | |
233 session.send(reason); | |
234 end | |
235 end | |
236 end | |
237 session.send("</stream:stream>"); | |
238 session.conn:close(); | |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
239 listener.ondisconnect(session.conn, (reason and (reason.text or reason.condition)) or reason or "session closed"); |
129 | 240 end |
241 end | |
242 | |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
243 --- Port listener |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
244 function listener.onconnect(conn) |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
245 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
|
246 sessions[conn] = session; |
129 | 247 |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
248 session.log("info", "Client connected"); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
249 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
250 -- 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
|
251 if conn:ssl() then |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
252 session.secure = true; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
253 end |
129 | 254 |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
255 if opt_keepalives then |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
256 conn:setoption("keepalive", opt_keepalives); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
257 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
258 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
259 session.close = session_close; |
129 | 260 |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
261 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
|
262 session.stream = stream; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
263 session.notopen = true; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
264 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
265 function session.reset_stream() |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
266 session.notopen = true; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
267 session.stream:reset(); |
129 | 268 end |
269 | |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
270 local filter = session.filter; |
690
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
271 local buffer = ""; |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
272 function session.data(data) |
690
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
273 local frame = parse_frame(data); |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
274 |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
275 module:log("debug", "Websocket received: %s (%i bytes)", frame.data, #frame.data); |
691
04662cc35280
mod_websocket: Answer ping frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
690
diff
changeset
|
276 if frame.opcode == 0x0 or frame.opcode == 0x1 then -- Text or continuation frame |
690
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
277 buffer = buffer .. frame.data; |
691
04662cc35280
mod_websocket: Answer ping frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
690
diff
changeset
|
278 elseif frame.opcode == 0x9 then -- Ping frame |
04662cc35280
mod_websocket: Answer ping frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
690
diff
changeset
|
279 frame.opcode = 0xA; |
04662cc35280
mod_websocket: Answer ping frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
690
diff
changeset
|
280 conn:write(build_frame(frame)); |
04662cc35280
mod_websocket: Answer ping frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
690
diff
changeset
|
281 return; |
690
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
282 else |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
283 log("warn", "Received frame with unsupported opcode %i", frame.opcode); |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
284 return; |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
285 end |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
286 |
690
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
287 if frame.FIN then |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
288 data = buffer; |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
289 buffer = ""; |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
290 |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
291 -- COMPAT: Current client implementations send a self-closing <stream:stream> |
692
2de21fa40382
mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents:
691
diff
changeset
|
292 if self_closing_stream then |
693
7d165cd6168c
mod_websocket: Only un-self-close <stream:stream>s
Florian Zeitz <florob@babelmonkeys.de>
parents:
692
diff
changeset
|
293 data = data:gsub("(<stream:stream.*)/>$", "%1>"); |
692
2de21fa40382
mod_websocket: Make sending a self-closing <stream:stream> tag configurable
Florian Zeitz <florob@babelmonkeys.de>
parents:
691
diff
changeset
|
294 end |
690
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
295 |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
296 data = filter("bytes/in", data); |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
297 if data then |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
298 local ok, err = stream:feed(data); |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
299 if ok then return; end |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
300 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
|
301 session:close("not-well-formed"); |
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
302 end |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
303 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
304 end |
129 | 305 |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
306 function session.send(s) |
687
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
307 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
|
308 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
309 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
310 if c2s_timeout then |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
311 add_task(c2s_timeout, function () |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
312 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
|
313 session:close("connection-timeout"); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
314 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
315 end); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
316 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
317 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
318 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
|
319 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
320 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
321 function listener.onincoming(conn, data) |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
322 local session = sessions[conn]; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
323 if session then |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
324 session.data(data); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
325 else |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
326 listener.onconnect(conn, data); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
327 session = sessions[conn]; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
328 session.data(data); |
129 | 329 end |
330 end | |
331 | |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
332 function listener.ondisconnect(conn, err) |
129 | 333 local session = sessions[conn]; |
334 if session then | |
335 (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
|
336 sm_destroy_session(session, err); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
337 sessions[conn] = nil; |
129 | 338 session = nil; |
339 end | |
340 end | |
341 | |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
342 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
|
343 sessions[conn] = session; |
129 | 344 end |
345 | |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
346 function handle_request(event, path) |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
347 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
|
348 |
689
4d21bd5dde49
mod_websocket: Be nice to non-websocket clients
Florian Zeitz <florob@babelmonkeys.de>
parents:
688
diff
changeset
|
349 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
|
350 response.headers.content_type = "text/html"; |
4d21bd5dde49
mod_websocket: Be nice to non-websocket clients
Florian Zeitz <florob@babelmonkeys.de>
parents:
688
diff
changeset
|
351 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
|
352 <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
|
353 </body></html>]]; |
4d21bd5dde49
mod_websocket: Be nice to non-websocket clients
Florian Zeitz <florob@babelmonkeys.de>
parents:
688
diff
changeset
|
354 end |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
355 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
356 response.conn:setlistener(listener); |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
357 response.status = "101 Switching Protocols"; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
358 response.headers.Upgrade = "websocket"; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
359 response.headers.Connection = "Upgrade"; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
360 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
|
361 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
|
362 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
363 return ""; |
129 | 364 end |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
365 |
688
e87678a52720
mod_websocket: Make this a shared module
Florian Zeitz <florob@babelmonkeys.de>
parents:
687
diff
changeset
|
366 function module.add_host(module) |
e87678a52720
mod_websocket: Make this a shared module
Florian Zeitz <florob@babelmonkeys.de>
parents:
687
diff
changeset
|
367 module:depends("http"); |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
368 module:provides("http", { |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
369 name = "xmpp-websocket"; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
370 route = { |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
371 ["GET /*"] = handle_request; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
372 }; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
373 }); |
129 | 374 end |