| 81 | } |
| 82 | |
| 83 | static std::string fetch_path_with_timeout_flag(HINTERNET hSession, |
| 84 | const wchar_t* host, |
| 85 | INTERNET_PORT port, |
| 86 | const std::wstring& path, |
| 87 | bool& out_timed_out) |
| 88 | { |
| 89 | out_timed_out = false; |
| 90 | std::string body; |
| 91 | |
| 92 | HINTERNET hConnect = WinHttpConnect(hSession, host, port, 0); |
| 93 | if (!hConnect) { |
| 94 | DWORD e = GetLastError(); |
| 95 | if (is_timeout_error(e)) out_timed_out = true; |
| 96 | return body; |
| 97 | } |
| 98 | |
| 99 | HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", path.c_str(), |
| 100 | NULL, WINHTTP_NO_REFERER, |
| 101 | WINHTTP_DEFAULT_ACCEPT_TYPES, |
| 102 | WINHTTP_FLAG_SECURE); |
| 103 | if (!hRequest) { |
| 104 | DWORD e = GetLastError(); |
| 105 | if (is_timeout_error(e)) out_timed_out = true; |
| 106 | WinHttpCloseHandle(hConnect); |
| 107 | return body; |
| 108 | } |
| 109 | |
| 110 | WinHttpAddRequestHeaders(hRequest, L"User-Agent: flm/1.0\r\n", (DWORD)-1L, WINHTTP_ADDREQ_FLAG_ADD); |
| 111 | |
| 112 | BOOL ok = WinHttpSendRequest(hRequest, |
| 113 | WINHTTP_NO_ADDITIONAL_HEADERS, 0, |
| 114 | WINHTTP_NO_REQUEST_DATA, 0, 0, 0); |
| 115 | if (!ok) { |
| 116 | DWORD e = GetLastError(); |
| 117 | if (is_timeout_error(e)) out_timed_out = true; |
| 118 | WinHttpCloseHandle(hRequest); |
| 119 | WinHttpCloseHandle(hConnect); |
| 120 | return body; |
| 121 | } |
| 122 | |
| 123 | ok = WinHttpReceiveResponse(hRequest, NULL); |
| 124 | if (!ok) { |
| 125 | DWORD e = GetLastError(); |
| 126 | if (is_timeout_error(e)) out_timed_out = true; |
| 127 | WinHttpCloseHandle(hRequest); |
| 128 | WinHttpCloseHandle(hConnect); |
| 129 | return body; |
| 130 | } |
| 131 | |
| 132 | for (;;) { |
| 133 | DWORD dwSize = 0; |
| 134 | if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) { |
| 135 | DWORD e = GetLastError(); |
| 136 | if (is_timeout_error(e)) out_timed_out = true; |
| 137 | break; |
| 138 | } |
| 139 | if (dwSize == 0) break; |
| 140 | std::string buf; buf.resize(dwSize); |
no test coverage detected