author | Matthew Wild <mwild1@gmail.com> |
Mon, 28 Sep 2020 16:36:12 +0100 | |
branch | 0.11 |
changeset 11115 | 55d8612ac357 |
parent 11114 | 67fb92e312f1 |
child 11117 | 10301c214f4e |
permissions | -rw-r--r-- |
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 |
-- |
6897
f7203c7cb7ff
mod_websocket: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents:
6896
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 |
|
6896
861790282dda
mod_websocket: Import util.timer and session close timeout config option (thanks fairuz)
Kim Alvefur <zash@zash.se>
parents:
6796
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; |
7764
e0e1f6d6fb4f
mod_websocket: Use contains_token from util.http for checking if the requested WebSocket sub-protocols include XMPP
Kim Alvefur <zash@zash.se>
parents:
7763
diff
changeset
|
17 |
local contains_token = require "util.http".contains_token; |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
18 |
local portmanager = require "core.portmanager"; |
6796
2bf1b7e2149a
mod_websocket: Import sessionmanager (fixes traceback)
Kim Alvefur <zash@zash.se>
parents:
6397
diff
changeset
|
19 |
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
|
20 |
local log = module._log; |
11111
ddd0007e0f1b
mod_websocket: Switch partial frame buffering to util.dbuffer
Matthew Wild <mwild1@gmail.com>
parents:
10620
diff
changeset
|
21 |
local dbuffer = require "util.dbuffer"; |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
22 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
23 |
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
|
24 |
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
|
25 |
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
|
26 |
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
|
27 |
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
|
28 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
29 |
local t_concat = table.concat; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
30 |
|
11111
ddd0007e0f1b
mod_websocket: Switch partial frame buffering to util.dbuffer
Matthew Wild <mwild1@gmail.com>
parents:
10620
diff
changeset
|
31 |
local stanza_size_limit = module:get_option_number("c2s_stanza_size_limit", 10 * 1024 * 1024); |
11113
7ec7dba7ba8b
mod_websocket: Add separate limit for frame buffer size
Matthew Wild <mwild1@gmail.com>
parents:
11112
diff
changeset
|
32 |
local frame_buffer_limit = module:get_option_number("websocket_frame_buffer_limit", 2 * stanza_size_limit); |
11111
ddd0007e0f1b
mod_websocket: Switch partial frame buffering to util.dbuffer
Matthew Wild <mwild1@gmail.com>
parents:
10620
diff
changeset
|
33 |
local frame_fragment_limit = module:get_option_number("websocket_frame_fragment_limit", 8); |
6896
861790282dda
mod_websocket: Import util.timer and session close timeout config option (thanks fairuz)
Kim Alvefur <zash@zash.se>
parents:
6796
diff
changeset
|
34 |
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
|
35 |
local consider_websocket_secure = module:get_option_boolean("consider_websocket_secure"); |
7765
2208e6cd0d9f
mod_websocket: Verify that the client-sent Origin header matches cross_domain_websocket (fixes #652)
Kim Alvefur <zash@zash.se>
parents:
7764
diff
changeset
|
36 |
local cross_domain = module:get_option_set("cross_domain_websocket", {}); |
2208e6cd0d9f
mod_websocket: Verify that the client-sent Origin header matches cross_domain_websocket (fixes #652)
Kim Alvefur <zash@zash.se>
parents:
7764
diff
changeset
|
37 |
if cross_domain:contains("*") or cross_domain:contains(true) then |
2208e6cd0d9f
mod_websocket: Verify that the client-sent Origin header matches cross_domain_websocket (fixes #652)
Kim Alvefur <zash@zash.se>
parents:
7764
diff
changeset
|
38 |
cross_domain = true; |
2208e6cd0d9f
mod_websocket: Verify that the client-sent Origin header matches cross_domain_websocket (fixes #652)
Kim Alvefur <zash@zash.se>
parents:
7764
diff
changeset
|
39 |
end |
2208e6cd0d9f
mod_websocket: Verify that the client-sent Origin header matches cross_domain_websocket (fixes #652)
Kim Alvefur <zash@zash.se>
parents:
7764
diff
changeset
|
40 |
|
2208e6cd0d9f
mod_websocket: Verify that the client-sent Origin header matches cross_domain_websocket (fixes #652)
Kim Alvefur <zash@zash.se>
parents:
7764
diff
changeset
|
41 |
local function check_origin(origin) |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
42 |
if cross_domain == true then |
7765
2208e6cd0d9f
mod_websocket: Verify that the client-sent Origin header matches cross_domain_websocket (fixes #652)
Kim Alvefur <zash@zash.se>
parents:
7764
diff
changeset
|
43 |
return true; |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
44 |
end |
7765
2208e6cd0d9f
mod_websocket: Verify that the client-sent Origin header matches cross_domain_websocket (fixes #652)
Kim Alvefur <zash@zash.se>
parents:
7764
diff
changeset
|
45 |
return cross_domain:contains(origin); |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
46 |
end |
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 |
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
|
49 |
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
|
50 |
local xmlns_client = "jabber:client"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
51 |
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
|
52 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
53 |
module:depends("c2s") |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
54 |
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
|
55 |
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
|
56 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
57 |
--- Session methods |
7941
3629f03817f8
mod_websocket: Make open_stream method behave like the one from util.xmppstream
Kim Alvefur <zash@zash.se>
parents:
7940
diff
changeset
|
58 |
local function session_open_stream(session, from, to) |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
59 |
local attr = { |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
60 |
xmlns = xmlns_framing, |
7940
5b03a8003659
mod_websocket: Include xml:lang attribute on stream <open> (fixes #840)
Kim Alvefur <zash@zash.se>
parents:
7917
diff
changeset
|
61 |
["xml:lang"] = "en", |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
62 |
version = "1.0", |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
63 |
id = session.streamid or "", |
7941
3629f03817f8
mod_websocket: Make open_stream method behave like the one from util.xmppstream
Kim Alvefur <zash@zash.se>
parents:
7940
diff
changeset
|
64 |
from = from or session.host, to = to, |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
65 |
}; |
7941
3629f03817f8
mod_websocket: Make open_stream method behave like the one from util.xmppstream
Kim Alvefur <zash@zash.se>
parents:
7940
diff
changeset
|
66 |
if session.stream_attrs then |
3629f03817f8
mod_websocket: Make open_stream method behave like the one from util.xmppstream
Kim Alvefur <zash@zash.se>
parents:
7940
diff
changeset
|
67 |
session:stream_attrs(from, to, attr) |
3629f03817f8
mod_websocket: Make open_stream method behave like the one from util.xmppstream
Kim Alvefur <zash@zash.se>
parents:
7940
diff
changeset
|
68 |
end |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
69 |
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
|
70 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
71 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
72 |
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
|
73 |
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
|
74 |
if session.conn then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
75 |
if session.notopen then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
76 |
session:open_stream(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
77 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
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
|
82 |
elseif type(reason) == "table" then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
83 |
if reason.condition then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
84 |
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
|
85 |
if reason.text then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
86 |
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
|
87 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
88 |
if reason.extra then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
89 |
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
|
90 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
91 |
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
|
92 |
stream_error = reason; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
93 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
94 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
95 |
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
|
96 |
session.send(stream_error); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
97 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
98 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
99 |
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
|
100 |
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
|
101 |
|
9418
02155a10c5e9
mod_websocket: Silence the one warning instead of ignoring the entire file
Kim Alvefur <zash@zash.se>
parents:
9381
diff
changeset
|
102 |
-- luacheck: ignore 422/reason |
02155a10c5e9
mod_websocket: Silence the one warning instead of ignoring the entire file
Kim Alvefur <zash@zash.se>
parents:
9381
diff
changeset
|
103 |
-- FIXME reason should be handled in common place |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
104 |
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
|
105 |
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
|
106 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
107 |
-- 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
|
108 |
local conn = session.conn; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
109 |
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
|
110 |
-- 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
|
111 |
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
|
112 |
if not session.destroyed then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
113 |
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
|
114 |
sm_destroy_session(session, reason); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
115 |
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
|
116 |
conn:close(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
117 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
118 |
end); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
119 |
else |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
120 |
sm_destroy_session(session, reason); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
121 |
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
|
122 |
conn:close(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
123 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
124 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
125 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
126 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
127 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
128 |
--- Filters |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
129 |
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
|
130 |
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
|
131 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
132 |
local oc = parse_xml(data); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
133 |
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
|
134 |
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
|
135 |
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
|
136 |
if oc.name == "open" then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
137 |
oc.name = "stream:stream"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
138 |
oc.attr.xmlns = nil; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
139 |
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
|
140 |
return oc:top_tag(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
141 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
142 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
143 |
return data; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
144 |
end |
6897
f7203c7cb7ff
mod_websocket: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents:
6896
diff
changeset
|
145 |
function handle_request(event) |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
146 |
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
|
147 |
local conn = response.conn; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
148 |
|
7917
a6eb3b6bf903
mod_websocket: Set connections starttls method to false to prevent mod_tls from offering starttls (fixes #837)
Kim Alvefur <zash@zash.se>
parents:
7767
diff
changeset
|
149 |
conn.starttls = false; -- Prevent mod_tls from believing starttls can be done |
a6eb3b6bf903
mod_websocket: Set connections starttls method to false to prevent mod_tls from offering starttls (fixes #837)
Kim Alvefur <zash@zash.se>
parents:
7767
diff
changeset
|
150 |
|
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
151 |
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
|
152 |
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
|
153 |
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
|
154 |
<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
|
155 |
</body></html>]]; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
156 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
157 |
|
7764
e0e1f6d6fb4f
mod_websocket: Use contains_token from util.http for checking if the requested WebSocket sub-protocols include XMPP
Kim Alvefur <zash@zash.se>
parents:
7763
diff
changeset
|
158 |
local wants_xmpp = contains_token(request.headers.sec_websocket_protocol or "", "xmpp"); |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
159 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
160 |
if not wants_xmpp then |
7763
801d4c8e0f58
mod_websocket: Add some debug messages
Kim Alvefur <zash@zash.se>
parents:
7719
diff
changeset
|
161 |
module:log("debug", "Client didn't want to talk XMPP, list of protocols was %s", request.headers.sec_websocket_protocol or "(empty)"); |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
162 |
return 501; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
163 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
164 |
|
7765
2208e6cd0d9f
mod_websocket: Verify that the client-sent Origin header matches cross_domain_websocket (fixes #652)
Kim Alvefur <zash@zash.se>
parents:
7764
diff
changeset
|
165 |
if not check_origin(request.headers.origin or "") then |
9804
5c5117d41133
mod_websocket: Include the value of cross_domain_websocket in debug message
Kim Alvefur <zash@zash.se>
parents:
9418
diff
changeset
|
166 |
module:log("debug", "Origin %s is not allowed by 'cross_domain_websocket' [ %s ]", request.headers.origin or "(missing header)", cross_domain); |
7765
2208e6cd0d9f
mod_websocket: Verify that the client-sent Origin header matches cross_domain_websocket (fixes #652)
Kim Alvefur <zash@zash.se>
parents:
7764
diff
changeset
|
167 |
return 403; |
2208e6cd0d9f
mod_websocket: Verify that the client-sent Origin header matches cross_domain_websocket (fixes #652)
Kim Alvefur <zash@zash.se>
parents:
7764
diff
changeset
|
168 |
end |
2208e6cd0d9f
mod_websocket: Verify that the client-sent Origin header matches cross_domain_websocket (fixes #652)
Kim Alvefur <zash@zash.se>
parents:
7764
diff
changeset
|
169 |
|
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
170 |
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
|
171 |
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
|
172 |
conn:close(); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
173 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
174 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
175 |
local dataBuffer; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
176 |
local function handle_frame(frame) |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
177 |
local opcode = frame.opcode; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
178 |
local length = frame.length; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
179 |
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
|
180 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
181 |
-- Error cases |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
182 |
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
|
183 |
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
|
184 |
return false; |
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 -- close frame |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
188 |
if length == 1 then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
189 |
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
|
190 |
return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
191 |
elseif length >= 2 then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
192 |
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
|
193 |
if status_code < 1000 then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
194 |
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
|
195 |
return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
196 |
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
|
197 |
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
|
198 |
return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
199 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
200 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
201 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
202 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
203 |
if opcode >= 0x8 then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
204 |
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
|
205 |
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
|
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 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
|
210 |
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
|
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 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
214 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
215 |
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
|
216 |
websocket_close(1002, "Reserved opcode"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
217 |
return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
218 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
219 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
220 |
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
|
221 |
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
|
222 |
return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
223 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
224 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
225 |
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
|
226 |
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
|
227 |
return false; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
228 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
229 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
230 |
-- Valid cases |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
231 |
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
|
232 |
dataBuffer[#dataBuffer+1] = frame.data; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
233 |
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
|
234 |
dataBuffer = {frame.data}; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
235 |
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
|
236 |
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
|
237 |
return; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
238 |
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
|
239 |
websocket_close(1000, "Goodbye"); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
240 |
return; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
241 |
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
|
242 |
frame.opcode = 0xA; |
10585
10d6d0d91f4e
mod_websocket: Clear mask bit when reflecting ping frames (fixes #1484)
Kim Alvefur <zash@zash.se>
parents:
10096
diff
changeset
|
243 |
frame.MASK = false; -- Clients send masked frames, servers don't, see #1484 |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
244 |
conn:write(build_frame(frame)); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
245 |
return ""; |
7317
e327e5b592f5
mod_websocket: Remove warning about unsolicited pong frames "MAY be sent unsolicited" per RFC 6455 (thanks mt)
Kim Alvefur <zash@zash.se>
parents:
7297
diff
changeset
|
246 |
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
|
247 |
return ""; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
248 |
else |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
249 |
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
|
250 |
return ""; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
251 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
252 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
253 |
if frame.FIN then |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
254 |
local data = t_concat(dataBuffer, ""); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
255 |
dataBuffer = nil; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
256 |
return data; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
257 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
258 |
return ""; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
259 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
260 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
261 |
conn:setlistener(c2s_listener); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
262 |
c2s_listener.onconnect(conn); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
263 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
264 |
local session = sessions[conn]; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
265 |
|
8598
d3bbff01df9d
mod_websocket: Transfer IP address derived by mod_http
Kim Alvefur <zash@zash.se>
parents:
8148
diff
changeset
|
266 |
-- Use upstream IP if a HTTP proxy was used |
d3bbff01df9d
mod_websocket: Transfer IP address derived by mod_http
Kim Alvefur <zash@zash.se>
parents:
8148
diff
changeset
|
267 |
-- See mod_http and #540 |
d3bbff01df9d
mod_websocket: Transfer IP address derived by mod_http
Kim Alvefur <zash@zash.se>
parents:
8148
diff
changeset
|
268 |
session.ip = request.ip; |
d3bbff01df9d
mod_websocket: Transfer IP address derived by mod_http
Kim Alvefur <zash@zash.se>
parents:
8148
diff
changeset
|
269 |
|
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
270 |
session.secure = consider_websocket_secure or session.secure; |
8792
4ae8dd415e94
mod_websocket: Store the request object on the session for use by other modules
Matthew Wild <mwild1@gmail.com>
parents:
8148
diff
changeset
|
271 |
session.websocket_request = request; |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
272 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
273 |
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
|
274 |
session.close = session_close; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
275 |
|
11113
7ec7dba7ba8b
mod_websocket: Add separate limit for frame buffer size
Matthew Wild <mwild1@gmail.com>
parents:
11112
diff
changeset
|
276 |
local frameBuffer = dbuffer.new(frame_buffer_limit, frame_fragment_limit); |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
277 |
add_filter(session, "bytes/in", function(data) |
11112
fa1821b56f75
mod_websocket: handle full frame buffer and raise stream error
Matthew Wild <mwild1@gmail.com>
parents:
11111
diff
changeset
|
278 |
if not frameBuffer:write(data) then |
fa1821b56f75
mod_websocket: handle full frame buffer and raise stream error
Matthew Wild <mwild1@gmail.com>
parents:
11111
diff
changeset
|
279 |
session.log("warn", "websocket frame buffer full - terminating session"); |
fa1821b56f75
mod_websocket: handle full frame buffer and raise stream error
Matthew Wild <mwild1@gmail.com>
parents:
11111
diff
changeset
|
280 |
session:close({ condition = "resource-constraint", text = "frame buffer exceeded" }); |
fa1821b56f75
mod_websocket: handle full frame buffer and raise stream error
Matthew Wild <mwild1@gmail.com>
parents:
11111
diff
changeset
|
281 |
return; |
fa1821b56f75
mod_websocket: handle full frame buffer and raise stream error
Matthew Wild <mwild1@gmail.com>
parents:
11111
diff
changeset
|
282 |
end |
11111
ddd0007e0f1b
mod_websocket: Switch partial frame buffering to util.dbuffer
Matthew Wild <mwild1@gmail.com>
parents:
10620
diff
changeset
|
283 |
|
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
284 |
local cache = {}; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
285 |
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
|
286 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
287 |
while frame do |
11114
67fb92e312f1
mod_websocket: Enforce stanza size limit and close stream
Matthew Wild <mwild1@gmail.com>
parents:
11113
diff
changeset
|
288 |
if length > stanza_size_limit then |
67fb92e312f1
mod_websocket: Enforce stanza size limit and close stream
Matthew Wild <mwild1@gmail.com>
parents:
11113
diff
changeset
|
289 |
session:close({ condition = "policy-violation", text = "stanza too large" }); |
67fb92e312f1
mod_websocket: Enforce stanza size limit and close stream
Matthew Wild <mwild1@gmail.com>
parents:
11113
diff
changeset
|
290 |
return; |
67fb92e312f1
mod_websocket: Enforce stanza size limit and close stream
Matthew Wild <mwild1@gmail.com>
parents:
11113
diff
changeset
|
291 |
end |
11111
ddd0007e0f1b
mod_websocket: Switch partial frame buffering to util.dbuffer
Matthew Wild <mwild1@gmail.com>
parents:
10620
diff
changeset
|
292 |
frameBuffer:discard(length); |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
293 |
local result = handle_frame(frame); |
11115
55d8612ac357
mod_websocket: Continue to process data already in the buffer after an error occurs
Matthew Wild <mwild1@gmail.com>
parents:
11114
diff
changeset
|
294 |
if not result then break; end |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
295 |
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
|
296 |
frame, length = parse_frame(frameBuffer); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
297 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
298 |
return t_concat(cache, ""); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
299 |
end); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
300 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
301 |
add_filter(session, "stanzas/out", function(stanza) |
10096
4b3c129e96f2
mod_websocket: Clone stanza before mutating (fixes #1398)
Kim Alvefur <zash@zash.se>
parents:
9809
diff
changeset
|
302 |
stanza = st.clone(stanza); |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
303 |
local attr = stanza.attr; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
304 |
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
|
305 |
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
|
306 |
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
|
307 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
308 |
return stanza; |
7297
5f4d0753c818
mod_websocket: Make sure stanza xmlns filter runs late in the chain
Kim Alvefur <zash@zash.se>
parents:
6897
diff
changeset
|
309 |
end, -1000); |
6397
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 |
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
|
312 |
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
|
313 |
end); |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
314 |
|
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
315 |
response.status_code = 101; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
316 |
response.headers.upgrade = "websocket"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
317 |
response.headers.connection = "Upgrade"; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
318 |
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
|
319 |
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
|
320 |
|
10620
37936c72846d
mod_websocket: Fire event on session creation (thanks Aaron van Meerten)
Matthew Wild <mwild1@gmail.com>
parents:
10585
diff
changeset
|
321 |
module:fire_event("websocket-session", { session = session, request = request }); |
37936c72846d
mod_websocket: Fire event on session creation (thanks Aaron van Meerten)
Matthew Wild <mwild1@gmail.com>
parents:
10585
diff
changeset
|
322 |
|
7763
801d4c8e0f58
mod_websocket: Add some debug messages
Kim Alvefur <zash@zash.se>
parents:
7719
diff
changeset
|
323 |
session.log("debug", "Sending WebSocket handshake"); |
801d4c8e0f58
mod_websocket: Add some debug messages
Kim Alvefur <zash@zash.se>
parents:
7719
diff
changeset
|
324 |
|
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
325 |
return ""; |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
326 |
end |
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
327 |
|
7318
4fd984d1e445
mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents:
7317
diff
changeset
|
328 |
local function keepalive(event) |
7343
7dea28dafc49
mod_websocket: Fix read timeout handler (thanks mt)
Kim Alvefur <zash@zash.se>
parents:
7318
diff
changeset
|
329 |
local session = event.session; |
7dea28dafc49
mod_websocket: Fix read timeout handler (thanks mt)
Kim Alvefur <zash@zash.se>
parents:
7318
diff
changeset
|
330 |
if session.open_stream == session_open_stream then |
7719
779a9ef6b4fd
mod_websocket: Set FIN flag on ping frames (fixes #773)
Kim Alvefur <zash@zash.se>
parents:
7343
diff
changeset
|
331 |
return session.conn:write(build_frame({ opcode = 0x9, FIN = true })); |
7343
7dea28dafc49
mod_websocket: Fix read timeout handler (thanks mt)
Kim Alvefur <zash@zash.se>
parents:
7318
diff
changeset
|
332 |
end |
7318
4fd984d1e445
mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents:
7317
diff
changeset
|
333 |
end |
4fd984d1e445
mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents:
7317
diff
changeset
|
334 |
|
4fd984d1e445
mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents:
7317
diff
changeset
|
335 |
module:hook("c2s-read-timeout", keepalive, -0.9); |
4fd984d1e445
mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents:
7317
diff
changeset
|
336 |
|
9381
a6f54df39624
mod_websocket: Serve HTTP in global context
Kim Alvefur <zash@zash.se>
parents:
8797
diff
changeset
|
337 |
module:depends("http"); |
a6f54df39624
mod_websocket: Serve HTTP in global context
Kim Alvefur <zash@zash.se>
parents:
8797
diff
changeset
|
338 |
module:provides("http", { |
a6f54df39624
mod_websocket: Serve HTTP in global context
Kim Alvefur <zash@zash.se>
parents:
8797
diff
changeset
|
339 |
name = "websocket"; |
a6f54df39624
mod_websocket: Serve HTTP in global context
Kim Alvefur <zash@zash.se>
parents:
8797
diff
changeset
|
340 |
default_path = "xmpp-websocket"; |
a6f54df39624
mod_websocket: Serve HTTP in global context
Kim Alvefur <zash@zash.se>
parents:
8797
diff
changeset
|
341 |
route = { |
a6f54df39624
mod_websocket: Serve HTTP in global context
Kim Alvefur <zash@zash.se>
parents:
8797
diff
changeset
|
342 |
["GET"] = handle_request; |
a6f54df39624
mod_websocket: Serve HTTP in global context
Kim Alvefur <zash@zash.se>
parents:
8797
diff
changeset
|
343 |
["GET /"] = handle_request; |
a6f54df39624
mod_websocket: Serve HTTP in global context
Kim Alvefur <zash@zash.se>
parents:
8797
diff
changeset
|
344 |
}; |
a6f54df39624
mod_websocket: Serve HTTP in global context
Kim Alvefur <zash@zash.se>
parents:
8797
diff
changeset
|
345 |
}); |
a6f54df39624
mod_websocket: Serve HTTP in global context
Kim Alvefur <zash@zash.se>
parents:
8797
diff
changeset
|
346 |
|
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
347 |
function module.add_host(module) |
7318
4fd984d1e445
mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents:
7317
diff
changeset
|
348 |
module:hook("c2s-read-timeout", keepalive, -0.9); |
7766
c5ce14539fc4
mod_websocket: Add the base URL of each host module is enabled on to 'cross_domain_websocket'
Kim Alvefur <zash@zash.se>
parents:
7765
diff
changeset
|
349 |
|
c5ce14539fc4
mod_websocket: Add the base URL of each host module is enabled on to 'cross_domain_websocket'
Kim Alvefur <zash@zash.se>
parents:
7765
diff
changeset
|
350 |
if cross_domain ~= true then |
c5ce14539fc4
mod_websocket: Add the base URL of each host module is enabled on to 'cross_domain_websocket'
Kim Alvefur <zash@zash.se>
parents:
7765
diff
changeset
|
351 |
local url = require "socket.url"; |
c5ce14539fc4
mod_websocket: Add the base URL of each host module is enabled on to 'cross_domain_websocket'
Kim Alvefur <zash@zash.se>
parents:
7765
diff
changeset
|
352 |
local ws_url = module:http_url("websocket", "xmpp-websocket"); |
c5ce14539fc4
mod_websocket: Add the base URL of each host module is enabled on to 'cross_domain_websocket'
Kim Alvefur <zash@zash.se>
parents:
7765
diff
changeset
|
353 |
local url_components = url.parse(ws_url); |
c5ce14539fc4
mod_websocket: Add the base URL of each host module is enabled on to 'cross_domain_websocket'
Kim Alvefur <zash@zash.se>
parents:
7765
diff
changeset
|
354 |
-- The 'Origin' consists of the base URL without path |
c5ce14539fc4
mod_websocket: Add the base URL of each host module is enabled on to 'cross_domain_websocket'
Kim Alvefur <zash@zash.se>
parents:
7765
diff
changeset
|
355 |
url_components.path = nil; |
c5ce14539fc4
mod_websocket: Add the base URL of each host module is enabled on to 'cross_domain_websocket'
Kim Alvefur <zash@zash.se>
parents:
7765
diff
changeset
|
356 |
local this_origin = url.build(url_components); |
7767
d6b96e42e8e6
mod_websocket: Allow per-host cross_domain_websocket, defaulting to the base URL of the current host
Kim Alvefur <zash@zash.se>
parents:
7766
diff
changeset
|
357 |
local local_cross_domain = module:get_option_set("cross_domain_websocket", { this_origin }); |
9809
7bfc4269dc36
mod_websocket: Log an error if cross_domain_websocket = true is set in a VirtualHost section
Kim Alvefur <zash@zash.se>
parents:
9804
diff
changeset
|
358 |
if local_cross_domain:contains(true) then |
7bfc4269dc36
mod_websocket: Log an error if cross_domain_websocket = true is set in a VirtualHost section
Kim Alvefur <zash@zash.se>
parents:
9804
diff
changeset
|
359 |
module:log("error", "cross_domain_websocket = true only works in the global section"); |
7bfc4269dc36
mod_websocket: Log an error if cross_domain_websocket = true is set in a VirtualHost section
Kim Alvefur <zash@zash.se>
parents:
9804
diff
changeset
|
360 |
return; |
7bfc4269dc36
mod_websocket: Log an error if cross_domain_websocket = true is set in a VirtualHost section
Kim Alvefur <zash@zash.se>
parents:
9804
diff
changeset
|
361 |
end |
7bfc4269dc36
mod_websocket: Log an error if cross_domain_websocket = true is set in a VirtualHost section
Kim Alvefur <zash@zash.se>
parents:
9804
diff
changeset
|
362 |
|
7767
d6b96e42e8e6
mod_websocket: Allow per-host cross_domain_websocket, defaulting to the base URL of the current host
Kim Alvefur <zash@zash.se>
parents:
7766
diff
changeset
|
363 |
-- Don't add / remove something added by another host |
d6b96e42e8e6
mod_websocket: Allow per-host cross_domain_websocket, defaulting to the base URL of the current host
Kim Alvefur <zash@zash.se>
parents:
7766
diff
changeset
|
364 |
-- This might be weird with random load order |
d6b96e42e8e6
mod_websocket: Allow per-host cross_domain_websocket, defaulting to the base URL of the current host
Kim Alvefur <zash@zash.se>
parents:
7766
diff
changeset
|
365 |
local_cross_domain:exclude(cross_domain); |
d6b96e42e8e6
mod_websocket: Allow per-host cross_domain_websocket, defaulting to the base URL of the current host
Kim Alvefur <zash@zash.se>
parents:
7766
diff
changeset
|
366 |
cross_domain:include(local_cross_domain); |
8148
4d0f5ea19851
mod_websocket: Convert set to string (syslog sink needs a better fix)
Kim Alvefur <zash@zash.se>
parents:
8055
diff
changeset
|
367 |
module:log("debug", "cross_domain = %s", tostring(cross_domain)); |
7766
c5ce14539fc4
mod_websocket: Add the base URL of each host module is enabled on to 'cross_domain_websocket'
Kim Alvefur <zash@zash.se>
parents:
7765
diff
changeset
|
368 |
function module.unload() |
7767
d6b96e42e8e6
mod_websocket: Allow per-host cross_domain_websocket, defaulting to the base URL of the current host
Kim Alvefur <zash@zash.se>
parents:
7766
diff
changeset
|
369 |
cross_domain:exclude(local_cross_domain); |
7766
c5ce14539fc4
mod_websocket: Add the base URL of each host module is enabled on to 'cross_domain_websocket'
Kim Alvefur <zash@zash.se>
parents:
7765
diff
changeset
|
370 |
end |
c5ce14539fc4
mod_websocket: Add the base URL of each host module is enabled on to 'cross_domain_websocket'
Kim Alvefur <zash@zash.se>
parents:
7765
diff
changeset
|
371 |
end |
6397
6f75f8043936
mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
372 |
end |