Annotate

bosh.lua @ 495:1ca531495c3d

squishy: Move some compat layers into verse namespace This way, if the prosody libraries are available, they could be used from the verse compat layres
author Kim Alvefur <zash@zash.se>
date Wed, 21 Jun 2023 21:03:05 +0200
parent 490:6b2f31da9610
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
490
6b2f31da9610 Update for new Prosody module namespace
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
2 local new_xmpp_stream = require "prosody.util.xmppstream".new;
6b2f31da9610 Update for new Prosody module namespace
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
3 local st = require "prosody.util.stanza";
6b2f31da9610 Update for new Prosody module namespace
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
4 local http = require "prosody.net.http";
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local stream_mt = setmetatable({}, { __index = verse.stream_mt });
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 stream_mt.__index = stream_mt;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local xmlns_stream = "http://etherx.jabber.org/streams";
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local xmlns_bosh = "http://jabber.org/protocol/httpbind";
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
12 local reconnect_timeout = 5;
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
13
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 function verse.new_bosh(logger, url)
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local stream = {
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 bosh_conn_pool = {};
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 bosh_waiting_requests = {};
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 bosh_rid = math.random(1,999999);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 bosh_outgoing_buffer = {};
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 bosh_url = url;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 conn = {};
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 };
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 function stream:reopen()
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 self.bosh_need_restart = true;
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
25 self:flush();
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local conn = verse.new(logger, stream);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 return setmetatable(conn, stream_mt);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 function stream_mt:connect()
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 self:_send_session_request();
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 function stream_mt:send(data)
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 self:debug("Putting into BOSH send buffer: %s", tostring(data));
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 self.bosh_outgoing_buffer[#self.bosh_outgoing_buffer+1] = st.clone(data);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 self:flush(); --TODO: Optimize by doing this on next tick (give a chance for data to buffer)
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
41 function stream_mt:flush()
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if self.connected
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 and #self.bosh_waiting_requests < self.bosh_max_requests
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
44 and (#self.bosh_waiting_requests == 0
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
45 or #self.bosh_outgoing_buffer > 0
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
46 or self.bosh_need_restart) then
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 self:debug("Flushing...");
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local payload = self:_make_body();
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local buffer = self.bosh_outgoing_buffer;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 for i, stanza in ipairs(buffer) do
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 payload:add_child(stanza);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 buffer[i] = nil;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
54 self:_make_request(payload);
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 else
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 self:debug("Decided not to flush.");
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
60 function stream_mt:_make_request(payload)
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
61 local request, err = http.request(self.bosh_url, { body = tostring(payload) }, function (response, code, request)
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
62 if code ~= 0 then
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
63 self.inactive_since = nil;
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
64 return self:_handle_response(response, code, request);
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
65 end
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 262
diff changeset
66
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
67 -- Connection issues, we need to retry this request
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
68 local time = os.time();
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
69 if not self.inactive_since then
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
70 self.inactive_since = time; -- So we know when it is time to give up
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
71 elseif time - self.inactive_since > self.bosh_max_inactivity then
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
72 return self:_disconnected();
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
73 else
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 262
diff changeset
74 self:debug("%d seconds left to reconnect, retrying in %d seconds...",
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
75 self.bosh_max_inactivity - (time - self.inactive_since), reconnect_timeout);
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
76 end
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 262
diff changeset
77
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
78 -- Set up reconnect timer
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
79 timer.add_task(reconnect_timeout, function ()
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
80 self:debug("Retrying request...");
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
81 -- Remove old request
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
82 for i, waiting_request in ipairs(self.bosh_waiting_requests) do
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
83 if waiting_request == request then
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
84 table.remove(self.bosh_waiting_requests, i);
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
85 break;
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
86 end
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
87 end
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
88 self:_make_request(payload);
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
89 end);
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
90 end);
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
91 if request then
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
92 table.insert(self.bosh_waiting_requests, request);
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
93 else
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
94 self:warn("Request failed instantly: %s", err);
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
95 end
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
96 end
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
97
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
98 function stream_mt:_disconnected()
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
99 self.connected = nil;
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
100 self:event("disconnected");
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
101 end
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
102
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 function stream_mt:_send_session_request()
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 local body = self:_make_body();
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 262
diff changeset
105
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 -- XEP-0124
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 body.attr.hold = "1";
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 body.attr.wait = "60";
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 body.attr["xml:lang"] = "en";
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 body.attr.ver = "1.6";
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 -- XEP-0206
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 body.attr.from = self.jid;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 body.attr.to = self.host;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 body.attr.secure = 'true';
411
db462d4feb44 verse: trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 262
diff changeset
116
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
117 http.request(self.bosh_url, { body = tostring(body) }, function (response, code)
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
118 if code == 0 then
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
119 -- Failed to connect
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
120 return self:_disconnected();
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
121 end
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 -- Handle session creation response
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 local payload = self:_parse_response(response)
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 if not payload then
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 self:warn("Invalid session creation response");
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
126 self:_disconnected();
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 return;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
129 self.bosh_sid = payload.attr.sid; -- Session id
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
130 self.bosh_wait = tonumber(payload.attr.wait); -- How long the server may hold connections for
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
131 self.bosh_hold = tonumber(payload.attr.hold); -- How many connections the server may hold
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
132 self.bosh_max_inactivity = tonumber(payload.attr.inactivity); -- Max amount of time with no connections
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
133 self.bosh_max_requests = tonumber(payload.attr.requests) or self.bosh_hold; -- Max simultaneous requests we can make
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 self.connected = true;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 self:event("connected");
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 self:_handle_response_payload(payload);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 end);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 function stream_mt:_handle_response(response, code, request)
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 if self.bosh_waiting_requests[1] ~= request then
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 self:warn("Server replied to request that wasn't the oldest");
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
143 for i, waiting_request in ipairs(self.bosh_waiting_requests) do
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
144 if waiting_request == request then
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
145 self.bosh_waiting_requests[i] = nil;
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
146 break;
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
147 end
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
148 end
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 else
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 table.remove(self.bosh_waiting_requests, 1);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 local payload = self:_parse_response(response);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 if payload then
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 self:_handle_response_payload(payload);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 end
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
156 self:flush();
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 function stream_mt:_handle_response_payload(payload)
261
c1404c69dec9 verse.bosh: Fix to loop over every child tag (childtags() is now too namespace-aware for this purpose)
Matthew Wild <mwild1@gmail.com>
parents: 161
diff changeset
160 local stanzas = payload.tags;
c1404c69dec9 verse.bosh: Fix to loop over every child tag (childtags() is now too namespace-aware for this purpose)
Matthew Wild <mwild1@gmail.com>
parents: 161
diff changeset
161 for i = 1, #stanzas do
c1404c69dec9 verse.bosh: Fix to loop over every child tag (childtags() is now too namespace-aware for this purpose)
Matthew Wild <mwild1@gmail.com>
parents: 161
diff changeset
162 local stanza = stanzas[i];
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 if stanza.attr.xmlns == xmlns_stream then
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 self:event("stream-"..stanza.name, stanza);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 elseif stanza.attr.xmlns then
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 self:event("stream/"..stanza.attr.xmlns, stanza);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 else
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 self:event("stanza", stanza);
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
171 if payload.attr.type == "terminate" then
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
172 self:_disconnected({reason = payload.attr.condition});
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
173 end
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 local stream_callbacks = {
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 stream_ns = "http://jabber.org/protocol/httpbind", stream_tag = "body",
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 default_ns = "jabber:client",
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 streamopened = function (session, attr) session.notopen = nil; session.payload = verse.stanza("body", attr); return true; end;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 handlestanza = function (session, stanza) session.payload:add_child(stanza); end;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 };
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 function stream_mt:_parse_response(response)
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 self:debug("Parsing response: %s", response);
93
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
184 if response == nil then
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
185 self:debug("%s", debug.traceback());
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
186 self:_disconnected();
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
187 return;
2442e751f3cb verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents: 89
diff changeset
188 end
262
f47afb171e6e verse.bosh: Minor change to pass Verse stream to stream callbacks (though it isn't currently used by them)
Matthew Wild <mwild1@gmail.com>
parents: 261
diff changeset
189 local session = { notopen = true, stream = self };
161
b177bcea2006 squishy, verse.client, verse.component, verse.bosh: Port to util.xmppstream instead of xmlhandlers which has been removed from Prosody. Also remove util.ztact from squishy for the same reason.
Matthew Wild <mwild1@gmail.com>
parents: 93
diff changeset
190 local stream = new_xmpp_stream(session, stream_callbacks);
b177bcea2006 squishy, verse.client, verse.component, verse.bosh: Port to util.xmppstream instead of xmlhandlers which has been removed from Prosody. Also remove util.ztact from squishy for the same reason.
Matthew Wild <mwild1@gmail.com>
parents: 93
diff changeset
191 stream:feed(response);
87
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 return session.payload;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 function stream_mt:_make_body()
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 self.bosh_rid = self.bosh_rid + 1;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 local body = verse.stanza("body", {
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 xmlns = xmlns_bosh;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 content = "text/xml; charset=utf-8";
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 sid = self.bosh_sid;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 rid = self.bosh_rid;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 });
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 if self.bosh_need_restart then
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 self.bosh_need_restart = nil;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 body.attr.restart = 'true';
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 end
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 return body;
d59073722924 verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 end