Skip to content

Commit bd0640e

Browse files
Bohdan Kurylovychborzun
authored andcommitted
Improved logs in NetworkWinHttp class
1. Improved logs in NetworkWinHttp class by adding the request_id to the corresponding logs and logging this pointer when destructing / constructing the Network object. 2. Also, added error-handling logs for corresponding WinHttp functions; 3. Removed unneeded lock when handling WinHttpOpenRequest failure; Relates to: OLPEDGE-1051 Signed-off-by: Bohdan Kurylovych <[email protected]>
1 parent 0601a42 commit bd0640e

File tree

1 file changed

+92
-48
lines changed

1 file changed

+92
-48
lines changed

olp-cpp-sdk-core/src/http/winhttp/NetworkWinHttp.cpp

Lines changed: 92 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,15 @@ NetworkWinHttp::NetworkWinHttp(size_t max_request_count)
230230
// used by the thread.
231231
thread_ = CreateThread(NULL, 0, NetworkWinHttp::Run, this, 0, NULL);
232232
SetThreadPriority(thread_, THREAD_PRIORITY_ABOVE_NORMAL);
233+
234+
OLP_SDK_LOG_TRACE(kLogTag, "Created NetworkWinHttp with address="
235+
<< this
236+
<< ", handles_count=" << max_request_count);
233237
}
234238

