forked from snowplow/snowplow-cpp-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client.cpp
More file actions
262 lines (210 loc) · 8.33 KB
/
http_client.cpp
File metadata and controls
262 lines (210 loc) · 8.33 KB
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
/*
Copyright (c) 2016 Snowplow Analytics Ltd. All rights reserved.
This program is licensed to you under the Apache License Version 2.0,
and you may not use this file except in compliance with the Apache License Version 2.0.
You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing,
software distributed under the Apache License Version 2.0 is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
#include "http_client.hpp"
// --- Common
HttpRequestResult HttpClient::http_post(const CrackedUrl url, const string & post_data, list<int> row_ids, bool oversize) {
HttpRequestResult res = HttpClient::http_request(POST, url, "", post_data, row_ids, oversize);
return res;
}
HttpRequestResult HttpClient::http_get(const CrackedUrl url, const string & query_string, list<int> row_ids, bool oversize) {
HttpRequestResult res = HttpClient::http_request(GET, url, query_string, "", row_ids, oversize);
return res;
}
// --- Testing
#if defined(SNOWPLOW_TEST_SUITE)
const string HttpClient::TRACKER_AGENT = string("Snowplow C++ Tracker (Integration tests)");
list<HttpClient::Request> HttpClient::requests_list;
mutex HttpClient::log_read_write;
int HttpClient::response_code = 200;
HttpRequestResult HttpClient::http_request(const RequestMethod method, CrackedUrl url, const string & query_string, const string & post_data, list<int> row_ids, bool oversize) {
lock_guard<mutex> guard(log_read_write);
HttpClient::Request r;
r.method = method;
r.query_string = query_string;
r.post_data = post_data;
r.row_ids = row_ids;
r.oversize = oversize;
requests_list.push_back(r);
return HttpRequestResult(0, response_code, row_ids, oversize);
}
void HttpClient::set_http_response_code(int http_response_code) {
lock_guard<mutex> guard(log_read_write);
response_code = http_response_code;
}
list<HttpClient::Request> HttpClient::get_requests_list() {
lock_guard<mutex> guard(log_read_write);
return requests_list;
}
void HttpClient::reset() {
lock_guard<mutex> guard(log_read_write);
requests_list.clear();
response_code = 200;
}
// --- Windows32
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
const string HttpClient::TRACKER_AGENT = string("Snowplow C++ Tracker (Win32)");
HttpRequestResult HttpClient::http_request(const RequestMethod method, CrackedUrl url, const string & query_string, const string & post_data, list<int> row_ids, bool oversize) {
HINTERNET h_internet = InternetOpen(
TEXT(HttpClient::TRACKER_AGENT.c_str()),
INTERNET_OPEN_TYPE_DIRECT,
NULL,
NULL,
0
);
if (h_internet == NULL) {
return HttpRequestResult(GetLastError(), 0, row_ids, oversize);
}
unsigned int use_port = url.get_port();
if (url.get_use_default_port()) {
if (url.get_is_https()) {
use_port = INTERNET_DEFAULT_HTTPS_PORT;
} else {
use_port = INTERNET_DEFAULT_HTTP_PORT;
}
}
HINTERNET h_connect = InternetConnect(
h_internet,
TEXT(url.get_hostname().c_str()),
use_port,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
NULL
);
if (h_connect == NULL) {
InternetCloseHandle(h_internet);
return HttpRequestResult(GetLastError(), 0, row_ids, oversize);
}
DWORD flags = 0 | INTERNET_FLAG_RELOAD;
if (url.get_is_https()) {
flags = flags | INTERNET_FLAG_SECURE;
}
string final_path = url.get_path();
string request_method_string;
LPVOID post_buf;
int post_buf_len;
if (method == GET) {
request_method_string = "GET";
post_buf = NULL;
post_buf_len = 0;
final_path += "?" + query_string;
} else {
request_method_string = "POST";
post_buf = (LPVOID)TEXT(post_data.c_str());
post_buf_len = strlen(TEXT(post_data.c_str()));
}
HINTERNET h_request = HttpOpenRequest(
h_connect,
TEXT(request_method_string.c_str()),
TEXT(final_path.c_str()),
NULL,
NULL,
NULL,
flags,
0
);
if (h_request == NULL) {
InternetCloseHandle(h_internet);
InternetCloseHandle(h_connect);
return HttpRequestResult(GetLastError(), 0, row_ids, oversize);
}
LPCSTR hdrs = TEXT("Content-Type: application/json; charset=utf-8");
BOOL is_sent = HttpSendRequest(h_request, hdrs, strlen(hdrs), post_buf, post_buf_len);
if (!is_sent) {
InternetCloseHandle(h_internet);
InternetCloseHandle(h_connect);
InternetCloseHandle(h_request);
return HttpRequestResult(GetLastError(), 0, row_ids, oversize);
}
string response;
const int buf_len = 1024;
char buff[buf_len];
BOOL is_more = true;
DWORD bytes_read = -1;
while (is_more && bytes_read != 0) {
is_more = InternetReadFile(h_request, buff, buf_len, &bytes_read);
response.append(buff, bytes_read);
}
DWORD http_status_code = 0;
DWORD length = sizeof(DWORD);
HttpQueryInfo(
h_request,
HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
&http_status_code,
&length,
NULL
);
InternetCloseHandle(h_request);
InternetCloseHandle(h_connect);
InternetCloseHandle(h_internet);
return HttpRequestResult(0, http_status_code, row_ids, oversize);
}
// --- Mac OSX
#elif defined(__APPLE__)
const string HttpClient::TRACKER_AGENT = string("Snowplow C++ Tracker (MacOSX)");
HttpRequestResult HttpClient::http_request(const RequestMethod method, CrackedUrl url, const string & query_string, const string & post_data, list<int> row_ids, bool oversize) {
// Get final url
string final_url = url.to_string();
if (method == GET) {
final_url += "?" + query_string;
}
// Create request
CFStringRef cf_url_str = CFStringCreateWithBytes(kCFAllocatorDefault, (const unsigned char *) final_url.c_str(), final_url.length(), kCFStringEncodingUTF8, false);
CFStringRef cf_content_type_str = CFStringCreateWithBytes(kCFAllocatorDefault, (const unsigned char *) SNOWPLOW_POST_CONTENT_TYPE.c_str(), SNOWPLOW_POST_CONTENT_TYPE.length(), kCFStringEncodingUTF8, false);
CFStringRef cf_user_agent_str = CFStringCreateWithBytes(kCFAllocatorDefault, (const unsigned char *) HttpClient::TRACKER_AGENT.c_str(), HttpClient::TRACKER_AGENT.length(), kCFStringEncodingUTF8, false);
CFURLRef cf_url = CFURLCreateWithString(kCFAllocatorDefault, cf_url_str, NULL);
CFHTTPMessageRef cf_http_req;
if (method == GET) {
cf_http_req = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("GET"), cf_url, kCFHTTPVersion1_1);
} else {
cf_http_req = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("POST"), cf_url, kCFHTTPVersion1_1);
CFDataRef cf_post_data = CFDataCreate(kCFAllocatorDefault, (const UInt8*) post_data.data(), post_data.size());
CFHTTPMessageSetBody(cf_http_req, cf_post_data);
if (cf_post_data) {
CFRelease(cf_post_data);
}
CFHTTPMessageSetHeaderFieldValue(cf_http_req, CFSTR("Content-Type"), cf_content_type_str);
}
CFHTTPMessageSetHeaderFieldValue(cf_http_req, CFSTR("User-Agent"), cf_user_agent_str);
CFHTTPMessageSetHeaderFieldValue(cf_http_req, CFSTR("Connection"), CFSTR("keep-alive"));
CFReadStreamRef cf_read_stream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, cf_http_req);
CFMutableDataRef cf_data_resp = CFDataCreateMutable(kCFAllocatorDefault, 0);
// Send request
CFReadStreamOpen(cf_read_stream);
CFIndex num_bytes_read;
do {
const int buff_size = 1024;
UInt8 buff[buff_size];
num_bytes_read = CFReadStreamRead(cf_read_stream, buff, buff_size);
if (num_bytes_read > 0) {
CFDataAppendBytes(cf_data_resp, buff, num_bytes_read);
} else if (num_bytes_read < 0) {
CFStreamError error = CFReadStreamGetError(cf_read_stream);
cerr << error.error << endl;
}
} while (num_bytes_read > 0);
// Process result
CFHTTPMessageRef cf_http_resp = (CFHTTPMessageRef) CFReadStreamCopyProperty(cf_read_stream, kCFStreamPropertyHTTPResponseHeader);
int cf_status_code = CFHTTPMessageGetResponseStatusCode(cf_http_resp);
// Release resources
CFReadStreamClose(cf_read_stream);
CFRelease(cf_url_str);
CFRelease(cf_content_type_str);
CFRelease(cf_user_agent_str);
CFRelease(cf_url);
CFRelease(cf_http_req);
CFRelease(cf_read_stream);
CFRelease(cf_data_resp);
CFRelease(cf_http_resp);
return HttpRequestResult(0, cf_status_code, row_ids, oversize);
}
#endif