yolobs-studio/UI/win-update/updater/http.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

532 lines
12 KiB
C++
Raw Permalink Normal View History

2018-05-29 19:13:02 +00:00
/*
* Copyright (c) 2017-2018 Hugh Bailey <obs.jim@gmail.com>
*
* 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.
*/
2017-04-19 19:54:15 +00:00
#include "Updater.hpp"
#include <algorithm>
using namespace std;
2019-09-22 21:19:10 +00:00
#define MAX_BUF_SIZE 262144
2017-04-19 19:54:15 +00:00
#define READ_BUF_SIZE 32768
/* ------------------------------------------------------------------------ */
class ZipStream {
z_stream strm = {};
bool initialized = false;
public:
inline ~ZipStream()
{
if (initialized)
inflateEnd(&strm);
}
2019-09-22 21:19:10 +00:00
inline operator z_stream *() { return &strm; }
inline z_stream *operator->() { return &strm; }
2017-04-19 19:54:15 +00:00
inline bool inflate()
{
int ret = inflateInit2(&strm, 16 + MAX_WBITS);
initialized = (ret == Z_OK);
return initialized;
}
};
/* ------------------------------------------------------------------------ */
static bool ReadZippedHTTPData(string &responseBuf, z_stream *strm,
2019-09-22 21:19:10 +00:00
string &zipBuf, const uint8_t *buffer,
DWORD outSize)
2017-04-19 19:54:15 +00:00
{
2017-06-29 19:01:10 +00:00
strm->avail_in = outSize;
2019-09-22 21:19:10 +00:00
strm->next_in = buffer;
2017-04-19 19:54:15 +00:00
2017-06-29 19:01:10 +00:00
do {
2017-04-19 19:54:15 +00:00
strm->avail_out = (uInt)zipBuf.size();
2019-09-22 21:19:10 +00:00
strm->next_out = (Bytef *)zipBuf.data();
2017-04-19 19:54:15 +00:00
int zret = inflate(strm, Z_NO_FLUSH);
if (zret != Z_STREAM_END && zret != Z_OK)
return false;
try {
responseBuf.append(zipBuf.data(),
2019-09-22 21:19:10 +00:00
zipBuf.size() - strm->avail_out);
2017-04-19 19:54:15 +00:00
} catch (...) {
return false;
}
} while (strm->avail_out == 0);
return true;
}
static bool ReadHTTPData(string &responseBuf, const uint8_t *buffer,
2019-09-22 21:19:10 +00:00
DWORD outSize)
2017-04-19 19:54:15 +00:00
{
try {
responseBuf.append((const char *)buffer, outSize);
} catch (...) {
return false;
}
return true;
}
2019-09-22 21:19:10 +00:00
bool HTTPPostData(const wchar_t *url, const BYTE *data, int dataLen,
const wchar_t *extraHeaders, int *responseCode,
string &responseBuf)
2017-04-19 19:54:15 +00:00
{
2019-09-22 21:19:10 +00:00
HttpHandle hSession;
HttpHandle hConnect;
HttpHandle hRequest;
string zipBuf;
2017-04-19 19:54:15 +00:00
URL_COMPONENTS urlComponents = {};
2019-09-22 21:19:10 +00:00
bool secure = false;
2017-04-19 19:54:15 +00:00
wchar_t hostName[256];
wchar_t path[1024];
const wchar_t *acceptTypes[] = {L"*/*", nullptr};
2018-02-19 19:54:37 +00:00
const DWORD tlsProtocols = WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2;
2017-04-19 19:54:15 +00:00
responseBuf.clear();
/* -------------------------------------- *
* get URL components */
urlComponents.dwStructSize = sizeof(urlComponents);
2019-09-22 21:19:10 +00:00
urlComponents.lpszHostName = hostName;
2017-04-19 19:54:15 +00:00
urlComponents.dwHostNameLength = _countof(hostName);
2019-09-22 21:19:10 +00:00
urlComponents.lpszUrlPath = path;
2017-04-19 19:54:15 +00:00
urlComponents.dwUrlPathLength = _countof(path);
WinHttpCrackUrl(url, 0, 0, &urlComponents);
if (urlComponents.nPort == 443)
secure = true;
/* -------------------------------------- *
* connect to server */
2018-02-19 19:54:37 +00:00
hSession = WinHttpOpen(L"OBS Studio Updater/2.1",
2019-09-22 21:19:10 +00:00
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS,
0);
2017-04-19 19:54:15 +00:00
if (!hSession) {
*responseCode = -1;
return false;
}
2018-02-19 19:54:37 +00:00
WinHttpSetOption(hSession, WINHTTP_OPTION_SECURE_PROTOCOLS,
2019-09-22 21:19:10 +00:00
(LPVOID)&tlsProtocols, sizeof(tlsProtocols));
2018-02-19 19:54:37 +00:00
2017-04-19 19:54:15 +00:00
hConnect = WinHttpConnect(hSession, hostName,
2019-09-22 21:19:10 +00:00
secure ? INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT,
0);
2017-04-19 19:54:15 +00:00
if (!hConnect) {
*responseCode = -2;
return false;
}
/* -------------------------------------- *
* request data */
2019-09-22 21:19:10 +00:00
hRequest = WinHttpOpenRequest(hConnect, L"POST", path, nullptr,
WINHTTP_NO_REFERER, acceptTypes,
secure ? WINHTTP_FLAG_SECURE |
WINHTTP_FLAG_REFRESH
: WINHTTP_FLAG_REFRESH);
2017-04-19 19:54:15 +00:00
if (!hRequest) {
*responseCode = -3;
return false;
}
bool bResults = !!WinHttpSendRequest(hRequest, extraHeaders,
2019-09-22 21:19:10 +00:00
extraHeaders ? -1 : 0,
(void *)data, dataLen, dataLen, 0);
2017-04-19 19:54:15 +00:00
/* -------------------------------------- *
* end request */
if (bResults) {
bResults = !!WinHttpReceiveResponse(hRequest, nullptr);
} else {
*responseCode = GetLastError();
return false;
}
/* -------------------------------------- *
* get headers */
wchar_t encoding[64];
2019-09-22 21:19:10 +00:00
DWORD encodingLen;
2017-04-19 19:54:15 +00:00
wchar_t statusCode[8];
2019-09-22 21:19:10 +00:00
DWORD statusCodeLen;
2017-04-19 19:54:15 +00:00
statusCodeLen = sizeof(statusCode);
2019-09-22 21:19:10 +00:00
if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE,
WINHTTP_HEADER_NAME_BY_INDEX, &statusCode,
&statusCodeLen, WINHTTP_NO_HEADER_INDEX)) {
2017-04-19 19:54:15 +00:00
*responseCode = -4;
return false;
} else {
statusCode[_countof(statusCode) - 1] = 0;
}
encodingLen = sizeof(encoding);
2019-09-22 21:19:10 +00:00
if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_CONTENT_ENCODING,
WINHTTP_HEADER_NAME_BY_INDEX, encoding,
&encodingLen, WINHTTP_NO_HEADER_INDEX)) {
2017-04-19 19:54:15 +00:00
encoding[0] = 0;
if (GetLastError() != ERROR_WINHTTP_HEADER_NOT_FOUND) {
*responseCode = -5;
return false;
}
} else {
encoding[_countof(encoding) - 1] = 0;
}
/* -------------------------------------- *
* allocate response data */
DWORD responseBufSize = MAX_BUF_SIZE;
try {
responseBuf.reserve(responseBufSize);
} catch (...) {
*responseCode = -6;
return false;
}
/* -------------------------------------- *
* if zipped, initialize zip data */
ZipStream strm;
bool gzip = wcscmp(encoding, L"gzip") == 0;
if (gzip) {
2019-09-22 21:19:10 +00:00
strm->zalloc = Z_NULL;
strm->zfree = Z_NULL;
strm->opaque = Z_NULL;
2017-04-19 19:54:15 +00:00
strm->avail_in = 0;
2019-09-22 21:19:10 +00:00
strm->next_in = Z_NULL;
2017-04-19 19:54:15 +00:00
if (!strm.inflate())
return false;
try {
zipBuf.resize(MAX_BUF_SIZE);
} 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,
2019-09-22 21:19:10 +00:00
&outSize)) {
2017-04-19 19:54:15 +00:00
*responseCode = -9;
return false;
}
if (!outSize)
break;
if (gzip) {
if (!ReadZippedHTTPData(responseBuf, strm, zipBuf,
buffer, outSize)) {
*responseCode = -6;
return false;
}
} else {
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 ReadHTTPZippedFile(z_stream *strm, HANDLE updateFile,
2019-09-22 21:19:10 +00:00
string &zipBuf, const uint8_t *buffer,
DWORD outSize, int *responseCode)
2017-04-19 19:54:15 +00:00
{
2017-06-29 19:01:10 +00:00
strm->avail_in = outSize;
2019-09-22 21:19:10 +00:00
strm->next_in = buffer;
2017-04-19 19:54:15 +00:00
2017-06-29 19:01:10 +00:00
do {
2017-04-19 19:54:15 +00:00
strm->avail_out = (uInt)zipBuf.size();
2019-09-22 21:19:10 +00:00
strm->next_out = (Bytef *)zipBuf.data();
2017-04-19 19:54:15 +00:00
int zret = inflate(strm, Z_NO_FLUSH);
if (zret != Z_STREAM_END && zret != Z_OK)
return false;
DWORD written;
2019-09-22 21:19:10 +00:00
if (!WriteFile(updateFile, zipBuf.data(),
MAX_BUF_SIZE - strm->avail_out, &written,
2017-04-19 19:54:15 +00:00
nullptr)) {
*responseCode = -10;
return false;
}
if (written != MAX_BUF_SIZE - strm->avail_out) {
*responseCode = -11;
return false;
}
completedFileSize += written;
} while (strm->avail_out == 0);
return true;
}
static bool ReadHTTPFile(HANDLE updateFile, const uint8_t *buffer,
2019-09-22 21:19:10 +00:00
DWORD outSize, int *responseCode)
2017-04-19 19:54:15 +00:00
{
DWORD written;
if (!WriteFile(updateFile, buffer, outSize, &written, nullptr)) {
*responseCode = -12;
return false;
}
if (written != outSize) {
*responseCode = -13;
return false;
}
completedFileSize += outSize;
return true;
}
2019-09-22 21:19:10 +00:00
bool HTTPGetFile(HINTERNET hConnect, const wchar_t *url,
const wchar_t *outputPath, const wchar_t *extraHeaders,
int *responseCode)
2017-04-19 19:54:15 +00:00
{
HttpHandle hRequest;
const wchar_t *acceptTypes[] = {L"*/*", nullptr};
URL_COMPONENTS urlComponents = {};
2019-09-22 21:19:10 +00:00
bool secure = false;
2017-04-19 19:54:15 +00:00
2019-09-22 21:19:10 +00:00
string zipBuf;
2017-04-19 19:54:15 +00:00
wchar_t hostName[256];
wchar_t path[1024];
/* -------------------------------------- *
* get URL components */
urlComponents.dwStructSize = sizeof(urlComponents);
2019-09-22 21:19:10 +00:00
urlComponents.lpszHostName = hostName;
2017-04-19 19:54:15 +00:00
urlComponents.dwHostNameLength = _countof(hostName);
2019-09-22 21:19:10 +00:00
urlComponents.lpszUrlPath = path;
2017-04-19 19:54:15 +00:00
urlComponents.dwUrlPathLength = _countof(path);
WinHttpCrackUrl(url, 0, 0, &urlComponents);
if (urlComponents.nPort == 443)
secure = true;
/* -------------------------------------- *
* request data */
2019-09-22 21:19:10 +00:00
hRequest = WinHttpOpenRequest(hConnect, L"GET", path, nullptr,
WINHTTP_NO_REFERER, acceptTypes,
secure ? WINHTTP_FLAG_SECURE |
WINHTTP_FLAG_REFRESH
: WINHTTP_FLAG_REFRESH);
2017-04-19 19:54:15 +00:00
if (!hRequest) {
*responseCode = -3;
return false;
}
bool bResults = !!WinHttpSendRequest(hRequest, extraHeaders,
2019-09-22 21:19:10 +00:00
extraHeaders ? -1 : 0,
WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
2017-04-19 19:54:15 +00:00
/* -------------------------------------- *
* end request */
if (bResults) {
bResults = !!WinHttpReceiveResponse(hRequest, nullptr);
} else {
*responseCode = GetLastError();
return false;
}
/* -------------------------------------- *
* get headers */
wchar_t encoding[64];
2019-09-22 21:19:10 +00:00
DWORD encodingLen;
2017-04-19 19:54:15 +00:00
wchar_t statusCode[8];
2019-09-22 21:19:10 +00:00
DWORD statusCodeLen;
2017-04-19 19:54:15 +00:00
statusCodeLen = sizeof(statusCode);
2019-09-22 21:19:10 +00:00
if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE,
WINHTTP_HEADER_NAME_BY_INDEX, &statusCode,
&statusCodeLen, WINHTTP_NO_HEADER_INDEX)) {
2017-04-19 19:54:15 +00:00
*responseCode = -4;
return false;
} else {
statusCode[_countof(statusCode) - 1] = 0;
}
encodingLen = sizeof(encoding);
2019-09-22 21:19:10 +00:00
if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_CONTENT_ENCODING,
WINHTTP_HEADER_NAME_BY_INDEX, encoding,
&encodingLen, WINHTTP_NO_HEADER_INDEX)) {
2017-04-19 19:54:15 +00:00
encoding[0] = 0;
if (GetLastError() != ERROR_WINHTTP_HEADER_NOT_FOUND) {
*responseCode = -5;
return false;
}
} else {
encoding[_countof(encoding) - 1] = 0;
}
/* -------------------------------------- *
* allocate response data */
ZipStream strm;
bool gzip = wcscmp(encoding, L"gzip") == 0;
if (gzip) {
2019-09-22 21:19:10 +00:00
strm->zalloc = Z_NULL;
strm->zfree = Z_NULL;
strm->opaque = Z_NULL;
2017-04-19 19:54:15 +00:00
strm->avail_in = 0;
2019-09-22 21:19:10 +00:00
strm->next_in = Z_NULL;
2017-04-19 19:54:15 +00:00
if (!strm.inflate())
return false;
try {
zipBuf.resize(MAX_BUF_SIZE);
} 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;
2019-09-22 21:19:10 +00:00
BYTE buffer[READ_BUF_SIZE];
2017-04-19 19:54:15 +00:00
DWORD dwSize, outSize;
2019-09-22 21:19:10 +00:00
int lastPosition = 0;
2017-04-19 19:54:15 +00:00
2019-09-22 21:19:10 +00:00
WinHandle updateFile = CreateFile(outputPath, GENERIC_WRITE, 0, nullptr,
CREATE_ALWAYS, 0, nullptr);
2017-04-19 19:54:15 +00:00
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,
2019-09-22 21:19:10 +00:00
&outSize)) {
2017-04-19 19:54:15 +00:00
*responseCode = -9;
return false;
} else {
if (!outSize)
break;
if (gzip) {
if (!ReadHTTPZippedFile(strm, updateFile,
2019-09-22 21:19:10 +00:00
zipBuf, buffer, outSize,
responseCode))
2017-04-19 19:54:15 +00:00
return false;
} else {
2019-09-22 21:19:10 +00:00
if (!ReadHTTPFile(updateFile, buffer, outSize,
responseCode))
2017-04-19 19:54:15 +00:00
return false;
}
int position = (int)(((float)completedFileSize /
2019-09-22 21:19:10 +00:00
(float)totalFileSize) *
100.0f);
2017-04-19 19:54:15 +00:00
if (position > lastPosition) {
lastPosition = position;
SendDlgItemMessage(hwndMain, IDC_PROGRESS,
2019-09-22 21:19:10 +00:00
PBM_SETPOS, position, 0);
2017-04-19 19:54:15 +00:00
}
}
if (WaitForSingleObject(cancelRequested, 0) == WAIT_OBJECT_0) {
*responseCode = -14;
return false;
}
} while (dwSize > 0);
return true;
}