235239
NetworkWinHttp::~NetworkWinHttp() {
240+
OLP_SDK_LOG_TRACE(kLogTag, "Destroying NetworkWinHttp, this=" << this);
241+
236242
std::vector<std::shared_ptr<ResultData>> pending_results;
237243
{
238244
std::unique_lock<std::recursive_mutex> lock(mutex_);
@@ -307,16 +313,15 @@ SendOutcome NetworkWinHttp::Send(NetworkRequest request,
307313

308314
std::wstring url(request.GetUrl().begin(), request.GetUrl().end());
309315
if (!WinHttpCrackUrl(url.c_str(), (DWORD)url.length(), 0, &url_components)) {
310-
OLP_SDK_LOG_WARNING(kLogTag, "WinHttpCrackUrl failed " << request.GetUrl()
311-
<< " "
312-
<< GetLastError());
316+
OLP_SDK_LOG_ERROR(kLogTag, "WinHttpCrackUrl failed, url="
317+
<< request.GetUrl()
318+
<< ", error=" << GetLastError());
313319
return SendOutcome(ErrorCode::INVALID_URL_ERROR);
314320
}
315321

316322
if ((url_components.nScheme != INTERNET_SCHEME_HTTP) &&
317323
(url_components.nScheme != INTERNET_SCHEME_HTTPS)) {
318-
OLP_SDK_LOG_WARNING(kLogTag,
319-
"Invalid scheme on request " << request.GetUrl());
324+
OLP_SDK_LOG_ERROR(kLogTag, "Invalid scheme, url=" << request.GetUrl());
320325
return SendOutcome(ErrorCode::IO_ERROR);
321326
}
322327

@@ -339,6 +344,9 @@ SendOutcome NetworkWinHttp::Send(NetworkRequest request,
339344
connection->http_connection =
340345
WinHttpConnect(http_session_, host_name.data(), port, 0);
341346
if (!connection->http_connection) {
347+
OLP_SDK_LOG_ERROR(kLogTag, "WinHttpConnect failed, url="
348+
<< request.GetUrl()
349+
<< ", error=" << GetLastError());
342350
return SendOutcome(ErrorCode::OFFLINE_ERROR);
343351
}
344352
}
@@ -348,6 +356,8 @@ SendOutcome NetworkWinHttp::Send(NetworkRequest request,
348356
handle = GetHandle(id, connection, callback, header_callback, data_callback,
349357
payload, request);
350358
if (handle == nullptr) {
359+
OLP_SDK_LOG_DEBUG(kLogTag,
360+
"All handles are in use, url=" << request.GetUrl());
351361
return SendOutcome(ErrorCode::NETWORK_OVERLOAD_ERROR);
352362
}
353363
}
@@ -388,9 +398,9 @@ SendOutcome NetworkWinHttp::Send(NetworkRequest request,
388398
url_components.lpszUrlPath, NULL, WINHTTP_NO_REFERER,
389399
WINHTTP_DEFAULT_ACCEPT_TYPES, flags);
390400
if (!http_request) {
391-
OLP_SDK_LOG_ERROR(kLogTag, "WinHttpOpenRequest failed " << GetLastError());
392-
393-
std::unique_lock<std::recursive_mutex> lock(mutex_);
401+
OLP_SDK_LOG_ERROR(kLogTag, "WinHttpOpenRequest failed, url="
402+
<< request.GetUrl()
403+
<< ", error=" << GetLastError());
394404

395405
return SendOutcome(ErrorCode::IO_ERROR);
396406
}
@@ -415,47 +425,46 @@ SendOutcome NetworkWinHttp::Send(NetworkRequest request,
415425

416426
if (!WinHttpSetOption(http_request, WINHTTP_OPTION_PROXY, &proxy_info,
417427
sizeof(proxy_info))) {
418-
OLP_SDK_LOG_WARNING(kLogTag, "WinHttpSetOption(Proxy) failed "
419-
<< GetLastError() << " "
420-
<< request.GetUrl());
428+
OLP_SDK_LOG_WARNING(kLogTag, "WinHttpSetOption(Proxy) failed, url="
429+
<< request.GetUrl()
430+
<< ", error=" << GetLastError());
421431
}
422432
if (!proxy.GetUsername().empty() && !proxy.GetPassword().empty()) {
423433
std::wstring proxy_username;
424434
const auto username_res =
425435
ConvertMultiByteToWideChar(proxy.GetUsername(), proxy_username);
436+
if (!username_res) {
437+
OLP_SDK_LOG_WARNING(kLogTag, "Proxy username conversion failure, url="
438+
<< request.GetUrl()
439+
<< ", error=" << GetLastError());
440+
}
426441

427442
std::wstring proxy_password;
428443
const auto password_res =
429444
ConvertMultiByteToWideChar(proxy.GetPassword(), proxy_password);
445+
if (!password_res) {
446+
OLP_SDK_LOG_WARNING(kLogTag, "Proxy password conversion failure, url="
447+
<< request.GetUrl()
448+
<< ", error=" << GetLastError());
449+
}
430450

431451
if (username_res && password_res) {
432452
LPCWSTR proxy_lpcwstr_username = proxy_username.c_str();
433453
if (!WinHttpSetOption(http_request, WINHTTP_OPTION_PROXY_USERNAME,
434454
const_cast<LPWSTR>(proxy_lpcwstr_username),
435455
wcslen(proxy_lpcwstr_username))) {
436-
OLP_SDK_LOG_WARNING(kLogTag,
437-
"WinHttpSetOption(proxy username) failed "
438-
<< GetLastError() << " " << request.GetUrl());
456+
OLP_SDK_LOG_WARNING(
457+
kLogTag, "WinHttpSetOption(proxy username) failed, url="
458+
<< request.GetUrl() << ", error=" << GetLastError());
439459
}
440460

441461
LPCWSTR proxy_lpcwstr_password = proxy_password.c_str();
442462
if (!WinHttpSetOption(http_request, WINHTTP_OPTION_PROXY_PASSWORD,
443463
const_cast<LPWSTR>(proxy_lpcwstr_password),
444464
wcslen(proxy_lpcwstr_password))) {
445-
OLP_SDK_LOG_WARNING(kLogTag,
446-
"WinHttpSetOption(proxy password) failed "
447-
<< GetLastError() << " " << request.GetUrl());
448-
}
449-
} else {
450-
if (!username_res) {
451-
OLP_SDK_LOG_WARNING(kLogTag, "Proxy username conversion failure "
452-
<< GetLastError() << " "
453-
<< request.GetUrl());
454-
}
455-
if (!password_res) {
456-
OLP_SDK_LOG_WARNING(kLogTag, "Proxy password conversion failure "
457-
<< GetLastError() << " "
458-
<< request.GetUrl());
465+
OLP_SDK_LOG_WARNING(
466+
kLogTag, "WinHttpSetOption(proxy password) failed, url="
467+
<< request.GetUrl() << ", error=" << GetLastError());
459468
}
460469
}
461470
}
@@ -496,25 +505,33 @@ SendOutcome NetworkWinHttp::Send(NetworkRequest request,
496505

497506
if (!WinHttpAddRequestHeaders(http_request, header_stream.str().c_str(),
498507
DWORD(-1), WINHTTP_ADDREQ_FLAG_ADD)) {
499-
OLP_SDK_LOG_ERROR(kLogTag,
500-
"WinHttpAddRequestHeaders() failed " << GetLastError());
508+
OLP_SDK_LOG_WARNING(kLogTag, "WinHttpAddRequestHeaders failed, url="
509+
<< request.GetUrl()
510+
<< ", error=" << GetLastError());
501511
}
502512

503513
/* Send the request */
504514
if (!WinHttpSendRequest(http_request, WINHTTP_NO_ADDITIONAL_HEADERS, 0,
505515
(LPVOID)content, content_length, content_length,
506516
(DWORD_PTR)handle)) {
507-
OLP_SDK_LOG_ERROR(kLogTag, "WinHttpSendRequest failed " << GetLastError());
517+
OLP_SDK_LOG_ERROR(kLogTag, "WinHttpSendRequest failed, url="
518+
<< request.GetUrl()
519+
<< ", error=" << GetLastError());
508520

509521
FreeHandle(id);
510522
return SendOutcome(ErrorCode::IO_ERROR);
511523
}
512524
handle->http_request = http_request;
513525

526+
OLP_SDK_LOG_DEBUG(kLogTag,
527+
"Send request, url=" << request.GetUrl() << ", id=" << id);
528+
514529
return SendOutcome(id);
515530
}
516531

517532
void NetworkWinHttp::Cancel(RequestId id) {
533+
OLP_SDK_LOG_TRACE(kLogTag, "Cancel request with id=" << id);
534+
518535
std::unique_lock<std::recursive_mutex> lock_guard(mutex_);
519536
auto handle = FindHandle(id);
520537
if (handle) {
@@ -532,7 +549,8 @@ void NetworkWinHttp::RequestCallback(HINTERNET, DWORD_PTR context, DWORD status,
532549

533550
RequestData* handle = reinterpret_cast<RequestData*>(context);
534551
if (!handle->connection_data || !handle->result_data) {
535-
OLP_SDK_LOG_WARNING(kLogTag, "RequestCallback to inactive handle");
552+
OLP_SDK_LOG_WARNING(kLogTag, "RequestCallback to inactive handle, id="
553+
<< handle->request_id);
536554
return;
537555
}
538556

@@ -548,10 +566,18 @@ void NetworkWinHttp::RequestCallback(HINTERNET, DWORD_PTR context, DWORD status,
548566
handle->result_data->cancelled = true;
549567
}
550568

569+
OLP_SDK_LOG_DEBUG(kLogTag, "RequestCallback - request error, status="
570+
<< handle->result_data->status
571+
<< ", id=" << handle->request_id);
572+
551573
handle->Complete();
552574
} else if (status == WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE) {
553575
// We have sent request, now receive a response
554-
WinHttpReceiveResponse(handle->http_request, NULL);
576+
if (!WinHttpReceiveResponse(handle->http_request, NULL)) {
577+
OLP_SDK_LOG_WARNING(kLogTag, "WinHttpReceiveResponse failed, id="
578+
<< handle->request_id
579+
<< ", error=" << GetLastError());
580+
}
555581
} else if (status == WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE) {
556582
Network::HeaderCallback callback = nullptr;
557583
{
@@ -709,10 +735,9 @@ void NetworkWinHttp::RequestCallback(HINTERNET, DWORD_PTR context, DWORD status,
709735
handle->strm.next_in = Z_NULL;
710736
inflateInit2(&handle->strm, 16 + MAX_WBITS);
711737
#else
712-
OLP_SDK_LOG_ERROR(
713-
kLogTag,
714-
"Gzip encoding but compression no supported and no "
715-
"ZLIB found");
738+
OLP_SDK_LOG_ERROR(kLogTag,
739+
"Gzip encoding failed - ZLIB not found, id="
740+
<< handle->request_id);
716741
#endif
717742
}
718743
LocalFree(str);
@@ -725,21 +750,30 @@ void NetworkWinHttp::RequestCallback(HINTERNET, DWORD_PTR context, DWORD status,
725750
}
726751

727752
// We have received headers, now receive data
728-
WinHttpQueryDataAvailable(handle->http_request, NULL);
753+
if (!WinHttpQueryDataAvailable(handle->http_request, NULL)) {
754+
OLP_SDK_LOG_WARNING(kLogTag, "WinHttpQueryDataAvailable failed, id="
755+
<< handle->request_id
756+
<< ", error=" << GetLastError());
757+
}
729758
} else if (status == WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE) {
730759
assert(status_info_length == sizeof(DWORD));
731760
DWORD size = *reinterpret_cast<DWORD*>(status_info);
732761
if (size > 0 && 416 != handle->result_data->status) {
733762
// Data is available read it
734763
LPVOID buffer = (LPVOID)LocalAlloc(LPTR, size);
735764
if (!buffer) {
736-
OLP_SDK_LOG_ERROR(kLogTag,
737-
"Out of memory reeceiving " << size << " bytes");
765+
OLP_SDK_LOG_ERROR(kLogTag, "Out of memory reeceiving "
766+
<< size
767+
<< " bytes, id=" << handle->request_id);
738768
handle->result_data->status = ERROR_NOT_ENOUGH_MEMORY;
739769
handle->Complete();
740770
return;
741771
}
742-
WinHttpReadData(handle->http_request, buffer, size, NULL);
772+
if (!WinHttpReadData(handle->http_request, buffer, size, NULL)) {
773+
OLP_SDK_LOG_WARNING(kLogTag, "WinHttpReadData failed, id="
774+
<< handle->request_id
775+
<< ", error=" << GetLastError());
776+
}
743777
} else {
744778
// Request is complete
745779
if (handle->result_data->status != 416) {
@@ -752,6 +786,9 @@ void NetworkWinHttp::RequestCallback(HINTERNET, DWORD_PTR context, DWORD status,
752786
}
753787
}
754788
handle->result_data->completed = true;
789+
OLP_SDK_LOG_DEBUG(
790+
kLogTag, "Completed request, id=" << handle->request_id << ", status="
791+
<< handle->result_data->status);
755792
handle->Complete();
756793
}
757794
} else if (status == WINHTTP_CALLBACK_STATUS_READ_COMPLETE) {
@@ -777,7 +814,8 @@ void NetworkWinHttp::RequestCallback(HINTERNET, DWORD_PTR context, DWORD status,
777814
int r = inflate(&handle->strm, Z_NO_FLUSH);
778815

779816
if ((r != Z_OK) && (r != Z_STREAM_END)) {
780-
OLP_SDK_LOG_ERROR(kLogTag, "Uncompression failed");
817+
OLP_SDK_LOG_ERROR(
818+
kLogTag, "Uncompression failed, id=" << handle->request_id);
781819
LocalFree((HLOCAL)compressed);
782820
LocalFree((HLOCAL)data_buffer);
783821
handle->result_data->status = ERROR_INVALID_BLOCK;
@@ -802,6 +840,8 @@ void NetworkWinHttp::RequestCallback(HINTERNET, DWORD_PTR context, DWORD status,
802840
}
803841
#endif
804842

843+
OLP_SDK_LOG_TRACE(kLogTag, "Received " << data_len << " bytes for id="
844+
<< handle->request_id);
805845
if (data_len) {
806846
std::uint64_t total_offset = 0;
807847

@@ -817,9 +857,8 @@ void NetworkWinHttp::RequestCallback(HINTERNET, DWORD_PTR context, DWORD status,
817857
std::streampos(handle->result_data->count)) {
818858
handle->payload->seekp(handle->result_data->count);
819859
if (handle->payload->fail()) {
820-
OLP_SDK_LOG_WARNING(
821-
kLogTag,
822-
"Reception stream doesn't support setting write point");
860+
OLP_SDK_LOG_WARNING(kLogTag, "Payload seekp() failed, id="
861+
<< handle->request_id);
823862
handle->payload->clear();
824863
}
825864
}
@@ -832,16 +871,21 @@ void NetworkWinHttp::RequestCallback(HINTERNET, DWORD_PTR context, DWORD status,
832871
LocalFree((HLOCAL)data_buffer);
833872
}
834873

835-
WinHttpQueryDataAvailable(handle->http_request, NULL);
874+
if (!WinHttpQueryDataAvailable(handle->http_request, NULL)) {
875+
OLP_SDK_LOG_WARNING(kLogTag, "WinHttpQueryDataAvailable failed, id="
876+
<< handle->request_id
877+
<< ", error=" << GetLastError());
878+
}
836879
} else if (status == WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING) {
837880
// Only now is it safe to free the handle
838881
// See
839882
// https://docs.microsoft.com/en-us/windows/desktop/api/winhttp/nf-winhttp-winhttpclosehandle
840883
handle->FreeHandle();
841884
return;
842885
} else {
843-
OLP_SDK_LOG_ERROR(kLogTag,
844-
"Unknown callback " << std::hex << status << std::dec);
886+
OLP_SDK_LOG_ERROR(
887+
kLogTag, "Unknown callback, status=" << std::hex << status << std::dec
888+
<< ", id=" << handle->request_id);
845889
}
846890
}
847891

0 commit comments

Comments
 (0)