| 91 | } |
| 92 | |
| 93 | downslib_error SymbolFileInfo::Download(std::string url, std::wstring fileName, std::string userAgent, |
| 94 | unsigned int timeout,downslib_cb cb, void* userdata){ |
| 95 | HINTERNET hInternet = nullptr; |
| 96 | HINTERNET hUrl = nullptr; |
| 97 | HANDLE hFile = nullptr; |
| 98 | HINTERNET hConnect = nullptr; |
| 99 | |
| 100 | Cleanup cleanup([&]() |
| 101 | { |
| 102 | DWORD lastError = ::GetLastError(); |
| 103 | if (hFile != INVALID_HANDLE_VALUE) { |
| 104 | bool doDelete = false; |
| 105 | LARGE_INTEGER fileSize; |
| 106 | if (lastError != ERROR_SUCCESS || (::GetFileSizeEx(hFile, &fileSize) && fileSize.QuadPart == 0)) { |
| 107 | doDelete = true; |
| 108 | } |
| 109 | ::CloseHandle(hFile); |
| 110 | if (doDelete) |
| 111 | DeleteFile(fileName.c_str()); |
| 112 | } |
| 113 | if (hUrl != nullptr) |
| 114 | ::InternetCloseHandle(hUrl); |
| 115 | if (hInternet != NULL) |
| 116 | ::InternetCloseHandle(hInternet); |
| 117 | if (hConnect != nullptr) |
| 118 | ::InternetCloseHandle(hConnect); |
| 119 | ::SetLastError(lastError); |
| 120 | }); |
| 121 | |
| 122 | hFile = ::CreateFile(fileName.c_str(), GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, nullptr, CREATE_ALWAYS, 0, nullptr); |
| 123 | if (hFile == INVALID_HANDLE_VALUE) |
| 124 | return downslib_error::createfile; |
| 125 | |
| 126 | hInternet = ::InternetOpenA(userAgent.c_str(), INTERNET_OPEN_TYPE_PRECONFIG, |
| 127 | nullptr, nullptr, 0); |
| 128 | |
| 129 | |
| 130 | if (!hInternet) |
| 131 | return downslib_error::inetopen; |
| 132 | |
| 133 | DWORD flags; |
| 134 | DWORD len = sizeof(flags); |
| 135 | |
| 136 | ::InternetSetOptionA(hInternet, INTERNET_OPTION_RECEIVE_TIMEOUT, &timeout, sizeof(timeout)); |
| 137 | flags = INTERNET_FLAG_RELOAD; |
| 138 | if (strncmp(url.c_str(), "https://", 8) == 0) |
| 139 | flags |= INTERNET_FLAG_SECURE; |
| 140 | |
| 141 | hUrl = InternetOpenUrlA(hInternet, url.c_str(), nullptr, 0, flags, 0); |
| 142 | DWORD error = ::GetLastError(); |
| 143 | if (error == ERROR_INTERNET_INVALID_CA) { |
| 144 | std::string serviceName = "msdl.microsoft.com"; |
| 145 | // Connect to the http server |
| 146 | hConnect = InternetConnectA(hInternet, serviceName.c_str(), |
| 147 | INTERNET_DEFAULT_HTTP_PORT, nullptr, |
| 148 | nullptr, INTERNET_SERVICE_HTTP, 0, 0); |
| 149 | int pos = url.find(".com") + 4; |
| 150 | std::string objName(url.begin() + pos, url.end()); |
nothing calls this directly
no test coverage detected