plugins/mod_websocket.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 20 Feb 2023 18:10:15 +0000
branch0.12
changeset 12898 0598d822614f
parent 12448 b33558969b3e
child 12981 74b9e05af71e
permissions -rw-r--r--
mod_websocket: Fire pre-session-close event (fixes #1800) This event was added in a7c183bb4e64 and is required to make mod_smacks know that a session was intentionally closed and shouldn't be hibernated (see fcea4d9e7502). Because this was missing from mod_websocket's session.close(), mod_smacks would always attempt to hibernate websocket sessions even if they closed cleanly. That mod_websocket has its own copy of session.close() is something to fix another day (probably not in the stable branch). So for now this commit makes the minimal change to get things working again. Thanks to Damian and the Jitsi team for reporting.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
11544
1937b3c3efb5 mod_c2s, mod_s2s, mod_component, mod_bosh, mod_websockets: Set default stanza size limits
Matthew Wild <mwild1@gmail.com>
parents: 11117
diff changeset
    31
local stanza_size_limit = module:get_option_number("c2s_stanza_size_limit", 1024 * 256);
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");
9799
02735bc82126 mod_websocket: Drop CORS code in favor of that in mod_http
Kim Alvefur <zash@zash.se>
parents: 9418
diff changeset
    36
local cross_domain = module:get_option("cross_domain_websocket");
02735bc82126 mod_websocket: Drop CORS code in favor of that in mod_http
Kim Alvefur <zash@zash.se>
parents: 9418
diff changeset
    37
if cross_domain ~= nil then
02735bc82126 mod_websocket: Drop CORS code in favor of that in mod_http
Kim Alvefur <zash@zash.se>
parents: 9418
diff changeset
    38
	module:log("info", "The 'cross_domain_websocket' option has been deprecated");
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
    39
end
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    40
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
    41
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
    42
local xmlns_client = "jabber:client";
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    43
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
    44
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    45
module:depends("c2s")
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    46
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
    47
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
    48
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    49
--- 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
    50
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
    51
	local attr = {
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    52
		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
    53
		["xml:lang"] = "en",
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    54
		version = "1.0",
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    55
		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
    56
		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
    57
	};
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
	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
    59
		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
    60
	end
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    61
	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
    62
end
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    63
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    64
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
    65
	local log = session.log or log;
12898
0598d822614f mod_websocket: Fire pre-session-close event (fixes #1800)
Matthew Wild <mwild1@gmail.com>
parents: 12448
diff changeset
    66
	local close_event_payload = { session = session, reason = reason };
0598d822614f mod_websocket: Fire pre-session-close event (fixes #1800)
Matthew Wild <mwild1@gmail.com>
parents: 12448
diff changeset
    67
	module:context(session.host):fire_event("pre-session-close", close_event_payload);
0598d822614f mod_websocket: Fire pre-session-close event (fixes #1800)
Matthew Wild <mwild1@gmail.com>
parents: 12448
diff changeset
    68
	reason = close_event_payload.reason;
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    69
	if session.conn then
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    70
		if session.notopen then
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    71
			session:open_stream();
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    72
		end
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    73
		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
    74
			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
    75
			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
    76
				stream_error:tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' });
11872
ae093c259da2 mod_c2s,etc: Identify stanza object with appropriate function
Kim Alvefur <zash@zash.se>
parents: 11775
diff changeset
    77
			elseif st.is_stanza(reason) then
ae093c259da2 mod_c2s,etc: Identify stanza object with appropriate function
Kim Alvefur <zash@zash.se>
parents: 11775
diff changeset
    78
				stream_error = reason;
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    79
			elseif type(reason) == "table" then
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    80
				if reason.condition then
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    81
					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
    82
					if reason.text then
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    83
						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
    84
					end
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    85
					if reason.extra then
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    86
						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
    87
					end
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
			end
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 10101
diff changeset
    90
			log("debug", "Disconnecting client, <stream:error> is: %s", stream_error);
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    91
			session.send(stream_error);
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    92
		end
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    93
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    94
		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
    95
		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
    96
9418
02155a10c5e9 mod_websocket: Silence the one warning instead of ignoring the entire file
Kim Alvefur <zash@zash.se>
parents: 9381
diff changeset
    97
		-- 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
    98
		-- 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
    99
		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
   100
		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
   101
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   102
		-- 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
   103
		local conn = session.conn;
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   104
		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
   105
			-- 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
   106
			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
   107
				if not session.destroyed then
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   108
					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
   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
		else
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   115
			sm_destroy_session(session, reason);
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   116
			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
   117
			conn:close();
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
	end
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   120
end
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   121
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   122
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   123
--- Filters
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   124
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
   125
	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
   126
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   127
	local oc = parse_xml(data);
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   128
	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
   129
	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
   130
	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
   131
	if oc.name == "open" then
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   132
		oc.name = "stream:stream";
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   133
		oc.attr.xmlns = nil;
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   134
		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
   135
		return oc:top_tag();
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   136
	end
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
	return data;
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   139
end
11117
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   140
11397
e6122e6a40a0 mod_websocket: Use mod_http_errors html template #1172
Kim Alvefur <zash@zash.se>
parents: 11388
diff changeset
   141
local default_get_response_text = "It works! Now point your WebSocket client to this URL to connect to Prosody."
e6122e6a40a0 mod_websocket: Use mod_http_errors html template #1172
Kim Alvefur <zash@zash.se>
parents: 11388
diff changeset
   142
local websocket_get_response_text = module:get_option_string("websocket_get_response_text", default_get_response_text)
e6122e6a40a0 mod_websocket: Use mod_http_errors html template #1172
Kim Alvefur <zash@zash.se>
parents: 11388
diff changeset
   143
10732
2764beb552cd mod_bosh, mod_websocket: Add config options to override GET responses
Matthew Wild <mwild1@gmail.com>
parents: 10621
diff changeset
   144
local default_get_response_body = [[<!DOCTYPE html><html><head><title>Websocket</title></head><body>
11397
e6122e6a40a0 mod_websocket: Use mod_http_errors html template #1172
Kim Alvefur <zash@zash.se>
parents: 11388
diff changeset
   145
<p>]]..websocket_get_response_text..[[</p>
10732
2764beb552cd mod_bosh, mod_websocket: Add config options to override GET responses
Matthew Wild <mwild1@gmail.com>
parents: 10621
diff changeset
   146
</body></html>]]
2764beb552cd mod_bosh, mod_websocket: Add config options to override GET responses
Matthew Wild <mwild1@gmail.com>
parents: 10621
diff changeset
   147
local websocket_get_response_body = module:get_option_string("websocket_get_response_body", default_get_response_body)
2764beb552cd mod_bosh, mod_websocket: Add config options to override GET responses
Matthew Wild <mwild1@gmail.com>
parents: 10621
diff changeset
   148
11117
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   149
local function validate_frame(frame, max_length)
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   150
	local opcode, length = frame.opcode, frame.length;
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   151
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   152
	if max_length and length > max_length then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   153
		return false, 1009, "Payload too large";
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   154
	end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   155
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   156
	-- Error cases
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   157
	if frame.RSV1 or frame.RSV2 or frame.RSV3 then -- Reserved bits non zero
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   158
		return false, 1002, "Reserved bits not zero";
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   159
	end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   160
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   161
	if opcode == 0x8 and frame.data then -- close frame
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   162
		if length == 1 then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   163
			return false, 1002, "Close frame with payload, but too short for status code";
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   164
		elseif length >= 2 then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   165
			local status_code = parse_close(frame.data)
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   166
			if status_code < 1000 then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   167
				return false, 1002, "Closed with invalid status code";
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   168
			elseif ((status_code > 1003 and status_code < 1007) or status_code > 1011) and status_code < 3000 then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   169
				return false, 1002, "Closed with reserved status code";
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   170
			end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   171
		end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   172
	end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   173
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   174
	if opcode >= 0x8 then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   175
		if length > 125 then -- Control frame with too much payload
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   176
			return false, 1002, "Payload too large";
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   177
		end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   178
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   179
		if not frame.FIN then -- Fragmented control frame
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   180
			return false, 1002, "Fragmented control frame";
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   181
		end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   182
	end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   183
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   184
	if (opcode > 0x2 and opcode < 0x8) or (opcode > 0xA) then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   185
		return false, 1002, "Reserved opcode";
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   186
	end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   187
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   188
	-- Check opcode
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   189
	if opcode == 0x2 then -- Binary frame
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   190
		return false, 1003, "Only text frames are supported, RFC 7395 3.2";
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   191
	elseif opcode == 0x8 then -- Close request
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   192
		return false, 1000, "Goodbye";
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   193
	end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   194
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   195
	-- Other (XMPP-specific) validity checks
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   196
	if not frame.FIN then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   197
		return false, 1003, "Continuation frames are not supported, RFC 7395 3.3.3";
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   198
	end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   199
	if opcode == 0x01 and frame.data and frame.data:byte(1, 1) ~= 60 then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   200
		return false, 1007, "Invalid payload start character, RFC 7395 3.3.3";
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   201
	end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   202
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   203
	return true;
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   204
end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   205
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   206
6897
f7203c7cb7ff mod_websocket: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 6896
diff changeset
   207
function handle_request(event)
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   208
	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
   209
	local conn = response.conn;
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   210
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
   211
	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
   212
10329
f2bbad04cf64 mod_websocket: Guard against upgrading to websocket from a HEAD request
Kim Alvefur <zash@zash.se>
parents: 10115
diff changeset
   213
	if not request.headers.sec_websocket_key or request.method ~= "GET" then
11397
e6122e6a40a0 mod_websocket: Use mod_http_errors html template #1172
Kim Alvefur <zash@zash.se>
parents: 11388
diff changeset
   214
		return module:fire_event("http-message", {
e6122e6a40a0 mod_websocket: Use mod_http_errors html template #1172
Kim Alvefur <zash@zash.se>
parents: 11388
diff changeset
   215
			response = event.response;
e6122e6a40a0 mod_websocket: Use mod_http_errors html template #1172
Kim Alvefur <zash@zash.se>
parents: 11388
diff changeset
   216
			---
e6122e6a40a0 mod_websocket: Use mod_http_errors html template #1172
Kim Alvefur <zash@zash.se>
parents: 11388
diff changeset
   217
			title = "Prosody WebSocket endpoint";
e6122e6a40a0 mod_websocket: Use mod_http_errors html template #1172
Kim Alvefur <zash@zash.se>
parents: 11388
diff changeset
   218
			message = websocket_get_response_text;
e6122e6a40a0 mod_websocket: Use mod_http_errors html template #1172
Kim Alvefur <zash@zash.se>
parents: 11388
diff changeset
   219
			warning = not (consider_websocket_secure or request.secure) and "This endpoint is not considered secure!" or nil;
e6122e6a40a0 mod_websocket: Use mod_http_errors html template #1172
Kim Alvefur <zash@zash.se>
parents: 11388
diff changeset
   220
		}) or websocket_get_response_body;
e6122e6a40a0 mod_websocket: Use mod_http_errors html template #1172
Kim Alvefur <zash@zash.se>
parents: 11388
diff changeset
   221
		end
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   222
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
   223
	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
   224
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   225
	if not wants_xmpp then
7763
801d4c8e0f58 mod_websocket: Add some debug messages
Kim Alvefur <zash@zash.se>
parents: 7719
diff changeset
   226
		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
   227
		return 501;
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
	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
   231
		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
   232
		conn:close();
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   233
	end
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   234
11117
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   235
	local function websocket_handle_error(session, code, message)
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   236
		if code == 1009 then -- stanza size limit exceeded
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   237
			-- we close the session, rather than the connection,
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   238
			-- otherwise a resuming client will simply resend the
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   239
			-- offending stanza
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   240
			session:close({ condition = "policy-violation", text = "stanza too large" });
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   241
		else
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   242
			websocket_close(code, message);
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   243
		end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   244
	end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   245
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   246
	local function handle_frame(frame)
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   247
		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
   248
11117
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   249
		-- Check frame makes sense
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   250
		local frame_ok, err_status, err_text = validate_frame(frame, stanza_size_limit);
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   251
		if not frame_ok then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   252
			return frame_ok, err_status, err_text;
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   253
		end
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   254
11117
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   255
		local opcode = frame.opcode;
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   256
		if opcode == 0x9 then -- Ping frame
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   257
			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
   258
			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
   259
			conn:write(build_frame(frame));
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   260
			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
   261
		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
   262
			return "";
11117
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   263
		elseif opcode ~= 0x1 then -- Not text frame (which is all we support)
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   264
			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
   265
			return "";
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
11117
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   268
		return frame.data;
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   269
	end
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   270
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   271
	conn:setlistener(c2s_listener);
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   272
	c2s_listener.onconnect(conn);
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   273
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   274
	local session = sessions[conn];
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   275
8598
d3bbff01df9d mod_websocket: Transfer IP address derived by mod_http
Kim Alvefur <zash@zash.se>
parents: 8148
diff changeset
   276
	-- 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
   277
	-- See mod_http and #540
d3bbff01df9d mod_websocket: Transfer IP address derived by mod_http
Kim Alvefur <zash@zash.se>
parents: 8148
diff changeset
   278
	session.ip = request.ip;
d3bbff01df9d mod_websocket: Transfer IP address derived by mod_http
Kim Alvefur <zash@zash.se>
parents: 8148
diff changeset
   279
11388
f9edf26c66fc mod_websocket: Inherit security status from http request
Kim Alvefur <zash@zash.se>
parents: 11118
diff changeset
   280
	session.secure = consider_websocket_secure or request.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
   281
	session.websocket_request = request;
6397
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
	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
   284
	session.close = session_close;
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   285
11113
7ec7dba7ba8b mod_websocket: Add separate limit for frame buffer size
Matthew Wild <mwild1@gmail.com>
parents: 11112
diff changeset
   286
	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
   287
	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
   288
		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
   289
			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
   290
			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
   291
			return;
fa1821b56f75 mod_websocket: handle full frame buffer and raise stream error
Matthew Wild <mwild1@gmail.com>
parents: 11111
diff changeset
   292
		end
11111
ddd0007e0f1b mod_websocket: Switch partial frame buffering to util.dbuffer
Matthew Wild <mwild1@gmail.com>
parents: 10620
diff changeset
   293
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   294
		local cache = {};
11117
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   295
		local frame, length, partial = parse_frame(frameBuffer);
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   296
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   297
		while frame do
11117
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   298
			frameBuffer:discard(length);
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   299
			local result, err_status, err_text = handle_frame(frame);
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   300
			if not result then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   301
				websocket_handle_error(session, err_status, err_text);
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   302
				break;
11114
67fb92e312f1 mod_websocket: Enforce stanza size limit and close stream
Matthew Wild <mwild1@gmail.com>
parents: 11113
diff changeset
   303
			end
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   304
			cache[#cache+1] = filter_open_close(result);
11117
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   305
			frame, length, partial = parse_frame(frameBuffer);
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   306
		end
11117
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   307
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   308
		if partial then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   309
			-- The header of the next frame is already in the buffer, run
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   310
			-- some early validation here
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   311
			local frame_ok, err_status, err_text = validate_frame(partial, stanza_size_limit);
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   312
			if not frame_ok then
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   313
				websocket_handle_error(session, err_status, err_text);
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   314
			end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   315
		end
10301c214f4e mod_websocket: Refactor frame validity checking, also check partially-received frames against constraints
Matthew Wild <mwild1@gmail.com>
parents: 11115
diff changeset
   316
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   317
		return t_concat(cache, "");
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   318
	end);
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   319
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   320
	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
   321
		stanza = st.clone(stanza);
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   322
		local attr = stanza.attr;
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   323
		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
   324
		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
   325
			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
   326
		end
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   327
		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
   328
	end, -1000);
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   329
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   330
	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
   331
		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
   332
	end);
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   333
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   334
	response.status_code = 101;
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   335
	response.headers.upgrade = "websocket";
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   336
	response.headers.connection = "Upgrade";
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   337
	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
   338
	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
   339
10620
37936c72846d mod_websocket: Fire event on session creation (thanks Aaron van Meerten)
Matthew Wild <mwild1@gmail.com>
parents: 10585
diff changeset
   340
	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
   341
7763
801d4c8e0f58 mod_websocket: Add some debug messages
Kim Alvefur <zash@zash.se>
parents: 7719
diff changeset
   342
	session.log("debug", "Sending WebSocket handshake");
801d4c8e0f58 mod_websocket: Add some debug messages
Kim Alvefur <zash@zash.se>
parents: 7719
diff changeset
   343
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   344
	return "";
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   345
end
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   346
7318
4fd984d1e445 mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents: 7317
diff changeset
   347
local function keepalive(event)
7343
7dea28dafc49 mod_websocket: Fix read timeout handler (thanks mt)
Kim Alvefur <zash@zash.se>
parents: 7318
diff changeset
   348
	local session = event.session;
7dea28dafc49 mod_websocket: Fix read timeout handler (thanks mt)
Kim Alvefur <zash@zash.se>
parents: 7318
diff changeset
   349
	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
   350
		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
   351
	end
7318
4fd984d1e445 mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents: 7317
diff changeset
   352
end
4fd984d1e445 mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents: 7317
diff changeset
   353
6397
6f75f8043936 mod_websocket: Initial commit (based on the prosody-modules version)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   354
function module.add_host(module)
7318
4fd984d1e445 mod_websocket: Send a ping on read timeout
Kim Alvefur <zash@zash.se>
parents: 7317
diff changeset
   355
	module:hook("c2s-read-timeout", keepalive, -0.9);
11775
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   356
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   357
	module:depends("http");
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   358
	module:provides("http", {
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   359
		name = "websocket";
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   360
		default_path = "xmpp-websocket";
12448
b33558969b3e mod_http (and dependent modules): Make CORS opt-in by default (fixes #1731)
Matthew Wild <mwild1@gmail.com>
parents: 12267
diff changeset
   361
		cors = {
b33558969b3e mod_http (and dependent modules): Make CORS opt-in by default (fixes #1731)
Matthew Wild <mwild1@gmail.com>
parents: 12267
diff changeset
   362
			enabled = true;
b33558969b3e mod_http (and dependent modules): Make CORS opt-in by default (fixes #1731)
Matthew Wild <mwild1@gmail.com>
parents: 12267
diff changeset
   363
		};
11775
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   364
		route = {
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   365
			["GET"] = handle_request;
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   366
			["GET /"] = handle_request;
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   367
		};
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   368
	});
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   369
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   370
	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
   371
end
11775
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   372
12267
168970ce8543 mod_websocket: Only enable host-agnostic HTTP routing when enabled globally
Kim Alvefur <zash@zash.se>
parents: 11872
diff changeset
   373
if require"core.modulemanager".get_modules_for_host("*"):contains(module.name) then
168970ce8543 mod_websocket: Only enable host-agnostic HTTP routing when enabled globally
Kim Alvefur <zash@zash.se>
parents: 11872
diff changeset
   374
	module:add_host();
168970ce8543 mod_websocket: Only enable host-agnostic HTTP routing when enabled globally
Kim Alvefur <zash@zash.se>
parents: 11872
diff changeset
   375
end