http.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "Updater.hpp"
  17. #include <algorithm>
  18. using namespace std;
  19. #define MAX_BUF_SIZE 262144
  20. #define READ_BUF_SIZE 32768
  21. /* ------------------------------------------------------------------------ */
  22. static bool ReadHTTPData(string &responseBuf, const uint8_t *buffer, DWORD outSize)
  23. {
  24. try {
  25. responseBuf.append((const char *)buffer, outSize);
  26. } catch (...) {
  27. return false;
  28. }
  29. return true;
  30. }
  31. bool HTTPPostData(const wchar_t *url, const BYTE *data, int dataLen, const wchar_t *extraHeaders, int *responseCode,
  32. string &responseBuf)
  33. {
  34. HttpHandle hSession;
  35. HttpHandle hConnect;
  36. HttpHandle hRequest;
  37. URL_COMPONENTS urlComponents = {};
  38. bool secure = false;
  39. wchar_t hostName[256];
  40. wchar_t path[1024];
  41. const wchar_t *acceptTypes[] = {L"*/*", nullptr};
  42. const DWORD tlsProtocols = WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2;
  43. const DWORD compressionFlags = WINHTTP_DECOMPRESSION_FLAG_ALL;
  44. responseBuf.clear();
  45. /* -------------------------------------- *
  46. * get URL components */
  47. urlComponents.dwStructSize = sizeof(urlComponents);
  48. urlComponents.lpszHostName = hostName;
  49. urlComponents.dwHostNameLength = _countof(hostName);
  50. urlComponents.lpszUrlPath = path;
  51. urlComponents.dwUrlPathLength = _countof(path);
  52. WinHttpCrackUrl(url, 0, 0, &urlComponents);
  53. if (urlComponents.nPort == 443)
  54. secure = true;
  55. /* -------------------------------------- *
  56. * connect to server */
  57. hSession = WinHttpOpen(L"OBS Studio Updater/3.0", WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY, WINHTTP_NO_PROXY_NAME,
  58. WINHTTP_NO_PROXY_BYPASS, 0);
  59. if (!hSession) {
  60. *responseCode = -1;
  61. return false;
  62. }
  63. WinHttpSetOption(hSession, WINHTTP_OPTION_SECURE_PROTOCOLS, (LPVOID)&tlsProtocols, sizeof(tlsProtocols));
  64. WinHttpSetOption(hSession, WINHTTP_OPTION_DECOMPRESSION, (LPVOID)&compressionFlags, sizeof(compressionFlags));
  65. hConnect = WinHttpConnect(hSession, hostName, secure ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT,
  66. 0);
  67. if (!hConnect) {
  68. *responseCode = -2;
  69. return false;
  70. }
  71. /* -------------------------------------- *
  72. * request data */
  73. hRequest = WinHttpOpenRequest(hConnect, L"POST", path, nullptr, WINHTTP_NO_REFERER, acceptTypes,
  74. secure ? WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH : WINHTTP_FLAG_REFRESH);
  75. if (!hRequest) {
  76. *responseCode = -3;
  77. return false;
  78. }
  79. bool bResults =
  80. !!WinHttpSendRequest(hRequest, extraHeaders, extraHeaders ? -1 : 0, (void *)data, dataLen, dataLen, 0);
  81. /* -------------------------------------- *
  82. * end request */
  83. if (bResults) {
  84. bResults = !!WinHttpReceiveResponse(hRequest, nullptr);
  85. } else {
  86. *responseCode = GetLastError();
  87. return false;
  88. }
  89. /* -------------------------------------- *
  90. * get headers */
  91. wchar_t statusCode[8];
  92. DWORD statusCodeLen;
  93. statusCodeLen = sizeof(statusCode);
  94. if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE, WINHTTP_HEADER_NAME_BY_INDEX, &statusCode,
  95. &statusCodeLen, WINHTTP_NO_HEADER_INDEX)) {
  96. *responseCode = -4;
  97. return false;
  98. } else {
  99. statusCode[_countof(statusCode) - 1] = 0;
  100. }
  101. /* -------------------------------------- *
  102. * allocate response data */
  103. DWORD responseBufSize = MAX_BUF_SIZE;
  104. try {
  105. responseBuf.reserve(responseBufSize);
  106. } catch (...) {
  107. *responseCode = -6;
  108. return false;
  109. }
  110. /* -------------------------------------- *
  111. * read data */
  112. *responseCode = wcstoul(statusCode, nullptr, 10);
  113. /* are we supposed to return true here? */
  114. if (!bResults || *responseCode != 200)
  115. return true;
  116. BYTE buffer[READ_BUF_SIZE];
  117. DWORD dwSize, outSize;
  118. do {
  119. /* Check for available data. */
  120. dwSize = 0;
  121. if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
  122. *responseCode = -8;
  123. return false;
  124. }
  125. dwSize = std::min(dwSize, (DWORD)sizeof(buffer));
  126. if (!WinHttpReadData(hRequest, (void *)buffer, dwSize, &outSize)) {
  127. *responseCode = -9;
  128. return false;
  129. }
  130. if (!outSize)
  131. break;
  132. if (!ReadHTTPData(responseBuf, buffer, outSize)) {
  133. *responseCode = -6;
  134. return false;
  135. }
  136. if (WaitForSingleObject(cancelRequested, 0) == WAIT_OBJECT_0) {
  137. *responseCode = -14;
  138. return false;
  139. }
  140. } while (dwSize > 0);
  141. return true;
  142. }
  143. /* ------------------------------------------------------------------------ */
  144. static bool ReadHTTPFile(HANDLE updateFile, const uint8_t *buffer, DWORD outSize, int *responseCode)
  145. {
  146. DWORD written;
  147. if (!WriteFile(updateFile, buffer, outSize, &written, nullptr)) {
  148. *responseCode = -12;
  149. return false;
  150. }
  151. if (written != outSize) {
  152. *responseCode = -13;
  153. return false;
  154. }
  155. completedFileSize += outSize;
  156. return true;
  157. }
  158. bool HTTPGetFile(HINTERNET hConnect, const wchar_t *url, const wchar_t *outputPath, const wchar_t *extraHeaders,
  159. int *responseCode)
  160. {
  161. HttpHandle hRequest;
  162. const wchar_t *acceptTypes[] = {L"*/*", nullptr};
  163. URL_COMPONENTS urlComponents = {};
  164. bool secure = false;
  165. wchar_t hostName[256];
  166. wchar_t path[1024];
  167. /* -------------------------------------- *
  168. * get URL components */
  169. urlComponents.dwStructSize = sizeof(urlComponents);
  170. urlComponents.lpszHostName = hostName;
  171. urlComponents.dwHostNameLength = _countof(hostName);
  172. urlComponents.lpszUrlPath = path;
  173. urlComponents.dwUrlPathLength = _countof(path);
  174. WinHttpCrackUrl(url, 0, 0, &urlComponents);
  175. if (urlComponents.nPort == 443)
  176. secure = true;
  177. /* -------------------------------------- *
  178. * request data */
  179. hRequest = WinHttpOpenRequest(hConnect, L"GET", path, nullptr, WINHTTP_NO_REFERER, acceptTypes,
  180. secure ? WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH : WINHTTP_FLAG_REFRESH);
  181. if (!hRequest) {
  182. *responseCode = -3;
  183. return false;
  184. }
  185. bool bResults =
  186. !!WinHttpSendRequest(hRequest, extraHeaders, extraHeaders ? -1 : 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
  187. /* -------------------------------------- *
  188. * end request */
  189. if (bResults) {
  190. bResults = !!WinHttpReceiveResponse(hRequest, nullptr);
  191. } else {
  192. *responseCode = GetLastError();
  193. return false;
  194. }
  195. /* -------------------------------------- *
  196. * get headers */
  197. wchar_t statusCode[8];
  198. DWORD statusCodeLen;
  199. statusCodeLen = sizeof(statusCode);
  200. if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE, WINHTTP_HEADER_NAME_BY_INDEX, &statusCode,
  201. &statusCodeLen, WINHTTP_NO_HEADER_INDEX)) {
  202. *responseCode = -4;
  203. return false;
  204. } else {
  205. statusCode[_countof(statusCode) - 1] = 0;
  206. }
  207. /* -------------------------------------- *
  208. * read data */
  209. *responseCode = wcstoul(statusCode, nullptr, 10);
  210. /* are we supposed to return true here? */
  211. if (!bResults || *responseCode != 200)
  212. return true;
  213. BYTE buffer[READ_BUF_SIZE];
  214. DWORD dwSize, outSize;
  215. int lastPosition = 0;
  216. WinHandle updateFile = CreateFile(outputPath, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
  217. if (!updateFile.Valid()) {
  218. *responseCode = -7;
  219. return false;
  220. }
  221. do {
  222. /* Check for available data. */
  223. dwSize = 0;
  224. if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
  225. *responseCode = -8;
  226. return false;
  227. }
  228. dwSize = std::min(dwSize, (DWORD)sizeof(buffer));
  229. if (!WinHttpReadData(hRequest, (void *)buffer, dwSize, &outSize)) {
  230. *responseCode = -9;
  231. return false;
  232. } else {
  233. if (!outSize)
  234. break;
  235. if (!ReadHTTPFile(updateFile, buffer, outSize, responseCode))
  236. return false;
  237. int position = (int)(((float)completedFileSize / (float)totalFileSize) * 100.0f);
  238. if (position > lastPosition) {
  239. lastPosition = position;
  240. SendDlgItemMessage(hwndMain, IDC_PROGRESS, PBM_SETPOS, position, 0);
  241. }
  242. }
  243. if (WaitForSingleObject(cancelRequested, 0) == WAIT_OBJECT_0) {
  244. *responseCode = -14;
  245. return false;
  246. }
  247. } while (dwSize > 0);
  248. return true;
  249. }
  250. bool HTTPGetBuffer(HINTERNET hConnect, const wchar_t *url, const wchar_t *extraHeaders, vector<std::byte> &out,
  251. int *responseCode)
  252. {
  253. HttpHandle hRequest;
  254. const wchar_t *acceptTypes[] = {L"*/*", nullptr};
  255. URL_COMPONENTS urlComponents = {};
  256. bool secure = false;
  257. wchar_t hostName[256];
  258. wchar_t path[1024];
  259. /* -------------------------------------- *
  260. * get URL components */
  261. urlComponents.dwStructSize = sizeof(urlComponents);
  262. urlComponents.lpszHostName = hostName;
  263. urlComponents.dwHostNameLength = _countof(hostName);
  264. urlComponents.lpszUrlPath = path;
  265. urlComponents.dwUrlPathLength = _countof(path);
  266. WinHttpCrackUrl(url, 0, 0, &urlComponents);
  267. if (urlComponents.nPort == 443)
  268. secure = true;
  269. /* -------------------------------------- *
  270. * request data */
  271. hRequest = WinHttpOpenRequest(hConnect, L"GET", path, nullptr, WINHTTP_NO_REFERER, acceptTypes,
  272. secure ? WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH : WINHTTP_FLAG_REFRESH);
  273. if (!hRequest) {
  274. *responseCode = -3;
  275. return false;
  276. }
  277. bool bResults =
  278. !!WinHttpSendRequest(hRequest, extraHeaders, extraHeaders ? -1 : 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
  279. /* -------------------------------------- *
  280. * end request */
  281. if (bResults) {
  282. bResults = !!WinHttpReceiveResponse(hRequest, nullptr);
  283. } else {
  284. *responseCode = GetLastError();
  285. return false;
  286. }
  287. /* -------------------------------------- *
  288. * get headers */
  289. wchar_t statusCode[8];
  290. DWORD statusCodeLen;
  291. statusCodeLen = sizeof(statusCode);
  292. if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE, WINHTTP_HEADER_NAME_BY_INDEX, &statusCode,
  293. &statusCodeLen, WINHTTP_NO_HEADER_INDEX)) {
  294. *responseCode = -4;
  295. return false;
  296. } else {
  297. statusCode[_countof(statusCode) - 1] = 0;
  298. }
  299. /* -------------------------------------- *
  300. * read data */
  301. *responseCode = wcstoul(statusCode, nullptr, 10);
  302. /* are we supposed to return true here? */
  303. if (!bResults || *responseCode != 200)
  304. return true;
  305. BYTE buffer[READ_BUF_SIZE];
  306. DWORD dwSize, outSize;
  307. int lastPosition = 0;
  308. do {
  309. /* Check for available data. */
  310. dwSize = 0;
  311. if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
  312. *responseCode = -8;
  313. return false;
  314. }
  315. dwSize = std::min(dwSize, (DWORD)sizeof(buffer));
  316. if (!WinHttpReadData(hRequest, (void *)buffer, dwSize, &outSize)) {
  317. *responseCode = -9;
  318. return false;
  319. } else {
  320. if (!outSize)
  321. break;
  322. out.insert(out.end(), (std::byte *)buffer, (std::byte *)buffer + outSize);
  323. completedFileSize += outSize;
  324. int position = (int)(((float)completedFileSize / (float)totalFileSize) * 100.0f);
  325. if (position > lastPosition) {
  326. lastPosition = position;
  327. SendDlgItemMessage(hwndMain, IDC_PROGRESS, PBM_SETPOS, position, 0);
  328. }
  329. }
  330. if (WaitForSingleObject(cancelRequested, 0) == WAIT_OBJECT_0) {
  331. *responseCode = -14;
  332. return false;
  333. }
  334. } while (dwSize > 0);
  335. return true;
  336. }