plugins/mod_bosh.lua
author Matthew Wild <mwild1@gmail.com>
Sun, 17 Mar 2024 10:10:24 +0000
changeset 13464 a688947fab1e
parent 13297 ad1ed84fdf13
permissions -rw-r--r--
mod_bosh: Set base_type on session This fixes a traceback with mod_saslauth. Ideally we move this to util.session at some point, though.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1112
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2099
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2099
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
5634
7298c9bbb30f mod_bosh: Some very minor whitespace/layout fixes
Matthew Wild <mwild1@gmail.com>
parents: 5188
diff changeset
     4
--
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1112
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1112
diff changeset
     6
-- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1112
diff changeset
     7
--
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1112
diff changeset
     8
9380
f2013233e20d mod_bosh: Make BOSH global again!
Kim Alvefur <zash@zash.se>
parents: 8921
diff changeset
     9
module:set_global();
f2013233e20d mod_bosh: Make BOSH global again!
Kim Alvefur <zash@zash.se>
parents: 8921
diff changeset
    10
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12448
diff changeset
    11
local new_xmpp_stream = require "prosody.util.xmppstream".new;
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12448
diff changeset
    12
local sm = require "prosody.core.sessionmanager";
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
    13
local sm_destroy_session = sm.destroy_session;
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12448
diff changeset
    14
local new_uuid = require "prosody.util.uuid".generate;
5013
ab693eea0869 mod_admin_adhoc, mod_admin_telnet, mod_bosh, mod_c2s, mod_component, mod_pep, mod_presence, mod_roster, mod_s2s: Import core_post_stanza from the global prosody table.
Kim Alvefur <zash@zash.se>
parents: 4998
diff changeset
    15
local core_process_stanza = prosody.core_process_stanza;
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12448
diff changeset
    16
local st = require "prosody.util.stanza";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12448
diff changeset
    17
local logger = require "prosody.util.logger";
8495
2dd07828ede1 mod_bosh: Use moduleapi logger
Kim Alvefur <zash@zash.se>
parents: 8494
diff changeset
    18
local log = module._log;
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12448
diff changeset
    19
local initialize_filters = require "prosody.util.filters".initialize;
5185
ca30b21946ef mod_bosh: Add bosh_max_wait config option, to limit the amount of time a client can request for the server to hold open requests
Matthew Wild <mwild1@gmail.com>
parents: 5179
diff changeset
    20
local math_min = math.min;
7393
3219b23c4255 mod_bosh: Remove unused imports (also mistake in merge)
Kim Alvefur <zash@zash.se>
parents: 7392
diff changeset
    21
local tostring, type = tostring, type;
5727
372ecf3630cf mod_bosh: pcall() core_process_stanza per stanza, to bring in line with other listeners. This ensures that stanzas following a traceback-causing stanza in a request will still be processed (as would happen on normal c2s).
Matthew Wild <mwild1@gmail.com>
parents: 5726
diff changeset
    22
local traceback = debug.traceback;
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12448
diff changeset
    23
local runner = require"prosody.util.async".runner;
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12448
diff changeset
    24
local nameprep = require "prosody.util.encodings".stringprep.nameprep;
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12448
diff changeset
    25
local cache = require "prosody.util.cache";
1665
2c72b725384e mod_bosh: Strip BOSH namespace from stanzas to allow for some clients which may send them without the correct xmlns
Matthew Wild <mwild1@gmail.com>
parents: 1664
diff changeset
    26
3448
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
    27
local xmlns_streams = "http://etherx.jabber.org/streams";
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
    28
local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send)
3449
0a74ce129a06 mod_bosh: Small change to use variable instead of hard-coded xmlns
Matthew Wild <mwild1@gmail.com>
parents: 3448
diff changeset
    30
0a74ce129a06 mod_bosh: Small change to use variable instead of hard-coded xmlns
Matthew Wild <mwild1@gmail.com>
parents: 3448
diff changeset
    31
local stream_callbacks = {
0a74ce129a06 mod_bosh: Small change to use variable instead of hard-coded xmlns
Matthew Wild <mwild1@gmail.com>
parents: 3448
diff changeset
    32
	stream_ns = xmlns_bosh, stream_tag = "body", default_ns = "jabber:client" };
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
7656
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
    34
-- These constants are implicitly assumed within the code, and cannot be changed
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
    35
local BOSH_HOLD = 1;
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
    36
local BOSH_MAX_REQUESTS = 2;
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
    37
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
    38
-- The number of seconds a BOSH session should remain open with no requests
13213
c8d949cf6b09 plugins: Switch to :get_option_period() for time range options
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
    39
local bosh_max_inactivity = module:get_option_period("bosh_max_inactivity", 60);
7656
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
    40
-- The minimum amount of time between requests with no payload
13213
c8d949cf6b09 plugins: Switch to :get_option_period() for time range options
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
    41
local bosh_max_polling = module:get_option_period("bosh_max_polling", 5);
7656
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
    42
-- The maximum amount of time that the server will hold onto a request before replying
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
    43
-- (the client can set this to a lower value when it connects, if it chooses)
13213
c8d949cf6b09 plugins: Switch to :get_option_period() for time range options
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
    44
local bosh_max_wait = module:get_option_period("bosh_max_wait", 120);
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
3070
3238b58fd118 mod_bosh: Add option consider_bosh_secure to treat BOSH sessions as encrypted even if they don't use HTTP (useful for when secure requests are proxied to Prosody over HTTP)
Matthew Wild <mwild1@gmail.com>
parents: 3043
diff changeset
    46
local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure");
9798
4b5c24f13d4a mod_bosh: Drop CORS code in favor of than in mod_http
Kim Alvefur <zash@zash.se>
parents: 9781
diff changeset
    47
local cross_domain = module:get_option("cross_domain_bosh");
13217
50324f66ca2a plugins: Use integer config API with interval specification where sensible
Kim Alvefur <zash@zash.se>
parents: 13213
diff changeset
    48
local stanza_size_limit = module:get_option_integer("c2s_stanza_size_limit", 1024*256, 10000);
2484
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    49
9798
4b5c24f13d4a mod_bosh: Drop CORS code in favor of than in mod_http
Kim Alvefur <zash@zash.se>
parents: 9781
diff changeset
    50
if cross_domain ~= nil then
4b5c24f13d4a mod_bosh: Drop CORS code in favor of than in mod_http
Kim Alvefur <zash@zash.se>
parents: 9781
diff changeset
    51
	module:log("info", "The 'cross_domain_bosh' option has been deprecated");
4b5c24f13d4a mod_bosh: Drop CORS code in favor of than in mod_http
Kim Alvefur <zash@zash.se>
parents: 9781
diff changeset
    52
end
2484
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    53
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
5179
f3662fea95d9 mod_bosh: Share sessions and inactive_sessions tables
Matthew Wild <mwild1@gmail.com>
parents: 5071
diff changeset
    56
-- All sessions, and sessions that have no requests open
7509
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
    57
local sessions = module:shared("sessions");
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
9994
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    59
local measure_active = module:measure("active_sessions", "amount");
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    60
local measure_inactive = module:measure("inactive_sessions", "amount");
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    61
local report_bad_host = module:measure("bad_host", "rate");
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    62
local report_bad_sid = module:measure("bad_sid", "rate");
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    63
local report_new_sid = module:measure("new_sid", "rate");
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    64
local report_timeout = module:measure("timeout", "rate");
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    65
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    66
module:hook("stats-update", function ()
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    67
	local active = 0;
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    68
	local inactive = 0;
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    69
	for _, session in pairs(sessions) do
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    70
		if #session.requests > 0 then
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    71
			active = active + 1;
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    72
		else
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    73
			inactive = inactive + 1;
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    74
		end
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    75
	end
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    76
	measure_active(active);
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    77
	measure_inactive(inactive);
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    78
end);
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    79
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
    80
