Software /
code /
prosody
Annotate
plugins/mod_bosh.lua @ 4447:57d7362a9312
mod_bosh: Fixed use of a private HTTP request property.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 09 Dec 2011 11:44:00 +0500 |
parent | 4442:bc0a68cae236 |
child | 4457:332a5d73e5b6 |
rev | line source |
---|---|
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"; |
3707
79f62694d36e
mod_bosh: Switch to util.xmppstream from xmlhandlers
Matthew Wild <mwild1@gmail.com>
parents:
3684
diff
changeset
|
13 local new_xmpp_stream = require "util.xmppstream".new; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local httpserver = require "net.httpserver"; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 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
|
16 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
|
17 local new_uuid = require "util.uuid".generate; |
3439
3d1eacf45230
mod_bosh: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
3322
diff
changeset
|
18 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
|
19 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
|
20 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
|
21 local logger = require "util.logger"; |
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
22 local log = logger.init("mod_bosh"); |
3684
bd071e3901dc
mod_bosh: Use util.timer for timers instead of server.addtimer.
Waqas Hussain <waqas20@gmail.com>
parents:
3542
diff
changeset
|
23 local timer = require "util.timer"; |
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) |
3449
0a74ce129a06
mod_bosh: Small change to use variable instead of hard-coded xmlns
Matthew Wild <mwild1@gmail.com>
parents:
3448
diff
changeset
|
28 |
0a74ce129a06
mod_bosh: Small change to use variable instead of hard-coded xmlns
Matthew Wild <mwild1@gmail.com>
parents:
3448
diff
changeset
|
29 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
|
30 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
|
31 |
4332
8154bc28e520
mod_bosh: Update to use typed variants of module:get_option(), makes it more tolerant to config variations and simplifies the code.
Matthew Wild <mwild1@gmail.com>
parents:
4327
diff
changeset
|
32 local BOSH_DEFAULT_HOLD = module:get_option_number("bosh_default_hold", 1); |
8154bc28e520
mod_bosh: Update to use typed variants of module:get_option(), makes it more tolerant to config variations and simplifies the code.
Matthew Wild <mwild1@gmail.com>
parents:
4327
diff
changeset
|
33 local BOSH_DEFAULT_INACTIVITY = module:get_option_number("bosh_max_inactivity", 60); |
8154bc28e520
mod_bosh: Update to use typed variants of module:get_option(), makes it more tolerant to config variations and simplifies the code.
Matthew Wild <mwild1@gmail.com>
parents:
4327
diff
changeset
|
34 local BOSH_DEFAULT_POLLING = module:get_option_number("bosh_max_polling", 5); |
8154bc28e520
mod_bosh: Update to use typed variants of module:get_option(), makes it more tolerant to config variations and simplifies the code.
Matthew Wild <mwild1@gmail.com>
parents:
4327
diff
changeset
|
35 local BOSH_DEFAULT_REQUESTS = module:get_option_number("bosh_max_requests", 2); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
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
|
37 local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure"); |
4442
bc0a68cae236
mod_bosh: Experimental option 'bosh_auto_cork' which witholds any response to a request until all stanzas in it have been processed.
Matthew Wild <mwild1@gmail.com>
parents:
4438
diff
changeset
|
38 local auto_cork = module:get_option_boolean("bosh_auto_cork", false); |
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
|
39 |
866
8958fe4b2391
mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents:
816
diff
changeset
|
40 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
|
41 |
4332
8154bc28e520
mod_bosh: Update to use typed variants of module:get_option(), makes it more tolerant to config variations and simplifies the code.
Matthew Wild <mwild1@gmail.com>
parents:
4327
diff
changeset
|
42 local cross_domain = module:get_option("cross_domain_bosh", false); |
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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
48 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
|
49 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
|
50 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
|
51 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
|
52 end |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
53 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
|
54 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
|
55 end |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
56 end |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
57 |
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
|
58 local trusted_proxies = module:get_option_set("trusted_proxies", {"127.0.0.1"})._items; |
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
|
59 |
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
|
60 local function get_ip_from_request(request) |
4447
57d7362a9312
mod_bosh: Fixed use of a private HTTP request property.
Waqas Hussain <waqas20@gmail.com>
parents:
4442
diff
changeset
|
61 local ip = request.conn:ip(); |
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
|
62 local forwarded_for = request.headers["x-forwarded-for"]; |
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
|
63 if forwarded_for then |
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
|
64 forwarded_for = forwarded_for..", "..ip; |
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
|
65 for forwarded_ip in forwarded_for:gmatch("[^%s,]+") do |
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
|
66 if not trusted_proxies[forwarded_ip] then |
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
|
67 ip = forwarded_ip; |
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
|
68 end |
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
|
69 end |
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
|
70 end |
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
|
71 return ip; |
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
|
72 end |
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
|
73 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 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
|
75 local os_time = os.time; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 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
|
78 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
|
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 local waiting_requests = {}; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 function on_destroy_request(request) |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 waiting_requests[request] = nil; |
947
84202314ab7f
mod_bosh: Fix to correctly timeout idle sessions
Matthew Wild <mwild1@gmail.com>
parents:
866
diff
changeset
|
84 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
|
85 if session then |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
86 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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 end |
816
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
92 end |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
93 |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
94 -- 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
|
95 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
|
96 if max_inactive and #requests == 0 then |
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
|
97 inactive_sessions[session] = os_time() + 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
|
98 (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
|
99 end |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
100 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 end |
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 function handle_request(method, body, request) |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 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
|
105 if request.method == "OPTIONS" then |
3542
2acaf129e1c3
mod_bosh: Don't add a Content-Type header to the HTTP OPTIONS reply.
Waqas Hussain <waqas20@gmail.com>
parents:
3492
diff
changeset
|
106 local headers = {}; |
2acaf129e1c3
mod_bosh: Don't add a Content-Type header to the HTTP OPTIONS reply.
Waqas Hussain <waqas20@gmail.com>
parents:
3492
diff
changeset
|
107 for k,v in pairs(default_headers) do headers[k] = v; end |
2acaf129e1c3
mod_bosh: Don't add a Content-Type header to the HTTP OPTIONS reply.
Waqas Hussain <waqas20@gmail.com>
parents:
3492
diff
changeset
|
108 headers["Content-Type"] = nil; |
2acaf129e1c3
mod_bosh: Don't add a Content-Type header to the HTTP OPTIONS reply.
Waqas Hussain <waqas20@gmail.com>
parents:
3492
diff
changeset
|
109 return { headers = headers, body = "" }; |
2484
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
110 else |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
111 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
|
112 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 end |
2473
3f4cfa375bd6
mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2467
diff
changeset
|
114 if not method then |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 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
|
116 return; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 end |
771
ecdf72f9b085
Remove redundant logging and debug printing from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
763
diff
changeset
|
118 --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
|
119 request.notopen = true; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 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
|
121 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
|
122 |
3707
79f62694d36e
mod_bosh: Switch to util.xmppstream from xmlhandlers
Matthew Wild <mwild1@gmail.com>
parents:
3684
diff
changeset
|
123 local stream = new_xmpp_stream(request, stream_callbacks); |
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
|
124 |
3707
79f62694d36e
mod_bosh: Switch to util.xmppstream from xmlhandlers
Matthew Wild <mwild1@gmail.com>
parents:
3684
diff
changeset
|
125 -- 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
|
126 -- the body are processed in this next line before it returns. |
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
|
127 -- In particular, the streamopened() stream callback is where |
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
|
128 -- much of the session logic happens, because it's where we first |
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
|
129 -- get to see the 'sid' of this request. |
3707
79f62694d36e
mod_bosh: Switch to util.xmppstream from xmlhandlers
Matthew Wild <mwild1@gmail.com>
parents:
3684
diff
changeset
|
130 stream:feed(body); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 |
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
|
132 -- 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
|
133 -- 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
|
134 -- giving a response or putting the request "on hold". |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 local session = sessions[request.sid]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 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
|
137 -- 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
|
138 -- a request open now, unmark it |
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
|
139 if inactive_sessions[session] and #session.requests > 0 then |
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
|
140 inactive_sessions[session] = nil; |
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
|
141 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
|
142 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 local r = session.requests; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 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
|
145 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
|
146 if #r > session.bosh_hold then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 -- 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
|
148 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
|
149 if #session.send_buffer > 0 then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 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
|
151 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
|
152 session.send_buffer = {}; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 else |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 -- or an empty response |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 log("debug", "...sending an empty response"); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 session.send(""); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 elseif #session.send_buffer > 0 then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 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
|
160 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
|
161 session.send_buffer = {}; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 session.send(resp); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 |
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
|
165 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
|
166 -- 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 |
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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 else |
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
|
179 return true; -- Inform httpserver we shall reply later |
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
|
180 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 |
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 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 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
|
186 |
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
|
187 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
|
188 |
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
|
189 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
|
190 (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
|
191 |
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
|
192 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
|
193 ["xmlns:stream"] = xmlns_streams }); |
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
|
194 |
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
|
195 |
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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 :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
|
201 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
|
202 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
|
203 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
|
204 :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
|
205 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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 |
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
|
218 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
|
219 |
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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 sessions[session.sid] = nil; |
4437
916681a9a7be
mod_bosh: Remove a session from inactive_sessions before destroying it
Matthew Wild <mwild1@gmail.com>
parents:
4436
diff
changeset
|
225 inactive_sessions[session] = 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
|
226 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
|
227 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 |
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
|
229 -- Handle the <body> tag in the request payload. |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 function stream_callbacks.streamopened(request, attr) |
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
|
231 local sid = attr.sid; |
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
|
232 log("debug", "BOSH body open (sid: %s)", sid or "<none>"); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 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
|
234 -- 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
|
235 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
|
236 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 -- 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
|
238 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
|
239 -- Unknown host |
1048
45fc590539cd
mod_bosh: Add log message for clients connecting to unknown host
Matthew Wild <mwild1@gmail.com>
parents:
1047
diff
changeset
|
240 log("debug", "BOSH client tried to connect to unknown host: %s", tostring(attr.to)); |
3492
6d782b1fcc8a
mod_bosh: Fix traceback when initiating a BOSH session to an unknown host
Matthew Wild <mwild1@gmail.com>
parents:
3472
diff
changeset
|
241 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
|
242 ["xmlns:stream"] = xmlns_streams, condition = "host-unknown" }); |
3492
6d782b1fcc8a
mod_bosh: Fix traceback when initiating a BOSH session to an unknown host
Matthew Wild <mwild1@gmail.com>
parents:
3472
diff
changeset
|
243 request:send(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
|
244 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
|
245 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
246 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
247 -- New session |
763
8e77a39826c2
mod_bosh: No need to tostring() uuids now
Matthew Wild <mwild1@gmail.com>
parents:
725
diff
changeset
|
248 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
|
249 local session = { |
3460
742f6e5a4066
mod_bosh: Don't adjust rid when creating a session, as this is no longer necessary and causes a log message ('rid too large') to be erroneously printed, fixes #203
Matthew Wild <mwild1@gmail.com>
parents:
3450
diff
changeset
|
250 type = "c2s_unauthed", conn = {}, sid = sid, rid = tonumber(attr.rid), host = attr.to, |
3071
39a870ae75d9
mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents:
3070
diff
changeset
|
251 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
|
252 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
|
253 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
|
254 close = bosh_close_stream, dispatch_stanza = core_process_stanza, |
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
|
255 log = logger.init("bosh"..sid), secure = consider_bosh_secure or request.secure, |
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
|
256 ip = get_ip_from_request(request); |
3071
39a870ae75d9
mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents:
3070
diff
changeset
|
257 }; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
258 sessions[sid] = session; |
1109
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
259 |
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
|
260 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
|
261 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
|
262 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
|
263 local response = { headers = default_headers } |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 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
|
265 -- 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
|
266 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
|
267 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
|
268 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
|
269 end |
2099
73e083d01449
mod_bosh: Don't log response XML
Matthew Wild <mwild1@gmail.com>
parents:
2084
diff
changeset
|
270 --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
|
271 local oldest_request = r[1]; |
4442
bc0a68cae236
mod_bosh: Experimental option 'bosh_auto_cork' which witholds any response to a request until all stanzas in it have been processed.
Matthew Wild <mwild1@gmail.com>
parents:
4438
diff
changeset
|
272 if oldest_request and (not(auto_cork) or waiting_requests[oldest_request]) then |
2099
73e083d01449
mod_bosh: Don't log response XML
Matthew Wild <mwild1@gmail.com>
parents:
2084
diff
changeset
|
273 log("debug", "We have an open request, so sending on that"); |
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
|
274 response.body = t_concat({ |
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
|
275 "<body xmlns='http://jabber.org/protocol/httpbind' ", |
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
|
276 session.bosh_terminate and "type='terminate' " or "", |
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
|
277 "sid='", sid, "' xmlns:stream = 'http://etherx.jabber.org/streams'>", |
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
|
278 tostring(s), |
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
|
279 "</body>" |
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
|
280 }); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 oldest_request:send(response); |
771
ecdf72f9b085
Remove redundant logging and debug printing from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
763
diff
changeset
|
282 --log("debug", "Sent"); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 if oldest_request.stayopen then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
284 if #r>1 then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
285 -- Move front request to back |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
286 t_insert(r, oldest_request); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
287 t_remove(r, 1); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
288 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
289 else |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
290 log("debug", "Destroying the request now..."); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
291 oldest_request:destroy(); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
292 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
293 elseif s ~= "" then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
294 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
|
295 -- Hmm, no requests are open :( |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
296 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
|
297 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
|
298 end |
4102
9df4e61c260b
mod_bosh: Return true from send()
Matthew Wild <mwild1@gmail.com>
parents:
4000
diff
changeset
|
299 return true; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
300 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
301 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
302 -- Send creation response |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
303 |
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 --xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh' |
2473
3f4cfa375bd6
mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2467
diff
changeset
|
308 local response = st.stanza("body", { xmlns = xmlns_bosh, |
3725
926ece7d0e67
mod_bosh: Fixes to the session creation response - add mandatory 'wait' attribute, remove optional 'maxpause' which we don't support, and reformat the code to prevent long lines and wacky indentation. Fixes #219.
Matthew Wild <mwild1@gmail.com>
parents:
3707
diff
changeset
|
309 wait = attr.wait, |
926ece7d0e67
mod_bosh: Fixes to the session creation response - add mandatory 'wait' attribute, remove optional 'maxpause' which we don't support, and reformat the code to prevent long lines and wacky indentation. Fixes #219.
Matthew Wild <mwild1@gmail.com>
parents:
3707
diff
changeset
|
310 inactivity = tostring(BOSH_DEFAULT_INACTIVITY), |
926ece7d0e67
mod_bosh: Fixes to the session creation response - add mandatory 'wait' attribute, remove optional 'maxpause' which we don't support, and reformat the code to prevent long lines and wacky indentation. Fixes #219.
Matthew Wild <mwild1@gmail.com>
parents:
3707
diff
changeset
|
311 polling = tostring(BOSH_DEFAULT_POLLING), |
926ece7d0e67
mod_bosh: Fixes to the session creation response - add mandatory 'wait' attribute, remove optional 'maxpause' which we don't support, and reformat the code to prevent long lines and wacky indentation. Fixes #219.
Matthew Wild <mwild1@gmail.com>
parents:
3707
diff
changeset
|
312 requests = tostring(BOSH_DEFAULT_REQUESTS), |
926ece7d0e67
mod_bosh: Fixes to the session creation response - add mandatory 'wait' attribute, remove optional 'maxpause' which we don't support, and reformat the code to prevent long lines and wacky indentation. Fixes #219.
Matthew Wild <mwild1@gmail.com>
parents:
3707
diff
changeset
|
313 hold = tostring(session.bosh_hold), |
926ece7d0e67
mod_bosh: Fixes to the session creation response - add mandatory 'wait' attribute, remove optional 'maxpause' which we don't support, and reformat the code to prevent long lines and wacky indentation. Fixes #219.
Matthew Wild <mwild1@gmail.com>
parents:
3707
diff
changeset
|
314 sid = sid, authid = sid, |
926ece7d0e67
mod_bosh: Fixes to the session creation response - add mandatory 'wait' attribute, remove optional 'maxpause' which we don't support, and reformat the code to prevent long lines and wacky indentation. Fixes #219.
Matthew Wild <mwild1@gmail.com>
parents:
3707
diff
changeset
|
315 ver = '1.6', from = session.host, |
926ece7d0e67
mod_bosh: Fixes to the session creation response - add mandatory 'wait' attribute, remove optional 'maxpause' which we don't support, and reformat the code to prevent long lines and wacky indentation. Fixes #219.
Matthew Wild <mwild1@gmail.com>
parents:
3707
diff
changeset
|
316 secure = 'true', ["xmpp:version"] = "1.0", |
926ece7d0e67
mod_bosh: Fixes to the session creation response - add mandatory 'wait' attribute, remove optional 'maxpause' which we don't support, and reformat the code to prevent long lines and wacky indentation. Fixes #219.
Matthew Wild <mwild1@gmail.com>
parents:
3707
diff
changeset
|
317 ["xmlns:xmpp"] = "urn:xmpp:xbosh", |
926ece7d0e67
mod_bosh: Fixes to the session creation response - add mandatory 'wait' attribute, remove optional 'maxpause' which we don't support, and reformat the code to prevent long lines and wacky indentation. Fixes #219.
Matthew Wild <mwild1@gmail.com>
parents:
3707
diff
changeset
|
318 ["xmlns:stream"] = "http://etherx.jabber.org/streams" |
926ece7d0e67
mod_bosh: Fixes to the session creation response - add mandatory 'wait' attribute, remove optional 'maxpause' which we don't support, and reformat the code to prevent long lines and wacky indentation. Fixes #219.
Matthew Wild <mwild1@gmail.com>
parents:
3707
diff
changeset
|
319 }):add_child(features); |
866
8958fe4b2391
mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents:
816
diff
changeset
|
320 request:send{ headers = default_headers, body = tostring(response) }; |
2473
3f4cfa375bd6
mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2467
diff
changeset
|
321 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
322 request.sid = sid; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
323 return; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
324 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
325 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
326 local session = sessions[sid]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
327 if not session then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
328 -- Unknown sid |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
329 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
|
330 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
|
331 request.notopen = nil; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
332 return; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
333 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
334 |
1663
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
335 if session.rid then |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
336 local rid = tonumber(attr.rid); |
1664
6587b6c2678e
mod_bosh: Calculate rid difference just once
Matthew Wild <mwild1@gmail.com>
parents:
1663
diff
changeset
|
337 local diff = rid - session.rid; |
6587b6c2678e
mod_bosh: Calculate rid difference just once
Matthew Wild <mwild1@gmail.com>
parents:
1663
diff
changeset
|
338 if diff > 1 then |
1663
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
339 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
|
340 elseif diff <= 0 then |
1663
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
341 -- 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
|
342 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
|
343 request.notopen = nil; |
3450
4bd78a5fee75
mod_bosh: Fix to properly ignore repeated requests
Matthew Wild <mwild1@gmail.com>
parents:
3449
diff
changeset
|
344 request.ignore = true; |
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
|
345 request.sid = sid; |
1663
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
346 t_insert(session.requests, request); |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
347 return; |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
348 end |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
349 session.rid = rid; |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
350 end |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
351 |
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
|
352 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
|
353 -- 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
|
354 -- 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
|
355 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
|
356 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
|
357 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
358 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
|
359 t_insert(session.requests, request); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
360 request.sid = sid; |
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
|
361 |
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
|
362 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
|
363 local features = st.stanza("stream:features"); |
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
|
364 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features }); |
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
|
365 fire_event("stream-features", session, features); |
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
|
366 session.send(features); |
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
|
367 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
|
368 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
369 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
370 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
371 function stream_callbacks.handlestanza(request, stanza) |
3450
4bd78a5fee75
mod_bosh: Fix to properly ignore repeated requests
Matthew Wild <mwild1@gmail.com>
parents:
3449
diff
changeset
|
372 if request.ignore then return; end |
725
96110075288b
Replacing pretty_print() with top_tag() for logging
Matthew Wild <mwild1@gmail.com>
parents:
701
diff
changeset
|
373 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
|
374 local session = sessions[request.sid]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
375 if session then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
376 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
|
377 stanza.attr.xmlns = nil; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
378 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
379 core_process_stanza(session, stanza); |
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 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
382 |
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
|
383 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
|
384 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
|
385 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
|
386 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
|
387 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
|
388 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
|
389 |
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
|
390 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
|
391 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
|
392 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
|
393 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
|
394 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
|
395 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
|
396 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
|
397 |
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
|
398 local dead_sessions = {}; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
399 function on_timer() |
660
b2c4a7ec31c6
Remove some old debugging code from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
636
diff
changeset
|
400 -- 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
|
401 -- 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
|
402 local now = os_time() + 3; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
403 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
|
404 if request.reply_before <= now then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
405 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
|
406 -- Send empty response to let the |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
407 -- client know we're still here |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
408 if request.conn then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
409 sessions[request.sid].send(""); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
410 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
411 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
412 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
|
413 |
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
|
414 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
|
415 local n_dead_sessions = 0; |
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
|
416 for session, close_after in pairs(inactive_sessions) do |
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
|
417 if close_after < now then |
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
|
418 (session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now); |
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
|
419 sessions[session.sid] = nil; |
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
|
420 inactive_sessions[session] = nil; |
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
|
421 n_dead_sessions = n_dead_sessions + 1; |
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
|
422 dead_sessions[n_dead_sessions] = session; |
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
|
423 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
|
424 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
|
425 |
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
|
426 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
|
427 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
|
428 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
|
429 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
|
430 end |
3684
bd071e3901dc
mod_bosh: Use util.timer for timers instead of server.addtimer.
Waqas Hussain <waqas20@gmail.com>
parents:
3542
diff
changeset
|
431 return 1; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
432 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
433 |
701
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
434 |
2354
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
435 local function setup() |
4332
8154bc28e520
mod_bosh: Update to use typed variants of module:get_option(), makes it more tolerant to config variations and simplifies the code.
Matthew Wild <mwild1@gmail.com>
parents:
4327
diff
changeset
|
436 local ports = module:get_option_array("bosh_ports") or { 5280 }; |
2354
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
437 httpserver.new_from_config(ports, handle_request, { base = "http-bind" }); |
3684
bd071e3901dc
mod_bosh: Use util.timer for timers instead of server.addtimer.
Waqas Hussain <waqas20@gmail.com>
parents:
3542
diff
changeset
|
438 timer.add_task(1, on_timer); |
2354
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
439 end |
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
440 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
|
441 setup(); |
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
442 else |
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
443 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
|
444 end |