Software /
code /
prosody
Annotate
plugins/mod_bosh.lua @ 5651:c968c8e3596d
Merge 0.9->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 07 Jun 2013 13:23:04 +0100 |
parent | 5649:c321f1fed851 |
parent | 5650:aa3465183b01 |
child | 5652:bfb290c8cd73 |
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 |
5634
7298c9bbb30f
mod_bosh: Some very minor whitespace/layout fixes
Matthew Wild <mwild1@gmail.com>
parents:
5188
diff
changeset
|
4 -- |
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
6 -- COPYING file in the source package for more information. |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
7 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
8 |
4623
403b56b78018
mod_posix, mod_bosh, mod_admin_telnet: Use module:set_global()
Kim Alvefur <zash@zash.se>
parents:
4457
diff
changeset
|
9 module:set_global(); -- Global module |
636
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; |
3707
79f62694d36e
mod_bosh: Switch to util.xmppstream from xmlhandlers
Matthew Wild <mwild1@gmail.com>
parents:
3684
diff
changeset
|
12 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
|
13 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
|
14 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
|
15 local new_uuid = require "util.uuid".generate; |
3439
3d1eacf45230
mod_bosh: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
3322
diff
changeset
|
16 local fire_event = prosody.events.fire_event; |
5013
ab693eea0869
mod_admin_adhoc, mod_admin_telnet, mod_bosh, mod_c2s, mod_component, mod_pep, mod_presence, mod_roster, mod_s2s: Import core_post_stanza from the global prosody table.
Kim Alvefur <zash@zash.se>
parents:
4998
diff
changeset
|
17 local core_process_stanza = prosody.core_process_stanza; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 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
|
19 local logger = require "util.logger"; |
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
20 local log = logger.init("mod_bosh"); |
5187
d71f731e8fe4
mod_bosh: Add support for stanza filters to BOSH sessions (needed by some plugins)
Matthew Wild <mwild1@gmail.com>
parents:
5185
diff
changeset
|
21 local initialize_filters = require "util.filters".initialize; |
5185
ca30b21946ef
mod_bosh: Add bosh_max_wait config option, to limit the amount of time a client can request for the server to hold open requests
Matthew Wild <mwild1@gmail.com>
parents:
5179
diff
changeset
|
22 local math_min = math.min; |
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
|
23 |
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
|
24 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
|
25 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
|
26 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
|
27 |
0a74ce129a06
mod_bosh: Small change to use variable instead of hard-coded xmlns
Matthew Wild <mwild1@gmail.com>
parents:
3448
diff
changeset
|
28 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
|
29 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
|
30 |
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
|
31 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
|
32 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
|
33 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
|
34 local BOSH_DEFAULT_REQUESTS = module:get_option_number("bosh_max_requests", 2); |
5185
ca30b21946ef
mod_bosh: Add bosh_max_wait config option, to limit the amount of time a client can request for the server to hold open requests
Matthew Wild <mwild1@gmail.com>
parents:
5179
diff
changeset
|
35 local bosh_max_wait = module:get_option_number("bosh_max_wait", 120); |
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"); |
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
|
38 local cross_domain = module:get_option("cross_domain_bosh", false); |
2484
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
39 |
5648
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
40 if type(cross_domain) == "table" then cross_domain = table.concat(cross_domain, ", "); end |
2484
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
41 |
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
|
42 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
|
43 |
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
|
44 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
|
45 local ip = request.conn:ip(); |
4998
f6c1f98419be
mod_bosh: Use new format for headers when checking for proxies to get the originating IP
Kim Alvefur <zash@zash.se>
parents:
4880
diff
changeset
|
46 local forwarded_for = request.headers.x_forwarded_for; |
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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 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
|
59 local os_time = os.time; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 |
5179
f3662fea95d9
mod_bosh: Share sessions and inactive_sessions tables
Matthew Wild <mwild1@gmail.com>
parents:
5071
diff
changeset
|
61 -- All sessions, and sessions that have no requests open |
f3662fea95d9
mod_bosh: Share sessions and inactive_sessions tables
Matthew Wild <mwild1@gmail.com>
parents:
5071
diff
changeset
|
62 local sessions, inactive_sessions = module:shared("sessions", "inactive_sessions"); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 |
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
|
64 -- 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
|
65 local waiting_requests = {}; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 function on_destroy_request(request) |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
67 log("debug", "Request destroyed: %s", tostring(request)); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 waiting_requests[request] = nil; |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
69 local session = sessions[request.context.sid]; |
816
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
70 if session then |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
71 local requests = session.requests; |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
72 for i, r in ipairs(requests) do |
3039
2fef108d7eb7
mod_bosh: Remove requests from the session table using table.remove(), prevents the possibility of holes in the array.
Matthew Wild <mwild1@gmail.com>
parents:
2959
diff
changeset
|
73 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
|
74 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
|
75 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
|
76 end |
816
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
77 end |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
78 |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
79 -- 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
|
80 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
|
81 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
|
82 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
|
83 (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
|
84 end |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
85 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 |
5648
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
88 local function set_cross_domain_headers(response) |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
89 local headers = response.headers; |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
90 headers.access_control_allow_methods = "GET, POST, OPTIONS"; |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
91 headers.access_control_allow_headers = "Content-Type"; |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
92 headers.access_control_max_age = "7200"; |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
93 |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
94 if cross_domain == true then |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
95 headers.access_control_allow_origin = "*"; |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
96 else |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
97 headers.access_control_allow_origin = cross_domain; |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
98 end |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
99 return response; |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
100 end |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
101 |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
102 function handle_OPTIONS(request) |
5648
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
103 return set_cross_domain_headers(request.response); |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
104 end |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
105 |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
106 function handle_POST(event) |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
107 log("debug", "Handling new request %s: %s\n----------", tostring(event.request), tostring(event.request.body)); |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
108 |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
109 local request, response = event.request, event.response; |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
110 response.on_destroy = on_destroy_request; |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
111 local body = request.body; |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
112 |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
113 local context = { request = request, response = response, notopen = true }; |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
114 local stream = new_xmpp_stream(context, stream_callbacks); |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
115 response.context = context; |
5648
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
116 |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
117 local headers = response.headers; |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
118 headers.content_type = "text/xml; charset=utf-8"; |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
119 |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
120 if cross_domain then |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
121 set_cross_domain_headers(response); |
f7f667c48d9a
mod_bosh: Clean up handling of response headers, set them only in one place
Matthew Wild <mwild1@gmail.com>
parents:
5647
diff
changeset
|
122 end |
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
|
123 |
3707
79f62694d36e
mod_bosh: Switch to util.xmppstream from xmlhandlers
Matthew Wild <mwild1@gmail.com>
parents:
3684
diff
changeset
|
124 -- 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
|
125 -- the body are processed in this next line before it returns. |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
126 -- In particular, the streamopened() stream callback is where |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
127 -- much of the session logic happens, because it's where we first |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
128 -- get to see the 'sid' of this request. |
5647
8767b47524c9
mod_bosh: Return errors when appropriate (invalid XML, missing sid)
Matthew Wild <mwild1@gmail.com>
parents:
5646
diff
changeset
|
129 if not stream:feed(body) then |
8767b47524c9
mod_bosh: Return errors when appropriate (invalid XML, missing sid)
Matthew Wild <mwild1@gmail.com>
parents:
5646
diff
changeset
|
130 module:log("warn", "Error parsing BOSH payload") |
8767b47524c9
mod_bosh: Return errors when appropriate (invalid XML, missing sid)
Matthew Wild <mwild1@gmail.com>
parents:
5646
diff
changeset
|
131 return 400; |
8767b47524c9
mod_bosh: Return errors when appropriate (invalid XML, missing sid)
Matthew Wild <mwild1@gmail.com>
parents:
5646
diff
changeset
|
132 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 |
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
|
134 -- 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
|
135 -- 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
|
136 -- giving a response or putting the request "on hold". |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
137 local session = sessions[context.sid]; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 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
|
139 -- 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
|
140 -- 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
|
141 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
|
142 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
|
143 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
|
144 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 local r = session.requests; |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
146 log("debug", "Session %s has %d out of %d requests open", context.sid, #r, session.bosh_hold); |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
147 log("debug", "and there are %d things in the send_buffer:", #session.send_buffer); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 if #r > session.bosh_hold then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 -- 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
|
150 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
|
151 if #session.send_buffer > 0 then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 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
|
153 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
|
154 session.send_buffer = {}; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 else |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 -- or an empty response |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 log("debug", "...sending an empty response"); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 session.send(""); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 elseif #session.send_buffer > 0 then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 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
|
162 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
|
163 session.send_buffer = {}; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 session.send(resp); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
167 if not response.finished then |
3042
b1961f6c9853
mod_bosh: Always give requests a destroy handler, so that the management of each session's request array and the inactive_sessions logic can happen in one place. Simplifies everything and concludes this series of BOSH fixes.
Matthew Wild <mwild1@gmail.com>
parents:
3041
diff
changeset
|
168 -- 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
|
169 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
|
170 if session.bosh_wait then |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
171 waiting_requests[response] = os_time() + session.bosh_wait; |
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
|
172 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 |
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
|
175 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
|
176 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
|
177 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
|
178 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
|
179 else |
4738
e95458712782
mod_bosh: Remove unused import of net.httpserver
Matthew Wild <mwild1@gmail.com>
parents:
4725
diff
changeset
|
180 return true; -- Inform http server we shall reply later |
4223
9fb6e8ec15ed
mod_bosh: Fix terminate logic - process any stanzas in a terminating request, and add type='terminate' to any responses generated by those stanzas. Finally, close all remaining open requests with type='terminate' and close the session. Fixes #211 (thanks Maranda + waqas).
Matthew Wild <mwild1@gmail.com>
parents:
4102
diff
changeset
|
181 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 end |
5647
8767b47524c9
mod_bosh: Return errors when appropriate (invalid XML, missing sid)
Matthew Wild <mwild1@gmail.com>
parents:
5646
diff
changeset
|
183 module:log("warn", "Unable to associate request with a session (incomplete request?)"); |
8767b47524c9
mod_bosh: Return errors when appropriate (invalid XML, missing sid)
Matthew Wild <mwild1@gmail.com>
parents:
5646
diff
changeset
|
184 return 400; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 |
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
|
187 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 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
|
189 |
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
|
190 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
|
191 |
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
|
192 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
|
193 (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
|
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 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
|
196 ["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
|
197 |
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 |
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 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
|
200 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
|
201 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
|
202 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
|
203 :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
|
204 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
|
205 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
|
206 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
|
207 :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
|
208 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
|
209 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
|
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 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
|
212 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
|
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 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
|
215 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
|
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 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
|
218 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
|
219 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
|
220 |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
221 local response_body = tostring(close_reply); |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
222 for _, held_request in ipairs(session.requests) do |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
223 held_request:send(response_body); |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
224 end |
5634
7298c9bbb30f
mod_bosh: Some very minor whitespace/layout fixes
Matthew Wild <mwild1@gmail.com>
parents:
5188
diff
changeset
|
225 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
|
226 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
|
227 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
|
228 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 |
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
|
230 -- Handle the <body> tag in the request payload. |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
231 function stream_callbacks.streamopened(context, attr) |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
232 local request, response = context.request, context.response; |
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
|
233 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
|
234 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
|
235 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
|
236 -- New session request |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
237 context.notopen = nil; -- Signals that we accept this opening tag |
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 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
239 -- 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
|
240 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
|
241 -- Unknown host |
1048
45fc590539cd
mod_bosh: Add log message for clients connecting to unknown host
Matthew Wild <mwild1@gmail.com>
parents:
1047
diff
changeset
|
242 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
|
243 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
|
244 ["xmlns:stream"] = xmlns_streams, condition = "host-unknown" }); |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
245 response: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
|
246 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
|
247 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
248 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
249 -- New session |
763
8e77a39826c2
mod_bosh: No need to tostring() uuids now
Matthew Wild <mwild1@gmail.com>
parents:
725
diff
changeset
|
250 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
|
251 local session = { |
5046
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
252 type = "c2s_unauthed", conn = {}, sid = sid, rid = tonumber(attr.rid)-1, host = attr.to, |
5185
ca30b21946ef
mod_bosh: Add bosh_max_wait config option, to limit the amount of time a client can request for the server to hold open requests
Matthew Wild <mwild1@gmail.com>
parents:
5179
diff
changeset
|
253 bosh_version = attr.ver, bosh_wait = math_min(attr.wait, bosh_max_wait), streamid = sid, |
3071
39a870ae75d9
mod_bosh: Re-layout session object creation to make lines shorter
Matthew Wild <mwild1@gmail.com>
parents:
3070
diff
changeset
|
254 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
|
255 requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream, |
5071
0382f456ac82
mod_bosh: Remove redundant code (send stream features in only one place) (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
5070
diff
changeset
|
256 close = bosh_close_stream, dispatch_stanza = core_process_stanza, notopen = true, |
3472
61cf3e7d7f07
mod_bosh: Support for reading the client's real IP through HTTP proxies from X-Forwarded-For
Matthew Wild <mwild1@gmail.com>
parents:
3460
diff
changeset
|
257 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
|
258 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
|
259 }; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 sessions[sid] = session; |
1109
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
261 |
5187
d71f731e8fe4
mod_bosh: Add support for stanza filters to BOSH sessions (needed by some plugins)
Matthew Wild <mwild1@gmail.com>
parents:
5185
diff
changeset
|
262 local filter = initialize_filters(session); |
d71f731e8fe4
mod_bosh: Add support for stanza filters to BOSH sessions (needed by some plugins)
Matthew Wild <mwild1@gmail.com>
parents:
5185
diff
changeset
|
263 |
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
|
264 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
|
265 log("info", "New BOSH session, assigned it sid '%s'", sid); |
5046
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
266 |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
267 -- Send creation response |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
268 local creating_session = true; |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
269 |
4769
c91bb217bf79
mod_bosh: Remove unused send_buffer variable
Matthew Wild <mwild1@gmail.com>
parents:
4768
diff
changeset
|
270 local r = session.requests; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 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
|
272 -- 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
|
273 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
|
274 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
|
275 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
|
276 end |
5187
d71f731e8fe4
mod_bosh: Add support for stanza filters to BOSH sessions (needed by some plugins)
Matthew Wild <mwild1@gmail.com>
parents:
5185
diff
changeset
|
277 s = filter("stanzas/out", s); |
2099
73e083d01449
mod_bosh: Don't log response XML
Matthew Wild <mwild1@gmail.com>
parents:
2084
diff
changeset
|
278 --log("debug", "Sending BOSH data: %s", tostring(s)); |
5046
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
279 t_insert(session.send_buffer, tostring(s)); |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
280 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 local oldest_request = r[1]; |
5046
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
282 if oldest_request and not session.bosh_processing then |
2099
73e083d01449
mod_bosh: Don't log response XML
Matthew Wild <mwild1@gmail.com>
parents:
2084
diff
changeset
|
283 log("debug", "We have an open request, so sending on that"); |
5046
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
284 local body_attr = { xmlns = "http://jabber.org/protocol/httpbind", |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
285 ["xmlns:stream"] = "http://etherx.jabber.org/streams"; |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
286 type = session.bosh_terminate and "terminate" or nil; |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
287 sid = sid; |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
288 }; |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
289 if creating_session then |
5644
f9cfe6f5d60f
mod_bosh: Reset creating_session to prevent putting unnecessary attributes into every BOSH response
Matthew Wild <mwild1@gmail.com>
parents:
5639
diff
changeset
|
290 creating_session = nil; |
5046
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
291 body_attr.inactivity = tostring(BOSH_DEFAULT_INACTIVITY); |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
292 body_attr.polling = tostring(BOSH_DEFAULT_POLLING); |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
293 body_attr.requests = tostring(BOSH_DEFAULT_REQUESTS); |
5185
ca30b21946ef
mod_bosh: Add bosh_max_wait config option, to limit the amount of time a client can request for the server to hold open requests
Matthew Wild <mwild1@gmail.com>
parents:
5179
diff
changeset
|
294 body_attr.wait = tostring(session.bosh_wait); |
5046
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
295 body_attr.hold = tostring(session.bosh_hold); |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
296 body_attr.authid = sid; |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
297 body_attr.secure = "true"; |
5634
7298c9bbb30f
mod_bosh: Some very minor whitespace/layout fixes
Matthew Wild <mwild1@gmail.com>
parents:
5188
diff
changeset
|
298 body_attr.ver = '1.6'; |
7298c9bbb30f
mod_bosh: Some very minor whitespace/layout fixes
Matthew Wild <mwild1@gmail.com>
parents:
5188
diff
changeset
|
299 body_attr.from = session.host; |
5046
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
300 body_attr["xmlns:xmpp"] = "urn:xmpp:xbosh"; |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
301 body_attr["xmpp:version"] = "1.0"; |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
302 end |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
303 oldest_request:send(st.stanza("body", body_attr):top_tag()..t_concat(session.send_buffer).."</body>"); |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
304 session.send_buffer = {}; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
305 end |
4102
9df4e61c260b
mod_bosh: Return true from send()
Matthew Wild <mwild1@gmail.com>
parents:
4000
diff
changeset
|
306 return true; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
307 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
308 request.sid = sid; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
309 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
310 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
311 local session = sessions[sid]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
312 if not session then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
313 -- Unknown sid |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
314 log("info", "Client tried to use sid '%s' which we don't know about", sid); |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
315 response:send(tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" }))); |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
316 context.notopen = nil; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
317 return; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
318 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
319 |
1663
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
320 if session.rid then |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
321 local rid = tonumber(attr.rid); |
1664
6587b6c2678e
mod_bosh: Calculate rid difference just once
Matthew Wild <mwild1@gmail.com>
parents:
1663
diff
changeset
|
322 local diff = rid - session.rid; |
6587b6c2678e
mod_bosh: Calculate rid difference just once
Matthew Wild <mwild1@gmail.com>
parents:
1663
diff
changeset
|
323 if diff > 1 then |
1663
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
324 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
|
325 elseif diff <= 0 then |
1663
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
326 -- Repeated, ignore |
5650
aa3465183b01
mod_bosh: Remove logging of request.id, it doesn't exist in the new HTTP server API (thanks Mikael Nordfeldth)
Matthew Wild <mwild1@gmail.com>
parents:
5644
diff
changeset
|
327 session.log("debug", "rid repeated, ignoring: %s (diff %d)", session.rid, diff); |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
328 context.notopen = nil; |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
329 context.ignore = true; |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
330 context.sid = sid; |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
331 t_insert(session.requests, response); |
1663
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
332 return; |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
333 end |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
334 session.rid = rid; |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
335 end |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
336 |
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
|
337 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
|
338 -- 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
|
339 -- 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
|
340 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
|
341 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
|
342 |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
343 context.notopen = nil; -- Signals that we accept this opening tag |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
344 t_insert(session.requests, response); |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
345 context.sid = sid; |
5046
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
346 session.bosh_processing = true; -- Used to suppress replies until processing of this request is done |
4438
7f51186ed28b
mod_bosh: Move stream:features sending until after the current request has been added to session.requests. Ensures correct inactivity logic.
Matthew Wild <mwild1@gmail.com>
parents:
4437
diff
changeset
|
347 |
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
|
348 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
|
349 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
|
350 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
|
351 fire_event("stream-features", session, features); |
5639
7a0e19e649b7
mod_bosh: Fix traceback in case session is destroyed during the stream-features event (thanks Biszkopcik)
Kim Alvefur <zash@zash.se>
parents:
5188
diff
changeset
|
352 session.send(tostring(features)); |
4438
7f51186ed28b
mod_bosh: Move stream:features sending until after the current request has been added to session.requests. Ensures correct inactivity logic.
Matthew Wild <mwild1@gmail.com>
parents:
4437
diff
changeset
|
353 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
|
354 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
355 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
356 |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
357 function stream_callbacks.handlestanza(context, stanza) |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
358 if context.ignore then return; end |
725
96110075288b
Replacing pretty_print() with top_tag() for logging
Matthew Wild <mwild1@gmail.com>
parents:
701
diff
changeset
|
359 log("debug", "BOSH stanza received: %s\n", stanza:top_tag()); |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
360 local session = sessions[context.sid]; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
361 if session then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
362 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
|
363 stanza.attr.xmlns = nil; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
364 end |
5187
d71f731e8fe4
mod_bosh: Add support for stanza filters to BOSH sessions (needed by some plugins)
Matthew Wild <mwild1@gmail.com>
parents:
5185
diff
changeset
|
365 stanza = session.filter("stanzas/in", stanza); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
366 core_process_stanza(session, stanza); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
367 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
368 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
369 |
5635
84d3d7b69f9a
mod_bosh: rename variable for clarity
Matthew Wild <mwild1@gmail.com>
parents:
5634
diff
changeset
|
370 function stream_callbacks.streamclosed(context) |
84d3d7b69f9a
mod_bosh: rename variable for clarity
Matthew Wild <mwild1@gmail.com>
parents:
5634
diff
changeset
|
371 local session = sessions[context.sid]; |
5046
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
372 if session then |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
373 session.bosh_processing = false; |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
374 if #session.send_buffer > 0 then |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
375 session.send(""); |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
376 end |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
377 end |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
378 end |
16c7b510694b
mod_bosh: Correctly handle data included in the session initiation request, and cork session while a request is being processed, preventing replying to requests when there may be more data to come, reducing round-trips.
Matthew Wild <mwild1@gmail.com>
parents:
5031
diff
changeset
|
379 |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
380 function stream_callbacks.error(context, error) |
3447
cc2dc55e66f9
mod_bosh: Add error callback for xmlhandlers, to handle the case of invalid or unusable XML in the request payload
Matthew Wild <mwild1@gmail.com>
parents:
3439
diff
changeset
|
381 log("debug", "Error parsing BOSH request payload; %s", error); |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
382 if not context.sid then |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
383 local response = context.response; |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
384 response.status_code = 400; |
4768
d232bd25ae58
mod_bosh: Fix request/response mixup
Matthew Wild <mwild1@gmail.com>
parents:
4767
diff
changeset
|
385 response:send(); |
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
|
386 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
|
387 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
|
388 |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
389 local session = sessions[context.sid]; |
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
|
390 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
|
391 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
|
392 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
|
393 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
|
394 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
|
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 |
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
|
397 local dead_sessions = {}; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
398 function on_timer() |
660
b2c4a7ec31c6
Remove some old debugging code from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
636
diff
changeset
|
399 -- 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
|
400 -- 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
|
401 local now = os_time() + 3; |
4690
55f690fdc915
mod_bosh: Optimisation, store reply_before value as waiting_requests value (saves a lookup)
Matthew Wild <mwild1@gmail.com>
parents:
4684
diff
changeset
|
402 for request, reply_before in pairs(waiting_requests) do |
55f690fdc915
mod_bosh: Optimisation, store reply_before value as waiting_requests value (saves a lookup)
Matthew Wild <mwild1@gmail.com>
parents:
4684
diff
changeset
|
403 if reply_before <= now then |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
404 log("debug", "%s was soon to timeout (at %d, now %d), sending empty response", tostring(request), reply_before, now); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
405 -- Send empty response to let the |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
406 -- client know we're still here |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
407 if request.conn then |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
408 sessions[request.context.sid].send(""); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
409 end |
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 |
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
|
412 |
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 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
|
414 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
|
415 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
|
416 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
|
417 (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
|
418 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
|
419 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
|
420 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
|
421 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
|
422 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
|
423 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
|
424 |
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 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
|
426 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
|
427 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
|
428 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
|
429 end |
3684
bd071e3901dc
mod_bosh: Use util.timer for timers instead of server.addtimer.
Waqas Hussain <waqas20@gmail.com>
parents:
3542
diff
changeset
|
430 return 1; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
431 end |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
432 module:add_timer(1, on_timer); |
701
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
433 |
4880
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
434 |
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
435 local GET_response = { |
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
436 headers = { |
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
437 content_type = "text/html"; |
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
438 }; |
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
439 body = [[<html><body> |
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
440 <p>It works! Now point your BOSH client to this URL to connect to Prosody.</p> |
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
441 <p>For more information see <a href="http://prosody.im/doc/setting_up_bosh">Prosody: Setting up BOSH</a>.</p> |
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
442 </body></html>]]; |
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
443 }; |
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
444 |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
445 function module.add_host(module) |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
446 module:depends("http"); |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
447 module:provides("http", { |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
448 default_path = "/http-bind"; |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
449 route = { |
4880
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
450 ["GET"] = GET_response; |
6d96e2e717c1
mod_bosh: Set Content-Type to text/html for GET response (thanks Medics)
Matthew Wild <mwild1@gmail.com>
parents:
4769
diff
changeset
|
451 ["GET /"] = GET_response; |
4725
d085db6285db
mod_bosh: Add routes without trailing-'/', so that both /http-bind and /http-bind/ work again
Matthew Wild <mwild1@gmail.com>
parents:
4697
diff
changeset
|
452 ["OPTIONS"] = handle_OPTIONS; |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
453 ["OPTIONS /"] = handle_OPTIONS; |
4725
d085db6285db
mod_bosh: Add routes without trailing-'/', so that both /http-bind and /http-bind/ work again
Matthew Wild <mwild1@gmail.com>
parents:
4697
diff
changeset
|
454 ["POST"] = handle_POST; |
4692
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
455 ["POST /"] = handle_POST; |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
456 }; |
8e7c683d78ca
mod_bosh: Large commit to update to mod_http/net.http.server APIs. Becomes a shared module.
Matthew Wild <mwild1@gmail.com>
parents:
4690
diff
changeset
|
457 }); |
2354
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
458 end |