-- Used to respond to idle sessions (those with waiting requests)
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
function on_destroy_request(request)
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
    82
	log("debug", "Request destroyed: %s", request);
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
    83
	local session = sessions[request.context.sid];
816
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    84
	if session then
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    85
		local requests = session.requests;
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
    86
		for i, r in ipairs(requests) do
3039
2fef108d7eb7 mod_bosh: Remove requests from the session table using table.remove(), prevents the possibility of holes in the array.
Matthew Wild <mwild1@gmail.com>
parents: 2959
diff changeset
    87
			if r == request then
2fef108d7eb7 mod_bosh: Remove requests from the session table using table.remove(), prevents the possibility of holes in the array.
Matthew Wild <mwild1@gmail.com>
parents: 2959
diff changeset
    88
				t_remove(requests, i);
2fef108d7eb7 mod_bosh: Remove requests from the session table using table.remove(), prevents the possibility of holes in the array.
Matthew Wild <mwild1@gmail.com>
parents: 2959
diff changeset
    89
				break;
2fef108d7eb7 mod_bosh: Remove requests from the session table using table.remove(), prevents the possibility of holes in the array.
Matthew Wild <mwild1@gmail.com>
parents: 2959
diff changeset
    90
			end
816
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    91
		end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
    92
816
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    93
		-- If this session now has no requests open, mark it as inactive
4436
aa79b3767f98 mod_bosh: Store time to destroy session in inactive_sessions, removing dependency on session.bosh_max_inactive in cleanup timer
Matthew Wild <mwild1@gmail.com>
parents: 4379
diff changeset
    94
		local max_inactive = session.bosh_max_inactive;
aa79b3767f98 mod_bosh: Store time to destroy session in inactive_sessions, removing dependency on session.bosh_max_inactive in cleanup timer
Matthew Wild <mwild1@gmail.com>
parents: 4379
diff changeset
    95
		if max_inactive and #requests == 0 then
7509
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
    96
			if session.inactive_timer then
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
    97
				session.inactive_timer:stop();
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
    98
			end
9994
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
    99
			session.inactive_timer = module:add_timer(max_inactive, session_timeout, session, request.context,
7509
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
   100
				"BOSH client silent for over "..max_inactive.." seconds");
4436
aa79b3767f98 mod_bosh: Store time to destroy session in inactive_sessions, removing dependency on session.bosh_max_inactive in cleanup timer
Matthew Wild <mwild1@gmail.com>
parents: 4379
diff changeset
   101
			(session.log or log)("debug", "BOSH session marked as inactive (for %ds)", max_inactive);
816
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
   102
		end
7509
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
   103
		if session.bosh_wait_timer then
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
   104
			session.bosh_wait_timer:stop();
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
   105
			session.bosh_wait_timer = nil;
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
   106
		end
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
   107
	end
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
   108
end
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
   109
9994
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
   110
function session_timeout(now, session, context, reason) -- luacheck: ignore 212/now
7658
132819f409dc mod_bosh: Fix typo
Matthew Wild <mwild1@gmail.com>
parents: 7657
diff changeset
   111
	if not session.destroyed then
9994
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
   112
		report_timeout();
7509
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
   113
		sessions[context.sid] = nil;
8cca24bea3e0 mod_bosh: Fix merge mistakes from c8923f882274
Kim Alvefur <zash@zash.se>
parents: 7394
diff changeset
   114
		sm_destroy_session(session, reason);
816
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
   115
	end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   118
function handle_POST(event)
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
   119
	log("debug", "Handling new request %s: %s\n----------", event.request, event.request.body);
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   120
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   121
	local request, response = event.request, event.response;
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   122
	response.on_destroy = on_destroy_request;
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   123
	local body = request.body;
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   124
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   125
	local context = { request = request, response = response, notopen = true };
11544
1937b3c3efb5 mod_c2s, mod_s2s, mod_component, mod_bosh, mod_websockets: Set default stanza size limits
Matthew Wild <mwild1@gmail.com>
parents: 11128
diff changeset
   126
	local stream = new_xmpp_stream(context, stream_callbacks, stanza_size_limit);
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   127
	response.context = context;
5648
f7f667c48d9a mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents: 5647
diff changeset
   128
f7f667c48d9a mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents: 5647
diff changeset
   129
	local headers = response.headers;
f7f667c48d9a mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents: 5647
diff changeset
   130
	headers.content_type = "text/xml; charset=utf-8";
f7f667c48d9a mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents: 5647
diff changeset
   131
3707
79f62694d36e mod_bosh: Switch to util.xmppstream from xmlhandlers
Matthew Wild <mwild1@gmail.com>
parents: 3684
diff changeset
   132
	-- stream:feed() calls the stream_callbacks, so all stanzas in
79f62694d36e mod_bosh: Switch to util.xmppstream from xmlhandlers
Matthew Wild <mwild1@gmail.com>
parents: 3684
diff changeset
   133
	-- the body are processed in this next line before it returns.
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   134
	-- In particular, the streamopened() stream callback is where
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   135
	-- much of the session logic happens, because it's where we first
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   136
	-- get to see the 'sid' of this request.
7379
f9a5d9f60561 mod_bosh: Log error returned from stream:feed()
Kim Alvefur <zash@zash.se>
parents: 7378
diff changeset
   137
	local ok, err = stream:feed(body);
f9a5d9f60561 mod_bosh: Log error returned from stream:feed()
Kim Alvefur <zash@zash.se>
parents: 7378
diff changeset
   138
	if not ok then
f9a5d9f60561 mod_bosh: Log error returned from stream:feed()
Kim Alvefur <zash@zash.se>
parents: 7378
diff changeset
   139
		module:log("warn", "Error parsing BOSH payload; %s", err)
7380
6c98e783272a mod_bosh: Return a proper BOSH error response instead of deprecated(?) status code (See #343)
Kim Alvefur <zash@zash.se>
parents: 7379
diff changeset
   140
		local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
6c98e783272a mod_bosh: Return a proper BOSH error response instead of deprecated(?) status code (See #343)
Kim Alvefur <zash@zash.se>
parents: 7379
diff changeset
   141
			["xmlns:stream"] = xmlns_streams, condition = "bad-request" });
