-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAsyncHttp.cpp
726 lines (604 loc) · 17.5 KB
/
AsyncHttp.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#include "AsyncHttp.h"
#include <Windows.h>
#include <atlstr.h>
#include <memory>
#include <fmt/format.h>
#include <fmt/xchar.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 5205)
#endif // _MSC_VER
namespace lxd {
HRESULT MyRequest::Initialize(
const WinHTTPWrappers::CConnection& connection,
LPCWSTR pwszObjectName,
LPCWSTR pwszVerb,
DWORD httpAuthScheme,
const std::pair<std::wstring_view, std::wstring_view>& cred
) {
if (httpAuthScheme) {
m_bHTTPPreauthentication = true;
m_dwHTTPPreauthenticationScheme = httpAuthScheme;
m_sHTTPUserName = cred.first;
m_sHTTPPassword = cred.second;
}
return WinHTTPWrappers::CAsyncDownloader::Initialize(connection, pwszObjectName, pwszVerb, nullptr, 0, 0, WINHTTP_FLAG_SECURE, BUFFER_SIZE);
}
// 关闭时必须调用此函数而不是 CAsyncDownloader::Close
void MyRequest::MyClose() {
ATL::CCritSecLock sl(m_cs.m_sec, true);
_isClosing = true;
sl.Unlock();
CAsyncDownloader::Close();
std::unique_lock<std::mutex> lk(m_mt);
m_cv.wait(lk, [&] { return m_fCanSafeExit; });
}
HRESULT MyRequest::OnCallbackComplete(
_In_ HRESULT hr,
_In_ HINTERNET hInternet,
_In_ DWORD dwInternetStatus,
_In_opt_ LPVOID lpvStatusInformation,
_In_ DWORD dwStatusInformationLength
) {
hr = WinHTTPWrappers::CAsyncDownloader::OnCallbackComplete(hr, hInternet, dwInternetStatus, lpvStatusInformation, dwStatusInformationLength);
_isRequesting = false;
SetCanSafeExit();
return hr;
}
HRESULT MyRequest::SendRequest(_In_reads_bytes_opt_(dwOptionalLength) LPVOID lpOptional, _In_ DWORD dwOptionalLength) {
HRESULT hr = WinHTTPWrappers::CAsyncDownloader::SendRequest(lpOptional, dwOptionalLength);
if (SUCCEEDED(hr)) {
_isRequesting = true;
SetCannotSafeExit();
}
return hr;
}
HRESULT MyRequest::OnCallback(
_In_ HINTERNET hInternet,
_In_ DWORD dwInternetStatus,
_In_opt_ LPVOID lpvStatusInformation,
_In_ DWORD dwStatusInformationLength
) {
HRESULT hr;
ATL::CCritSecLock sl(m_cs.m_sec, true);
if (!_isRequesting) {
// 请求完成后取消不是错误
return S_OK;
}
if (_isClosing) {
// 已调用 Close,此时句柄已失效。在收到 HANDLE_CLOSING 之前不处理任何消息
if (dwInternetStatus != WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING) {
return S_OK;
}
hr = OnRequestErrorCallback(hInternet, dwInternetStatus, lpvStatusInformation, dwStatusInformationLength);
SetStatusCallback(nullptr, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS);
_isClosing = false;
} else {
hr = WinHTTPWrappers::CAsyncDownloader::OnCallback(hInternet, dwInternetStatus, lpvStatusInformation, dwStatusInformationLength);
}
return hr;
}
DWORD MyRequest::GetLastStatusCode() {
bool valid;
DWORD c = WinHTTPWrappers::CAsyncDownloader::GetLastStatusCode(valid);
return valid ? c : 0;
}
void MyRequest::Wait() {
std::unique_lock<std::mutex> lk(m_mt);
m_cv.wait(lk, [&] { return m_fCanSafeExit; });
}
bool MyRequest::IsRequesting() {
ATL::CCritSecLock sl(m_cs.m_sec, true);
return _isRequesting;
}
void MyRequest::SetCanSafeExit() {
m_mt.lock();
m_fCanSafeExit = true;
m_mt.unlock();
m_cv.notify_all();
}
void MyRequest::SetCannotSafeExit() {
m_mt.lock();
m_fCanSafeExit = false;
m_mt.unlock();
}
HRESULT GetRequest::Initialize(
const WinHTTPWrappers::CConnection& connection,
LPCWSTR pwszObjectName,
std::function<void(HRESULT, DWORD, std::string_view)> onComplete,
std::function<void(float)> onProgress,
const std::map<std::wstring, std::wstring> headers,
DWORD httpAuthScheme,
const std::pair<std::wstring_view, std::wstring_view>& cred
) {
HRESULT hr = MyRequest::Initialize(connection, pwszObjectName, L"GET", httpAuthScheme, cred);
if (FAILED(hr)) {
return hr;
}
_onComplete = onComplete;
_onProgress = onProgress;
_headers = headers;
return hr;
}
HRESULT GetRequest::OnCallbackComplete(
_In_ HRESULT hr,
_In_ HINTERNET hInternet,
_In_ DWORD dwInternetStatus,
_In_opt_ LPVOID lpvStatusInformation,
_In_ DWORD dwStatusInformationLength
) {
if (_onComplete) {
std::string response;
if (SUCCEEDED(hr)) {
if (!m_sFileToDownloadInto.empty()) {
response = "file";
} else {
response = { m_Response.begin(), m_Response.end() };
};
}
_onComplete(hr, GetLastStatusCode(), response);
}
return MyRequest::OnCallbackComplete(hr, hInternet, dwInternetStatus, lpvStatusInformation, dwStatusInformationLength);
}
HRESULT GetRequest::OnReadData(_In_reads_bytes_(dwBytesRead) const void* lpvBuffer, _In_ DWORD dwBytesRead) {
HRESULT hr = MyRequest::OnReadData(lpvBuffer, dwBytesRead);
ATL::CCritSecLock sl(m_cs.m_sec, true);
if (_onProgress) {
if (m_nContentLength <= 0) {
_onProgress(0);
} else {
_onProgress(float(m_nTotalBytesRead) / m_nContentLength);
}
}
return hr;
}
std::wstring GetRequest::GetHeaders() {
std::wstring headers = MyRequest::GetHeaders();
for (const auto& [name, value] : _headers) {
headers.append(fmt::format(L"{}: {}\r\n", name, value));
}
return headers;
}
GetRequestTask::GetRequestTask(
const WinHTTPWrappers::CSession& session,
LPCWSTR pwszServerName,
LPCWSTR pwszObjectName,
std::function<void(HRESULT, DWORD, std::string_view)> onComplete,
std::function<void(float progress)> onProgress,
const std::map<std::wstring, std::wstring>& headers,
std::wstring_view fileToSave,
DWORD httpAuthScheme,
const std::pair<std::wstring_view, std::wstring_view>& cred
) {
_conn.reset(new WinHTTPWrappers::CConnection());
HRESULT hr = _conn->Initialize(session, pwszServerName);
if (FAILED(hr)) {
onComplete(hr, 0, "");
return;
}
_getReq.reset(new GetRequest());
if (!fileToSave.empty()) {
_getReq->m_sFileToDownloadInto = fileToSave;
}
hr = _getReq->Initialize(*_conn, pwszObjectName, onComplete, onProgress, headers, httpAuthScheme, cred);
if (FAILED(hr)) {
onComplete(hr, 0, "");
return;
}
hr = _getReq->SendRequest();
if (FAILED(hr)) {
onComplete(hr, 0, "");
return;
}
return;
}
void GetRequestTask::Wait() {
if (_getReq) {
_getReq->Wait();
}
}
void GetRequestTask::Cancel() {
if (_getReq) {
_getReq->MyClose();
}
}
bool GetRequestTask::IsRequesting() {
if (!_getReq) {
return false;
}
return _getReq->IsRequesting();
}
void PostRequest::MyClose() {
MyRequest::MyClose();
ATL::CCritSecLock sl(m_cs.m_sec, true);
_isWriteDataCompleted = false;
_bufferPos = 0;
}
HRESULT PostRequest::Initialize(
const WinHTTPWrappers::CConnection& connection,
LPCWSTR pwszObjectName,
std::function<void(HRESULT, DWORD, std::string_view)> onComplete,
std::function<void(float)> onProgress,
const std::map<std::wstring, std::wstring>& headers,
const std::vector<std::pair<std::string_view, std::string_view>>& formData,
const std::vector<std::tuple<std::string_view, std::string_view, std::string_view>>& files,
DWORD httpAuthScheme,
const std::pair<std::wstring_view, std::wstring_view>& cred
) {
_headers[L"Content-Type"] = fmt::format(L"multipart/form-data;boundary=\"{}\"", _formDataBoundaryW);
return Initialize(connection, pwszObjectName, onComplete, onProgress, headers, MergeFormData(formData, files), httpAuthScheme, cred);
}
HRESULT PostRequest::Initialize(
const WinHTTPWrappers::CConnection& connection,
LPCWSTR pwszObjectName,
std::function<void(HRESULT, DWORD, std::string_view)> onComplete,
std::function<void(float progress)> onProgress,
const std::map<std::wstring, std::wstring>& headers,
std::string_view body,
DWORD httpAuthScheme,
const std::pair<std::wstring_view, std::wstring_view>& cred
) {
_headers.insert(headers.begin(), headers.end());
_body = body;
m_lpRequest = _body.c_str();
m_dwRequestSize = (DWORD)_body.size();
HRESULT hr = MyRequest::Initialize(connection, pwszObjectName, L"POST", httpAuthScheme, cred);
if (FAILED(hr)) {
return hr;
}
_onComplete = onComplete;
_onProgress = onProgress;
return hr;
}
HRESULT PostRequest::OnCallbackComplete(
_In_ HRESULT hr,
_In_ HINTERNET hInternet,
_In_ DWORD dwInternetStatus,
_In_opt_ LPVOID lpvStatusInformation,
_In_ DWORD dwStatusInformationLength
) {
if (_onComplete) {
std::string response;
if (SUCCEEDED(hr)) {
response = std::string(m_Response.begin(), m_Response.end());
}
_onComplete(hr, GetLastStatusCode(), response);
}
return MyRequest::OnCallbackComplete(hr, hInternet, dwInternetStatus, lpvStatusInformation, dwStatusInformationLength);
}
HRESULT PostRequest::OnWriteData() {
ATL::CCritSecLock sl(m_cs.m_sec, true);
if (_isWriteDataCompleted) {
return S_OK;
}
HRESULT hr;
// 从缓冲区读取body数据
if (m_dwRequestSize) {
ATLASSERT(m_lpRequest != nullptr); //m_pbyRequest should be provided if m_dwRequestSize is non-zero
// 为了显示进度条分段传输
unsigned int nBytesWrite = _bufferPos + BUFFER_SIZE <= m_dwRequestSize ? BUFFER_SIZE : m_dwRequestSize - _bufferPos;
hr = WriteData((BYTE*)m_lpRequest + _bufferPos, nBytesWrite, nullptr);
if (FAILED(hr))
return hr;
_bufferPos += nBytesWrite;
if (_onProgress) {
_onProgress(float(_bufferPos) / m_dwRequestSize);
}
hr = _bufferPos >= m_dwRequestSize ? S_FALSE : S_OK;
} else {
//There's nothing more to upload so return S_FALSE
hr = S_FALSE;
}
if (hr == S_FALSE) {
// 所有数据已写入
_isWriteDataCompleted = true;
}
return hr;
}
std::wstring PostRequest::GetHeaders() {
std::wstring headers = MyRequest::GetHeaders();
for (const auto& [name, value] : _headers) {
headers.append(fmt::format(L"{}: {}\r\n", name, value));
}
return headers;
}
std::string PostRequest::MergeFormData(
const std::vector<std::pair<std::string_view, std::string_view>>& pairs,
const std::vector<std::tuple<std::string_view, std::string_view, std::string_view>>& files
) const {
std::string result;
for (const auto& pair : pairs) {
result += fmt::format("--{}\r\nContent-Disposition: form-data; name=\"{}\"\r\n\r\n{}\r\n", _formDataBoundary, pair.first, pair.second);
}
for (const auto& file : files) {
result += fmt::format(
"--{}\r\nContent-Disposition: form-data; name=\"{}\"; filename=\"{}\"\r\n\r\n{}\r\n",
_formDataBoundary, std::get<0>(file), std::get<1>(file), std::get<2>(file)
);
}
result += fmt::format("--{}--\r\n", _formDataBoundary);
return result;
}
PostRequestTask::PostRequestTask(
const WinHTTPWrappers::CSession& session,
LPCWSTR pwszServerName,
LPCWSTR pwszObjectName,
std::function<void(HRESULT, DWORD, std::string_view)> onComplete,
std::function<void(float progress)> onProgress,
const std::map<std::wstring, std::wstring>& headers,
const std::vector<std::pair<std::string_view, std::string_view>>& pairs,
const std::vector<std::tuple<std::string_view, std::string_view, std::string_view>>& files,
DWORD httpAuthScheme,
const std::pair<std::wstring_view,
std::wstring_view>& cred
) {
_conn.reset(new WinHTTPWrappers::CConnection());
HRESULT hr = _conn->Initialize(session, pwszServerName);
if (FAILED(hr)) {
onComplete(hr, 0, "");
return;
}
_postReq.reset(new PostRequest());
hr = _postReq->Initialize(
*_conn,
pwszObjectName,
onComplete,
onProgress,
headers,
pairs,
files,
httpAuthScheme,
cred
);
if (FAILED(hr)) {
onComplete(hr, 0, "");
return;
}
hr = _postReq->SendRequest();
if (FAILED(hr)) {
onComplete(hr, 0, "");
return;
}
}
PostRequestTask::PostRequestTask(
const WinHTTPWrappers::CSession& session,
LPCWSTR pwszServerName,
LPCWSTR pwszObjectName,
std::function<void(HRESULT, DWORD, std::string_view)> onComplete,
std::function<void(float progress)> onProgress,
const std::map<std::wstring, std::wstring>& headers,
std::string_view body,
DWORD httpAuthScheme,
const std::pair<std::wstring_view, std::wstring_view>& cred
) {
_conn.reset(new WinHTTPWrappers::CConnection());
HRESULT hr = _conn->Initialize(session, pwszServerName);
if (FAILED(hr)) {
onComplete(hr, 0, "");
return;
}
_postReq.reset(new PostRequest());
hr = _postReq->Initialize(
*_conn,
pwszObjectName,
onComplete,
onProgress,
headers,
body,
httpAuthScheme,
cred
);
if (FAILED(hr)) {
onComplete(hr, 0, "");
return;
}
hr = _postReq->SendRequest();
if (FAILED(hr)) {
onComplete(hr, 0, "");
return;
}
}
void PostRequestTask::Wait() {
if (_postReq) {
_postReq->Wait();
}
}
void PostRequestTask::Cancel() {
if (_postReq) {
_postReq->MyClose();
}
}
bool PostRequestTask::IsRequesting() {
if (!_postReq) {
return false;
}
return _postReq->IsRequesting();
}
HttpClient::HttpClient() {
[[maybe_unused]] HRESULT hr = _session.Initialize(L"WinHttpWrap", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS);
assert(SUCCEEDED(hr));
}
HttpClient::~HttpClient() {
CancelAll();
}
unsigned int HttpClient::GetAsync(
std::wstring_view url,
std::function<void(HRESULT, DWORD, std::string_view)> onComplete,
std::function<void(float progress)> onProgress,
std::wstring_view fileToSave,
DWORD httpAuthScheme,
const std::pair<std::wstring_view, std::wstring_view>& cred,
const std::map<std::wstring, std::wstring>& headers
) {
std::scoped_lock lk(_mutex);
URL_COMPONENTS urlComp{};
urlComp.dwStructSize = sizeof(urlComp);
urlComp.dwSchemeLength = (DWORD)-1;
urlComp.dwHostNameLength = (DWORD)-1;
urlComp.dwUrlPathLength = (DWORD)-1;
urlComp.dwExtraInfoLength = (DWORD)-1;
BOOL rs = WinHttpCrackUrl(url.data(), (DWORD)url.size(), 0, &urlComp);
if (rs != TRUE) {
if (onComplete) {
onComplete(ATL::AtlHresultFromLastError(), 0, "");
}
return {};
}
unsigned int id = _NewTaskId();
auto completeCb = [this, onComplete, id](HRESULT hr, DWORD lastStatusCode, std::string_view response) {
if (onComplete) {
onComplete(hr, lastStatusCode, response);
}
std::scoped_lock lk(_mutex);
_tasks.erase(id);
};
_tasks.emplace(id, new GetRequestTask(
_session,
CStringW(urlComp.lpszHostName, urlComp.dwHostNameLength),
CStringW(urlComp.lpszUrlPath, urlComp.dwUrlPathLength),
completeCb,
onProgress,
headers,
fileToSave,
httpAuthScheme,
cred
));
return id;
}
unsigned int HttpClient::PostFormDataAsync(
std::wstring_view url,
std::function<void(HRESULT, DWORD, std::string_view)> onComplete,
std::function<void(float progress)> onProgress,
const std::vector<std::pair<std::string_view, std::string_view>>& pairs,
const std::vector<std::tuple<std::string_view, std::string_view, std::string_view>>& files,
DWORD httpAuthScheme,
const std::pair<std::wstring_view, std::wstring_view>& cred,
const std::map<std::wstring, std::wstring>& headers
) {
std::scoped_lock lk(_mutex);
URL_COMPONENTS urlComp{};
urlComp.dwStructSize = sizeof(urlComp);
urlComp.dwSchemeLength = (DWORD)-1;
urlComp.dwHostNameLength = (DWORD)-1;
urlComp.dwUrlPathLength = (DWORD)-1;
urlComp.dwExtraInfoLength = (DWORD)-1;
BOOL rs = WinHttpCrackUrl(url.data(), (DWORD)url.size(), 0, &urlComp);
if (rs != TRUE) {
if (onComplete) {
onComplete(ATL::AtlHresultFromLastError(), 0, "");
}
return {};
}
unsigned int id = _NewTaskId();
auto completeCb = [onComplete, this, id](HRESULT hr, DWORD lastStatusCode, std::string_view response) {
if (onComplete) {
onComplete(hr, lastStatusCode, response);
}
std::scoped_lock lk(_mutex);
if (_tasks.find(id) != _tasks.end()) {
_tasks.erase(id);
}
};
_tasks.emplace(id, new PostRequestTask(
_session,
CStringW(urlComp.lpszHostName, urlComp.dwHostNameLength),
CStringW(urlComp.lpszUrlPath, urlComp.dwUrlPathLength + urlComp.dwExtraInfoLength),
completeCb,
onProgress,
headers,
pairs,
files,
httpAuthScheme,
cred
));
return id;
}
unsigned int HttpClient::PostAsync(
std::wstring_view url,
std::function<void(HRESULT hr, DWORD lastStatusCode, std::string_view response)> onComplete,
std::function<void(float progress)> onProgress,
std::string_view body,
DWORD httpAuthScheme,
const std::pair<std::wstring_view, std::wstring_view>& cred,
const std::map<std::wstring, std::wstring>& headers
) {
std::scoped_lock lk(_mutex);
URL_COMPONENTS urlComp{};
urlComp.dwStructSize = sizeof(urlComp);
urlComp.dwSchemeLength = (DWORD)-1;
urlComp.dwHostNameLength = (DWORD)-1;
urlComp.dwUrlPathLength = (DWORD)-1;
urlComp.dwExtraInfoLength = (DWORD)-1;
BOOL rs = WinHttpCrackUrl(url.data(), (DWORD)url.size(), 0, &urlComp);
if (rs != TRUE) {
if (onComplete) {
onComplete(ATL::AtlHresultFromLastError(), 0, "");
}
return {};
}
unsigned int id = _NewTaskId();
auto completeCb = [onComplete, this, id](HRESULT hr, DWORD lastStatusCode, std::string_view response) {
if (onComplete) {
onComplete(hr, lastStatusCode, response);
}
std::scoped_lock lk(_mutex);
if (_tasks.find(id) != _tasks.end()) {
_tasks.erase(id);
}
};
_tasks.emplace(id, new PostRequestTask(
_session,
CStringW(urlComp.lpszHostName, urlComp.dwHostNameLength),
CStringW(urlComp.lpszUrlPath, urlComp.dwUrlPathLength + urlComp.dwExtraInfoLength),
completeCb,
onProgress,
headers,
body,
httpAuthScheme,
cred
));
return id;
}
bool HttpClient::Wait(unsigned int requestId) {
std::unique_lock lk(_mutex);
auto it = _tasks.find(requestId);
if (it == _tasks.end()) {
return false;
}
lk.unlock();
it->second->Wait();
return true;
}
void HttpClient::WaitAll() {
std::scoped_lock lk(_mutex);
for (const auto& pair : _tasks) {
pair.second->Wait();
}
}
void HttpClient::CancelAll() {
std::scoped_lock lk(_mutex);
for (const auto& pair : _tasks) {
pair.second->Cancel();
}
}
bool HttpClient::Cancel(unsigned int requestId) {
std::scoped_lock lk(_mutex);
auto it = _tasks.find(requestId);
if (it == _tasks.end()) {
return false;
}
it->second->Cancel();
return true;
}
bool HttpClient::IsRequesting(unsigned int requestId) {
std::scoped_lock lk(_mutex);
auto it = _tasks.find(requestId);
if (it == _tasks.end()) {
return false;
}
return it->second->IsRequesting();
}
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER