plugins/mod_bosh.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 06 Aug 2010 01:59:43 +0100
changeset 3448 0ca7c3864431
parent 3447 cc2dc55e66f9
child 3449 0a74ce129a06
permissions -rw-r--r--
mod_bosh: Much improve session:close() for BOSH sessions, so it now matches in usage normal session:close()
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
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1112
diff changeset
     4
-- 
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
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
module.host = "*" -- Global module
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
1042
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 947
diff changeset
    11
local hosts = _G.hosts;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local lxp = require "lxp";
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
local init_xmlhandlers = require "core.xmlhandlers"
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
local server = require "net.server";
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
local httpserver = require "net.httpserver";
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
local sm = require "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
    17
local sm_destroy_session = sm.destroy_session;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
local new_uuid = require "util.uuid".generate;
3439
3d1eacf45230 mod_bosh: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 3322
diff changeset
    19
local fire_event = prosody.events.fire_event;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
local core_process_stanza = core_process_stanza;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
local st = require "util.stanza";
1109
bb21eb3cd364 mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 1049
diff changeset
    22
local logger = require "util.logger";
bb21eb3cd364 mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 1049
diff changeset
    23
local log = logger.init("mod_bosh");
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
    24
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
    25
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
    26
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
    27
local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send)
2961
db3c0ecce3f4 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2925 2959
diff changeset
    28
