Software /
code /
prosody
Annotate
plugins/mod_websocket.lua @ 7625:6afafd6d4ef0
net.server_epoll: Move management of fd registry to :setflags
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 22 Aug 2016 17:19:49 +0200 |
parent | 7340:7dea28dafc49 |
child | 7716:779a9ef6b4fd |
rev | line source |
---|---|
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
1 -- Prosody IM |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
2 -- Copyright (C) 2012-2014 Florian Zeitz |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
3 -- |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
4 -- This project is MIT/X11 licensed. Please see the |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
5 -- COPYING file in the source package for more information. |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
6 -- |
6894
f7203c7cb7ff
mod_websocket: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents:
6893
diff
changeset
|
7 -- luacheck: ignore 431/log |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
8 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
9 module:set_global(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
10 |
6893
861790282dda
mod_websocket: Import util.timer and session close timeout config option (thanks fairuz)
Kim Alvefur <zash@zash.se>
parents:
6793
diff
changeset
|
11 local add_task = require "util.timer".add_task; |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
12 local add_filter = require "util.filters".add_filter; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
13 local sha1 = require "util.hashes".sha1; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
14 local base64 = require "util.encodings".base64.encode; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
15 local st = require "util.stanza"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
16 local parse_xml = require "util.xml".parse; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
17 local portmanager = require "core.portmanager"; |
6793
2bf1b7e2149a
mod_websocket: Import sessionmanager (fixes traceback)
Kim Alvefur <zash@zash.se>
parents:
6397
diff
changeset
|
18 local sm_destroy_session = require"core.sessionmanager".destroy_session; |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
19 local log = module._log; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
20 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
21 local websocket_frames = require"net.websocket.frames"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
22 local parse_frame = websocket_frames.parse; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
23 local build_frame = websocket_frames.build; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
24 local build_close = websocket_frames.build_close; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
25 local parse_close = websocket_frames.parse_close; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
26 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
27 local t_concat = table.concat; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
28 |
6893
861790282dda
mod_websocket: Import util.timer and session close timeout config option (thanks fairuz)
Kim Alvefur <zash@zash.se>
parents:
6793
diff
changeset
|
29 local stream_close_timeout = module:get_option_number("c2s_close_timeout", 5); |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
30 local consider_websocket_secure = module:get_option_boolean("consider_websocket_secure"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
31 local cross_domain = module:get_option("cross_domain_websocket"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
32 if cross_domain then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
33 if cross_domain == true then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
34 cross_domain = "*"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
35 elseif type(cross_domain) == "table" then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
36 cross_domain = t_concat(cross_domain, ", "); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
37 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
38 if type(cross_domain) ~= "string" then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
39 cross_domain = nil; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
40 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
41 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
42 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
43 local xmlns_framing = "urn:ietf:params:xml:ns:xmpp-framing"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
44 local xmlns_streams = "http://etherx.jabber.org/streams"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
45 local xmlns_client = "jabber:client"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
46 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
47 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
48 module:depends("c2s") |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
49 local sessions = module:shared("c2s/sessions"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
50 local c2s_listener = portmanager.get_service("c2s").listener; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
51 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
52 --- Session methods |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
53 local function session_open_stream(session) |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
54 local attr = { |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
55 xmlns = xmlns_framing, |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
56 version = "1.0", |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
57 id = session.streamid or "", |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
58 from = session.host |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
59 }; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
60 session.send(st.stanza("open", attr)); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
61 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
62 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
63 local function session_close(session, reason) |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
64 local log = session.log or log; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
65 if session.conn then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
66 if session.notopen then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
67 session:open_stream(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
68 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
69 if reason then -- nil == no err, initiated by us, false == initiated by client |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
70 local stream_error = st.stanza("stream:error"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
71 if type(reason) == "string" then -- assume stream error |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
72 stream_error:tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
73 elseif type(reason) == "table" then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
74 if reason.condition then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
75 stream_error:tag(reason.condition, stream_xmlns_attr):up(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
76 if reason.text then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
77 stream_error:tag("text", stream_xmlns_attr):text(reason.text):up(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
78 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
79 if reason.extra then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
80 stream_error:add_child(reason.extra); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
81 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
82 elseif reason.name then -- a stanza |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
83 stream_error = reason; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
84 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
85 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
86 log("debug", "Disconnecting client, <stream:error> is: %s", tostring(stream_error)); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
87 session.send(stream_error); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
88 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
89 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
90 session.send(st.stanza("close", { xmlns = xmlns_framing })); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
91 function session.send() return false; end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
92 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
93 local reason = (reason and (reason.name or reason.text or reason.condition)) or reason; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
94 session.log("debug", "c2s stream for %s closed: %s", session.full_jid or ("<"..session.ip..">"), reason or "session closed"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
95 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
96 -- Authenticated incoming stream may still be sending us stanzas, so wait for </stream:stream> from remote |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
97 local conn = session.conn; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
98 if reason == nil and not session.notopen and session.type == "c2s" then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
99 -- Grace time to process data from authenticated cleanly-closed stream |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
100 add_task(stream_close_timeout, function () |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
101 if not session.destroyed then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
102 session.log("warn", "Failed to receive a stream close response, closing connection anyway..."); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
103 sm_destroy_session(session, reason); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
104 conn:write(build_close(1000, "Stream closed")); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
105 conn:close(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
106 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
107 end); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
108 else |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
109 sm_destroy_session(session, reason); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
110 conn:write(build_close(1000, "Stream closed")); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
111 conn:close(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
112 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
113 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
114 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
115 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
116 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
117 --- Filters |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
118 local function filter_open_close(data) |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
119 if not data:find(xmlns_framing, 1, true) then return data; end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
120 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
121 local oc = parse_xml(data); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
122 if not oc then return data; end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
123 if oc.attr.xmlns ~= xmlns_framing then return data; end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
124 if oc.name == "close" then return "</stream:stream>"; end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
125 if oc.name == "open" then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
126 oc.name = "stream:stream"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
127 oc.attr.xmlns = nil; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
128 oc.attr["xmlns:stream"] = xmlns_streams; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
129 return oc:top_tag(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
130 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
131 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
132 return data; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
133 end |
6894
f7203c7cb7ff
mod_websocket: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents:
6893
diff
changeset
|
134 function handle_request(event) |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
135 local request, response = event.request, event.response; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
136 local conn = response.conn; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
137 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
138 if not request.headers.sec_websocket_key then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
139 response.headers.content_type = "text/html"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
140 return [[<!DOCTYPE html><html><head><title>Websocket</title></head><body> |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
141 <p>It works! Now point your WebSocket client to this URL to connect to Prosody.</p> |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
142 </body></html>]]; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
143 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
144 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
145 local wants_xmpp = false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
146 (request.headers.sec_websocket_protocol or ""):gsub("([^,]*),?", function (proto) |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
147 if proto == "xmpp" then wants_xmpp = true; end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
148 end); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
149 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
150 if not wants_xmpp then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
151 return 501; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
152 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
153 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
154 local function websocket_close(code, message) |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
155 conn:write(build_close(code, message)); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
156 conn:close(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
157 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
158 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
159 local dataBuffer; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
160 local function handle_frame(frame) |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
161 local opcode = frame.opcode; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
162 local length = frame.length; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
163 module:log("debug", "Websocket received frame: opcode=%0x, %i bytes", frame.opcode, #frame.data); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
164 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
165 -- Error cases |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
166 if frame.RSV1 or frame.RSV2 or frame.RSV3 then -- Reserved bits non zero |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
167 websocket_close(1002, "Reserved bits not zero"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
168 return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
169 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
170 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
171 if opcode == 0x8 then -- close frame |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
172 if length == 1 then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
173 websocket_close(1002, "Close frame with payload, but too short for status code"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
174 return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
175 elseif length >= 2 then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
176 local status_code = parse_close(frame.data) |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
177 if status_code < 1000 then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
178 websocket_close(1002, "Closed with invalid status code"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
179 return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
180 elseif ((status_code > 1003 and status_code < 1007) or status_code > 1011) and status_code < 3000 then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
181 websocket_close(1002, "Closed with reserved status code"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
182 return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
183 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
184 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
185 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
186 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
187 if opcode >= 0x8 then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
188 if length > 125 then -- Control frame with too much payload |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
189 websocket_close(1002, "Payload too large"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
190 return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
191 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
192 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
193 if not frame.FIN then -- Fragmented control frame |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
194 websocket_close(1002, "Fragmented control frame"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
195 return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
196 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
197 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
198 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
199 if (opcode > 0x2 and opcode < 0x8) or (opcode > 0xA) then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
200 websocket_close(1002, "Reserved opcode"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
201 return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
202 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
203 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
204 if opcode == 0x0 and not dataBuffer then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
205 websocket_close(1002, "Unexpected continuation frame"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
206 return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
207 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
208 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
209 if (opcode == 0x1 or opcode == 0x2) and dataBuffer then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
210 websocket_close(1002, "Continuation frame expected"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
211 return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
212 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
213 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
214 -- Valid cases |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
215 if opcode == 0x0 then -- Continuation frame |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
216 dataBuffer[#dataBuffer+1] = frame.data; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
217 elseif opcode == 0x1 then -- Text frame |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
218 dataBuffer = {frame.data}; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
219 elseif opcode == 0x2 then -- Binary frame |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
220 websocket_close(1003, "Only text frames are supported"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
221 return; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
222 elseif opcode == 0x8 then -- Close request |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
223 websocket_close(1000, "Goodbye"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
224 return; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
225 elseif opcode == 0x9 then -- Ping frame |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
226 frame.opcode = 0xA; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
227 conn:write(build_frame(frame)); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
228 return ""; |
7314
e327e5b592f5
mod_websocket: Remove warning about unsolicited pong frames "MAY be sent unsolicited" per RFC 6455 (thanks mt)
Kim Alvefur <zash@zash.se>
parents:
7294
diff
changeset
|
229 elseif opcode == 0xA then -- Pong frame, MAY be sent unsolicited, eg as keepalive |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
230 return ""; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
231 else |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
232 log("warn", "Received frame with unsupported opcode %i", opcode); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
233 return ""; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
234 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
235 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
236 if frame.FIN then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
237 local data = t_concat(dataBuffer, ""); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
238 dataBuffer = nil; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
239 return data; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
240 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
241 return ""; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
242 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
243 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
244 conn:setlistener(c2s_listener); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
245 c2s_listener.onconnect(conn); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
246 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
247 local session = sessions[conn]; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
248 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
249 session.secure = consider_websocket_secure or session.secure; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
250 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
251 session.open_stream = session_open_stream; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
252 session.close = session_close; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
253 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
254 local frameBuffer = ""; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
255 add_filter(session, "bytes/in", function(data) |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
256 local cache = {}; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
257 frameBuffer = frameBuffer .. data; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
258 local frame, length = parse_frame(frameBuffer); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
259 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
260 while frame do |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
261 frameBuffer = frameBuffer:sub(length + 1); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
262 local result = handle_frame(frame); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
263 if not result then return; end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
264 cache[#cache+1] = filter_open_close(result); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
265 frame, length = parse_frame(frameBuffer); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
266 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
267 return t_concat(cache, ""); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
268 end); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
269 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
270 add_filter(session, "stanzas/out", function(stanza) |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
271 local attr = stanza.attr; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
272 attr.xmlns = attr.xmlns or xmlns_client; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
273 if stanza.name:find("^stream:") then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
274 attr["xmlns:stream"] = attr["xmlns:stream"] or xmlns_streams; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
275 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
276 return stanza; |
7294
5f4d0753c818
mod_websocket: Make sure stanza xmlns filter runs late in the chain
Kim Alvefur <zash@zash.se>
parents:
6894
diff
changeset
|
277 end, -1000); |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
278 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
279 add_filter(session, "bytes/out", function(data) |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
280 return build_frame({ FIN = true, opcode = 0x01, data = tostring(data)}); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
281 end); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
282 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
283 response.status_code = 101; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
284 response.headers.upgrade = "websocket"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
285 response.headers.connection = "Upgrade"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
286 response.headers.sec_webSocket_accept = base64(sha1(request.headers.sec_websocket_key .. "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
287 response.headers.sec_webSocket_protocol = "xmpp"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
288 response.headers.access_control_allow_origin = cross_domain; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
289 |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
290 return ""; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
291 end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
292 |
7315
4fd984d1e445
mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents:
7314
diff
changeset
|
293 local function keepalive(event) |
7340
7dea28dafc49
mod_websocket: Fix read timeout handler (thanks mt)
Kim Alvefur <zash@zash.se>
parents:
7315
diff
changeset
|
294 local session = event.session; |
7dea28dafc49
mod_websocket: Fix read timeout handler (thanks mt)
Kim Alvefur <zash@zash.se>
parents:
7315
diff
changeset
|
295 if session.open_stream == session_open_stream then |
7dea28dafc49
mod_websocket: Fix read timeout handler (thanks mt)
Kim Alvefur <zash@zash.se>
parents:
7315
diff
changeset
|
296 return session.conn:write(build_frame({ opcode = 0x9, })); |
7dea28dafc49
mod_websocket: Fix read timeout handler (thanks mt)
Kim Alvefur <zash@zash.se>
parents:
7315
diff
changeset
|
297 end |
7315
4fd984d1e445
mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents:
7314
diff
changeset
|
298 end |
4fd984d1e445
mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents:
7314
diff
changeset
|
299 |
4fd984d1e445
mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents:
7314
diff
changeset
|
300 module:hook("c2s-read-timeout", keepalive, -0.9); |
4fd984d1e445
mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents:
7314
diff
changeset
|
301 |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
302 function module.add_host(module) |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
303 module:depends("http"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
304 module:provides("http", { |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
305 name = "websocket"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
306 default_path = "xmpp-websocket"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
307 route = { |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
308 ["GET"] = handle_request; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
309 ["GET /"] = handle_request; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
310 }; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
311 }); |
7315
4fd984d1e445
mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents:
7314
diff
changeset
|
312 module:hook("c2s-read-timeout", keepalive, -0.9); |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
313 end |