diff --git a/Release/src/http/client/http_client_winhttp.cpp b/Release/src/http/client/http_client_winhttp.cpp index 6976fbc771..e960058fd9 100644 --- a/Release/src/http/client/http_client_winhttp.cpp +++ b/Release/src/http/client/http_client_winhttp.cpp @@ -1723,23 +1723,27 @@ class winhttp_client final : public _http_client_communicator } } - static utility::string_t get_request_url(HINTERNET hRequestHandle) + static std::wstring get_request_url(HINTERNET hRequestHandle) { - DWORD urlSize{ 0 }; - if(FALSE == WinHttpQueryOption(hRequestHandle, WINHTTP_OPTION_URL, nullptr, &urlSize) || urlSize == 0) + std::wstring url; + auto urlSize = static_cast(url.capacity()) * 2; // use initial small string optimization capacity + for (;;) { - return U(""); - } - - auto urlwchar = new WCHAR[urlSize / sizeof(WCHAR)]; - - WinHttpQueryOption(hRequestHandle, WINHTTP_OPTION_URL, (void*)urlwchar, &urlSize); - - utility::string_t url(urlwchar); - - delete[] urlwchar; + url.resize(urlSize / sizeof(wchar_t)); + if (WinHttpQueryOption(hRequestHandle, WINHTTP_OPTION_URL, &url[0], &urlSize)) + { + url.resize(wcslen(url.c_str())); + return url; + } - return url; + const auto lastError = GetLastError(); + if (lastError != ERROR_INSUFFICIENT_BUFFER || urlSize == 0) + { + url.clear(); + url.shrink_to_fit(); + return url; + } + } } // Returns true if we handle successfully and resending the request