Mercurial > prosody-modules
annotate mod_websocket/mod_websocket.lua @ 1475:58d48afca54d
mod_privacy_lists: Cache privacy lists for better performance with high traffic
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 27 Jul 2014 14:38:55 +0100 |
parents | 4e36b68d68da |
children |
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) 2012 Florian Zeitz |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
3 -- |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
4 -- 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
|
5 -- 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
|
6 -- |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
7 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
8 module:set_global(); |
129 | 9 |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
10 local add_filter = require "util.filters".add_filter; |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
11 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
|
12 local base64 = require "util.encodings".base64.encode; |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
13 local softreq = require "util.dependencies".softreq; |
1340
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
14 local st = require "util.stanza"; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
15 local parse_xml = require "util.xml".parse; |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
16 local portmanager = require "core.portmanager"; |
1340
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
17 local sm_destroy_session = sessionmanager.destroy_session; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
18 local log = module._log; |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
19 |
905
eae665bc2122
mod_websocket: Fix require to work with lua-bitop (Thanks István and Zash)
Florian Zeitz <florob@babelmonkeys.de>
parents:
895
diff
changeset
|
20 local bit; |
eae665bc2122
mod_websocket: Fix require to work with lua-bitop (Thanks István and Zash)
Florian Zeitz <florob@babelmonkeys.de>
parents:
895
diff
changeset
|
21 pcall(function() bit = require"bit"; end); |
eae665bc2122
mod_websocket: Fix require to work with lua-bitop (Thanks István and Zash)
Florian Zeitz <florob@babelmonkeys.de>
parents:
895
diff
changeset
|
22 bit = bit or softreq"bit32" |
eae665bc2122
mod_websocket: Fix require to work with lua-bitop (Thanks István and Zash)
Florian Zeitz <florob@babelmonkeys.de>
parents:
895
diff
changeset
|
23 if not bit then module:log("error", "No bit module found. Either LuaJIT 2, lua-bitop or Lua 5.2 is required"); end |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
24 local band = bit.band; |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
25 local bxor = bit.bxor; |
910
c469a2b2d77d
mod_websocket: Avoid floating point division
Florian Zeitz <florob@babelmonkeys.de>
parents:
909
diff
changeset
|
26 local rshift = bit.rshift; |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
27 |
1026
e254cf49e14d
mod_websocket: Call string and table functions through locals
Florian Zeitz <florob@babelmonkeys.de>
parents:
1025
diff
changeset
|
28 local t_concat = table.concat; |
e254cf49e14d
mod_websocket: Call string and table functions through locals
Florian Zeitz <florob@babelmonkeys.de>
parents:
1025
diff
changeset
|
29 local s_byte = string.byte; |
e254cf49e14d
mod_websocket: Call string and table functions through locals
Florian Zeitz <florob@babelmonkeys.de>
parents:
1025
diff
changeset
|
30 local s_char= string.char; |
e254cf49e14d
mod_websocket: Call string and table functions through locals
Florian Zeitz <florob@babelmonkeys.de>
parents:
1025
diff
changeset
|
31 |
1193
bbe278a56b0a
mod_websocket: Add consider_websocket_secure option
Florian Zeitz <florob@babelmonkeys.de>
parents:
1029
diff
changeset
|
32 local consider_websocket_secure = module:get_option_boolean("consider_websocket_secure"); |
856
7eb54ed58856
mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents:
853
diff
changeset
|
33 local cross_domain = module:get_option("cross_domain_websocket"); |
7eb54ed58856
mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents:
853
diff
changeset
|
34 if cross_domain then |
7eb54ed58856
mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents:
853
diff
changeset
|
35 if cross_domain == true then |
7eb54ed58856
mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents:
853
diff
changeset
|
36 cross_domain = "*"; |
7eb54ed58856
mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents:
853
diff
changeset
|
37 elseif type(cross_domain) == "table" then |
1026
e254cf49e14d
mod_websocket: Call string and table functions through locals
Florian Zeitz <florob@babelmonkeys.de>
parents:
1025
diff
changeset
|
38 cross_domain = t_concat(cross_domain, ", "); |
856
7eb54ed58856
mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents:
853
diff
changeset
|
39 end |
7eb54ed58856
mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents:
853
diff
changeset
|
40 if type(cross_domain) ~= "string" then |
7eb54ed58856
mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents:
853
diff
changeset
|
41 cross_domain = nil; |
7eb54ed58856
mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents:
853
diff
changeset
|
42 end |
7eb54ed58856
mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents:
853
diff
changeset
|
43 end |
7eb54ed58856
mod_websocket: Add CORS support
Florian Zeitz <florob@babelmonkeys.de>
parents:
853
diff
changeset
|
44 |
1340
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
45 local xmlns_framing = "urn:ietf:params:xml:ns:xmpp-framing"; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
46 local xmlns_streams = "http://etherx.jabber.org/streams"; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
47 local xmlns_client = "jabber:client"; |
1364
cc77341af5ee
mod_websocket: Multiple fixes for sending stream errors
Florian Zeitz <florob@babelmonkeys.de>
parents:
1340
diff
changeset
|
48 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; |
1340
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
49 |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
50 module:depends("c2s") |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
51 local sessions = module:shared("c2s/sessions"); |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
52 local c2s_listener = portmanager.get_service("c2s").listener; |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
53 |
677
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
54 -- Websocket helpers |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
55 local function parse_frame(frame) |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
56 local result = {}; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
57 local pos = 1; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
58 local length_bytes = 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 |
1026
e254cf49e14d
mod_websocket: Call string and table functions through locals
Florian Zeitz <florob@babelmonkeys.de>
parents:
1025
diff
changeset
|
63 tmp_byte = s_byte(frame, pos); |
677
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; |
1026
e254cf49e14d
mod_websocket: Call string and table functions through locals
Florian Zeitz <florob@babelmonkeys.de>
parents:
1025
diff
changeset
|
71 tmp_byte = s_byte(frame, pos); |
677
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; |
1026
e254cf49e14d
mod_websocket: Call string and table functions through locals
Florian Zeitz <florob@babelmonkeys.de>
parents:
1025
diff
changeset
|
87 result.length = result.length * 256 + s_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 |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
93 local counter = 0; |
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
94 local data = {}; |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
95 local key = {s_byte(frame, pos+1), s_byte(frame, pos+2), |
1026
e254cf49e14d
mod_websocket: Call string and table functions through locals
Florian Zeitz <florob@babelmonkeys.de>
parents:
1025
diff
changeset
|
96 s_byte(frame, pos+3), s_byte(frame, pos+4)} |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
97 result.key = key; |
677
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
98 |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
99 pos = pos + 5; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
100 for i = pos, pos + result.length - 1 do |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
101 data[#data+1] = s_char(bxor(key[counter+1], s_byte(frame, i))); |
677
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
102 counter = (counter + 1) % 4; |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
103 end |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
104 result.data = t_concat(data, ""); |
677
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
105 else |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
106 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
|
107 end |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
108 |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
109 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
|
110 end |
eeb41cd5e9f3
mod_websocket: Move frame handling into a separate function
Florian Zeitz <florob@babelmonkeys.de>
parents:
676
diff
changeset
|
111 |
687
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
112 local function build_frame(desc) |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
113 local length; |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
114 local result = {}; |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
115 local data = desc.data or ""; |
687
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
116 |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
117 result[#result+1] = s_char(0x80 * (desc.FIN and 1 or 0) + desc.opcode); |
687
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
118 |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
119 length = #data; |
687
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
120 if length <= 125 then -- 7-bit length |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
121 result[#result+1] = s_char(length); |
687
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
122 elseif length <= 0xFFFF then -- 2-byte length |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
123 result[#result+1] = s_char(126); |
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
124 result[#result+1] = s_char(rshift(length, 8)) .. s_char(length%0x100); |
687
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
125 else -- 8-byte length |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
126 result[#result+1] = s_char(127); |
1025
b56a9aa171b3
mod_websocket: Fix length calculation
Florian Zeitz <florob@babelmonkeys.de>
parents:
969
diff
changeset
|
127 local length_bytes = {}; |
b56a9aa171b3
mod_websocket: Fix length calculation
Florian Zeitz <florob@babelmonkeys.de>
parents:
969
diff
changeset
|
128 for i = 8, 1, -1 do |
1026
e254cf49e14d
mod_websocket: Call string and table functions through locals
Florian Zeitz <florob@babelmonkeys.de>
parents:
1025
diff
changeset
|
129 length_bytes[i] = s_char(length % 0x100); |
1025
b56a9aa171b3
mod_websocket: Fix length calculation
Florian Zeitz <florob@babelmonkeys.de>
parents:
969
diff
changeset
|
130 length = rshift(length, 8); |
687
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
131 end |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
132 result[#result+1] = t_concat(length_bytes, ""); |
687
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
133 end |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
134 |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
135 result[#result+1] = data; |
687
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
136 |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
137 return t_concat(result, ""); |
687
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
138 end |
d141375ece4b
mod_websocket: Move frame building into a function
Florian Zeitz <florob@babelmonkeys.de>
parents:
677
diff
changeset
|
139 |
1340
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
140 --- Session methods |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
141 local function session_open_stream(session) |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
142 local attr = { |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
143 xmlns = xmlns_framing, |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
144 version = "1.0", |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
145 id = session.streamid or "", |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
146 from = session.host |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
147 }; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
148 session.send(st.stanza("open", attr)); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
149 end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
150 |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
151 local function session_close(session, reason) |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
152 local log = session.log or log; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
153 if session.conn then |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
154 if session.notopen then |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
155 session:open_stream(); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
156 end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
157 if reason then -- nil == no err, initiated by us, false == initiated by client |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
158 local stream_error = st.stanza("stream:error"); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
159 if type(reason) == "string" then -- assume stream error |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
160 stream_error:tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
161 elseif type(reason) == "table" then |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
162 if reason.condition then |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
163 stream_error:tag(reason.condition, stream_xmlns_attr):up(); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
164 if reason.text then |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
165 stream_error:tag("text", stream_xmlns_attr):text(reason.text):up(); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
166 end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
167 if reason.extra then |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
168 stream_error:add_child(reason.extra); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
169 end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
170 elseif reason.name then -- a stanza |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
171 stream_error = reason; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
172 end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
173 end |
1364
cc77341af5ee
mod_websocket: Multiple fixes for sending stream errors
Florian Zeitz <florob@babelmonkeys.de>
parents:
1340
diff
changeset
|
174 log("debug", "Disconnecting client, <stream:error> is: %s", tostring(stream_error)); |
1340
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
175 session.send(stream_error); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
176 end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
177 |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
178 session.send(st.stanza("close", { xmlns = xmlns_framing })); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
179 function session.send() return false; end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
180 |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
181 local reason = (reason and (reason.name or reason.text or reason.condition)) or reason; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
182 session.log("debug", "c2s stream for %s closed: %s", session.full_jid or ("<"..session.ip..">"), reason or "session closed"); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
183 |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
184 -- Authenticated incoming stream may still be sending us stanzas, so wait for </stream:stream> from remote |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
185 local conn = session.conn; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
186 if reason == nil and not session.notopen and session.type == "c2s" then |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
187 -- Grace time to process data from authenticated cleanly-closed stream |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
188 add_task(stream_close_timeout, function () |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
189 if not session.destroyed then |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
190 session.log("warn", "Failed to receive a stream close response, closing connection anyway..."); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
191 sm_destroy_session(session, reason); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
192 -- Sends close with code 1000 and message "Stream closed" |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
193 local data = s_char(0x03) .. s_char(0xe8) .. "Stream closed"; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
194 conn:write(build_frame({opcode = 0x8, FIN = true, data = data})); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
195 conn:close(); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
196 end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
197 end); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
198 else |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
199 sm_destroy_session(session, reason); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
200 -- Sends close with code 1000 and message "Stream closed" |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
201 local data = s_char(0x03) .. s_char(0xe8) .. "Stream closed"; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
202 conn:write(build_frame({opcode = 0x8, FIN = true, data = data})); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
203 conn:close(); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
204 end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
205 end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
206 end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
207 |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
208 |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
209 --- Filter stuff |
1340
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
210 local function filter_open_close(data) |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
211 if not data:find(xmlns_framing, 1, true) then return data; end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
212 |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
213 local oc = parse_xml(data); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
214 if not oc then return data; end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
215 if oc.attr.xmlns ~= xmlns_framing then return data; end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
216 if oc.name == "close" then return "</stream:stream>"; end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
217 if oc.name == "open" then |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
218 oc.name = "stream:stream"; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
219 oc.attr.xmlns = nil; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
220 oc.attr["xmlns:stream"] = xmlns_streams; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
221 return oc:top_tag(); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
222 end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
223 |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
224 return data; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
225 end |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
226 function handle_request(event, path) |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
227 local request, response = event.request, event.response; |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
228 local conn = response.conn; |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
229 |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
230 if not request.headers.sec_websocket_key then |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
231 response.headers.content_type = "text/html"; |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
232 return [[<!DOCTYPE html><html><head><title>Websocket</title></head><body> |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
233 <p>It works! Now point your WebSocket client to this URL to connect to Prosody.</p> |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
234 </body></html>]]; |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
235 end |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
236 |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
237 local wants_xmpp = false; |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
238 (request.headers.sec_websocket_protocol or ""):gsub("([^,]*),?", function (proto) |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
239 if proto == "xmpp" then wants_xmpp = true; end |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
240 end); |
129 | 241 |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
242 if not wants_xmpp then |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
243 return 501; |
129 | 244 end |
245 | |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
246 local function websocket_close(code, message) |
1026
e254cf49e14d
mod_websocket: Call string and table functions through locals
Florian Zeitz <florob@babelmonkeys.de>
parents:
1025
diff
changeset
|
247 local data = s_char(rshift(code, 8)) .. s_char(code%0x100) .. message; |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
248 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
|
249 conn:close(); |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
250 end |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
251 |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
252 local dataBuffer; |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
253 local function handle_frame(frame) |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
254 local opcode = frame.opcode; |
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
255 local length = frame.length; |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
256 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
|
257 |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
258 -- Error cases |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
259 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
|
260 websocket_close(1002, "Reserved bits not zero"); |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
261 return false; |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
262 end |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
263 |
1029
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
264 if opcode == 0x8 then |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
265 if length == 1 then |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
266 websocket_close(1002, "Close frame with payload, but too short for status code"); |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
267 return false; |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
268 elseif length >= 2 then |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
269 local status_code = s_byte(frame.data, 1) * 256 + s_byte(frame.data, 2) |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
270 if status_code < 1000 then |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
271 websocket_close(1002, "Closed with invalid status code"); |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
272 return false; |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
273 elseif ((status_code > 1003 and status_code < 1007) or status_code > 1011) and status_code < 3000 then |
1272
717a08403b26
mod_websocket: Fix typo "Cosed" -> "Closed"
Florian Zeitz <florob@babelmonkeys.de>
parents:
1193
diff
changeset
|
274 websocket_close(1002, "Closed with reserved status code"); |
1029
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
275 return false; |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
276 end |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
277 end |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
278 end |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
279 |
1029
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
280 if opcode >= 0x8 then |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
281 if length > 125 then -- Control frame with too much payload |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
282 websocket_close(1002, "Payload too large"); |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
283 return false; |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
284 end |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
285 |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
286 if not frame.FIN then -- Fragmented control frame |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
287 websocket_close(1002, "Fragmented control frame"); |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
288 return false; |
9d85aded2fb6
mod_websocket: Add some more error checks for close frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
1028
diff
changeset
|
289 end |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
290 end |
690
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
291 |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
292 if (opcode > 0x2 and opcode < 0x8) or (opcode > 0xA) then |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
293 websocket_close(1002, "Reserved opcode"); |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
294 return false; |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
295 end |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
296 |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
297 if opcode == 0x0 and not dataBuffer then |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
298 websocket_close(1002, "Unexpected continuation frame"); |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
299 return false; |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
300 end |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
301 |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
302 if (opcode == 0x1 or opcode == 0x2) and dataBuffer then |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
303 websocket_close(1002, "Continuation frame expected"); |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
304 return false; |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
305 end |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
306 |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
307 -- Valid cases |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
308 if opcode == 0x0 then -- Continuation frame |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
309 dataBuffer[#dataBuffer+1] = frame.data; |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
310 elseif opcode == 0x1 then -- Text frame |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
311 dataBuffer = {frame.data}; |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
312 elseif opcode == 0x2 then -- Binary frame |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
313 websocket_close(1003, "Only text frames are supported"); |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
314 return; |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
315 elseif opcode == 0x8 then -- Close request |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
316 websocket_close(1000, "Goodbye"); |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
317 return; |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
318 elseif opcode == 0x9 then -- Ping frame |
691
04662cc35280
mod_websocket: Answer ping frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
690
diff
changeset
|
319 frame.opcode = 0xA; |
04662cc35280
mod_websocket: Answer ping frames
Florian Zeitz <florob@babelmonkeys.de>
parents:
690
diff
changeset
|
320 conn:write(build_frame(frame)); |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
321 return ""; |
1397
4e36b68d68da
mod_websocket: Handle pong messages
Florian Zeitz <florob@babelmonkeys.de>
parents:
1364
diff
changeset
|
322 elseif opcode == 0xA then -- Pong frame |
4e36b68d68da
mod_websocket: Handle pong messages
Florian Zeitz <florob@babelmonkeys.de>
parents:
1364
diff
changeset
|
323 module:log("warn", "Received unexpected pong frame: " .. tostring(frame.data)); |
4e36b68d68da
mod_websocket: Handle pong messages
Florian Zeitz <florob@babelmonkeys.de>
parents:
1364
diff
changeset
|
324 return ""; |
690
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
325 else |
1028
81065638299d
mod_websocket: Access some values via locales instead of through tables
Florian Zeitz <florob@babelmonkeys.de>
parents:
1027
diff
changeset
|
326 log("warn", "Received frame with unsupported opcode %i", opcode); |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
327 return ""; |
690
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
328 end |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
329 |
690
5acc203972f3
mod_websocket: Add fragmentation support
Florian Zeitz <florob@babelmonkeys.de>
parents:
689
diff
changeset
|
330 if frame.FIN then |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
331 local data = t_concat(dataBuffer, ""); |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
332 dataBuffer = nil; |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
333 return data; |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
334 end |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
335 return ""; |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
336 end |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
337 |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
338 conn:setlistener(c2s_listener); |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
339 c2s_listener.onconnect(conn); |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
340 |
1193
bbe278a56b0a
mod_websocket: Add consider_websocket_secure option
Florian Zeitz <florob@babelmonkeys.de>
parents:
1029
diff
changeset
|
341 local session = sessions[conn]; |
bbe278a56b0a
mod_websocket: Add consider_websocket_secure option
Florian Zeitz <florob@babelmonkeys.de>
parents:
1029
diff
changeset
|
342 |
bbe278a56b0a
mod_websocket: Add consider_websocket_secure option
Florian Zeitz <florob@babelmonkeys.de>
parents:
1029
diff
changeset
|
343 session.secure = consider_websocket_secure or session.secure; |
bbe278a56b0a
mod_websocket: Add consider_websocket_secure option
Florian Zeitz <florob@babelmonkeys.de>
parents:
1029
diff
changeset
|
344 |
1340
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
345 session.open_stream = session_open_stream; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
346 session.close = session_close; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
347 |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
348 local frameBuffer = ""; |
1193
bbe278a56b0a
mod_websocket: Add consider_websocket_secure option
Florian Zeitz <florob@babelmonkeys.de>
parents:
1029
diff
changeset
|
349 add_filter(session, "bytes/in", function(data) |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
350 local cache = {}; |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
351 frameBuffer = frameBuffer .. data; |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
352 local frame, length = parse_frame(frameBuffer); |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
353 |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
354 while frame do |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
355 frameBuffer = frameBuffer:sub(length + 1); |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
356 local result = handle_frame(frame); |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
357 if not result then return; end |
1340
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
358 cache[#cache+1] = filter_open_close(result); |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
359 frame, length = parse_frame(frameBuffer); |
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
360 end |
1027
6a2dfa8af421
mod_websocket: Optimize string concatenation
Florian Zeitz <florob@babelmonkeys.de>
parents:
1026
diff
changeset
|
361 return t_concat(cache, ""); |
844
a0987c0e4e1d
mod_websocket: Check whether the xmpp sub-protocol was requested
Florian Zeitz <florob@babelmonkeys.de>
parents:
842
diff
changeset
|
362 end); |
a0987c0e4e1d
mod_websocket: Check whether the xmpp sub-protocol was requested
Florian Zeitz <florob@babelmonkeys.de>
parents:
842
diff
changeset
|
363 |
1193
bbe278a56b0a
mod_websocket: Add consider_websocket_secure option
Florian Zeitz <florob@babelmonkeys.de>
parents:
1029
diff
changeset
|
364 add_filter(session, "bytes/out", function(data) |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
365 return build_frame({ FIN = true, opcode = 0x01, data = tostring(data)}); |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
366 end); |
842
32df4d13f178
mod_websocket: More robust frame handling
Florian Zeitz <florob@babelmonkeys.de>
parents:
840
diff
changeset
|
367 |
1340
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
368 add_filter(session, "stanzas/out", function(stanza) |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
369 local attr = stanza.attr; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
370 attr.xmlns = attr.xmlns or xmlns_client; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
371 if stanza.name:find("^stream:") then |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
372 attr["xmlns:stream"] = attr["xmlns:stream"] or xmlns_streams; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
373 end |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
374 return stanza; |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
375 end); |
3ffd64b4ab59
mod_websocket: Update to draft-ietf-xmpp-websocket-01
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
376 |
969
8eba9d4809d2
mod_websocket: Set status_code instead of status
Florian Zeitz <florob@babelmonkeys.de>
parents:
910
diff
changeset
|
377 response.status_code = 101; |
895
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
378 response.headers.upgrade = "websocket"; |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
379 response.headers.connection = "Upgrade"; |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
380 response.headers.sec_webSocket_accept = base64(sha1(request.headers.sec_websocket_key .. "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")); |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
381 response.headers.sec_webSocket_protocol = "xmpp"; |
1f4d77104da5
mod_websocket: Simplify by getting the c2s_listener from mod_c2s
Florian Zeitz <florob@babelmonkeys.de>
parents:
857
diff
changeset
|
382 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
|
383 |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
384 return ""; |
129 | 385 end |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
386 |
688
e87678a52720
mod_websocket: Make this a shared module
Florian Zeitz <florob@babelmonkeys.de>
parents:
687
diff
changeset
|
387 function module.add_host(module) |
e87678a52720
mod_websocket: Make this a shared module
Florian Zeitz <florob@babelmonkeys.de>
parents:
687
diff
changeset
|
388 module:depends("http"); |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
389 module:provides("http", { |
909
ec4c6e8f277d
mod_websocket: Change the HTTP name to websocket, and override the default_path instead
Florian Zeitz <florob@babelmonkeys.de>
parents:
905
diff
changeset
|
390 name = "websocket"; |
ec4c6e8f277d
mod_websocket: Change the HTTP name to websocket, and override the default_path instead
Florian Zeitz <florob@babelmonkeys.de>
parents:
905
diff
changeset
|
391 default_path = "xmpp-websocket"; |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
392 route = { |
849
6e2ec1825182
mod_websocket: Accept GET request without trailing /
Florian Zeitz <florob@babelmonkeys.de>
parents:
844
diff
changeset
|
393 ["GET"] = handle_request; |
6e2ec1825182
mod_websocket: Accept GET request without trailing /
Florian Zeitz <florob@babelmonkeys.de>
parents:
844
diff
changeset
|
394 ["GET /"] = handle_request; |
676
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
395 }; |
54fa9d6d7809
mod_websocket: New mod_c2s based version, still WIP
Florian Zeitz <florob@babelmonkeys.de>
parents:
129
diff
changeset
|
396 }); |
129 | 397 end |