6c98e783272a mod_bosh: Return a proper BOSH error response instead of deprecated(?) status code (See #343)
Kim Alvefur <zash@zash.se>
parents: 7379
diff changeset
   142
		return tostring(close_reply);
5647
8767b47524c9 mod_bosh: Return errors when appropriate (invalid XML, missing sid)
Matthew Wild <mwild1@gmail.com>
parents: 5646
diff changeset
   143
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
   144
4438
7f51186ed28b mod_bosh: Move stream:features sending until after the current request has been added to session.requests. Ensures correct inactivity logic.
Matthew Wild <mwild1@gmail.com>
parents: 4437
diff changeset
   145
	-- Stanzas (if any) in the request have now been processed, and
7f51186ed28b mod_bosh: Move stream:features sending until after the current request has been added to session.requests. Ensures correct inactivity logic.
Matthew Wild <mwild1@gmail.com>
parents: 4437
diff changeset
   146
	-- we take care of the high-level BOSH logic here, including
7f51186ed28b mod_bosh: Move stream:features sending until after the current request has been added to session.requests. Ensures correct inactivity logic.
Matthew Wild <mwild1@gmail.com>
parents: 4437
diff changeset
   147
	-- giving a response or putting the request "on hold".
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   148
	local session = sessions[context.sid];
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
	if session then
4308
50e1a3dc2b50 mod_bosh: Mark a session as active when a request comes in, even if we don't end up holding that request, fixes BOSH ghosts (thanks smoku)
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   150
		-- Session was marked as inactive, since we have
50e1a3dc2b50 mod_bosh: Mark a session as active when a request comes in, even if we don't end up holding that request, fixes BOSH ghosts (thanks smoku)
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   151
		-- a request open now, unmark it
6992
118858bf47cd mod_bosh: Instead of a global once-per-second timer add a timer for each session when needed
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
   152
		if session.inactive_timer and #session.requests > 0 then
118858bf47cd mod_bosh: Instead of a global once-per-second timer add a timer for each session when needed
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
   153
			session.inactive_timer:stop();
118858bf47cd mod_bosh: Instead of a global once-per-second timer add a timer for each session when needed
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
   154
			session.inactive_timer = nil;
118858bf47cd mod_bosh: Instead of a global once-per-second timer add a timer for each session when needed
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
   155
		end
118858bf47cd mod_bosh: Instead of a global once-per-second timer add a timer for each session when needed
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
   156
118858bf47cd mod_bosh: Instead of a global once-per-second timer add a timer for each session when needed
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
   157
		if session.bosh_wait_timer then
118858bf47cd mod_bosh: Instead of a global once-per-second timer add a timer for each session when needed
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
   158
			session.bosh_wait_timer:stop();
118858bf47cd mod_bosh: Instead of a global once-per-second timer add a timer for each session when needed
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
   159
			session.bosh_wait_timer = nil;
4308
50e1a3dc2b50 mod_bosh: Mark a session as active when a request comes in, even if we don't end up holding that request, fixes BOSH ghosts (thanks smoku)
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   160
		end
4000
ca91d6e1d802 mod_bosh: Fix for miscalculating inactivity, causing disconnects under a steady stream of traffic
Matthew Wild <mwild1@gmail.com>
parents: 3725
diff changeset
   161
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
		local r = session.requests;
7656
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
   163
		log("debug", "Session %s has %d out of %d requests open", context.sid, #r, BOSH_HOLD);
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   164
		log("debug", "and there are %d things in the send_buffer:", #session.send_buffer);
7656
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
   165
		if #r > BOSH_HOLD then
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
			-- We are holding too many requests, send what's in the buffer,
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
			log("debug", "We are holding too many requests, so...");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
			if #session.send_buffer > 0 then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
				log("debug", "...sending what is in the buffer")
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
				session.send(t_concat(session.send_buffer));
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
				session.send_buffer = {};
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   172
			else
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
				-- or an empty response
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   174
				log("debug", "...sending an empty response");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   175
				session.send("");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
			end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   177
		elseif #session.send_buffer > 0 then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   178
			log("debug", "Session has data in the send buffer, will send now..");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   179
			local resp = t_concat(session.send_buffer);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   180
			session.send_buffer = {};
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   181
			session.send(resp);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   182
		end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
   183
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   184
		if not response.finished then
3042
b1961f6c9853 mod_bosh: Always give requests a destroy handler, so that the management of each session's request array and the inactive_sessions logic can happen in one place. Simplifies everything and concludes this series of BOSH fixes.
Matthew Wild <mwild1@gmail.com>
parents: 3041
diff changeset
   185
			-- We're keeping this request open, to respond later
b1961f6c9853 mod_bosh: Always give requests a destroy handler, so that the management of each session's request array and the inactive_sessions logic can happen in one place. Simplifies everything and concludes this series of BOSH fixes.
Matthew Wild <mwild1@gmail.com>
parents: 3041
diff changeset
   186
			log("debug", "Have nothing to say, so leaving request unanswered for now");
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
		end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
   188
4223
9fb6e8ec15ed mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   189
		if session.bosh_terminate then
9fb6e8ec15ed mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   190
			session.log("debug", "Closing session with %d requests open", #session.requests);
9fb6e8ec15ed mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   191
			session:close();
9fb6e8ec15ed mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   192
			return nil;
9fb6e8ec15ed mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   193
		else
7659
296543556065 mod_bosh: Update BOSH wait timeout logic to work despite the addition of deferred requests
Matthew Wild <mwild1@gmail.com>
parents: 7658
diff changeset
   194
			if session.bosh_wait and #session.requests > 0 then
296543556065 mod_bosh: Update BOSH wait timeout logic to work despite the addition of deferred requests
Matthew Wild <mwild1@gmail.com>
parents: 7658
diff changeset
   195
				session.bosh_wait_timer = module:add_timer(session.bosh_wait, after_bosh_wait, session.requests[1], session)
296543556065 mod_bosh: Update BOSH wait timeout logic to work despite the addition of deferred requests
Matthew Wild <mwild1@gmail.com>
parents: 7658
diff changeset
   196
			end
296543556065 mod_bosh: Update BOSH wait timeout logic to work despite the addition of deferred requests
Matthew Wild <mwild1@gmail.com>
parents: 7658
diff changeset
   197
4738
e95458712782 mod_bosh: Remove unused import of net.httpserver
Matthew Wild <mwild1@gmail.com>
parents: 4725
diff changeset
   198
			return true; -- Inform http server we shall reply later
4223
9fb6e8ec15ed mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   199
		end
8768
d5ff0982d3e6 mod_bosh: Fix for 7be8f649d97d to skip error handling and allow other modules to handle the request
Matthew Wild <mwild1@gmail.com>
parents: 8755
diff changeset
   200
	elseif response.finished or context.ignore_request then
8921
f69b3e39e0c1 mod_bosh: Add extra debug logging to help with #1134
Kim Alvefur <zash@zash.se>
parents: 8846
diff changeset
   201
		if response.finished then
f69b3e39e0c1 mod_bosh: Add extra debug logging to help with #1134
Kim Alvefur <zash@zash.se>
parents: 8846
diff changeset
   202
			module:log("debug", "Response finished");
f69b3e39e0c1 mod_bosh: Add extra debug logging to help with #1134
Kim Alvefur <zash@zash.se>
parents: 8846
diff changeset
   203
		end
f69b3e39e0c1 mod_bosh: Add extra debug logging to help with #1134
Kim Alvefur <zash@zash.se>
parents: 8846
diff changeset
   204
		if context.ignore_request then
f69b3e39e0c1 mod_bosh: Add extra debug logging to help with #1134
Kim Alvefur <zash@zash.se>
parents: 8846
diff changeset
   205
			module:log("debug", "Ignoring this request");
f69b3e39e0c1 mod_bosh: Add extra debug logging to help with #1134
Kim Alvefur <zash@zash.se>
parents: 8846
diff changeset
   206
		end
8770
7738838a013d mod_bosh: Fix inconsistent whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8768
diff changeset
   207
		-- A response has been sent already, or we're ignoring this request
7738838a013d mod_bosh: Fix inconsistent whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8768
diff changeset
   208
		-- (e.g. so a different instance of the module can handle it)
8768
d5ff0982d3e6 mod_bosh: Fix for 7be8f649d97d to skip error handling and allow other modules to handle the request
Matthew Wild <mwild1@gmail.com>
parents: 8755
diff changeset
   209
		return;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   210
	end
5647
8767b47524c9 mod_bosh: Return errors when appropriate (invalid XML, missing sid)
Matthew Wild <mwild1@gmail.com>
parents: 5646
diff changeset
   211
	module:log("warn", "Unable to associate request with a session (incomplete request?)");
9994
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
   212
	report_bad_sid();
7380
6c98e783272a mod_bosh: Return a proper BOSH error response instead of deprecated(?) status code (See #343)
Kim Alvefur <zash@zash.se>
parents: 7379
diff changeset
   213
	local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
6c98e783272a mod_bosh: Return a proper BOSH error response instead of deprecated(?) status code (See #343)
Kim Alvefur <zash@zash.se>
parents: 7379
diff changeset
   214
		["xmlns:stream"] = xmlns_streams, condition = "item-not-found" });
6c98e783272a mod_bosh: Return a proper BOSH error response instead of deprecated(?) status code (See #343)
Kim Alvefur <zash@zash.se>
parents: 7379
diff changeset
   215
	return tostring(close_reply) .. "\n";
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   216
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   217
7394
c381106173d0 mod_bosh: Add annotations to ignore unused arguments [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7393
diff changeset
   218
function after_bosh_wait(now, request, session) -- luacheck: ignore 212
6992
118858bf47cd mod_bosh: Instead of a global once-per-second timer add a timer for each session when needed
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
   219
	if request.conn then
118858bf47cd mod_bosh: Instead of a global once-per-second timer add a timer for each session when needed
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
   220
		session.send("");
118858bf47cd mod_bosh: Instead of a global once-per-second timer add a timer for each session when needed
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
   221
	end
118858bf47cd mod_bosh: Instead of a global once-per-second timer add a timer for each session when needed
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
   222
end
684
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   223
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
local function bosh_reset_stream(session) session.notopen = true; end
684
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   225
3448
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   226
local stream_xmlns_attr = { xmlns = "urn:ietf:params:xml:ns:xmpp-streams" };
684
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   227
local function bosh_close_stream(session, reason)
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
   228
	(session.log or log)("info", "BOSH client disconnected: %s", (reason and reason.condition or reason) or "session close");
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
   229
3448
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   230
	local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
4379
e4d88f4a780c mod_bosh: s/xmlns:streams/xmlns:stream/ - fixes #265 (thanks Tim)
Matthew Wild <mwild1@gmail.com>
parents: 4332
diff changeset
   231
		["xmlns:stream"] = xmlns_streams });
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
   232
3448
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   233
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   234
	if reason then
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   235
		close_reply.attr.condition = "remote-stream-error";
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   236
		if type(reason) == "string" then -- assume stream error
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   237
			close_reply:tag("stream:error")
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   238
				:tag(reason, {xmlns = xmlns_xmpp_streams});
11872
ae093c259da2 mod_c2s,etc: Identify stanza object with appropriate function
Kim Alvefur <zash@zash.se>
parents: 11812
diff changeset
   239
		elseif st.is_stanza(reason) then
ae093c259da2 mod_c2s,etc: Identify stanza object with appropriate function
Kim Alvefur <zash@zash.se>
parents: 11812
diff changeset
   240
			close_reply = reason;
3448
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   241
		elseif type(reason) == "table" then
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   242
			if reason.condition then
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   243
				close_reply:tag("stream:error")
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   244
					:tag(reason.condition, stream_xmlns_attr):up();
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   245
				if reason.text then
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   246
					close_reply:tag("text", stream_xmlns_attr):text(reason.text):up();
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   247
				end
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   248
				if reason.extra then
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   249
					close_reply:add_child(reason.extra);
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   250
				end
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   251
			end
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   252
		end
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
   253
		log("info", "Disconnecting client, <stream:error> is: %s", close_reply);
3448
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   254
	end
0ca7c3864431 mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
Matthew Wild <mwild1@gmail.com>
parents: 3447
diff changeset
   255
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   256
	local response_body = tostring(close_reply);
684
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   257
	for _, held_request in ipairs(session.requests) do
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   258
		held_request:send(response_body);
684
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   259
	end
5634
7298c9bbb30f mod_bosh: Some very minor whitespace/layout fixes
Matthew Wild <mwild1@gmail.com>
parents: 5188
diff changeset
   260
	sessions[session.sid] = nil;
684
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   261
	sm_destroy_session(session);
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   262
end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   263
6531
f0687c313cf1 mod_bosh: Use util.async
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   264
local runner_callbacks = { };
f0687c313cf1 mod_bosh: Use util.async
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   265
4438
7f51186ed28b mod_bosh: Move stream:features sending until after the current request has been added to session.requests. Ensures correct inactivity logic.
Matthew Wild <mwild1@gmail.com>
parents: 4437
diff changeset
   266
-- Handle the <body> tag in the request payload.
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   267
function stream_callbacks.streamopened(context, attr)
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   268
	local request, response = context.request, context.response;
8750
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   269
	local sid, rid = attr.sid, tonumber(attr.rid);
4327
98ae0d0b4d07 mod_bosh: Fix logging when no sid present, fix a missing semi-colon, avoid an extra useless table lookup (thanks Thomas)
Matthew Wild <mwild1@gmail.com>
parents: 4316
diff changeset
   270
	log("debug", "BOSH body open (sid: %s)", sid or "<none>");
8750
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   271
	context.rid = rid;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   272
	if not sid then
679
9506bf204b1a Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents: 660
diff changeset
   273
		-- New session request
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   274
		context.notopen = nil; -- Signals that we accept this opening tag
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
   275
10381
4c36bc28b99e mod_bosh: Abort early if request is missing hostname
Kim Alvefur <zash@zash.se>
parents: 10115
diff changeset
   276
		if not attr.to then
4c36bc28b99e mod_bosh: Abort early if request is missing hostname
Kim Alvefur <zash@zash.se>
parents: 10115
diff changeset
   277
			log("debug", "BOSH client tried to connect without specifying a host");
4c36bc28b99e mod_bosh: Abort early if request is missing hostname
Kim Alvefur <zash@zash.se>
parents: 10115
diff changeset
   278
			report_bad_host();
4c36bc28b99e mod_bosh: Abort early if request is missing hostname
Kim Alvefur <zash@zash.se>
parents: 10115
diff changeset
   279
			local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
4c36bc28b99e mod_bosh: Abort early if request is missing hostname
Kim Alvefur <zash@zash.se>
parents: 10115
diff changeset
   280
				["xmlns:stream"] = xmlns_streams, condition = "improper-addressing" });
4c36bc28b99e mod_bosh: Abort early if request is missing hostname
Kim Alvefur <zash@zash.se>
parents: 10115
diff changeset
   281
			response:send(tostring(close_reply));
4c36bc28b99e mod_bosh: Abort early if request is missing hostname
Kim Alvefur <zash@zash.se>
parents: 10115
diff changeset
   282
			return;
4c36bc28b99e mod_bosh: Abort early if request is missing hostname
Kim Alvefur <zash@zash.se>
parents: 10115
diff changeset
   283
		end
4c36bc28b99e mod_bosh: Abort early if request is missing hostname
Kim Alvefur <zash@zash.se>
parents: 10115
diff changeset
   284
7381
d15cfe8627ad mod_bosh: Validate 'to' host (see #343)
Kim Alvefur <zash@zash.se>
parents: 7380
diff changeset
   285
		local to_host = nameprep(attr.to);
d15cfe8627ad mod_bosh: Validate 'to' host (see #343)
Kim Alvefur <zash@zash.se>
parents: 7380
diff changeset
   286
		if not to_host then
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
   287
			log("debug", "BOSH client tried to connect to invalid host: %s", attr.to);
9994
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
   288
			report_bad_host();
7381
d15cfe8627ad mod_bosh: Validate 'to' host (see #343)
Kim Alvefur <zash@zash.se>
parents: 7380
diff changeset
   289
			local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
d15cfe8627ad mod_bosh: Validate 'to' host (see #343)
Kim Alvefur <zash@zash.se>
parents: 7380
diff changeset
   290
				["xmlns:stream"] = xmlns_streams, condition = "improper-addressing" });
d15cfe8627ad mod_bosh: Validate 'to' host (see #343)
Kim Alvefur <zash@zash.se>
parents: 7380
diff changeset
   291
			response:send(tostring(close_reply));
d15cfe8627ad mod_bosh: Validate 'to' host (see #343)
Kim Alvefur <zash@zash.se>
parents: 7380
diff changeset
   292
			return;
684
b7d85c6a0002 Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents: 683
diff changeset
   293
		end
11127
0f4260f99ea2 mod_bosh: Pick out the 'wait' before checking it instead of earlier
Kim Alvefur <zash@zash.se>
parents: 9781
diff changeset
   294
11128
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   295
		if not prosody.hosts[to_host] then
11731
f3aee8a825cc Fix various spelling errors (thanks codespell)
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   296
			log("debug", "BOSH client tried to connect to non-existent host: %s", attr.to);
11130
cc6b1dab01a2 mod_bosh: Count connection attempts non-VirtualHost as "bad host" (stats)
Kim Alvefur <zash@zash.se>
parents: 11129
diff changeset
   297
			report_bad_host();
11128
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   298
			local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   299
				["xmlns:stream"] = xmlns_streams, condition = "improper-addressing" });
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   300
			response:send(tostring(close_reply));
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   301
			return;
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   302
		end
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   303
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   304
		if prosody.hosts[to_host].type ~= "local" then
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   305
			log("debug", "BOSH client tried to connect to %s host: %s", prosody.hosts[to_host].type, attr.to);
11130
cc6b1dab01a2 mod_bosh: Count connection attempts non-VirtualHost as "bad host" (stats)
Kim Alvefur <zash@zash.se>
parents: 11129
diff changeset
   306
			report_bad_host();
11128
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   307
			local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   308
				["xmlns:stream"] = xmlns_streams, condition = "improper-addressing" });
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   309
			response:send(tostring(close_reply));
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   310
			return;
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   311
		end
1aea75b63d0a mod_bosh: Ensure that stream is directed to a VirtualHost (fixes #425)
Kim Alvefur <zash@zash.se>
parents: 11127
diff changeset
   312
11127
0f4260f99ea2 mod_bosh: Pick out the 'wait' before checking it instead of earlier
Kim Alvefur <zash@zash.se>
parents: 9781
diff changeset
   313
		local wait = tonumber(attr.wait);
9781
2e07d2f71599 mod_bosh: Handle missing wait attribute (fixes #1288)
Kim Alvefur <zash@zash.se>
parents: 9383
diff changeset
   314
		if not rid or (not attr.wait or not wait or wait < 0 or wait % 1 ~= 0) then
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
   315
			log("debug", "BOSH client sent invalid rid or wait attributes: rid=%s, wait=%s", attr.rid, attr.wait);
7382
250855633092 mod_bosh: Validate that 'sid' and 'wait' have sane values (fixes #475, also see #343)
Kim Alvefur <zash@zash.se>
parents: 7381
diff changeset
   316
			local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
250855633092 mod_bosh: Validate that 'sid' and 'wait' have sane values (fixes #475, also see #343)
Kim Alvefur <zash@zash.se>
parents: 7381
diff changeset
   317
				["xmlns:stream"] = xmlns_streams, condition = "bad-request" });
250855633092 mod_bosh: Validate that 'sid' and 'wait' have sane values (fixes #475, also see #343)
Kim Alvefur <zash@zash.se>
parents: 7381
diff changeset
   318
			response:send(tostring(close_reply));
250855633092 mod_bosh: Validate that 'sid' and 'wait' have sane values (fixes #475, also see #343)
Kim Alvefur <zash@zash.se>
parents: 7381
diff changeset
   319
			return;
250855633092 mod_bosh: Validate that 'sid' and 'wait' have sane values (fixes #475, also see #343)
Kim Alvefur <zash@zash.se>
parents: 7381
diff changeset
   320
		end
250855633092 mod_bosh: Validate that 'sid' and 'wait' have sane values (fixes #475, also see #343)
Kim Alvefur <zash@zash.se>
parents: 7381
diff changeset
   321
250855633092 mod_bosh: Validate that 'sid' and 'wait' have sane values (fixes #475, also see #343)
Kim Alvefur <zash@zash.se>
parents: 7381
diff changeset
   322
		wait = math_min(wait, bosh_max_wait);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
   323
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   324
		-- New session
763
8e77a39826c2 mod_bosh: No need to tostring() uuids now
Matthew Wild <mwild1@gmail.com>
parents: 725
diff changeset
   325
		sid = new_uuid();
11812
c24580a214f3 mod_bosh: Fix typo in comment
Kim Alvefur <zash@zash.se>
parents: 11775
diff changeset
   326
		-- TODO use util.session
3071
39a870ae75d9 mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents: 3070
diff changeset
   327
		local session = {
13464
a688947fab1e mod_bosh: Set base_type on session
Matthew Wild <mwild1@gmail.com>
parents: 13297
diff changeset
   328
			base_type = "c2s", type = "c2s_unauthed", conn = request.conn, sid = sid, host = attr.to,
8750
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   329
			rid = rid - 1, -- Hack for initial session setup, "previous" rid was $current_request - 1
7382
250855633092 mod_bosh: Validate that 'sid' and 'wait' have sane values (fixes #475, also see #343)
Kim Alvefur <zash@zash.se>
parents: 7381
diff changeset
   330
			bosh_version = attr.ver, bosh_wait = wait, streamid = sid,
8755
8f2da579a790 mod_bosh: Increase number of stored responses to ensure we always keep responses within the rid window available
Matthew Wild <mwild1@gmail.com>
parents: 8750
diff changeset
   331
			bosh_max_inactive = bosh_max_inactivity, bosh_responses = cache.new(BOSH_HOLD+1):table();
3071
39a870ae75d9 mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents: 3070
diff changeset
   332
			requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream,
5071
0382f456ac82 mod_bosh: Remove redundant code (send stream features in only one place) (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents: 5070
diff changeset
   333
			close = bosh_close_stream, dispatch_stanza = core_process_stanza, notopen = true,
3472
61cf3e7d7f07 mod_bosh: Support for reading the client's real IP through HTTP proxies from X-Forwarded-For
Matthew Wild <mwild1@gmail.com>
parents: 3460
diff changeset
   334
			log = logger.init("bosh"..sid),	secure = consider_bosh_secure or request.secure,
8597
b4a0bc46c82d mod_http: Set request.ip on all HTTP requests (moves code out of mod_bosh) (fixes #540)
Kim Alvefur <zash@zash.se>
parents: 8528
diff changeset
   335
			ip = request.ip;
3071
39a870ae75d9 mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents: 3070
diff changeset
   336
		};
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   337
		sessions[sid] = session;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
   338
6531
f0687c313cf1 mod_bosh: Use util.async
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   339
		session.thread = runner(function (stanza)
f0687c313cf1 mod_bosh: Use util.async
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   340
			session:dispatch_stanza(stanza);
f0687c313cf1 mod_bosh: Use util.async
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   341
		end, runner_callbacks, session);
f0687c313cf1 mod_bosh: Use util.async
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   342
5187
d71f731e8fe4 mod_bosh: Add support for stanza filters to BOSH sessions (needed by some plugins)
Matthew Wild <mwild1@gmail.com>
parents: 5185
diff changeset
   343
		local filter = initialize_filters(session);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
   344
3472
61cf3e7d7f07 mod_bosh: Support for reading the client's real IP through HTTP proxies from X-Forwarded-For
Matthew Wild <mwild1@gmail.com>
parents: 3460
diff changeset
   345
		session.log("debug", "BOSH session created for request from %s", session.ip);
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   346
		log("info", "New BOSH session, assigned it sid '%s'", sid);
9994
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
   347
		report_new_sid();
5046
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   348
8496
d424fe42b4d2 mod_bosh: Use module API to fire events
Kim Alvefur <zash@zash.se>
parents: 8495
diff changeset
   349
		module:fire_event("bosh-session", { session = session, request = request });
7050
9ca2b720ad43 mod_bosh: Fire event when BOSH session is created
Matthew Wild <mwild1@gmail.com>
parents: 6531
diff changeset
   350
5046
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   351
		-- Send creation response
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   352
		local creating_session = true;
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   353
4769
c91bb217bf79 mod_bosh: Remove unused send_buffer variable
Matthew Wild <mwild1@gmail.com>
parents: 4768
diff changeset
   354
		local r = session.requests;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   355
		function session.send(s)
3322
c4e107e7c883 mod_bosh: Add jabber:client namespace to stanzas with no namespace
Matthew Wild <mwild1@gmail.com>
parents: 3071
diff changeset
   356
			-- We need to ensure that outgoing stanzas have the jabber:client xmlns
c4e107e7c883 mod_bosh: Add jabber:client namespace to stanzas with no namespace
Matthew Wild <mwild1@gmail.com>
parents: 3071
diff changeset
   357
			if s.attr and not s.attr.xmlns then
c4e107e7c883 mod_bosh: Add jabber:client namespace to stanzas with no namespace
Matthew Wild <mwild1@gmail.com>
parents: 3071
diff changeset
   358
				s = st.clone(s);
c4e107e7c883 mod_bosh: Add jabber:client namespace to stanzas with no namespace
Matthew Wild <mwild1@gmail.com>
parents: 3071
diff changeset
   359
				s.attr.xmlns = "jabber:client";
c4e107e7c883 mod_bosh: Add jabber:client namespace to stanzas with no namespace
Matthew Wild <mwild1@gmail.com>
parents: 3071
diff changeset
   360
			end
5187
d71f731e8fe4 mod_bosh: Add support for stanza filters to BOSH sessions (needed by some plugins)
Matthew Wild <mwild1@gmail.com>
parents: 5185
diff changeset
   361
			s = filter("stanzas/out", s);
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
   362
			--log("debug", "Sending BOSH data: %s", s);
7329
d11701e86702 mod_bosh: Skip sending stanzas removed out by filters (fixes #657)
Kim Alvefur <zash@zash.se>
parents: 7286
diff changeset
   363
			if not s then return true end
5046
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   364
			t_insert(session.send_buffer, tostring(s));
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   365
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   366
			local oldest_request = r[1];
5046
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   367
			if oldest_request and not session.bosh_processing then
2099
73e083d01449 mod_bosh: Don't log response XML
Matthew Wild <mwild1@gmail.com>
parents: 2084
diff changeset
   368
				log("debug", "We have an open request, so sending on that");
5046
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   369
				local body_attr = { xmlns = "http://jabber.org/protocol/httpbind",
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   370
					["xmlns:stream"] = "http://etherx.jabber.org/streams";
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   371
					type = session.bosh_terminate and "terminate" or nil;
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   372
					sid = sid;
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   373
				};
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   374
				if creating_session then
5644
f9cfe6f5d60f mod_bosh: Reset creating_session to prevent putting unnecessary attributes into every BOSH response
Matthew Wild <mwild1@gmail.com>
parents: 5639
diff changeset
   375
					creating_session = nil;
7656
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
   376
					body_attr.requests = tostring(BOSH_MAX_REQUESTS);
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
   377
					body_attr.hold = tostring(BOSH_HOLD);
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
   378
					body_attr.inactivity = tostring(bosh_max_inactivity);
17e42f793341 mod_bosh: Make 'hold' and 'requests' fixed to '1' and '2' respectively, as this is what all implementations realistically use
Matthew Wild <mwild1@gmail.com>
parents: 7655
diff changeset
   379
					body_attr.polling = tostring(bosh_max_polling);
5185
ca30b21946ef mod_bosh: Add bosh_max_wait config option, to limit the amount of time a client can request for the server to hold open requests
Matthew Wild <mwild1@gmail.com>
parents: 5179
diff changeset
   380
					body_attr.wait = tostring(session.bosh_wait);
5046
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   381
					body_attr.authid = sid;
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   382
					body_attr.secure = "true";
5670
9218a0e81a41 mod_bosh: Fix global write
Matthew Wild <mwild1@gmail.com>
parents: 5650
diff changeset
   383
					body_attr.ver  = '1.6';
9218a0e81a41 mod_bosh: Fix global write
Matthew Wild <mwild1@gmail.com>
parents: 5650
diff changeset
   384
					body_attr.from = session.host;
5046
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   385
					body_attr["xmlns:xmpp"] = "urn:xmpp:xbosh";
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   386
					body_attr["xmpp:version"] = "1.0";
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   387
				end
8750
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   388
				local response_xml = st.stanza("body", body_attr):top_tag()..t_concat(session.send_buffer).."</body>";
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   389
				session.bosh_responses[oldest_request.context.rid] = response_xml;
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   390
				oldest_request:send(response_xml);
5046
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   391
				session.send_buffer = {};
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   392
			end
4102
9df4e61c260b mod_bosh: Return true from send()
Matthew Wild <mwild1@gmail.com>
parents: 4000
diff changeset
   393
			return true;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   394
		end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   395
		request.sid = sid;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   396
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
   397
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   398
	local session = sessions[sid];
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   399
	if not session then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   400
		-- Unknown sid
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   401
		log("info", "Client tried to use sid '%s' which we don't know about", sid);
9994
f122972b77b2 mod_bosh: Added metrics for active/inactive sessions, new BOSH sessions, BOSH errors, and timeouts (finishes #998)
Arc Riley <arcriley@gmail.com>
parents: 9803
diff changeset
   402
		report_bad_sid();
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   403
		response:send(tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" })));
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   404
		context.notopen = nil;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   405
		return;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   406
	end
8096
8d1fd6d34bda mod_bosh: Update session.conn to point to the current connection (fixes #890)
Kim Alvefur <zash@zash.se>
parents: 5727
diff changeset
   407
8d1fd6d34bda mod_bosh: Update session.conn to point to the current connection (fixes #890)
Kim Alvefur <zash@zash.se>
parents: 5727
diff changeset
   408
	session.conn = request.conn;
8100
e24263db3380 Merge 0.9->0.10
Kim Alvefur <zash@zash.se>
parents: 7662 8096
diff changeset
   409
1663
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   410
	if session.rid then
1664
6587b6c2678e mod_bosh: Calculate rid difference just once
Matthew Wild <mwild1@gmail.com>
parents: 1663
diff changeset
   411
		local diff = rid - session.rid;
7654
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   412
		-- Diff should be 1 for a healthy request
8750
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   413
		session.log("debug", "rid: %d, sess: %s, diff: %d", rid, session.rid, diff)
7654
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   414
		if diff ~= 1 then
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   415
			context.sid = sid;
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   416
			context.notopen = nil;
8749
df1ca586c68d mod_bosh: Some additonal comments to improve code readability
Matthew Wild <mwild1@gmail.com>
parents: 8748
diff changeset
   417
			if diff == 2 then -- Missed a request
7654
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   418
				-- Hold request, but don't process it (ouch!)
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   419
				session.log("debug", "rid skipped: %d, deferring this request", rid-1)
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   420
				context.defer = true;
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   421
				session.bosh_deferred = { context = context, sid = sid, rid = rid, terminate = attr.type == "terminate" };
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   422
				return;
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   423
			end
8749
df1ca586c68d mod_bosh: Some additonal comments to improve code readability
Matthew Wild <mwild1@gmail.com>
parents: 8748
diff changeset
   424
			-- Set a marker to indicate that stanzas in this request should NOT be processed
df1ca586c68d mod_bosh: Some additonal comments to improve code readability
Matthew Wild <mwild1@gmail.com>
parents: 8748
diff changeset
   425
			-- (these stanzas will already be in the XML parser's buffer)
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   426
			context.ignore = true;
8750
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   427
			if session.bosh_responses[rid] then
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   428
				-- Re-send past response, ignore stanzas in this request
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   429
				session.log("debug", "rid repeated within window, replaying old response");
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   430
				response:send(session.bosh_responses[rid]);
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   431
				return;
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   432
			elseif diff == 0 then
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   433
				session.log("debug", "current rid repeated, ignoring stanzas");
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   434
				t_insert(session.requests, response);
f91d45a692f0 mod_bosh: Improve connection robustness with better handling of unexpected rids
Matthew Wild <mwild1@gmail.com>
parents: 8749
diff changeset
   435
				context.sid = sid;
7654
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   436
				return;
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   437
			end
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   438
			-- Session broken, destroy it
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   439
			session.log("debug", "rid out of range: %d (diff %d)", rid, diff);
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   440
			response:send(tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" })));
1663
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   441
			return;
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   442
		end
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   443
		session.rid = rid;
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   444
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
   445
4223
9fb6e8ec15ed mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   446
	if attr.type == "terminate" then
9fb6e8ec15ed mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   447
		-- Client wants to end this session, which we'll do
9fb6e8ec15ed mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   448
		-- after processing any stanzas in this request
9fb6e8ec15ed mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   449
		session.bosh_terminate = true;
9fb6e8ec15ed mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   450
	end
9fb6e8ec15ed mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents: 4102
diff changeset
   451
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   452
	context.notopen = nil; -- Signals that we accept this opening tag
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   453
	t_insert(session.requests, response);
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   454
	context.sid = sid;
5046
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   455
	session.bosh_processing = true; -- Used to suppress replies until processing of this request is done
4438
7f51186ed28b mod_bosh: Move stream:features sending until after the current request has been added to session.requests. Ensures correct inactivity logic.
Matthew Wild <mwild1@gmail.com>
parents: 4437
diff changeset
   456
7f51186ed28b mod_bosh: Move stream:features sending until after the current request has been added to session.requests. Ensures correct inactivity logic.
Matthew Wild <mwild1@gmail.com>
parents: 4437
diff changeset
   457
	if session.notopen then
7f51186ed28b mod_bosh: Move stream:features sending until after the current request has been added to session.requests. Ensures correct inactivity logic.
Matthew Wild <mwild1@gmail.com>
parents: 4437
diff changeset
   458
		local features = st.stanza("stream:features");
13297
ad1ed84fdf13 mod_bosh: Include stream attributes in stream-features event
Matthew Wild <mwild1@gmail.com>
parents: 13217
diff changeset
   459
		module:context(session.host):fire_event("stream-features", { origin = session, features = features, stream = attr });
5658
97c1c1bdd7bc mod_bosh: Don't tostring() stream:features when passing to session.send().
Waqas Hussain <waqas20@gmail.com>
parents: 5654
diff changeset
   460
		session.send(features);
4438
7f51186ed28b mod_bosh: Move stream:features sending until after the current request has been added to session.requests. Ensures correct inactivity logic.
Matthew Wild <mwild1@gmail.com>
parents: 4437
diff changeset
   461
		session.notopen = nil;
7f51186ed28b mod_bosh: Move stream:features sending until after the current request has been added to session.requests. Ensures correct inactivity logic.
Matthew Wild <mwild1@gmail.com>
parents: 4437
diff changeset
   462
	end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   463
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   464
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
   465
local function handleerr(err) log("error", "Traceback[bosh]: %s", traceback(err, 2)); end
6531
f0687c313cf1 mod_bosh: Use util.async
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   466
7394
c381106173d0 mod_bosh: Add annotations to ignore unused arguments [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7393
diff changeset
   467
function runner_callbacks:error(err) -- luacheck: ignore 212/self
6531
f0687c313cf1 mod_bosh: Use util.async
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   468
	return handleerr(err);
f0687c313cf1 mod_bosh: Use util.async
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   469
end
f0687c313cf1 mod_bosh: Use util.async
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   470
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   471
function stream_callbacks.handlestanza(context, stanza)
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   472
	if context.ignore then return; end
725
96110075288b Replacing pretty_print() with top_tag() for logging
Matthew Wild <mwild1@gmail.com>
parents: 701
diff changeset
   473
	log("debug", "BOSH stanza received: %s\n", stanza:top_tag());
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   474
	local session = sessions[context.sid];
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   475
	if session then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   476
		if stanza.attr.xmlns == xmlns_bosh then
2959
62a3f824292a mod_bosh: Default stanza namespace should be jabber:client (fixes BOSH to work with recent namespace fix)
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
   477
			stanza.attr.xmlns = nil;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   478
		end
7654
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   479
		if context.defer and session.bosh_deferred then
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   480
			log("debug", "Deferring this stanza");
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   481
			t_insert(session.bosh_deferred, stanza);
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   482
		else
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   483
			stanza = session.filter("stanzas/in", stanza);
7657
b40776ee2aef Merge 0.10->trunk
Matthew Wild <mwild1@gmail.com>
parents: 7509 7656
diff changeset
   484
			session.thread:run(stanza);
5726
3bccc68a38e3 mod_bosh: Only pass stanza to core_process_stanza if it wasn't dropped by filters
Matthew Wild <mwild1@gmail.com>
parents: 5671
diff changeset
   485
		end
7655
7cc3d6c764ce mod_bosh: Log when a stanza isn't handled because we can't find a session for it
Matthew Wild <mwild1@gmail.com>
parents: 7654
diff changeset
   486
	else
7cc3d6c764ce mod_bosh: Log when a stanza isn't handled because we can't find a session for it
Matthew Wild <mwild1@gmail.com>
parents: 7654
diff changeset
   487
		log("debug", "No session for this stanza! (sid: %s)", context.sid or "none!");
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   488
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   489
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   490
5635
84d3d7b69f9a mod_bosh: rename variable for clarity
Matthew Wild <mwild1@gmail.com>
parents: 5634
diff changeset
   491
function stream_callbacks.streamclosed(context)
84d3d7b69f9a mod_bosh: rename variable for clarity
Matthew Wild <mwild1@gmail.com>
parents: 5634
diff changeset
   492
	local session = sessions[context.sid];
5046
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   493
	if session then
7654
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   494
		if not context.defer and session.bosh_deferred then
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   495
			-- Handle deferred stanzas now
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   496
			local deferred_stanzas = session.bosh_deferred;
8494
f134be08a499 mod_bosh: Rename variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8493
diff changeset
   497
			local deferred_context = deferred_stanzas.context;
7654
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   498
			session.bosh_deferred = nil;
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   499
			log("debug", "Handling deferred stanzas from rid %d", deferred_stanzas.rid);
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   500
			session.rid = deferred_stanzas.rid;
8494
f134be08a499 mod_bosh: Rename variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8493
diff changeset
   501
			t_insert(session.requests, deferred_context.response);
7654
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   502
			for _, stanza in ipairs(deferred_stanzas) do
8494
f134be08a499 mod_bosh: Rename variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8493
diff changeset
   503
				stream_callbacks.handlestanza(deferred_context, stanza);
7654
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   504
			end
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   505
			if deferred_stanzas.terminate then
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   506
				session.bosh_terminate = true;
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   507
			end
55f11a6806bc mod_bosh: Correctly handle requests arriving out of order (thanks Jitsi folk!)
Matthew Wild <mwild1@gmail.com>
parents: 7390
diff changeset
   508
		end
5046
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   509
		session.bosh_processing = false;
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   510
		if #session.send_buffer > 0 then
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   511
			session.send("");
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   512
		end
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   513
	end
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   514
end
16c7b510694b mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents: 5031
diff changeset
   515
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   516
function stream_callbacks.error(context, error)
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   517
	if not context.sid then
8747
0f4a4d8ac3dd mod_bosh: Improve logging - parse errors will now log through the session logger if possible
Matthew Wild <mwild1@gmail.com>
parents: 8746
diff changeset
   518
		log("debug", "Error parsing BOSH request payload; %s", error);
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   519
		local response = context.response;
7383
d24d88feed76 mod_bosh: Return a proper BOSH error response from XML parse error callback (see #343)
Kim Alvefur <zash@zash.se>
parents: 7382
diff changeset
   520
		local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
d24d88feed76 mod_bosh: Return a proper BOSH error response from XML parse error callback (see #343)
Kim Alvefur <zash@zash.se>
parents: 7382
diff changeset
   521
			["xmlns:stream"] = xmlns_streams, condition = "bad-request" });
d24d88feed76 mod_bosh: Return a proper BOSH error response from XML parse error callback (see #343)
Kim Alvefur <zash@zash.se>
parents: 7382
diff changeset
   522
		response:send(tostring(close_reply));
3447
cc2dc55e66f9 mod_bosh: Add error callback for xmlhandlers, to handle the case of invalid or unusable XML in the request payload
Matthew Wild <mwild1@gmail.com>
parents: 3439
diff changeset
   523
		return;
cc2dc55e66f9 mod_bosh: Add error callback for xmlhandlers, to handle the case of invalid or unusable XML in the request payload
Matthew Wild <mwild1@gmail.com>
parents: 3439
diff changeset
   524
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5748
diff changeset
   525
4692
8e7c683d78ca mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents: 4690
diff changeset
   526
	local session = sessions[context.sid];
8747
0f4a4d8ac3dd mod_bosh: Improve logging - parse errors will now log through the session logger if possible
Matthew Wild <mwild1@gmail.com>
parents: 8746
diff changeset
   527
	(session and session.log or log)("warn", "Error parsing BOSH request payload; %s", error);
3447
cc2dc55e66f9 mod_bosh: Add error callback for xmlhandlers, to handle the case of invalid or unusable XML in the request payload
Matthew Wild <mwild1@gmail.com>
parents: 3439
diff changeset
   528
	if error == "stream-error" then -- Remote stream error, we close normally
cc2dc55e66f9 mod_bosh: Add error callback for xmlhandlers, to handle the case of invalid or unusable XML in the request payload
Matthew Wild <mwild1@gmail.com>
parents: 3439
diff changeset
   529
		session:close();
cc2dc55e66f9 mod_bosh: Add error callback for xmlhandlers, to handle the case of invalid or unusable XML in the request payload
Matthew Wild <mwild1@gmail.com>
parents: 3439
diff changeset
   530
	else
cc2dc55e66f9 mod_bosh: Add error callback for xmlhandlers, to handle the case of invalid or unusable XML in the request payload
Matthew Wild <mwild1@gmail.com>
parents: 3439
diff changeset
   531
		session:close({ condition = "bad-format", text = "Error processing stream" });
cc2dc55e66f9 mod_bosh: Add error callback for xmlhandlers, to handle the case of invalid or unusable XML in the request payload
Matthew Wild <mwild1@gmail.com>
parents: 3439
diff changeset
   532
	end
cc2dc55e66f9 mod_bosh: Add error callback for xmlhandlers, to handle the case of invalid or unusable XML in the request payload
Matthew Wild <mwild1@gmail.com>
parents: 3439
diff changeset
   533
end
cc2dc55e66f9 mod_bosh: Add error callback for xmlhandlers, to handle the case of invalid or unusable XML in the request payload
Matthew Wild <mwild1@gmail.com>
parents: 3439
diff changeset
   534
11395
8eff5c744395 mod_bosh: Use message template from mod_http_error
Kim Alvefur <zash@zash.se>
parents: 11130
diff changeset
   535
local function GET_response(event)
8eff5c744395 mod_bosh: Use message template from mod_http_error
Kim Alvefur <zash@zash.se>
parents: 11130
diff changeset
   536
	return module:fire_event("http-message", {
8eff5c744395 mod_bosh: Use message template from mod_http_error
Kim Alvefur <zash@zash.se>
parents: 11130
diff changeset
   537
		response = event.response;
8eff5c744395 mod_bosh: Use message template from mod_http_error
Kim Alvefur <zash@zash.se>
parents: 11130
diff changeset
   538
		---
8eff5c744395 mod_bosh: Use message template from mod_http_error
Kim Alvefur <zash@zash.se>
parents: 11130
diff changeset
   539
		title = "Prosody BOSH endpoint";
8eff5c744395 mod_bosh: Use message template from mod_http_error
Kim Alvefur <zash@zash.se>
parents: 11130
diff changeset
   540
		message = "It works! Now point your BOSH client to this URL to connect to Prosody.";
11396
a76493b75dec mod_bosh: Include warning if endpoint accessed insecurely (#1172)
Kim Alvefur <zash@zash.se>
parents: 11395
diff changeset
   541
		warning = not (consider_bosh_secure or event.request.secure) and "This endpoint is not considered secure!" or nil;
11395
8eff5c744395 mod_bosh: Use message template from mod_http_error
Kim Alvefur <zash@zash.se>
parents: 11130
diff changeset
   542
		-- <p>For more information see <a href="https://prosody.im/doc/setting_up_bosh">Prosody: Setting up BOSH</a>.</p>
8eff5c744395 mod_bosh: Use message template from mod_http_error
Kim Alvefur <zash@zash.se>
parents: 11130
diff changeset
   543
	}) or "This is the Prosody BOSH endpoint.";
8eff5c744395 mod_bosh: Use message template from mod_http_error
Kim Alvefur <zash@zash.se>
parents: 11130
diff changeset
   544
end
4880
6d96e2e717c1 mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents: 4769
diff changeset
   545
11775
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   546
function module.add_host(module)
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   547
	module:depends("http");
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   548
	module:provides("http", {
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   549
		default_path = "/http-bind";
12448
b33558969b3e mod_http (and dependent modules): Make CORS opt-in by default (fixes #1731)
Matthew Wild <mwild1@gmail.com>
parents: 12266
diff changeset
   550
		cors = {
b33558969b3e mod_http (and dependent modules): Make CORS opt-in by default (fixes #1731)
Matthew Wild <mwild1@gmail.com>
parents: 12266
diff changeset
   551
			enabled = true;
b33558969b3e mod_http (and dependent modules): Make CORS opt-in by default (fixes #1731)
Matthew Wild <mwild1@gmail.com>
parents: 12266
diff changeset
   552
		};
11775
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   553
		route = {
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   554
			["GET"] = GET_response;
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   555
			["GET /"] = GET_response;
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   556
			["POST"] = handle_POST;
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   557
			["POST /"] = handle_POST;
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   558
		};
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   559
	});
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   560
end
4c0802b52673 mod_bosh,mod_websocket: Make into global-shared modules (...again)
Kim Alvefur <zash@zash.se>
parents: 11737
diff changeset
   561
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12448
diff changeset
   562
if require"prosody.core.modulemanager".get_modules_for_host("*"):contains(module.name) then
12266
50525021c2c7 mod_bosh: Only enable host-agnostic HTTP routing when enabled globally
Kim Alvefur <zash@zash.se>
parents: 11872
diff changeset
   563
	module:add_host();
50525021c2c7 mod_bosh: Only enable host-agnostic HTTP routing when enabled globally
Kim Alvefur <zash@zash.se>
parents: 11872
diff changeset
   564
end