-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy path_test_curl.cpp
More file actions
356 lines (302 loc) · 7.39 KB
/
_test_curl.cpp
File metadata and controls
356 lines (302 loc) · 7.39 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
* @file _test_curl.cpp
* @brief
*
* @author Yonhgwhan, Roh (somma@somma.kr)
* @date 2020/02/24 21:41 created.
* @copyright (C)Somma, Inc. All rights reserved.
**/
#include "stdafx.h"
#include "_MyLib/src/curl_client.h"
#include "_MyLib/src/curl_client_support.h"
#include "_MyLib/src/StopWatch.h"
#include "json/json.h"
/// @brief
bool test_curl_https_down_with_auth()
{
_mem_dump_console
_mem_check_begin
{
std::string url;
std::string id;
std::string pw;
std::cout << "URL: ";
std::cin >> url;
std::cout << "ID : ";
std::cin >> id;
std::cout << "PW : ";
std::cin >> pw;
//
// 다운로드 임시파일 생성
//
std::wstring file_path = L"c:\\dbg\\download.dat";
handle_ptr temp_file(open_file_to_write(file_path.c_str()),
[](HANDLE h)
{
if (INVALID_HANDLE_VALUE != h)
{
CloseHandle(h);
}
});
if (temp_file.get() == INVALID_HANDLE_VALUE)
{
log_err
"Can not create file. path=%ws",
file_path.c_str()
log_end;
return false;
}
//
// creates a download context
//
http_download_ctx ctx(url.c_str(), temp_file.get());
//
// Create curl client
//
auto _cc = std::make_unique<curl_client>();
if (!_cc)
{
log_err "not enought memory" log_end;
return false;
}
if (true != _cc->initialize())
{
log_err "curl client initialize() failed." log_end;
return false;
}
//
// enable auth
//
_cc->enable_auth(id.c_str(), pw.c_str());
StopWatch sw; sw.Start();
//
// download
//
HTTP_CODE http_response_code = 404;
_ASSERTE(true == _cc->http_download_file(&ctx,
http_response_code));
if (200 == http_response_code)
{
_ASSERTE(is_file_existsW(file_path));
}
sw.Stop();
std::string md5;
std::string sha2;
_ASSERTE(ctx.get_md5(md5));
_ASSERTE(ctx.get_sha2(sha2));
log_info
"download url=%s, local=%ws, md5=%s, sha2=%s, elapsed=%f sec",
url.c_str(),
file_path.c_str(),
md5.c_str(),
sha2.c_str(),
sw.GetDurationSecond()
log_end;
_ASSERTE(_cc);
}
_mem_check_end;
return true;
}
/// @brief
bool test_curl_https()
{
pcurl_client _curl_client = new curl_client();
if (nullptr == _curl_client)
{
log_err "not enought memory" log_end;
return false;
}
std::stringstream http_header;
http_header << "authorization: Bearer "
<< "FOO";
if (true != _curl_client->initialize())
{
log_err "curl client initialize() failed." log_end;
return false;
}
_curl_client->append_header("authorization", http_header.str().c_str());
const char* url = "https://www.google.com/";
HTTP_CODE http_response_code = 404;
std::string res;
_ASSERTE(true == _curl_client->http_get(url, http_response_code, res));
_ASSERTE(_curl_client != nullptr);
delete _curl_client; _curl_client = nullptr;
return true;
}
/// @brief
bool test_curl_http()
{
pcurl_client _curl_client = new curl_client();
if (nullptr == _curl_client)
{
log_err "not enought memory" log_end;
return false;
}
if (true != _curl_client->initialize())
{
log_err "curl client initialize() failed." log_end;
return false;
}
const char* url = "http://192.168.10.200:5601/app/kibana#/monster?_g=()";
HTTP_CODE http_response_code = 404;
CMemoryStream stream;
_ASSERTE(true == _curl_client->http_get(url, http_response_code, stream));
auto dumps = dump_memory(0LL,
(unsigned char*)(stream.GetMemory()),
(UINT32)stream.GetSize());
std::for_each(dumps.begin(),
dumps.end(),
[](std::string& dump)
{
printf("%s\n", dump.c_str());
});
_ASSERTE(_curl_client != nullptr);
delete _curl_client; _curl_client = nullptr;
return true;
}
/// @brief
bool test_curl_http_upload()
{
pcurl_client client = new curl_client();
const wchar_t* path = L"C:\\Windows\\System32\\notepad.exe";
const char* url = "http://localhost:33330/upload";
HTTP_CODE response_code;
std::string response;
client->initialize();
std::map<std::string, std::string> forms;
forms["group_guid"] = "invalid_group_guid";
forms["host_guid"] = "invalid_host_guid";
forms["full_path"] = "invalid_full_path";
bool ret = false;
do
{
if (true != client->http_file_upload(url, path, forms, response_code, response))
{
log_err "http_file_upload() failed. path=%ws, url=%s, status_code=%u",
path,
url,
response_code
log_end;
break;
}
ret = true;
} while (false);
if (true == ret)
{
log_info "file upload success. path=%ws, url=%s, status_code=%u",
path,
url,
response_code
log_end;
}
if (nullptr != client)
{
delete client; client = nullptr;
}
return false;
}
bool test_curl_http_post_with_response_header()
{
_mem_check_begin
{
pcurl_client _curl_client = new curl_client();
if (nullptr == _curl_client)
{
log_err "not enought memory" log_end;
return false;
}
if (true != _curl_client->initialize())
{
log_err "curl client initialize() failed." log_end;
return false;
}
//
// 자체 서버 켜놓고 테스트 진행 필요
// Python -m http.server
//
const char* url = nullptr;
_ASSERTE(nullptr != url);
HTTP_CODE http_response_code = 404;
CMemoryStream stream;
std::map<std::string, std::string> http_response_header;
Json::Value root;
root["host_name"] = "Test";
root["host_ip"] = "127.0.0.1";
Json::StreamWriterBuilder builder;
std::stringstream request_body;
try
{
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(root, &request_body);
}
catch (const std::exception& ex)
{
log_err "Exception, Can not create request body with json. ex=%s", ex.what() log_end;
return false;
}
_ASSERTE(true == _curl_client->http_post(url,
request_body.str().c_str(),
http_response_code,
http_response_header,
stream));
_ASSERTE(0 < http_response_header.size());
_ASSERTE(_curl_client != nullptr);
delete _curl_client; _curl_client = nullptr;
}
_mem_check_end;
return true;
}
/// @brief `python -m http.server` 명령으로 간단하게 웹서버 띄워놓고 테스트
bool test_curl_http_patch()
{
_mem_check_begin
{
pcurl_client _client = new curl_client();
if (nullptr == _client)
{
log_err "not enought memory" log_end;
return false;
}
if (true != _client->initialize())
{
log_err "curl client initialize() failed." log_end;
return false;
}
auto url = "localhost:8000";
_client->append_header("accept", "application/json");
_client->append_header("content-type", "application/json");
Json::Value json;
json["data"]["revision"] = 0; // 서버에서 관리하는 값이므로 무의미함
json["data"]["mode"] = 0;
json["data"]["allow_procs"].append("");
json["data"]["allow_ips"].append("");
json["data"]["allow_domains"].append("");
json["data"]["block_procs"].append("");
json["data"]["block_ips"].append("");
json["data"]["block_domains"].append("");
HTTP_CODE http_response_code = 404;
CMemoryStream stream;
Json::StreamWriterBuilder builder;
std::stringstream request_body;
try
{
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(json, &request_body);
}
catch (const std::exception& ex)
{
log_err "Exception, Can not create request body with json. ex=%s", ex.what() log_end;
return false;
}
_ASSERTE(true == _client->http_patch(
url,
request_body.str().c_str(),
http_response_code,
stream));
log_info "http requested. ret code=%u", http_response_code log_end;
_ASSERTE(_client != nullptr);
delete _client; _client = nullptr;
}
_mem_check_end;
return true;
}