Software /
code /
prosody
Annotate
plugins/mod_bosh.lua @ 2608:68c43aaf681f
mod_bosh: Fire stream-features event using new events API.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 12 Feb 2010 04:11:50 +0500 |
parent | 2486:f0335b7284b1 |
child | 2925:692b3c6c5bd2 |
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 |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
4 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
6 -- COPYING file in the source package for more information. |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
7 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
8 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 module.host = "*" -- Global module |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
1042
a3d77353c18a
mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents:
947
diff
changeset
|
11 local hosts = _G.hosts; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local lxp = require "lxp"; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local init_xmlhandlers = require "core.xmlhandlers" |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local server = require "net.server"; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local httpserver = require "net.httpserver"; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local sm = require "core.sessionmanager"; |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
17 local sm_destroy_session = sm.destroy_session; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local new_uuid = require "util.uuid".generate; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local fire_event = require "core.eventmanager".fire_event; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 local core_process_stanza = core_process_stanza; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local st = require "util.stanza"; |
1109
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
22 local logger = require "util.logger"; |
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
23 local log = logger.init("mod_bosh"); |
1665
2c72b725384e
mod_bosh: Strip BOSH namespace from stanzas to allow for some clients which may send them without the correct xmlns
Matthew Wild <mwild1@gmail.com>
parents:
1664
diff
changeset
|
24 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send) |
2467
8098310d0dfd
mod_bosh: Update for new xmlhandlers syntax
Matthew Wild <mwild1@gmail.com>
parents:
2373
diff
changeset
|
26 local stream_callbacks = { stream_ns = "http://jabber.org/protocol/httpbind", stream_tag = "body", default_ns = xmlns_bosh }; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
1655
dc42bf326713
mod_bosh: Updated to use module:get_option instead of configmanager
Waqas Hussain <waqas20@gmail.com>
parents:
1633
diff
changeset
|
28 local BOSH_DEFAULT_HOLD = tonumber(module:get_option("bosh_default_hold")) or 1; |
dc42bf326713
mod_bosh: Updated to use module:get_option instead of configmanager
Waqas Hussain <waqas20@gmail.com>
parents:
1633
diff
changeset
|
29 local BOSH_DEFAULT_INACTIVITY = tonumber(module:get_option("bosh_max_inactivity")) or 60; |
dc42bf326713
mod_bosh: Updated to use module:get_option instead of configmanager
Waqas Hussain <waqas20@gmail.com>
parents:
1633
diff
changeset
|
30 local BOSH_DEFAULT_POLLING = tonumber(module:get_option("bosh_max_polling")) or 5; |
dc42bf326713
mod_bosh: Updated to use module:get_option instead of configmanager
Waqas Hussain <waqas20@gmail.com>
parents:
1633
diff
changeset
|
31 local BOSH_DEFAULT_REQUESTS = tonumber(module:get_option("bosh_max_requests")) or 2; |
dc42bf326713
mod_bosh: Updated to use module:get_option instead of configmanager
Waqas Hussain <waqas20@gmail.com>
parents:
1633
diff
changeset
|
32 local BOSH_DEFAULT_MAXPAUSE = tonumber(module:get_option("bosh_max_pause")) or 300; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
2486 | 34 local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8" }; |
1662
b31ea419dff5
mod_bosh: Correct reply when closing sessions
Matthew Wild <mwild1@gmail.com>
parents:
1655
diff
changeset
|
35 local session_close_reply = { headers = default_headers, body = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate" }), attr = {} }; |
866
8958fe4b2391
mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents:
816
diff
changeset
|
36 |
2484
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
37 local cross_domain = module:get_option("cross_domain_bosh"); |
2485
ace62f19076d
mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents:
2484
diff
changeset
|
38 if cross_domain then |
ace62f19076d
mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents:
2484
diff
changeset
|
39 default_headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"; |
ace62f19076d
mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents:
2484
diff
changeset
|
40 default_headers["Access-Control-Allow-Headers"] = "Content-Type"; |
ace62f19076d
mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents:
2484
diff
changeset
|
41 default_headers["Access-Control-Max-Age"] = "7200"; |
2484
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
42 |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
43 if cross_domain == true then |
2485
ace62f19076d
mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents:
2484
diff
changeset
|
44 default_headers["Access-Control-Allow-Origin"] = "*"; |
2484
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
45 elseif type(cross_domain) == "table" then |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
46 cross_domain = table.concat(cross_domain, ", "); |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
47 end |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
48 if type(cross_domain) == "string" then |
2485
ace62f19076d
mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents:
2484
diff
changeset
|
49 default_headers["Access-Control-Allow-Origin"] = cross_domain; |
2484
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
50 end |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
51 end |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
52 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 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
|
54 local os_time = os.time; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local sessions = {}; |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
57 local inactive_sessions = {}; -- Sessions which have no open requests |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 |
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
|
59 -- 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
|
60 local waiting_requests = {}; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 function on_destroy_request(request) |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 waiting_requests[request] = nil; |
947
84202314ab7f
mod_bosh: Fix to correctly timeout idle sessions
Matthew Wild <mwild1@gmail.com>
parents:
866
diff
changeset
|
63 local session = sessions[request.sid]; |
816
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
64 if session then |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
65 local requests = session.requests; |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
66 for i,r in pairs(requests) do |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
67 if r == request then requests[i] = nil; break; end |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
68 end |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
69 |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
70 -- If this session now has no requests open, mark it as inactive |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
71 if #requests == 0 and session.bosh_max_inactive and not inactive_sessions[session] then |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
72 inactive_sessions[session] = os_time(); |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
73 (session.log or log)("debug", "BOSH session marked as inactive at %d", inactive_sessions[session]); |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
74 end |
c031ead9896d
mod_bosh: Possible fix for invalid key to next crash
Matthew Wild <mwild1@gmail.com>
parents:
772
diff
changeset
|
75 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 function handle_request(method, body, request) |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 if (not body) or request.method ~= "POST" then |
2484
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
80 if request.method == "OPTIONS" then |
2485
ace62f19076d
mod_bosh: Simplify cross-domain support, and make it work - default is for cross-domain to be disallowed
Matthew Wild <mwild1@gmail.com>
parents:
2484
diff
changeset
|
81 return { headers = default_headers, body = "" }; |
2484
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
82 else |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
83 return "<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>"; |
cf924f587410
mod_bosh: Support for cross-domain access control using CORS
Matthew Wild <mwild1@gmail.com>
parents:
2473
diff
changeset
|
84 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 end |
2473
3f4cfa375bd6
mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2467
diff
changeset
|
86 if not method then |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 log("debug", "Request %s suffered error %s", tostring(request.id), body); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 return; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 end |
771
ecdf72f9b085
Remove redundant logging and debug printing from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
763
diff
changeset
|
90 --log("debug", "Handling new request %s: %s\n----------", request.id, tostring(body)); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 request.notopen = true; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 request.log = log; |
2077
e33658f6052c
Changed separator between attribute names and prefixes from '|' to '\1' (optimization and cleanup).
Waqas Hussain <waqas20@gmail.com>
parents:
1869
diff
changeset
|
93 local parser = lxp.new(init_xmlhandlers(request, stream_callbacks), "\1"); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 parser:parse(body); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 local session = sessions[request.sid]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 if session then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 local r = session.requests; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 log("debug", "Session %s has %d out of %d requests open", request.sid, #r, session.bosh_hold); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 log("debug", "and there are %d things in the send_buffer", #session.send_buffer); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 if #r > session.bosh_hold then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 -- 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
|
104 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
|
105 if #session.send_buffer > 0 then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 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
|
107 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
|
108 session.send_buffer = {}; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 else |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 -- or an empty response |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 log("debug", "...sending an empty response"); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 session.send(""); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 elseif #session.send_buffer > 0 then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 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
|
116 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
|
117 session.send_buffer = {}; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 session.send(resp); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 if not request.destroyed and session.bosh_wait then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 request.reply_before = os_time() + session.bosh_wait; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 request.on_destroy = on_destroy_request; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 waiting_requests[request] = true; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 |
771
ecdf72f9b085
Remove redundant logging and debug printing from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
763
diff
changeset
|
127 log("debug", "Have nothing to say, so leaving request unanswered for now"); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 return true; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 |
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
|
132 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 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
|
134 |
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
|
135 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
|
136 (session.log or log)("info", "BOSH client disconnected"); |
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
|
137 session_close_reply.attr.condition = 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
|
138 for _, held_request in ipairs(session.requests) do |
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
|
139 held_request:send(session_close_reply); |
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
|
140 held_request:destroy(); |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
141 end |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
142 sessions[session.sid] = nil; |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
143 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
|
144 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 function stream_callbacks.streamopened(request, attr) |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 log("debug", "BOSH body open (sid: %s)", attr.sid); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 local sid = attr.sid |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 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
|
150 -- New session request |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
151 request.notopen = nil; -- Signals that we accept this opening tag |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
152 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 -- 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
|
154 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
|
155 -- Unknown host |
1048
45fc590539cd
mod_bosh: Add log message for clients connecting to unknown host
Matthew Wild <mwild1@gmail.com>
parents:
1047
diff
changeset
|
156 log("debug", "BOSH client tried to connect to unknown host: %s", tostring(attr.to)); |
1633
7812459eeed7
mod_bosh: Fix error reply for host-unknown errors
Matthew Wild <mwild1@gmail.com>
parents:
1551
diff
changeset
|
157 session_close_reply.body.attr.condition = "host-unknown"; |
7812459eeed7
mod_bosh: Fix error reply for host-unknown errors
Matthew Wild <mwild1@gmail.com>
parents:
1551
diff
changeset
|
158 request:send(session_close_reply); |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
159 request.notopen = nil |
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
160 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
|
161 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 -- New session |
763
8e77a39826c2
mod_bosh: No need to tostring() uuids now
Matthew Wild <mwild1@gmail.com>
parents:
725
diff
changeset
|
164 sid = new_uuid(); |
1663
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
165 local session = { type = "c2s_unauthed", conn = {}, sid = sid, rid = tonumber(attr.rid), host = attr.to, bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid, |
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
|
166 bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY, |
1109
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
167 requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream, close = bosh_close_stream, |
1541
591732da1306
mod_bosh: Mark a session as secure if it is created with HTTPS
Matthew Wild <mwild1@gmail.com>
parents:
1537
diff
changeset
|
168 dispatch_stanza = core_process_stanza, log = logger.init("bosh"..sid), secure = request.secure }; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 sessions[sid] = session; |
1109
bb21eb3cd364
mod_bosh: Give BOSH sessions a logger (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1049
diff
changeset
|
170 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 log("info", "New BOSH session, assigned it sid '%s'", sid); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 local r, send_buffer = session.requests, session.send_buffer; |
866
8958fe4b2391
mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents:
816
diff
changeset
|
173 local response = { headers = default_headers } |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 function session.send(s) |
2099
73e083d01449
mod_bosh: Don't log response XML
Matthew Wild <mwild1@gmail.com>
parents:
2084
diff
changeset
|
175 --log("debug", "Sending BOSH data: %s", tostring(s)); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 local oldest_request = r[1]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 while oldest_request and oldest_request.destroyed do |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 t_remove(r, 1); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 waiting_requests[oldest_request] = nil; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 oldest_request = r[1]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 if oldest_request then |
2099
73e083d01449
mod_bosh: Don't log response XML
Matthew Wild <mwild1@gmail.com>
parents:
2084
diff
changeset
|
183 log("debug", "We have an open request, so sending on that"); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 response.body = t_concat{"<body xmlns='http://jabber.org/protocol/httpbind' sid='", sid, "' xmlns:stream = 'http://etherx.jabber.org/streams'>", tostring(s), "</body>" }; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 oldest_request:send(response); |
771
ecdf72f9b085
Remove redundant logging and debug printing from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
763
diff
changeset
|
186 --log("debug", "Sent"); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 if oldest_request.stayopen then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 if #r>1 then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 -- Move front request to back |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 t_insert(r, oldest_request); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 t_remove(r, 1); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 else |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 log("debug", "Destroying the request now..."); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 oldest_request:destroy(); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 t_remove(r, 1); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 elseif s ~= "" then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 log("debug", "Saved to send buffer because there are %d open requests", #r); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 -- Hmm, no requests are open :( |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 t_insert(session.send_buffer, tostring(s)); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 log("debug", "There are now %d things in the send_buffer", #session.send_buffer); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 -- Send creation response |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 local features = st.stanza("stream:features"); |
2608
68c43aaf681f
mod_bosh: Fire stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2486
diff
changeset
|
209 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features }); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
210 fire_event("stream-features", session, features); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 --xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh' |
2473
3f4cfa375bd6
mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2467
diff
changeset
|
212 local response = st.stanza("body", { xmlns = xmlns_bosh, |
3f4cfa375bd6
mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2467
diff
changeset
|
213 inactivity = tostring(BOSH_DEFAULT_INACTIVITY), polling = tostring(BOSH_DEFAULT_POLLING), requests = tostring(BOSH_DEFAULT_REQUESTS), hold = tostring(session.bosh_hold), maxpause = "120", |
3f4cfa375bd6
mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2467
diff
changeset
|
214 sid = sid, authid = sid, ver = '1.6', from = session.host, secure = 'true', ["xmpp:version"] = "1.0", |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 ["xmlns:xmpp"] = "urn:xmpp:xbosh", ["xmlns:stream"] = "http://etherx.jabber.org/streams" }):add_child(features); |
866
8958fe4b2391
mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents:
816
diff
changeset
|
216 request:send{ headers = default_headers, body = tostring(response) }; |
2473
3f4cfa375bd6
mod_bosh: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2467
diff
changeset
|
217 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 request.sid = sid; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 return; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 local session = sessions[sid]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 if not session then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 -- Unknown sid |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 log("info", "Client tried to use sid '%s' which we don't know about", sid); |
866
8958fe4b2391
mod_bosh: Set Content-Type in response headers
Matthew Wild <mwild1@gmail.com>
parents:
816
diff
changeset
|
226 request:send{ headers = default_headers, body = tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" })) }; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 request.notopen = nil; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 return; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 |
1663
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
231 if session.rid then |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
232 local rid = tonumber(attr.rid); |
1664
6587b6c2678e
mod_bosh: Calculate rid difference just once
Matthew Wild <mwild1@gmail.com>
parents:
1663
diff
changeset
|
233 local diff = rid - session.rid; |
6587b6c2678e
mod_bosh: Calculate rid difference just once
Matthew Wild <mwild1@gmail.com>
parents:
1663
diff
changeset
|
234 if diff > 1 then |
1663
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
235 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
|
236 elseif diff <= 0 then |
1663
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
237 -- Repeated, ignore |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
238 session.log("debug", "rid repeated (on request %s), ignoring: %d", request.id, session.rid); |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
239 request.notopen = nil; |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
240 t_insert(session.requests, request); |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
241 return; |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
242 end |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
243 session.rid = rid; |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
244 end |
b30c4d0bbe84
mod_bosh: Basic handling of rids (more to come)
Matthew Wild <mwild1@gmail.com>
parents:
1662
diff
changeset
|
245 |
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
|
246 if attr.type == "terminate" then |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
247 -- Client wants to end this session |
684
b7d85c6a0002
Implement session:close() for BOSH, and add checking for attempts to connect to hosts we don't serve
Matthew Wild <mwild1@gmail.com>
parents:
683
diff
changeset
|
248 session:close(); |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
249 request.notopen = nil; |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
250 return; |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
251 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
|
252 |
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
|
253 -- If session was inactive, make sure it is now marked as not |
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
|
254 if #session.requests == 0 then |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
255 (session.log or log)("debug", "BOSH client now active again at %d", os_time()); |
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
|
256 inactive_sessions[session] = nil; |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
257 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
|
258 |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
259 if session.notopen then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 local features = st.stanza("stream:features"); |
2608
68c43aaf681f
mod_bosh: Fire stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2486
diff
changeset
|
261 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features }); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
262 fire_event("stream-features", session, features); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
263 session.send(features); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 session.notopen = nil; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
266 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
267 request.notopen = nil; -- Signals that we accept this opening tag |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
268 t_insert(session.requests, request); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
269 request.sid = sid; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 function stream_callbacks.handlestanza(request, stanza) |
725
96110075288b
Replacing pretty_print() with top_tag() for logging
Matthew Wild <mwild1@gmail.com>
parents:
701
diff
changeset
|
273 log("debug", "BOSH stanza received: %s\n", stanza:top_tag()); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
274 local session = sessions[request.sid]; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
275 if session then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
276 if stanza.attr.xmlns == xmlns_bosh then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
277 stanza.attr.xmlns = "jabber:client"; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 end |
2373
13090fc6ad90
mod_bosh: Update for new connection API
Matthew Wild <mwild1@gmail.com>
parents:
2354
diff
changeset
|
279 session.ip = request.handler:ip(); |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
280 core_process_stanza(session, stanza); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
282 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 |
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
|
284 local dead_sessions = {}; |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
285 function on_timer() |
660
b2c4a7ec31c6
Remove some old debugging code from mod_bosh
Matthew Wild <mwild1@gmail.com>
parents:
636
diff
changeset
|
286 -- 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
|
287 -- 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
|
288 local now = os_time() + 3; |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
289 for request in pairs(waiting_requests) do |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
290 if request.reply_before <= now then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
291 log("debug", "%s was soon to timeout, sending empty response", request.id); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
292 -- Send empty response to let the |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
293 -- client know we're still here |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
294 if request.conn then |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
295 sessions[request.sid].send(""); |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
296 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
297 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
298 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
|
299 |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
300 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
|
301 local n_dead_sessions = 0; |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
302 for session, inactive_since in pairs(inactive_sessions) do |
692
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
303 if session.bosh_max_inactive then |
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
304 if now - inactive_since > session.bosh_max_inactive then |
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
305 (session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now); |
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
306 sessions[session.sid] = nil; |
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
307 inactive_sessions[session] = nil; |
1864
b9389286eece
mod_bosh: Fix for 'invalid key to next' error when 2 clients lose connection at the same time
Matthew Wild <mwild1@gmail.com>
parents:
1633
diff
changeset
|
308 n_dead_sessions = n_dead_sessions + 1; |
b9389286eece
mod_bosh: Fix for 'invalid key to next' error when 2 clients lose connection at the same time
Matthew Wild <mwild1@gmail.com>
parents:
1633
diff
changeset
|
309 dead_sessions[n_dead_sessions] = session; |
692
4a218377f4e3
BOSH: Fix for error when closed session was in inactive_sessions list
Matthew Wild <mwild1@gmail.com>
parents:
684
diff
changeset
|
310 end |
693
d8b793e612a9
BOSH: Make previous fix a bit more efficient
Matthew Wild <mwild1@gmail.com>
parents:
692
diff
changeset
|
311 else |
679
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
312 inactive_sessions[session] = nil; |
9506bf204b1a
Numerous BOSH improvements... handle client disconnects, either explicit or implicit through inactivity; allow specifying BOSH default parameters through config; fix to prevent prematurely closing request connections in some cases, before they were replied to
Matthew Wild <mwild1@gmail.com>
parents:
660
diff
changeset
|
313 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
|
314 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
|
315 |
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
|
316 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
|
317 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
|
318 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
|
319 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
|
320 end |
636
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
321 end |
9c9c671ecc50
Initial mod_bosh, works, kind of, but quite incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
322 |
701
dc67e3cffff4
BOSH: Allow BOSH servers to be configured through config file
Matthew Wild <mwild1@gmail.com>
parents:
693
diff
changeset
|
323 |
2354
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
324 local function setup() |
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
325 local ports = module:get_option("bosh_ports") or { 5280 }; |
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
326 httpserver.new_from_config(ports, handle_request, { base = "http-bind" }); |
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
327 server.addtimer(on_timer); |
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
328 end |
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
329 if prosody.start_time then -- already started |
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
330 setup(); |
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
331 else |
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
332 prosody.events.add_handler("server-started", setup); |
5a00668ba0ab
mod_bosh: Delay setup until after server is started.
Waqas Hussain <waqas20@gmail.com>
parents:
2099
diff
changeset
|
333 end |