123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- /*
- * Copyright (c) 2023 Lain Bailey <[email protected]>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
- #include "Updater.hpp"
- #include <algorithm>
- using namespace std;
- #define MAX_BUF_SIZE 262144
- #define READ_BUF_SIZE 32768
- /* ------------------------------------------------------------------------ */
- static bool ReadHTTPData(string &responseBuf, const uint8_t *buffer, DWORD outSize)
- {
- try {
- responseBuf.append((const char *)buffer, outSize);
- } catch (...) {
- return false;
- }
- return true;
- }
- bool HTTPPostData(const wchar_t *url, const BYTE *data, int dataLen, const wchar_t *extraHeaders, int *responseCode,
- string &responseBuf)
- {
- HttpHandle hSession;
- HttpHandle hConnect;
- HttpHandle hRequest;
- URL_COMPONENTS urlComponents = {};
- bool secure = false;
- wchar_t hostName[256];
- wchar_t path[1024];
- const wchar_t *acceptTypes[] = {L"*/*", nullptr};
- const DWORD tlsProtocols = WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2;
- const DWORD compressionFlags = WINHTTP_DECOMPRESSION_FLAG_ALL;
- responseBuf.clear();
- /* -------------------------------------- *
- * get URL components */
- urlComponents.dwStructSize = sizeof(urlComponents);
- urlComponents.lpszHostName = hostName;
- urlComponents.dwHostNameLength = _countof(hostName);
- urlComponents.lpszUrlPath = path;
- urlComponents.dwUrlPathLength = _countof(path);
- WinHttpCrackUrl(url, 0, 0, &urlComponents);
- if (urlComponents.nPort == 443)
- secure = true;
- /* -------------------------------------- *
- * connect to server */
- hSession = WinHttpOpen(L"OBS Studio Updater/3.0", WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY, WINHTTP_NO_PROXY_NAME,
- WINHTTP_NO_PROXY_BYPASS, 0);
- if (!hSession) {
- *responseCode = -1;
- return false;
- }
- WinHttpSetOption(hSession, WINHTTP_OPTION_SECURE_PROTOCOLS, (LPVOID)&tlsProtocols, sizeof(tlsProtocols));
- WinHttpSetOption(hSession, WINHTTP_OPTION_DECOMPRESSION, (LPVOID)&compressionFlags, sizeof(compressionFlags));
- hConnect = WinHttpConnect(hSession, hostName, secure ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT,
- 0);
- if (!hConnect) {
- *responseCode = -2;
- return false;
- }
- /* -------------------------------------- *
- * request data */
- hRequest = WinHttpOpenRequest(hConnect, L"POST", path, nullptr, WINHTTP_NO_REFERER, acceptTypes,
- secure ? WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH : WINHTTP_FLAG_REFRESH);
- if (!hRequest) {
- *responseCode = -3;
- return false;
- }
- bool bResults =
- !!WinHttpSendRequest(hRequest, extraHeaders, extraHeaders ? -1 : 0, (void *)data, dataLen, dataLen, 0);
- /* -------------------------------------- *
- * end request */
- if (bResults) {
- bResults = !!WinHttpReceiveResponse(hRequest, nullptr);
- } else {
- *responseCode = GetLastError();
- return false;
- }
- /* -------------------------------------- *
- * get headers */
- wchar_t statusCode[8];
- DWORD statusCodeLen;
- statusCodeLen = sizeof(statusCode);
- if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE, WINHTTP_HEADER_NAME_BY_INDEX, &statusCode,
- &statusCodeLen, WINHTTP_NO_HEADER_INDEX)) {
- *responseCode = -4;
- return false;
- } else {
- statusCode[_countof(statusCode) - 1] = 0;
- }
- /* -------------------------------------- *
- * allocate response data */
- DWORD responseBufSize = MAX_BUF_SIZE;
- try {
- responseBuf.reserve(responseBufSize);
- } catch (...) {
- *responseCode = -6;
- return false;
- }
- /* -------------------------------------- *
- * read data */
- *responseCode = wcstoul(statusCode, nullptr, 10);
- /* are we supposed to return true here? */
- if (!bResults || *responseCode != 200)
- return true;
- BYTE buffer[READ_BUF_SIZE];
- DWORD dwSize, outSize;
- do {
- /* Check for available data. */
- dwSize = 0;
- if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
- *responseCode = -8;
- return false;
- }
- dwSize = std::min(dwSize, (DWORD)sizeof(buffer));
- if (!WinHttpReadData(hRequest, (void *)buffer, dwSize, &outSize)) {
- *responseCode = -9;
- return false;
- }
- if (!outSize)
- break;
- if (!ReadHTTPData(responseBuf, buffer, outSize)) {
- *responseCode = -6;
- return false;
- }
- if (WaitForSingleObject(cancelRequested, 0) == WAIT_OBJECT_0) {
- *responseCode = -14;
- return false;
- }
- } while (dwSize > 0);
- return true;
- }
- /* ------------------------------------------------------------------------ */
- static bool ReadHTTPFile(HANDLE updateFile, const uint8_t *buffer, DWORD outSize, int *responseCode)
- {
- DWORD written;
- if (!WriteFile(updateFile, buffer, outSize, &written, nullptr)) {
- *responseCode = -12;
- return false;
- }
- if (written != outSize) {
- *responseCode = -13;
- return false;
- }
- completedFileSize += outSize;
- return true;
- }
- bool HTTPGetFile(HINTERNET hConnect, const wchar_t *url, const wchar_t *outputPath, const wchar_t *extraHeaders,
- int *responseCode)
- {
- HttpHandle hRequest;
- const wchar_t *acceptTypes[] = {L"*/*", nullptr};
- URL_COMPONENTS urlComponents = {};
- bool secure = false;
- wchar_t hostName[256];
- wchar_t path[1024];
- /* -------------------------------------- *
- * get URL components */
- urlComponents.dwStructSize = sizeof(urlComponents);
- urlComponents.lpszHostName = hostName;
- urlComponents.dwHostNameLength = _countof(hostName);
- urlComponents.lpszUrlPath = path;
- urlComponents.dwUrlPathLength = _countof(path);
- WinHttpCrackUrl(url, 0, 0, &urlComponents);
- if (urlComponents.nPort == 443)
- secure = true;
- /* -------------------------------------- *
- * request data */
- hRequest = WinHttpOpenRequest(hConnect, L"GET", path, nullptr, WINHTTP_NO_REFERER, acceptTypes,
- secure ? WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH : WINHTTP_FLAG_REFRESH);
- if (!hRequest) {
- *responseCode = -3;
- return false;
- }
- bool bResults =
- !!WinHttpSendRequest(hRequest, extraHeaders, extraHeaders ? -1 : 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
- /* -------------------------------------- *
- * end request */
- if (bResults) {
- bResults = !!WinHttpReceiveResponse(hRequest, nullptr);
- } else {
- *responseCode = GetLastError();
- return false;
- }
- /* -------------------------------------- *
- * get headers */
- wchar_t statusCode[8];
- DWORD statusCodeLen;
- statusCodeLen = sizeof(statusCode);
- if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE, WINHTTP_HEADER_NAME_BY_INDEX, &statusCode,
- &statusCodeLen, WINHTTP_NO_HEADER_INDEX)) {
- *responseCode = -4;
- return false;
- } else {
- statusCode[_countof(statusCode) - 1] = 0;
- }
- /* -------------------------------------- *
- * read data */
- *responseCode = wcstoul(statusCode, nullptr, 10);
- /* are we supposed to return true here? */
- if (!bResults || *responseCode != 200)
- return true;
- BYTE buffer[READ_BUF_SIZE];
- DWORD dwSize, outSize;
- int lastPosition = 0;
- WinHandle updateFile = CreateFile(outputPath, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
- if (!updateFile.Valid()) {
- *responseCode = -7;
- return false;
- }
- do {
- /* Check for available data. */
- dwSize = 0;
- if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
- *responseCode = -8;
- return false;
- }
- dwSize = std::min(dwSize, (DWORD)sizeof(buffer));
- if (!WinHttpReadData(hRequest, (void *)buffer, dwSize, &outSize)) {
- *responseCode = -9;
- return false;
- } else {
- if (!outSize)
- break;
- if (!ReadHTTPFile(updateFile, buffer, outSize, responseCode))
- return false;
- int position = (int)(((float)completedFileSize / (float)totalFileSize) * 100.0f);
- if (position > lastPosition) {
- lastPosition = position;
- SendDlgItemMessage(hwndMain, IDC_PROGRESS, PBM_SETPOS, position, 0);
- }
- }
- if (WaitForSingleObject(cancelRequested, 0) == WAIT_OBJECT_0) {
- *responseCode = -14;
- return false;
- }
- } while (dwSize > 0);
- return true;
- }
- bool HTTPGetBuffer(HINTERNET hConnect, const wchar_t *url, const wchar_t *extraHeaders, vector<std::byte> &out,
- int *responseCode)
- {
- HttpHandle hRequest;
- const wchar_t *acceptTypes[] = {L"*/*", nullptr};
- URL_COMPONENTS urlComponents = {};
- bool secure = false;
- wchar_t hostName[256];
- wchar_t path[1024];
- /* -------------------------------------- *
- * get URL components */
- urlComponents.dwStructSize = sizeof(urlComponents);
- urlComponents.lpszHostName = hostName;
- urlComponents.dwHostNameLength = _countof(hostName);
- urlComponents.lpszUrlPath = path;
- urlComponents.dwUrlPathLength = _countof(path);
- WinHttpCrackUrl(url, 0, 0, &urlComponents);
- if (urlComponents.nPort == 443)
- secure = true;
- /* -------------------------------------- *
- * request data */
- hRequest = WinHttpOpenRequest(hConnect, L"GET", path, nullptr, WINHTTP_NO_REFERER, acceptTypes,
- secure ? WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH : WINHTTP_FLAG_REFRESH);
- if (!hRequest) {
- *responseCode = -3;
- return false;
- }
- bool bResults =
- !!WinHttpSendRequest(hRequest, extraHeaders, extraHeaders ? -1 : 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
- /* -------------------------------------- *
- * end request */
- if (bResults) {
- bResults = !!WinHttpReceiveResponse(hRequest, nullptr);
- } else {
- *responseCode = GetLastError();
- return false;
- }
- /* -------------------------------------- *
- * get headers */
- wchar_t statusCode[8];
- DWORD statusCodeLen;
- statusCodeLen = sizeof(statusCode);
- if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE, WINHTTP_HEADER_NAME_BY_INDEX, &statusCode,
- &statusCodeLen, WINHTTP_NO_HEADER_INDEX)) {
- *responseCode = -4;
- return false;
- } else {
- statusCode[_countof(statusCode) - 1] = 0;
- }
- /* -------------------------------------- *
- * read data */
- *responseCode = wcstoul(statusCode, nullptr, 10);
- /* are we supposed to return true here? */
- if (!bResults || *responseCode != 200)
- return true;
- BYTE buffer[READ_BUF_SIZE];
- DWORD dwSize, outSize;
- int lastPosition = 0;
- do {
- /* Check for available data. */
- dwSize = 0;
- if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
- *responseCode = -8;
- return false;
- }
- dwSize = std::min(dwSize, (DWORD)sizeof(buffer));
- if (!WinHttpReadData(hRequest, (void *)buffer, dwSize, &outSize)) {
- *responseCode = -9;
- return false;
- } else {
- if (!outSize)
- break;
- out.insert(out.end(), (std::byte *)buffer, (std::byte *)buffer + outSize);
- completedFileSize += outSize;
- int position = (int)(((float)completedFileSize / (float)totalFileSize) * 100.0f);
- if (position > lastPosition) {
- lastPosition = position;
- SendDlgItemMessage(hwndMain, IDC_PROGRESS, PBM_SETPOS, position, 0);
- }
- }
- if (WaitForSingleObject(cancelRequested, 0) == WAIT_OBJECT_0) {
- *responseCode = -14;
- return false;
- }
- } while (dwSize > 0);
- return true;
- }
|