Debian Codegraph @

code/client/cl_http_curl.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 2006 Tony J. White ([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 #ifdef USE_INTERNAL_CURL_HEADERS
28 #include "curl/curl.h"
29 #else
30 #include <curl/curl.h>
31 #endif
32
33 #ifdef __APPLE__
34 #define DEFAULT_CURL_LIB "libcurl.dylib"
35 #else
36 #define DEFAULT_CURL_LIB "libcurl.so.4"
37 #define ALTERNATE_CURL_LIB "libcurl.so.3"
38 #endif
39
40 #include "../sys/sys_loadlib.h"
41
42 cvar_t *cl_cURLLib;
43
44 char* (*qcurl_version)(void);
45
46 CURL* (*qcurl_easy_init)(void);
47 CURLcode (*qcurl_easy_setopt)(CURL *curl, CURLoption option, ...);
48 CURLcode (*qcurl_easy_perform)(CURL *curl);
49 void (*qcurl_easy_cleanup)(CURL *curl);
50 CURLcode (*qcurl_easy_getinfo)(CURL *curl, CURLINFO info, ...);
51 CURL* (*qcurl_easy_duphandle)(CURL *curl);
52 void (*qcurl_easy_reset)(CURL *curl);
53 const char *(*qcurl_easy_strerror)(CURLcode);
54
55 CURLM* (*qcurl_multi_init)(void);
56 CURLMcode (*qcurl_multi_add_handle)(CURLM *multi_handle,
57 CURL *curl_handle);
58 CURLMcode (*qcurl_multi_remove_handle)(CURLM *multi_handle,
59 CURL *curl_handle);
60 CURLMcode (*qcurl_multi_fdset)(CURLM *multi_handle,
61 fd_set *read_fd_set,
62 fd_set *write_fd_set,
63 fd_set *exc_fd_set,
64 int *max_fd);
65 CURLMcode (*qcurl_multi_perform)(CURLM *multi_handle,
66 int *running_handles);
67 CURLMcode (*qcurl_multi_cleanup)(CURLM *multi_handle);
68 CURLMsg *(*qcurl_multi_info_read)(CURLM *multi_handle,
69 int *msgs_in_queue);
70 const char *(*qcurl_multi_strerror)(CURLMcode);
71
72 static void *cURLLib = NULL;
73 static qboolean cURLSymbolLoadFailed = qfalse;
74
75 static CURL *downloadCURL = NULL;
76 static CURLM *downloadCURLM = NULL;
77
78 /*
79 =================
80 GPA
81 =================
82 */
83 static void *GPA(char *str)
84 {
85 void *rv;
86
87 rv = Sys_LoadFunction(cURLLib, str);
88 if(!rv)
89 {
90 Com_Printf("Can't load symbol %s\n", str);
91 cURLSymbolLoadFailed = qtrue;
92 return NULL;
93 }
94 else
95 {
96 Com_DPrintf("Loaded symbol %s (0x%p)\n", str, rv);
97 return rv;
98 }
99 }
100
101 /*
102 =================
103 CL_HTTP_Init
104 =================
105 */
106 qboolean CL_HTTP_Init(void)
107 {
108 if(cURLLib)
109 return qtrue;
110
111 cl_cURLLib = Cvar_Get("cl_cURLLib", DEFAULT_CURL_LIB, CVAR_ARCHIVE | CVAR_PROTECTED);
112
113 Com_Printf("Loading \"%s\"...", cl_cURLLib->string);
114 if(!(cURLLib = Sys_LoadDll(cl_cURLLib->string, qtrue)))
115 {
116 #ifdef ALTERNATE_CURL_LIB
117 // On some linux distributions there is no libcurl.so.3, but only libcurl.so.4. That one works too.
118 if(!(cURLLib = Sys_LoadDll(ALTERNATE_CURL_LIB, qtrue)))
119 #endif
120 return qfalse;
121 }
122
123 cURLSymbolLoadFailed = qfalse;
124
125 qcurl_version = GPA("curl_version");
126
127 qcurl_easy_init = GPA("curl_easy_init");
128 qcurl_easy_setopt = GPA("curl_easy_setopt");
129 qcurl_easy_perform = GPA("curl_easy_perform");
130 qcurl_easy_cleanup = GPA("curl_easy_cleanup");
131 qcurl_easy_getinfo = GPA("curl_easy_getinfo");
132 qcurl_easy_duphandle = GPA("curl_easy_duphandle");
133 qcurl_easy_reset = GPA("curl_easy_reset");
134 qcurl_easy_strerror = GPA("curl_easy_strerror");
135
136 qcurl_multi_init = GPA("curl_multi_init");
137 qcurl_multi_add_handle = GPA("curl_multi_add_handle");
138 qcurl_multi_remove_handle = GPA("curl_multi_remove_handle");
139 qcurl_multi_fdset = GPA("curl_multi_fdset");
140 qcurl_multi_perform = GPA("curl_multi_perform");
141 qcurl_multi_cleanup = GPA("curl_multi_cleanup");
142 qcurl_multi_info_read = GPA("curl_multi_info_read");
143 qcurl_multi_strerror = GPA("curl_multi_strerror");
144
145 if(cURLSymbolLoadFailed)
146 {
147 CL_HTTP_Shutdown();
148 Com_Printf("FAIL One or more symbols not found\n");
149 return qfalse;
150 }
151 Com_Printf("OK\n");
152
153 return qtrue;
154 }
155
156 /*
157 =================
158 CL_HTTP_Available
159 =================
160 */
161 qboolean CL_HTTP_Available(void)
162 {
163 return cURLLib != NULL;
164 }
165
166 static void CL_cURL_Cleanup(void)
167 {
168 if(downloadCURLM) {
169 CURLMcode result;
170
171 if(downloadCURL) {
172 result = qcurl_multi_remove_handle(downloadCURLM,
173 downloadCURL);
174 if(result != CURLM_OK) {
175 Com_DPrintf("qcurl_multi_remove_handle failed: %s\n", qcurl_multi_strerror(result));
176 }
177 qcurl_easy_cleanup(downloadCURL);
178 }
179 result = qcurl_multi_cleanup(downloadCURLM);
180 if(result != CURLM_OK) {
181 Com_DPrintf("CL_cURL_Cleanup: qcurl_multi_cleanup failed: %s\n", qcurl_multi_strerror(result));
182 }
183 downloadCURLM = NULL;
184 downloadCURL = NULL;
185 }
186 else if(downloadCURL) {
187 qcurl_easy_cleanup(downloadCURL);
188 downloadCURL = NULL;
189 }
190 }
191
192 /*
193 =================
194 CL_HTTP_Shutdown
195 =================
196 */
197 void CL_HTTP_Shutdown( void )
198 {
199 CL_cURL_Cleanup();
200
201 if(cURLLib)
202 {
203 Sys_UnloadLibrary(cURLLib);
204 cURLLib = NULL;
205 }
206 qcurl_easy_init = NULL;
207 qcurl_easy_setopt = NULL;
208 qcurl_easy_perform = NULL;
209 qcurl_easy_cleanup = NULL;
210 qcurl_easy_getinfo = NULL;
211 qcurl_easy_duphandle = NULL;
212 qcurl_easy_reset = NULL;
213
214 qcurl_multi_init = NULL;
215 qcurl_multi_add_handle = NULL;
216 qcurl_multi_remove_handle = NULL;
217 qcurl_multi_fdset = NULL;
218 qcurl_multi_perform = NULL;
219 qcurl_multi_cleanup = NULL;
220 qcurl_multi_info_read = NULL;
221 qcurl_multi_strerror = NULL;
222 }
223
224 static int CL_cURL_CallbackProgress(void *clientp, curl_off_t dltotal, curl_off_t dlnow,
225 curl_off_t ultotal, curl_off_t ulnow)
226 {
227 clc.downloadSize = (int)dltotal;
228 Cvar_SetValue( "cl_downloadSize", clc.downloadSize );
229 clc.downloadCount = (int)dlnow;
230 Cvar_SetValue( "cl_downloadCount", clc.downloadCount );
231 return 0;
232 }
233
234 static size_t CL_cURL_CallbackWrite(void *buffer, size_t size, size_t nmemb,
235 void *stream)
236 {
237 FS_Write( buffer, size*nmemb, ((fileHandle_t*)stream)[0] );
238 return size*nmemb;
239 }
240
241 CURLcode qcurl_easy_setopt_warn(CURL *curl, CURLoption option, ...)
242 {
243 CURLcode result;
244
245 va_list argp;
246 va_start(argp, option);
247
248 if(option < CURLOPTTYPE_OBJECTPOINT) {
249 long longValue = va_arg(argp, long);
250 result = qcurl_easy_setopt(curl, option, longValue);
251 } else if(option < CURLOPTTYPE_OFF_T) {
252 void *pointerValue = va_arg(argp, void *);
253 result = qcurl_easy_setopt(curl, option, pointerValue);
254 } else {
255 curl_off_t offsetValue = va_arg(argp, curl_off_t);
256 result = qcurl_easy_setopt(curl, option, offsetValue);
257 }
258
259 if(result != CURLE_OK) {
260 Com_DPrintf("qcurl_easy_setopt failed: %s\n", qcurl_easy_strerror(result));
261 }
262 va_end(argp);
263
264 return result;
265 }
266
267 void CL_HTTP_BeginDownload( const char *remoteURL )
268 {
269 CURLMcode result;
270
271 CL_cURL_Cleanup();
272
273 downloadCURL = qcurl_easy_init();
274 if(!downloadCURL) {
275 Com_Error(ERR_DROP, "CL_HTTP_BeginDownload: qcurl_easy_init() "
276 "failed");
277 return;
278 }
279
280 if(com_developer->integer)
281 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_VERBOSE, 1);
282 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_URL, remoteURL);
283 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_TRANSFERTEXT, 0);
284 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_REFERER, va("ioQ3://%s",
285 NET_AdrToString(clc.serverAddress)));
286 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_USERAGENT, va("%s %s",
287 Q3_VERSION, qcurl_version()));
288 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_WRITEFUNCTION,
289 CL_cURL_CallbackWrite);
290 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_WRITEDATA, &clc.download);
291 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_NOPROGRESS, 0);
292 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_XFERINFOFUNCTION,
293 CL_cURL_CallbackProgress);
294 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_PROGRESSDATA, NULL);
295 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_FAILONERROR, 1);
296 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_FOLLOWLOCATION, 1);
297 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_MAXREDIRS, 5);
298 #if CURL_AT_LEAST_VERSION(7,85,0)
299 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_PROTOCOLS_STR, "http,https");
300 #else
301 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
302 #endif
303 qcurl_easy_setopt_warn(downloadCURL, CURLOPT_BUFFERSIZE, CURL_MAX_READ_SIZE);
304 downloadCURLM = qcurl_multi_init();
305 if(!downloadCURLM) {
306 qcurl_easy_cleanup(downloadCURL);
307 downloadCURL = NULL;
308 Com_Error(ERR_DROP, "CL_HTTP_BeginDownload: qcurl_multi_init() "
309 "failed");
310 return;
311 }
312 result = qcurl_multi_add_handle(downloadCURLM, downloadCURL);
313 if(result != CURLM_OK) {
314 qcurl_easy_cleanup(downloadCURL);
315 downloadCURL = NULL;
316 Com_Error(ERR_DROP,"CL_HTTP_BeginDownload: qcurl_multi_add_handle() failed: %s", qcurl_multi_strerror(result));
317 return;
318 }
319 }
320
321 qboolean CL_HTTP_PerformDownload(void)
322 {
323 CURLMcode res;
324 CURLMsg *msg;
325 int c;
326 int i = 0;
327
328 res = qcurl_multi_perform(downloadCURLM, &c);
329 while(res == CURLM_CALL_MULTI_PERFORM && i < 100) {
330 res = qcurl_multi_perform(downloadCURLM, &c);
331 i++;
332 }
333 if(res == CURLM_CALL_MULTI_PERFORM)
334 return qfalse;
335 msg = qcurl_multi_info_read(downloadCURLM, &c);
336 if(msg == NULL) {
337 return qfalse;
338 }
339 if(msg->msg != CURLMSG_DONE || msg->data.result != CURLE_OK) {
340 long code;
341
342 qcurl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE,
343 &code);
344 Com_Error(ERR_DROP, "Download Error: %s Code: %ld URL: %s",
345 qcurl_easy_strerror(msg->data.result),
346 code, clc.downloadURL);
347 }
348
349 return qtrue;
350 }
351
352 #endif /* USE_HTTP */