Debian Codegraph @

code/client/cl_http_windows.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 2025 Tim Angus ([email protected])
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 CL_HTTP_Init
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 CL_HTTP_Available
72 =================
73 */
74 qboolean CL_HTTP_Available()
75 {
76 return hInternet != NULL;
77 }
78
79 /*
80 =================
81 CL_HTTP_Shutdown
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 CL_HTTP_BeginDownload
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 CL_HTTP_PerformDownload
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 /* USE_HTTP */