forked from Countly/countly-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountlyConnectionQueue.cpp
301 lines (257 loc) · 8.17 KB
/
CountlyConnectionQueue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
CountlyConnectionQueue.cpp
CountlyCpp
Created by Benoit Girard on 26/10/14.
The MIT License (MIT)
Copyright (c) 2015 Kontrol SAS (tanker.io)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "CountlyConnectionQueue.h"
#include <assert.h>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#ifdef _WIN32
#include <Windows.h>
#include <WinHTTP.h>
#else
#include <curl/curl.h>
#endif
#include "Countly.h"
// https://count.ly/resources/reference/server-api
#define KEEPALIVE 30 // send keepalive every 30s
#define BUFFSIZE 512
using std::string;
namespace CountlyCpp {
CountlyConnectionQueue::CountlyConnectionQueue() {
_maxEvents = 50;
_version = COUNTLY_VERSION;
_lastSend = 0;
_beginSessionSent = false;
#ifndef _WIN32
curl_global_init(CURL_GLOBAL_ALL);
#endif
}
CountlyConnectionQueue::~CountlyConnectionQueue() {
if (_beginSessionSent) {
string URI = "/i?app_key=" + _appKey + "&device_id=" +
_deviceId + "&end_session=1";
HTTPGET(URI);
}
#ifndef _WIN32
curl_global_cleanup();
#endif
}
void CountlyConnectionQueue::SetAppKey(string key) {
_appKey = key;
}
void CountlyConnectionQueue::SetAppHost(string host, int port) {
_appHost = host;
_appPort = port;
if (host.find("http://") == 0) {
_https = false;
_appHostName = host.substr(7);
} else if (_appHost.find("https://") == 0) {
_https = true;
_appHostName = host.substr(8);
} else {
assert(0);
}
// deal with http://bla.com/
size_t p = _appHostName.find("/");
if (p != string::npos)
_appHostName = _appHostName.substr(0, p);
}
void CountlyConnectionQueue::SetMaxEventsPerMessage(int maxEvents) {
_maxEvents = maxEvents;
}
void CountlyConnectionQueue::SetMetrics(string os, string os_version,
string device, string resolution, string carrier, string app_version) {
_os = os;
_os_version = os_version;
_device = device;
_resolution = resolution;
_carrier = carrier;
_app_version = app_version;
}
bool CountlyConnectionQueue::BeginSession() {
string URI = "/i?app_key=" + _appKey + "&device_id=" +
_deviceId + "&sdk_version=" + _version + "&begin_session=1";
bool metricsOk = false;
string metrics = "{\n";
if (_os.size()) {
metrics += "\"_os\":\"" + _os + "\"";
metricsOk = true;
}
if (_os_version.size()) {
if (metricsOk) metrics += ",\n";
metrics += "\"_os_version\":\"" + _os_version + "\"";
metricsOk = true;
}
if (_device.size()) {
if (metricsOk) metrics += ",\n";
metrics += "\"_device\":\"" + _device + "\"";
metricsOk = true;
}
if (_resolution.size()) {
if (metricsOk) metrics += ",\n";
metrics += "\"_resolution\":\"" + _resolution + "\"";
metricsOk = true;
}
if (_carrier.size()) {
if (metricsOk) metrics += ",\n";
metrics += "\"_carrier\":\"" + _carrier + "\"";
metricsOk = true;
}
if (_app_version.size()) {
if (metricsOk) metrics += ",\n";
metrics += "\"_app_version\":\"" + _app_version + "\"";
metricsOk = true;
}
metrics += "\n}";
if (metricsOk)
URI += "&metrics=" + URLEncode(metrics);
if (!HTTPGET(URI)) return false;
_lastSend = Countly::GetTimestamp();
return true;
}
// returns true only if no more events to send
bool CountlyConnectionQueue::UpdateSession(CountlyEventQueue* queue) {
int evtId;
std::vector<int> evtIds;
string all;
string URI;
if (!_deviceId.size())
_deviceId = queue->GetDeviceId();
if (!_beginSessionSent) {
if (!BeginSession()) return false;
_beginSessionSent = true;
}
string json;
for (int i = 0; i < _maxEvents; i++) {
json = queue->PopEvent(&evtId, i);
if (evtId == -1) break;
evtIds.push_back(evtId);
if (i > 0) all = all + ",";
all = all + json;
}
if (evtIds.size() == 0) {
if (Countly::GetTimestamp() - _lastSend > KEEPALIVE * 1000) {
std::ostringstream URI;
URI << "/i?app_key=" + _appKey + "&device_id=" +
_deviceId + "&session_duration=";
URI << KEEPALIVE;
if (!HTTPGET(URI.str())) return false;
_lastSend = Countly::GetTimestamp();
}
return true; // true is only here. no events
// and successful keepalive (if needed)
}
all = "[" + all + "]";
URI = "/i?app_key=" + _appKey + "&device_id=" +
_deviceId + "&events=" + URLEncode(all);
if (!HTTPGET(URI)) return false;
_lastSend = Countly::GetTimestamp();
for (size_t i = 0; i < evtIds.size(); i++)
queue->ClearEvent(evtIds[i]);
return false; // false! see above
}
string CountlyConnectionQueue::URLEncode(const string &value) {
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
string::value_type c = (*i);
// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
}
// Any other characters are percent-encoded
escaped << '%' << std::setw(2) << int((unsigned char) c);
}
return escaped.str();
}
bool CountlyConnectionQueue::HTTPGET(string URI) {
bool ok = false;
// printf("%s\n", URI.c_str());
#ifndef _WIN32
CURL* curl;
CURLcode code;
curl = curl_easy_init();
if (curl) {
std::stringstream fullURI;
fullURI << (_https ? "https://" : "http://");
fullURI << _appHostName << ":" << _appPort << URI;
curl_easy_setopt(curl, CURLOPT_URL, fullURI.str().c_str());
curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
code = curl_easy_perform(curl);
if (code == CURLE_OK) {
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
ok = (code == 200);
}
curl_easy_cleanup(curl);
}
#else
HINTERNET hSession;
HINTERNET hConnect;
HINTERNET hRequest;
hSession = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (hSession) {
wchar_t wideHostName[256];
MultiByteToWideChar(0, 0, _appHostName.c_str(), -1, wideHostName, 256);
hConnect = WinHttpConnect(hSession, wideHostName, _appPort, 0);
}
if (hConnect) {
wchar_t wideURI[65536];
MultiByteToWideChar(0, 0, URI.c_str(), -1, wideURI, 65536);
hRequest = WinHttpOpenRequest(hConnect, L"GET", wideURI, NULL,
WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
_https ? WINHTTP_FLAG_SECURE : 0);
}
if (hRequest) {
std::stringstream headers;
headers << "User-Agent: Countly " << Countly::GetVersion();
wchar_t wideHeaders[256];
MultiByteToWideChar(0, 0, headers.str().c_str(), -1, wideHeaders, 256);
ok = WinHttpSendRequest(hRequest, wideHeaders, headers.str().size(),
WINHTTP_NO_REQUEST_DATA, 0, 0, 0) != 0;
if (ok) {
ok = WinHttpReceiveResponse(hRequest, NULL) != 0;
if (ok) {
DWORD dwStatusCode = 0;
DWORD dwSize = sizeof(dwStatusCode);
WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE |
WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX,
&dwStatusCode, &dwSize, WINHTTP_NO_HEADER_INDEX);
ok = dwStatusCode == 200;
}
}
WinHttpCloseHandle(hRequest);
}
if (hConnect) {
WinHttpCloseHandle(hConnect);
}
if (hSession) {
WinHttpCloseHandle(hSession);
}
#endif
return ok;
}
} // namespace CountlyCpp