| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#ifdef USE_HTTP |
| 24 |
|
| 25 |
#include "client.h" |
| 26 |
|
| 27 |
#include <windows.h> |
| 28 |
#include <wininet.h> |
| 29 |
|
| 30 |
static HINTERNET hInternet = NULL; |
| 31 |
static HINTERNET hUrl = NULL; |
| 32 |
|
| 33 |
static Q_PRINTF_FUNC(2, 3) void DropIf(qboolean condition, const char *fmt, ...) |
| 34 |
{ |
| 35 |
char buffer[1024]; |
| 36 |
|
| 37 |
if (!condition) |
| 38 |
return; |
| 39 |
|
| 40 |
va_list argptr; |
| 41 |
va_start(argptr, fmt); |
| 42 |
Q_vsnprintf(buffer, sizeof(buffer), fmt, argptr); |
| 43 |
va_end(argptr); |
| 44 |
|
| 45 |
Com_Error(ERR_DROP, "Download Error: %s URL: %s", buffer, clc.downloadURL); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
qboolean CL_HTTP_Init() |
| 54 |
{ |
| 55 |
OSVERSIONINFO osvi = {sizeof(OSVERSIONINFO)}; |
| 56 |
const char *windowsVersion = GetVersionEx(&osvi) ? va("Windows %lu.%lu (build %lu)", |
| 57 |
osvi.dwMajorVersion, |
| 58 |
osvi.dwMinorVersion, |
| 59 |
osvi.dwBuildNumber) |
| 60 |
: "Windows"; |
| 61 |
|
| 62 |
hInternet = InternetOpenA( |
| 63 |
va("%s %s", Q3_VERSION, windowsVersion), |
| 64 |
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); |
| 65 |
|
| 66 |
return hInternet != NULL; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
qboolean CL_HTTP_Available() |
| 75 |
{ |
| 76 |
return hInternet != NULL; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
void CL_HTTP_Shutdown(void) |
| 85 |
{ |
| 86 |
if (hInternet) |
| 87 |
{ |
| 88 |
InternetCloseHandle(hInternet); |
| 89 |
hInternet = NULL; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
void CL_HTTP_BeginDownload(const char *remoteURL) |
| 99 |
{ |
| 100 |
DWORD httpCode = 0; |
| 101 |
DWORD contentLength = 0; |
| 102 |
DWORD len = sizeof(httpCode); |
| 103 |
DWORD zero = 0; |
| 104 |
BOOL success; |
| 105 |
|
| 106 |
hUrl = InternetOpenUrlA(hInternet, remoteURL, |
| 107 |
va("Referer: ioQ3://%s\r\n", NET_AdrToString(clc.serverAddress)), (DWORD)-1, |
| 108 |
INTERNET_FLAG_HYPERLINK | |
| 109 |
INTERNET_FLAG_NO_CACHE_WRITE | |
| 110 |
INTERNET_FLAG_NO_COOKIES | |
| 111 |
INTERNET_FLAG_NO_UI | |
| 112 |
INTERNET_FLAG_RESYNCHRONIZE | |
| 113 |
INTERNET_FLAG_RELOAD, |
| 114 |
0); |
| 115 |
|
| 116 |
DropIf(hUrl == NULL, "InternetOpenUrlA failed %lu", GetLastError()); |
| 117 |
|
| 118 |
success = HttpQueryInfo(hUrl, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &httpCode, &len, &zero); |
| 119 |
DropIf(!success, "Get HTTP_QUERY_STATUS_CODE failed %lu", GetLastError()); |
| 120 |
|
| 121 |
DropIf(httpCode >= 400, "HTTP code %lu", httpCode); |
| 122 |
DropIf(httpCode != 200, "Unhandled HTTP code %lu", httpCode); |
| 123 |
|
| 124 |
success = HttpQueryInfo(hUrl, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &contentLength, &len, &zero); |
| 125 |
DropIf(!success, "Get HTTP_QUERY_CONTENT_LENGTH failed %lu", GetLastError()); |
| 126 |
|
| 127 |
clc.downloadSize = (int)contentLength; |
| 128 |
Cvar_SetValue("cl_downloadSize", clc.downloadSize); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
qboolean CL_HTTP_PerformDownload(void) |
| 137 |
{ |
| 138 |
static BYTE readBuffer[256 * 1024]; |
| 139 |
DWORD bytesRead = 0; |
| 140 |
BOOL success; |
| 141 |
|
| 142 |
DropIf(hUrl == NULL, "hUrl is NULL"); |
| 143 |
|
| 144 |
success = InternetReadFile(hUrl, readBuffer, sizeof(readBuffer), &bytesRead); |
| 145 |
DropIf(!success, "InternetReadFile failed %lu", GetLastError()); |
| 146 |
|
| 147 |
if (bytesRead > 0) |
| 148 |
{ |
| 149 |
clc.downloadCount += bytesRead; |
| 150 |
Cvar_SetValue("cl_downloadCount", clc.downloadCount); |
| 151 |
|
| 152 |
DWORD bytesWritten = (DWORD)FS_Write(readBuffer, bytesRead, clc.download); |
| 153 |
DropIf(bytesWritten != bytesRead, "bytesWritten != bytesRead"); |
| 154 |
|
| 155 |
return qfalse; |
| 156 |
} |
| 157 |
|
| 158 |
InternetCloseHandle(hUrl); |
| 159 |
return qtrue; |
| 160 |
} |
| 161 |
|
| 162 |
#endif |