local stream_callbacks = { stream_ns = "http://jabber.org/protocol/httpbind", 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
    29
1655
dc42bf326713 mod_bosh: Updated to use module:get_option instead of configmanager
Waqas Hussain <waqas20@gmail.com>
parents: 1633
diff changeset
    30
local BOSH_DEFAULT_HOLD = tonumber(module:get_option("bosh_default_hold")) or 1;
dc42bf326713 mod_bosh: Updated to use module:get_option instead of configmanager
Waqas Hussain <waqas20@gmail.com>
parents: 1633
diff changeset
    31
local BOSH_DEFAULT_INACTIVITY = tonumber(module:get_option("bosh_max_inactivity")) or 60;
dc42bf326713 mod_bosh: Updated to use module:get_option instead of configmanager
Waqas Hussain <waqas20@gmail.com>
parents: 1633
diff changeset
    32
local BOSH_DEFAULT_POLLING = tonumber(module:get_option("bosh_max_polling")) or 5;
dc42bf326713 mod_bosh: Updated to use module:get_option instead of configmanager
Waqas Hussain <waqas20@gmail.com>
parents: 1633
diff changeset
    33
local BOSH_DEFAULT_REQUESTS = tonumber(module:get_option("bosh_max_requests")) or 2;
dc42bf326713 mod_bosh: Updated to use module:get_option instead of configmanager
Waqas Hussain <waqas20@gmail.com>
parents: 1633
diff changeset
    34
local BOSH_DEFAULT_MAXPAUSE = tonumber(module:get_option("bosh_max_pause")) or 300;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
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
    36
local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure");
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
    37
866
8958fe4b2391 mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents: 816
diff changeset
    38
local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8" };
8958fe4b2391 mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents: 816
diff changeset
    39
2484
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    40
local cross_domain = module:get_option("cross_domain_bosh");
2485
ace62f19076d mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents: 2484
diff changeset
    41
if cross_domain then
ace62f19076d mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents: 2484
diff changeset
    42
	default_headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
ace62f19076d mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents: 2484
diff changeset
    43
	default_headers["Access-Control-Allow-Headers"] = "Content-Type";
ace62f19076d mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents: 2484
diff changeset
    44
	default_headers["Access-Control-Max-Age"] = "7200";
2484
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    45
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    46
	if cross_domain == true then
2485
ace62f19076d mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents: 2484
diff changeset
    47
		default_headers["Access-Control-Allow-Origin"] = "*";
2484
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    48
	elseif type(cross_domain) == "table" then
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    49
		cross_domain = table.concat(cross_domain, ", ");
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    50
	end
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    51
	if type(cross_domain) == "string" then
2485
ace62f19076d mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents: 2484
diff changeset
    52
		default_headers["Access-Control-Allow-Origin"] = cross_domain;
2484
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    53
	end
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    54
end
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    55
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
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
    57
local os_time = os.time;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
local sessions = {};
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
    60
local inactive_sessions = {}; -- Sessions which have no open requests
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
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
    62
-- 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
    63
local waiting_requests = {};
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
function on_destroy_request(request)
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
	waiting_requests[request] = nil;
947
84202314ab7f mod_bosh: Fix to correctly timeout idle sessions
Matthew Wild <mwild1@gmail.com>
parents: 866
diff changeset
    66
	local session = sessions[request.sid];
816
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    67
	if session then
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    68
		local requests = session.requests;
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
    69
		for i,r in ipairs(requests) do
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
    70
			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
    71
				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
    72
				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
    73
			end
816
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    74
		end
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    75
		
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    76
		-- If this session now has no requests open, mark it as inactive
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    77
		if #requests == 0 and session.bosh_max_inactive and not inactive_sessions[session] then
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    78
			inactive_sessions[session] = os_time();
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    79
			(session.log or log)("debug", "BOSH session marked as inactive at %d", inactive_sessions[session]);
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    80
		end
c031ead9896d mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    81
	end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
function handle_request(method, body, request)
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
	if (not body) or request.method ~= "POST" then
2484
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    86
		if request.method == "OPTIONS" then
2485
ace62f19076d mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents: 2484
diff changeset
    87
			return { headers = default_headers, body = "" };
2484
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    88
		else
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    89
			return "<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>";
cf924f587410 mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents: 2473
diff changeset
    90
		end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
	end
2473
3f4cfa375bd6 mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2467
diff changeset
    92
	if not method then
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
		log("debug", "Request %s suffered error %s", tostring(request.id), body);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
		return;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
	end
771
ecdf72f9b085 Remove redundant logging and debug printing from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 763
diff changeset
    96
	--log("debug", "Handling new request %s: %s\n----------", request.id, tostring(body));
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
	request.notopen = true;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
	request.log = log;
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
    99
	request.on_destroy = on_destroy_request;
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
   100
	
2077
e33658f6052c Changed separator between attribute names and prefixes from '|' to '\1' (optimization and cleanup).
Waqas Hussain <waqas20@gmail.com>
parents: 1869
diff changeset
   101
	local parser = lxp.new(init_xmlhandlers(request, stream_callbacks), "\1");
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
	
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
	parser:parse(body);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
	
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
	local session = sessions[request.sid];
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
	if session then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
		local r = session.requests;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
		log("debug", "Session %s has %d out of %d requests open", request.sid, #r, session.bosh_hold);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
		log("debug", "and there are %d things in the send_buffer", #session.send_buffer);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
		if #r > session.bosh_hold then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
			-- 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
   112
			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
   113
			if #session.send_buffer > 0 then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
				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
   115
				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
   116
				session.send_buffer = {};
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
			else
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
				-- or an empty response
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
				log("debug", "...sending an empty response");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
				session.send("");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
			end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
		elseif #session.send_buffer > 0 then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
			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
   124
			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
   125
			session.send_buffer = {};
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
			session.send(resp);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
		end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
		
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
   129
		if not request.destroyed then
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
   130
			-- 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
   131
			log("debug", "Have nothing to say, so leaving request unanswered for now");
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
   132
			if session.bosh_wait then
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
   133
				request.reply_before = os_time() + session.bosh_wait;
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
   134
				waiting_requests[request] = true;
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
   135
			end
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
   136
			if inactive_sessions[session] then
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
   137
				-- Session was marked as inactive, since we have
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
   138
				-- a request open now, unmark it
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
   139
				inactive_sessions[session] = nil;
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
   140
			end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
		end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
		
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
   143
		return true; -- Inform httpserver we shall reply later
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
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
   147
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
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
   149
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
   150
local stream_xmlns_attr = { xmlns = "urn:ietf:params:xml:ns:xmpp-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
   151
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
   152
local function bosh_close_stream(session, reason)
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
   153
	(session.log or log)("info", "BOSH client disconnected");
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
   154
	
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
   155
	local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
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
   156
		["xmlns:streams"] = xmlns_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
   157
	
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
   158
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
   159
	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
   160
		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
   161
		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
   162
			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
   163
				:tag(reason, {xmlns = xmlns_xmpp_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
   164
		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
   165
			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
   166
				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
   167
					: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
   168
				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
   169
					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
   170
				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
   171
				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
   172
					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
   173
				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
   174
			elseif reason.name then -- a stanza
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
   175
				close_reply = reason;
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
   176
			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
   177
		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
   178
		log("info", "Disconnecting client, <stream:error> is: %s", tostring(close_reply));
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
   179
	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
   180
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
   181
	local session_close_response = { headers = default_headers, body = tostring(close_reply) };
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
   182
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
   183
	--FIXME: Quite sure we shouldn't reply to all requests with the error
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
   184
	for _, held_request in ipairs(session.requests) do
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
   185
		held_request:send(session_close_response);
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
   186
		held_request:destroy();
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
   187
	end
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
   188
	sessions[session.sid]  = nil;
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
   189
	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
   190
end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   191
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   192
function stream_callbacks.streamopened(request, attr)
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   193
	log("debug", "BOSH body open (sid: %s)", attr.sid);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   194
	local sid = attr.sid
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   195
	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
   196
		-- New session request
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
   197
		request.notopen = nil; -- Signals that we accept this opening tag
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
   198
		
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   199
		-- TODO: Sanity checks here (rid, to, known host, etc.)
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
   200
		if not hosts[attr.to] then
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
   201
			-- Unknown host
1048
45fc590539cd mod_bosh: Add log message for clients connecting to unknown host
Matthew Wild <mwild1@gmail.com>
parents: 1047
diff changeset
   202
			log("debug", "BOSH client tried to connect to unknown host: %s", tostring(attr.to));
1633
7812459eeed7 mod_bosh: Fix error reply for host-unknown errors
Matthew Wild <mwild1@gmail.com>
parents: 1551
diff changeset
   203
			session_close_reply.body.attr.condition = "host-unknown";
7812459eeed7 mod_bosh: Fix error reply for host-unknown errors
Matthew Wild <mwild1@gmail.com>
parents: 1551
diff changeset
   204
			request:send(session_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
   205
			request.notopen = nil
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
   206
			return;
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
   207
		end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   208
		
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   209
		-- New session
763
8e77a39826c2 mod_bosh: No need to tostring() uuids now
Matthew Wild <mwild1@gmail.com>
parents: 725
diff changeset
   210
		sid = new_uuid();
3071
39a870ae75d9 mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents: 3070
diff changeset
   211
		local session = {
39a870ae75d9 mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents: 3070
diff changeset
   212
			type = "c2s_unauthed", conn = {}, sid = sid, rid = tonumber(attr.rid)-1, host = attr.to,
39a870ae75d9 mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents: 3070
diff changeset
   213
			bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid,
39a870ae75d9 mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents: 3070
diff changeset
   214
			bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY,
39a870ae75d9 mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents: 3070
diff changeset
   215
			requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream,
39a870ae75d9 mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents: 3070
diff changeset
   216
			close = bosh_close_stream, dispatch_stanza = core_process_stanza,
39a870ae75d9 mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents: 3070
diff changeset
   217
			log = logger.init("bosh"..sid),	secure = consider_bosh_secure or request.secure
39a870ae75d9 mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents: 3070
diff changeset
   218
		};
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   219
		sessions[sid] = session;
1109
bb21eb3cd364 mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents: 1049
diff changeset
   220
		
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   221
		log("info", "New BOSH session, assigned it sid '%s'", sid);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   222
		local r, send_buffer = session.requests, session.send_buffer;
866
8958fe4b2391 mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents: 816
diff changeset
   223
		local response = { headers = default_headers }
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
		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
   225
			-- 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
   226
			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
   227
				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
   228
				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
   229
			end
2099
73e083d01449 mod_bosh: Don't log response XML
Matthew Wild <mwild1@gmail.com>
parents: 2084
diff changeset
   230
			--log("debug", "Sending BOSH data: %s", tostring(s));
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   231
			local oldest_request = r[1];
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   232
			if oldest_request then
2099
73e083d01449 mod_bosh: Don't log response XML
Matthew Wild <mwild1@gmail.com>
parents: 2084
diff changeset
   233
				log("debug", "We have an open request, so sending on that");
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   234
				response.body = t_concat{"<body xmlns='http://jabber.org/protocol/httpbind' sid='", sid, "' xmlns:stream = 'http://etherx.jabber.org/streams'>", tostring(s), "</body>" };
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   235
				oldest_request:send(response);
771
ecdf72f9b085 Remove redundant logging and debug printing from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 763
diff changeset
   236
				--log("debug", "Sent");
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   237
				if oldest_request.stayopen then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   238
					if #r>1 then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   239
						-- Move front request to back
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   240
						t_insert(r, oldest_request);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   241
						t_remove(r, 1);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   242
					end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   243
				else
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   244
					log("debug", "Destroying the request now...");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   245
					oldest_request:destroy();
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   246
				end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   247
			elseif s ~= "" then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   248
				log("debug", "Saved to send buffer because there are %d open requests", #r);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   249
				-- Hmm, no requests are open :(
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   250
				t_insert(session.send_buffer, tostring(s));
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   251
				log("debug", "There are now %d things in the send_buffer", #session.send_buffer);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   252
			end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   253
		end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   254
		
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   255
		-- Send creation response
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   256
		
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   257
		local features = st.stanza("stream:features");
2608
68c43aaf681f mod_bosh: Fire stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2486
diff changeset
   258
		hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   259
		fire_event("stream-features", session, features);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   260
		--xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh'
2473
3f4cfa375bd6 mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2467
diff changeset
   261
		local response = st.stanza("body", { xmlns = xmlns_bosh,
3f4cfa375bd6 mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2467
diff changeset
   262
									inactivity = tostring(BOSH_DEFAULT_INACTIVITY), polling = tostring(BOSH_DEFAULT_POLLING), requests = tostring(BOSH_DEFAULT_REQUESTS), hold = tostring(session.bosh_hold), maxpause = "120",
3f4cfa375bd6 mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2467
diff changeset
   263
									sid = sid, authid = sid, ver  = '1.6', from = session.host, secure = 'true', ["xmpp:version"] = "1.0",
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   264
									["xmlns:xmpp"] = "urn:xmpp:xbosh", ["xmlns:stream"] = "http://etherx.jabber.org/streams" }):add_child(features);
866
8958fe4b2391 mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents: 816
diff changeset
   265
		request:send{ headers = default_headers, body = tostring(response) };
2473
3f4cfa375bd6 mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2467
diff changeset
   266
		
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   267
		request.sid = sid;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   268
		return;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   269
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   270
	
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   271
	local session = sessions[sid];
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   272
	if not session then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   273
		-- Unknown sid
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   274
		log("info", "Client tried to use sid '%s' which we don't know about", sid);
866
8958fe4b2391 mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents: 816
diff changeset
   275
		request:send{ headers = default_headers, body = tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" })) };
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   276
		request.notopen = nil;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   277
		return;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   278
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   279
	
1663
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   280
	if session.rid then
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   281
		local rid = tonumber(attr.rid);
1664
6587b6c2678e mod_bosh: Calculate rid difference just once
Matthew Wild <mwild1@gmail.com>
parents: 1663
diff changeset
   282
		local diff = rid - session.rid;
6587b6c2678e mod_bosh: Calculate rid difference just once
Matthew Wild <mwild1@gmail.com>
parents: 1663
diff changeset
   283
		if diff > 1 then
1663
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   284
			session.log("warn", "rid too large (means a request was lost). Last rid: %d New rid: %s", session.rid, attr.rid);
1664
6587b6c2678e mod_bosh: Calculate rid difference just once
Matthew Wild <mwild1@gmail.com>
parents: 1663
diff changeset
   285
		elseif diff <= 0 then
1663
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   286
			-- Repeated, ignore
3041
7489ac1d5938 mod_bosh: Fix handling of rids by not dropping requests with repeated rids (assign them their sid instead), and always starting a session with first_rid-1.
Matthew Wild <mwild1@gmail.com>
parents: 3040
diff changeset
   287
			session.log("debug", "rid repeated (on request %s), ignoring: %s (diff %d)", request.id, session.rid, diff);
1663
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   288
			request.notopen = nil;
3041
7489ac1d5938 mod_bosh: Fix handling of rids by not dropping requests with repeated rids (assign them their sid instead), and always starting a session with first_rid-1.
Matthew Wild <mwild1@gmail.com>
parents: 3040
diff changeset
   289
			request.sid = sid;
1663
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   290
			t_insert(session.requests, request);
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   291
			return;
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   292
		end
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   293
		session.rid = rid;
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   294
	end
b30c4d0bbe84 mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents: 1662
diff changeset
   295
	
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
   296
	if attr.type == "terminate" then
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
   297
		-- Client wants to end this session
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
   298
		session:close();
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
   299
		request.notopen = nil;
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
   300
		return;
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
   301
	end
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
   302
	
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   303
	if session.notopen then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   304
		local features = st.stanza("stream:features");
2608
68c43aaf681f mod_bosh: Fire stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2486
diff changeset
   305
		hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   306
		fire_event("stream-features", session, features);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   307
		session.send(features);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   308
		session.notopen = nil;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   309
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   310
	
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   311
	request.notopen = nil; -- Signals that we accept this opening tag
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   312
	t_insert(session.requests, request);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   313
	request.sid = sid;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   314
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   315
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   316
function stream_callbacks.handlestanza(request, stanza)
725
96110075288b Replacing pretty_print() with top_tag() for logging
Matthew Wild <mwild1@gmail.com>
parents: 701
diff changeset
   317
	log("debug", "BOSH stanza received: %s\n", stanza:top_tag());
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   318
	local session = sessions[request.sid];
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   319
	if session then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   320
		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
   321
			stanza.attr.xmlns = nil;
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   322
		end
2373
13090fc6ad90 mod_bosh: Update for new connection API
Matthew Wild <mwild1@gmail.com>
parents: 2354
diff changeset
   323
		session.ip = request.handler:ip();
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   324
		core_process_stanza(session, stanza);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   325
	end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   326
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   327
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
   328
function stream_callbacks.error(request, error)
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
   329
	log("debug", "Error parsing BOSH request payload; %s", error);
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
   330
	if not request.sid then
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
   331
		request:send({ headers = default_headers, status = "400 Bad Request" });
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
   332
		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
   333
	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
   334
	
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
   335
	local session = sessions[request.sid];
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
   336
	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
   337
		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
   338
	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
   339
		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
   340
	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
   341
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
   342
1864
b9389286eece mod_bosh: Fix for 'invalid key to next' error when 2 clients lose connection at the same time
Matthew Wild <mwild1@gmail.com>
parents: 1633
diff changeset
   343
local dead_sessions = {};
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   344
function on_timer()
660
b2c4a7ec31c6 Remove some old debugging code from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 636
diff changeset
   345
	-- log("debug", "Checking for requests soon to timeout...");
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   346
	-- Identify requests timing out within the next few seconds
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   347
	local now = os_time() + 3;
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   348
	for request in pairs(waiting_requests) do
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   349
		if request.reply_before <= now then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   350
			log("debug", "%s was soon to timeout, sending empty response", request.id);
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   351
			-- Send empty response to let the
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   352
			-- client know we're still here
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   353
			if request.conn then
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   354
				sessions[request.sid].send("");
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   355
			end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   356
		end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   357
	end
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
   358
	
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
   359
	now = now - 3;
1864
b9389286eece mod_bosh: Fix for 'invalid key to next' error when 2 clients lose connection at the same time
Matthew Wild <mwild1@gmail.com>
parents: 1633
diff changeset
   360
	local n_dead_sessions = 0;
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
   361
	for session, inactive_since in pairs(inactive_sessions) do
692
4a218377f4e3 BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents: 684
diff changeset
   362
		if session.bosh_max_inactive then
4a218377f4e3 BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents: 684
diff changeset
   363
			if now - inactive_since > session.bosh_max_inactive then
4a218377f4e3 BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents: 684
diff changeset
   364
				(session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now);
4a218377f4e3 BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents: 684
diff changeset
   365
				sessions[session.sid]  = nil;
4a218377f4e3 BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents: 684
diff changeset
   366
				inactive_sessions[session] = nil;
1864
b9389286eece mod_bosh: Fix for 'invalid key to next' error when 2 clients lose connection at the same time
Matthew Wild <mwild1@gmail.com>
parents: 1633
diff changeset
   367
				n_dead_sessions = n_dead_sessions + 1;
b9389286eece mod_bosh: Fix for 'invalid key to next' error when 2 clients lose connection at the same time
Matthew Wild <mwild1@gmail.com>
parents: 1633
diff changeset
   368
				dead_sessions[n_dead_sessions] = session;
692
4a218377f4e3 BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents: 684
diff changeset
   369
			end
693
d8b793e612a9 BOSH: Make previous fix a bit more efficient
Matthew Wild <mwild1@gmail.com>
parents: 692
diff changeset
   370
		else
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
   371
			inactive_sessions[session] = nil;
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
   372
		end
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
   373
	end
1864
b9389286eece mod_bosh: Fix for 'invalid key to next' error when 2 clients lose connection at the same time
Matthew Wild <mwild1@gmail.com>
parents: 1633
diff changeset
   374
b9389286eece mod_bosh: Fix for 'invalid key to next' error when 2 clients lose connection at the same time
Matthew Wild <mwild1@gmail.com>
parents: 1633
diff changeset
   375
	for i=1,n_dead_sessions do
b9389286eece mod_bosh: Fix for 'invalid key to next' error when 2 clients lose connection at the same time
Matthew Wild <mwild1@gmail.com>
parents: 1633
diff changeset
   376
		local session = dead_sessions[i];
b9389286eece mod_bosh: Fix for 'invalid key to next' error when 2 clients lose connection at the same time
Matthew Wild <mwild1@gmail.com>
parents: 1633
diff changeset
   377
		dead_sessions[i] = nil;
b9389286eece mod_bosh: Fix for 'invalid key to next' error when 2 clients lose connection at the same time
Matthew Wild <mwild1@gmail.com>
parents: 1633
diff changeset
   378
		sm_destroy_session(session, "BOSH client silent for over "..session.bosh_max_inactive.." seconds");
b9389286eece mod_bosh: Fix for 'invalid key to next' error when 2 clients lose connection at the same time
Matthew Wild <mwild1@gmail.com>
parents: 1633
diff changeset
   379
	end
636
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   380
end
9c9c671ecc50 Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   381
701
dc67e3cffff4 BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents: 693
diff changeset
   382
2354
5a00668ba0ab mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents: 2099
diff changeset
   383
local function setup()
5a00668ba0ab mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents: 2099
diff changeset
   384
	local ports = module:get_option("bosh_ports") or { 5280 };
5a00668ba0ab mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents: 2099
diff changeset
   385
	httpserver.new_from_config(ports, handle_request, { base = "http-bind" });
5a00668ba0ab mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents: 2099
diff changeset
   386
	server.addtimer(on_timer);
5a00668ba0ab mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents: 2099
diff changeset
   387
end
5a00668ba0ab mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents: 2099
diff changeset
   388
if prosody.start_time then -- already started
5a00668ba0ab mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents: 2099
diff changeset
   389
	setup();
5a00668ba0ab mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents: 2099
diff changeset
   390
else
5a00668ba0ab mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents: 2099
diff changeset
   391
	prosody.events.add_handler("server-started", setup);
5a00668ba0ab mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents: 2099
diff changeset
   392
end