1 | /* |
---|
2 | * ulinet.c |
---|
3 | * |
---|
4 | * Author : Gérald FENOY |
---|
5 | * |
---|
6 | * Copyright (c) 2008-2019 GeoLabs SARL |
---|
7 | * |
---|
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
9 | * of this software and associated documentation files (the "Software"), to deal |
---|
10 | * in the Software without restriction, including without limitation the rights |
---|
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
12 | * copies of the Software, and to permit persons to whom the Software is |
---|
13 | * furnished to do so, subject to the following conditions: |
---|
14 | * |
---|
15 | * The above copyright notice and this permission notice shall be included in |
---|
16 | * all copies or substantial portions of the Software. |
---|
17 | * |
---|
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
24 | * THE SOFTWARE. |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #define _ULINET |
---|
29 | #define MAX_WAIT_MSECS 180*1000 /* Wait max. 180 seconds */ |
---|
30 | #include "ulinet.h" |
---|
31 | #include "server_internal.h" |
---|
32 | #include <assert.h> |
---|
33 | #include <ctype.h> |
---|
34 | #include "fcgi_stdio.h" |
---|
35 | |
---|
36 | /** |
---|
37 | * Write the downloaded content to a _HINTERNET structure |
---|
38 | * |
---|
39 | * @param buffer the buffer to read |
---|
40 | * @param size size of each member |
---|
41 | * @param nmemb number of element to read |
---|
42 | * @param data the _HINTERNET structure to write in |
---|
43 | * @return the size red, -1 if buffer is NULL |
---|
44 | */ |
---|
45 | size_t write_data_into(void *buffer, size_t size, size_t nmemb, void *data){ |
---|
46 | size_t realsize = size * nmemb; |
---|
47 | _HINTERNET *psInternet; |
---|
48 | if(buffer==NULL){ |
---|
49 | buffer=NULL; |
---|
50 | return -1; |
---|
51 | } |
---|
52 | psInternet=(_HINTERNET *)data; |
---|
53 | if(psInternet->pabyData){ |
---|
54 | psInternet->pabyData=(unsigned char*)realloc(psInternet->pabyData,psInternet->nDataLen+realsize+1); |
---|
55 | psInternet->nDataAlloc+=psInternet->nDataLen+realsize+1; |
---|
56 | } |
---|
57 | else{ |
---|
58 | psInternet->pabyData=(unsigned char*)malloc(psInternet->nDataLen+realsize+1); |
---|
59 | psInternet->nDataAlloc=realsize+1; |
---|
60 | } |
---|
61 | |
---|
62 | if (psInternet->pabyData) { |
---|
63 | memcpy( psInternet->pabyData + psInternet->nDataLen, buffer, realsize); |
---|
64 | psInternet->nDataLen += realsize; |
---|
65 | psInternet->pabyData[psInternet->nDataLen] = 0; |
---|
66 | } |
---|
67 | |
---|
68 | buffer=NULL; |
---|
69 | return realsize; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Write the downloaded content in the file pouted by the _HINTERNET structure |
---|
74 | * |
---|
75 | * @param buffer the buffer to read |
---|
76 | * @param size size of each member |
---|
77 | * @param nmemb number of element to read |
---|
78 | * @param data the _HINTERNET structure to write in |
---|
79 | * @return the size red, -1 if buffer is NULL |
---|
80 | */ |
---|
81 | size_t write_data_into_file(void *buffer, size_t size, size_t nmemb, void *data) |
---|
82 | { |
---|
83 | size_t realsize = size * nmemb; |
---|
84 | size_t writen=0; |
---|
85 | _HINTERNET *psInternet; |
---|
86 | if(buffer==NULL){ |
---|
87 | buffer=NULL; |
---|
88 | return -1; |
---|
89 | } |
---|
90 | psInternet=(_HINTERNET *)data; |
---|
91 | writen+=fwrite(buffer, size, nmemb, psInternet->file); |
---|
92 | fflush(psInternet->file); |
---|
93 | psInternet->nDataLen += realsize; |
---|
94 | |
---|
95 | buffer=NULL; |
---|
96 | return realsize; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | /** |
---|
101 | * In case of presence of "Set-Cookie" in the headers red, store the cookie |
---|
102 | * identifier in cookie |
---|
103 | * |
---|
104 | * @param buffer the buffer to read |
---|
105 | * @param size size of each member |
---|
106 | * @param nmemb number of element to read |
---|
107 | * @param data the _HINTERNET structure to write in |
---|
108 | * @return the size red, -1 if buffer is NULL |
---|
109 | * @see cookie |
---|
110 | */ |
---|
111 | size_t header_write_data(void *buffer, size_t size, size_t nmemb, void *data){ |
---|
112 | if(strncmp("Set-Cookie: ",(char*)buffer,12)==0){ |
---|
113 | int i; |
---|
114 | char* tmp; |
---|
115 | for(i=0;i<12;i++) |
---|
116 | #ifndef WIN32 |
---|
117 | buffer++; |
---|
118 | #else |
---|
119 | ; |
---|
120 | #endif |
---|
121 | tmp=strtok((char*) buffer,";"); // knut: added cast to char* |
---|
122 | int cnt=0; |
---|
123 | _HINTERNET *psInternet=(_HINTERNET *)data; |
---|
124 | if(tmp!=NULL && psInternet!=NULL){ |
---|
125 | psInternet->cookie=(char*)malloc(sizeof(char)*(strlen(tmp)+1)); |
---|
126 | sprintf(psInternet->cookie,"%s",tmp); |
---|
127 | } |
---|
128 | } |
---|
129 | return size * nmemb;//write_data_into(buffer,size,nmemb,data,HEADER); |
---|
130 | }; |
---|
131 | |
---|
132 | /** |
---|
133 | * Define the proxy to use for a CURL handler |
---|
134 | * |
---|
135 | * @param handle the CURL handler |
---|
136 | * @param host the proxy host (including http://) |
---|
137 | * @param port the proxy port |
---|
138 | */ |
---|
139 | void setProxy(CURL* handle,char* host,long port){ |
---|
140 | char* proxyDef=(char*)malloc((strlen(host)+10+2)*sizeof(char)); |
---|
141 | sprintf(proxyDef,"%s:%ld",host,port); |
---|
142 | curl_easy_setopt(handle,CURLOPT_PROXY,proxyDef); |
---|
143 | free(proxyDef); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * MACOSX |
---|
148 | */ |
---|
149 | #if defined(macintosh) || (defined(__MACH__) && defined(__APPLE__)) |
---|
150 | |
---|
151 | |
---|
152 | char* CFStringToCString(CFStringRef dest,char *buffer){ |
---|
153 | CFStringEncoding encoding = kCFStringEncodingUTF8; |
---|
154 | Boolean bool2 = CFStringGetCString(dest,buffer,1024,encoding); |
---|
155 | if(bool2){ |
---|
156 | printf("Loaded into local_buffer"); |
---|
157 | return buffer; |
---|
158 | } |
---|
159 | return NULL; |
---|
160 | } |
---|
161 | |
---|
162 | OSStatus setProxiesForProtcol(CURL* handle,const char *proto){ |
---|
163 | OSStatus err; |
---|
164 | CFDictionaryRef proxyDict; |
---|
165 | CFArrayRef proxies; |
---|
166 | |
---|
167 | CFStringRef key_enabled = NULL; |
---|
168 | CFStringRef key_host = NULL; |
---|
169 | CFStringRef key_port = NULL; |
---|
170 | |
---|
171 | bool proxy_enabled; |
---|
172 | char *proxy_host; |
---|
173 | long proxy_port; |
---|
174 | |
---|
175 | proxyDict = NULL; |
---|
176 | proxies = NULL; |
---|
177 | |
---|
178 | err = noErr; |
---|
179 | proxyDict = SCDynamicStoreCopyProxies(NULL); |
---|
180 | |
---|
181 | if(strncmp(proto,"http",4)==0){ |
---|
182 | key_enabled=kSCPropNetProxiesHTTPEnable; |
---|
183 | key_host=kSCPropNetProxiesHTTPProxy; |
---|
184 | key_port=kSCPropNetProxiesHTTPPort; |
---|
185 | } |
---|
186 | else |
---|
187 | if(strncmp(proto,"https",5)==0){ |
---|
188 | key_enabled=kSCPropNetProxiesHTTPSEnable; |
---|
189 | key_host=kSCPropNetProxiesHTTPSProxy; |
---|
190 | key_port=kSCPropNetProxiesHTTPSPort; |
---|
191 | } |
---|
192 | |
---|
193 | CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_enabled),kCFNumberIntType,&proxy_enabled); |
---|
194 | if(proxy_enabled){ |
---|
195 | CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_port),CFNumberGetType(CFDictionaryGetValue(proxyDict,key_port)),&proxy_port); |
---|
196 | char buffer[1024]; |
---|
197 | CFStringToCString(CFDictionaryGetValue(proxyDict,key_host),buffer); |
---|
198 | proxy_host=buffer; |
---|
199 | |
---|
200 | #ifdef MSG_LAF_VERBOSE |
---|
201 | printf("\n**[PROXY SETTINGS DETECTION %s (%d) %s:%li (%s)]**\n",proto,proxy_enabled,(char*)proxy_host,proxy_port,buffer); |
---|
202 | #endif |
---|
203 | |
---|
204 | if (proxyDict == NULL) { |
---|
205 | err = coreFoundationUnknownErr; |
---|
206 | } |
---|
207 | |
---|
208 | setProxy(handle,proxy_host,proxy_port); |
---|
209 | } |
---|
210 | return err; |
---|
211 | } |
---|
212 | #else |
---|
213 | /** |
---|
214 | * Should autodetect the proxy configuration (do nothing on linux) |
---|
215 | * |
---|
216 | * @param handle a CURL handle |
---|
217 | * @param proto the protocol requiring the use of a proxy |
---|
218 | */ |
---|
219 | int setProxiesForProtcol(CURL* handle,const char *proto){ |
---|
220 | #ifdef MSG_LAF_VERBOSE |
---|
221 | fprintf( stderr, "setProxiesForProtocol (do nothing) ...\n" ); |
---|
222 | #endif |
---|
223 | return 0; |
---|
224 | } |
---|
225 | #endif |
---|
226 | |
---|
227 | /** |
---|
228 | * Create a HINTERNET |
---|
229 | * |
---|
230 | * @param lpszAgent the HTPP User-Agent to use to send requests |
---|
231 | * @param dwAccessType type of access required |
---|
232 | * @param lpszProxyName the name of the proxy server(s) to use |
---|
233 | * @param lpszProxyBypass ip address or host names which should not be routed |
---|
234 | * through the proxy |
---|
235 | * @param dwFlags Options (INTERNET_FLAG_ASYNC,INTERNET_FLAG_FROM_CACHE,INTERNET_FLAG_OFFLINE) |
---|
236 | * @return the created HINTERNET |
---|
237 | */ |
---|
238 | HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){ |
---|
239 | HINTERNET ret; |
---|
240 | ret.handle=curl_multi_init(); |
---|
241 | ret.agent=zStrdup(lpszAgent); |
---|
242 | ret.nb=0; |
---|
243 | ret.waitingRequests[ret.nb] = NULL; |
---|
244 | ret.ihandle[ret.nb].header=NULL; |
---|
245 | ret.ihandle[ret.nb].handle=NULL; |
---|
246 | ret.ihandle[ret.nb].hasCacheFile=0; |
---|
247 | ret.ihandle[ret.nb].nDataAlloc = 0; |
---|
248 | ret.ihandle[ret.nb].url = NULL; |
---|
249 | ret.ihandle[ret.nb].mimeType = NULL; |
---|
250 | ret.ihandle[ret.nb].cookie = NULL; |
---|
251 | ret.ihandle[ret.nb].nDataLen = 0; |
---|
252 | ret.ihandle[ret.nb].nDataAlloc = 0; |
---|
253 | ret.ihandle[ret.nb].pabyData = NULL; |
---|
254 | ret.ihandle[ret.nb].post = NULL; |
---|
255 | return ret; |
---|
256 | } |
---|
257 | |
---|
258 | /** |
---|
259 | * Verify if the URL should use a shared cache or not. |
---|
260 | * |
---|
261 | * In case the security section contains a key named "shared", then if the |
---|
262 | * domain listed in the shared key are contained in the url given as parameter |
---|
263 | * then it return "SHARED" in other cases, it returns "OTHER". |
---|
264 | * |
---|
265 | * @param conf the main configuration file maps |
---|
266 | * @param url the URL to evaluate |
---|
267 | * @return a string "SHARED" in case the host is in a domain listed in the |
---|
268 | * shared key, "OTHER" in other cases. |
---|
269 | */ |
---|
270 | char* getProvenance(maps* conf,const char* url){ |
---|
271 | map* sharedCache=getMapFromMaps(conf,"security","shared"); |
---|
272 | char *res="OTHER"; |
---|
273 | char *paths[2]={ |
---|
274 | "mapserverAddress", |
---|
275 | "tmpUrl" |
---|
276 | }; |
---|
277 | if(sharedCache!=NULL){ |
---|
278 | char *hosts=sharedCache->value; |
---|
279 | char *curs=strtok(hosts,","); |
---|
280 | while(curs!=NULL){ |
---|
281 | if(strstr(url,curs)==NULL){ |
---|
282 | return "SHARED"; |
---|
283 | } |
---|
284 | curs=strtok(NULL,","); |
---|
285 | } |
---|
286 | } |
---|
287 | for(int i=0;i<2;i++){ |
---|
288 | sharedCache=getMapFromMaps(conf,"main",paths[i]); |
---|
289 | if(sharedCache!=NULL){ |
---|
290 | if(strstr(url,sharedCache->value)!=NULL){ |
---|
291 | return "LOCAL"; |
---|
292 | } |
---|
293 | } |
---|
294 | } |
---|
295 | return res; |
---|
296 | } |
---|
297 | |
---|
298 | /** |
---|
299 | * Add missing headers to an existing _HINTERNET |
---|
300 | * |
---|
301 | * |
---|
302 | * @param handle the _HINTERNET pointer |
---|
303 | * @param key the header parameter name |
---|
304 | * @param value the header parameter value |
---|
305 | * @return 0 if the operation succeeded, -1 in other case. |
---|
306 | */ |
---|
307 | int AddMissingHeaderEntry(_HINTERNET* handle,const char* key,const char* value){ |
---|
308 | size_t length=strlen(key)+strlen(value)+3; |
---|
309 | char *entry=(char*)malloc((length)*sizeof(char)); |
---|
310 | if(entry==NULL) |
---|
311 | return -1; |
---|
312 | snprintf (entry, length, "%s: %s", key, value); |
---|
313 | handle->header = curl_slist_append (handle->header, entry); |
---|
314 | free(entry); |
---|
315 | return 0; |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | * Verify if a host is protected (appear in [security] > hosts) |
---|
320 | * |
---|
321 | * @param protectedHosts string containing all the protected hosts (coma separated) |
---|
322 | * @param url string used to extract the host from |
---|
323 | * @return 1 if the host is listed as protected, 0 in other case |
---|
324 | */ |
---|
325 | int isProtectedHost(const char* protectedHosts, const char* url) { |
---|
326 | char *token, *saveptr; |
---|
327 | int cnt; |
---|
328 | |
---|
329 | // knut: make a copy of url since strtok family modifies first argument and cannot be used on constant strings |
---|
330 | char* urlcpy = (char*)malloc(sizeof(char)*(strlen(url) + 1)); |
---|
331 | urlcpy = strncpy(urlcpy, url, strlen(url) + 1); // since count > strlen(url), a null character is properly appended |
---|
332 | |
---|
333 | //token = strtok_r (url, "//", &saveptr); |
---|
334 | token = strtok_r(urlcpy, "//", &saveptr); // knut |
---|
335 | cnt = 0; |
---|
336 | while (token != NULL && cnt <= 1) { |
---|
337 | fprintf(stderr, "%s %d %s \n", __FILE__, __LINE__, token); |
---|
338 | if (cnt == 1) |
---|
339 | fprintf(stderr, "%s %d %s \n", __FILE__, __LINE__, strstr(protectedHosts, token)); |
---|
340 | fflush(stderr); |
---|
341 | if (cnt == 1 && strstr(protectedHosts, token) != NULL) { |
---|
342 | fprintf(stderr, "%s %d %s \n", __FILE__, __LINE__, strstr(protectedHosts, token)); |
---|
343 | free(urlcpy); |
---|
344 | return 1; |
---|
345 | } |
---|
346 | token = strtok_r(NULL, "/", &saveptr); |
---|
347 | cnt += 1; |
---|
348 | } |
---|
349 | free(urlcpy); |
---|
350 | return 0; |
---|
351 | } |
---|
352 | |
---|
353 | /** |
---|
354 | * Add headers defined in [security] > attributes to an existing HINTERNET |
---|
355 | * @see isProtectedHost, AddMissingHeaderEntry |
---|
356 | * |
---|
357 | * @param handle the _HINTERNET pointer |
---|
358 | * @param conf the header parameter name |
---|
359 | * @param value the header parameter value |
---|
360 | * @return 0 if the operation succeeded, -1 in other case. |
---|
361 | */ |
---|
362 | void AddHeaderEntries(HINTERNET* handle,maps* conf){ |
---|
363 | map* passThrough=getMapFromMaps(conf,"security","attributes"); |
---|
364 | map* targetHosts=getMapFromMaps(conf,"security","hosts"); |
---|
365 | char* passedHeader[10]; |
---|
366 | int cnt=0; |
---|
367 | if(passThrough!=NULL && targetHosts!=NULL){ |
---|
368 | char *tmp=zStrdup(passThrough->value); |
---|
369 | int i; |
---|
370 | for(i=0;i<handle->nb;i++){ |
---|
371 | if(strstr(targetHosts->value,"*")!=NULL || isProtectedHost(targetHosts->value,handle->ihandle[i].url)==1){ |
---|
372 | char *token, *saveptr; |
---|
373 | token = strtok_r (tmp, ",", &saveptr); |
---|
374 | while (token != NULL){ |
---|
375 | size_t length=strlen(token)+6; |
---|
376 | char* tmp1=(char*)malloc(length*sizeof(char)); |
---|
377 | map* tmpMap; |
---|
378 | snprintf(tmp1,6,"HTTP_"); |
---|
379 | int j; |
---|
380 | for(j=0;token[j]!='\0';j++){ |
---|
381 | if(token[j]!='-') |
---|
382 | tmp1[5+j]=toupper(token[j]); |
---|
383 | else |
---|
384 | tmp1[5+j]='_'; |
---|
385 | tmp1[5+j+1]='\0'; |
---|
386 | } |
---|
387 | tmpMap = getMapFromMaps(conf,"renv",tmp1); |
---|
388 | if(tmpMap!=NULL){ |
---|
389 | AddMissingHeaderEntry(&handle->ihandle[i],token,tmpMap->value); |
---|
390 | } |
---|
391 | if(handle->ihandle[i].header!=NULL) |
---|
392 | curl_easy_setopt(handle->ihandle[i].handle,CURLOPT_HTTPHEADER,handle->ihandle[i].header); |
---|
393 | free(tmp1); |
---|
394 | cnt+=1; |
---|
395 | token = strtok_r (NULL, ",", &saveptr); |
---|
396 | } |
---|
397 | } |
---|
398 | } |
---|
399 | free(tmp); |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | /** |
---|
404 | * Close a HINTERNET connection and free allocated resources |
---|
405 | * |
---|
406 | * @param handle0 the HINTERNET connection to close |
---|
407 | */ |
---|
408 | void InternetCloseHandle(HINTERNET* handle0){ |
---|
409 | int i=0; |
---|
410 | if(handle0!=NULL){ |
---|
411 | for(i=0;i<handle0->nb;i++){ |
---|
412 | _HINTERNET handle=handle0->ihandle[i]; |
---|
413 | if(handle.hasCacheFile>0){ |
---|
414 | fclose(handle.file); |
---|
415 | zUnlink(handle.filename); |
---|
416 | free(handle.filename); |
---|
417 | } |
---|
418 | else{ |
---|
419 | handle.pabyData = NULL; |
---|
420 | handle.nDataAlloc = handle.nDataLen = 0; |
---|
421 | } |
---|
422 | if(handle.header!=NULL){ |
---|
423 | curl_slist_free_all(handle.header); |
---|
424 | handle.header=NULL; |
---|
425 | } |
---|
426 | if(handle.post!=NULL){ |
---|
427 | free(handle.post); |
---|
428 | handle.post=NULL; |
---|
429 | } |
---|
430 | if(handle.url!=NULL){ |
---|
431 | free(handle.url); |
---|
432 | handle.url=NULL; |
---|
433 | } |
---|
434 | if(handle.mimeType!=NULL){ |
---|
435 | free(handle.mimeType); |
---|
436 | handle.mimeType=NULL; |
---|
437 | } |
---|
438 | if(handle.cookie!=NULL){ |
---|
439 | free(handle.cookie); |
---|
440 | handle.cookie=NULL; |
---|
441 | } |
---|
442 | if(handle0->waitingRequests[i]!=NULL){ |
---|
443 | free(handle0->waitingRequests[i]); |
---|
444 | handle0->waitingRequests[i]=NULL; |
---|
445 | } |
---|
446 | } |
---|
447 | if(handle0->handle) |
---|
448 | curl_multi_cleanup(handle0->handle); |
---|
449 | if(handle0->agent!=NULL){ |
---|
450 | free(handle0->agent); |
---|
451 | handle0->agent=NULL; |
---|
452 | } |
---|
453 | } |
---|
454 | } |
---|
455 | |
---|
456 | /** |
---|
457 | * Create a new element in the download queue |
---|
458 | * |
---|
459 | * @param hInternet the HINTERNET connection to add the download link |
---|
460 | * @param lpszUrl the url to download |
---|
461 | * @param lpszHeaders the additional headers to be sent to the HTTP server |
---|
462 | * @param dwHeadersLength the size of the additional headers |
---|
463 | * @param dwFlags desired download mode (INTERNET_FLAG_NO_CACHE_WRITE for not using cache file) |
---|
464 | * @param dwContext not used |
---|
465 | * @param conf the main configuration file maps pointer |
---|
466 | * @return the updated HINTERNET |
---|
467 | */ |
---|
468 | HINTERNET InternetOpenUrl(HINTERNET* hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext,const maps* conf){ |
---|
469 | |
---|
470 | char filename[255]; |
---|
471 | int ldwFlags=INTERNET_FLAG_NEED_FILE; |
---|
472 | struct MemoryStruct header; |
---|
473 | map* memUse=getMapFromMaps((maps*) conf,"main","memory"); // knut: addad cast to maps* |
---|
474 | |
---|
475 | hInternet->ihandle[hInternet->nb].handle=curl_easy_init( ); |
---|
476 | hInternet->ihandle[hInternet->nb].hasCacheFile=0; |
---|
477 | hInternet->ihandle[hInternet->nb].nDataAlloc = 0; |
---|
478 | hInternet->ihandle[hInternet->nb].url = NULL; |
---|
479 | hInternet->ihandle[hInternet->nb].mimeType = NULL; |
---|
480 | hInternet->ihandle[hInternet->nb].cookie = NULL; |
---|
481 | hInternet->ihandle[hInternet->nb].nDataLen = 0; |
---|
482 | hInternet->ihandle[hInternet->nb].id = hInternet->nb; |
---|
483 | hInternet->ihandle[hInternet->nb].nDataAlloc = 0; |
---|
484 | hInternet->ihandle[hInternet->nb].code = -1; |
---|
485 | hInternet->ihandle[hInternet->nb].pabyData = NULL; |
---|
486 | hInternet->ihandle[hInternet->nb].post = NULL; |
---|
487 | |
---|
488 | map* caBundle=getMapFromMaps((maps*) conf,"curl","cainfo"); |
---|
489 | if(caBundle!=NULL) |
---|
490 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_CAINFO,caBundle->value); |
---|
491 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIEFILE, "ALL"); |
---|
492 | #ifndef TIGER |
---|
493 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIELIST, "ALL"); |
---|
494 | #endif |
---|
495 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_USERAGENT, hInternet->agent); |
---|
496 | |
---|
497 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_FOLLOWLOCATION,1); |
---|
498 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_MAXREDIRS,3); |
---|
499 | |
---|
500 | header.memory=NULL; |
---|
501 | header.size = 0; |
---|
502 | |
---|
503 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_HEADERFUNCTION, header_write_data); |
---|
504 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEHEADER, (void *)&header); |
---|
505 | |
---|
506 | #ifdef MSG_LAF_VERBOSE |
---|
507 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_VERBOSE, 1); |
---|
508 | #endif |
---|
509 | |
---|
510 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0) |
---|
511 | ldwFlags=INTERNET_FLAG_NO_CACHE_WRITE; |
---|
512 | |
---|
513 | switch(ldwFlags) |
---|
514 | { |
---|
515 | case INTERNET_FLAG_NO_CACHE_WRITE: |
---|
516 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into); |
---|
517 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]); |
---|
518 | hInternet->ihandle[hInternet->nb].hasCacheFile=-1; |
---|
519 | break; |
---|
520 | default: |
---|
521 | memset(filename,0,255); |
---|
522 | char* tmpUuid=get_uuid(); |
---|
523 | map* tmpPath=NULL; |
---|
524 | if(conf!=NULL){ |
---|
525 | tmpPath=getMapFromMaps((maps*) conf,"main","tmpPath"); // knut added cast to maps* |
---|
526 | } |
---|
527 | if(tmpPath==NULL) |
---|
528 | sprintf(filename,"/tmp/ZOO_Cache%s", tmpUuid); |
---|
529 | else |
---|
530 | sprintf(filename,"%s/ZOO_Cache%s", tmpPath->value,tmpUuid); |
---|
531 | free(tmpUuid); |
---|
532 | hInternet->ihandle[hInternet->nb].filename=zStrdup(filename); |
---|
533 | hInternet->ihandle[hInternet->nb].file=fopen(hInternet->ihandle[hInternet->nb].filename,"w+"); |
---|
534 | hInternet->ihandle[hInternet->nb].hasCacheFile=1; |
---|
535 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into_file); |
---|
536 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]); |
---|
537 | break; |
---|
538 | } |
---|
539 | #ifdef ULINET_DEBUG |
---|
540 | fprintf(stderr,"URL (%s)\nBODY (%s)\n",lpszUrl,lpszHeaders); |
---|
541 | #endif |
---|
542 | if(lpszHeaders!=NULL && strlen(lpszHeaders)>0){ |
---|
543 | #ifdef MSG_LAF_VERBOSE |
---|
544 | fprintf(stderr,"FROM ULINET !!"); |
---|
545 | fprintf(stderr,"HEADER : [%s] %d\n",lpszHeaders,dwHeadersLength); |
---|
546 | #endif |
---|
547 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POST,1); |
---|
548 | #ifdef ULINET_DEBUG |
---|
549 | fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength); |
---|
550 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_VERBOSE,1); |
---|
551 | #endif |
---|
552 | hInternet->ihandle[hInternet->nb].post=zStrdup(lpszHeaders); |
---|
553 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDS,hInternet->ihandle[hInternet->nb].post); |
---|
554 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDSIZE,(long)dwHeadersLength); |
---|
555 | } |
---|
556 | if(hInternet->ihandle[hInternet->nb].header!=NULL) |
---|
557 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_HTTPHEADER,hInternet->ihandle[hInternet->nb].header); |
---|
558 | |
---|
559 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_URL,lpszUrl); |
---|
560 | hInternet->ihandle[hInternet->nb].url = zStrdup(lpszUrl); |
---|
561 | |
---|
562 | curl_multi_add_handle(hInternet->handle,hInternet->ihandle[hInternet->nb].handle); |
---|
563 | |
---|
564 | hInternet->ihandle[hInternet->nb].header=NULL; |
---|
565 | ++hInternet->nb; |
---|
566 | hInternet->ihandle[hInternet->nb].header=NULL; |
---|
567 | |
---|
568 | #ifdef ULINET_DEBUG |
---|
569 | fprintf(stderr,"DEBUG MIMETYPE: %s\n",hInternet.mimeType); |
---|
570 | fflush(stderr); |
---|
571 | #endif |
---|
572 | return *hInternet; |
---|
573 | }; |
---|
574 | |
---|
575 | /** |
---|
576 | * Download all opened urls in the queue |
---|
577 | * |
---|
578 | * @param hInternet the HINTERNET structure containing the queue |
---|
579 | * @return 0 |
---|
580 | */ |
---|
581 | int processDownloads(HINTERNET* hInternet){ |
---|
582 | int still_running=0,numfds; |
---|
583 | int msgs_left=0; |
---|
584 | int i=0; |
---|
585 | do{ |
---|
586 | CURLMcode mc; |
---|
587 | mc = curl_multi_perform(hInternet->handle, &still_running); |
---|
588 | if(mc==CURLM_OK){ |
---|
589 | #if LIBCURL_VERSION_MINOR >= 28 |
---|
590 | mc = curl_multi_wait(hInternet->handle, NULL, 0, 1000, &numfds); |
---|
591 | #else |
---|
592 | struct timeval timeout; |
---|
593 | fd_set fdread; |
---|
594 | fd_set fdwrite; |
---|
595 | fd_set fdexcep; |
---|
596 | int maxfd = -1; |
---|
597 | |
---|
598 | long curl_timeo = -1; |
---|
599 | |
---|
600 | FD_ZERO(&fdread); |
---|
601 | FD_ZERO(&fdwrite); |
---|
602 | FD_ZERO(&fdexcep); |
---|
603 | |
---|
604 | /* set a suitable timeout to play around with */ |
---|
605 | timeout.tv_sec = 1; |
---|
606 | timeout.tv_usec = 0; |
---|
607 | |
---|
608 | curl_multi_timeout(hInternet->handle, &curl_timeo); |
---|
609 | if(curl_timeo >= 0) { |
---|
610 | timeout.tv_sec = curl_timeo / 1000; |
---|
611 | if(timeout.tv_sec > 1) |
---|
612 | timeout.tv_sec = 1; |
---|
613 | else |
---|
614 | timeout.tv_usec = (curl_timeo % 1000) * 1000; |
---|
615 | } |
---|
616 | |
---|
617 | /* get file descriptors from the transfers */ |
---|
618 | mc = curl_multi_fdset(hInternet->handle, &fdread, &fdwrite, &fdexcep, &maxfd); |
---|
619 | #endif |
---|
620 | } |
---|
621 | if(mc != CURLM_OK) { |
---|
622 | fprintf(stderr, "curl_multi failed, code %d.n", mc); |
---|
623 | break; |
---|
624 | } |
---|
625 | }while(still_running); |
---|
626 | for(i=0;i<hInternet->nb;i++){ |
---|
627 | char *tmp; |
---|
628 | curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_CONTENT_TYPE,&tmp); |
---|
629 | if(tmp!=NULL) |
---|
630 | hInternet->ihandle[i].mimeType=zStrdup(tmp); |
---|
631 | curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_RESPONSE_CODE,&(hInternet->ihandle[i].code)); |
---|
632 | curl_multi_remove_handle(hInternet->handle, hInternet->ihandle[i].handle); |
---|
633 | curl_easy_cleanup(hInternet->ihandle[i].handle); |
---|
634 | } |
---|
635 | return 0; |
---|
636 | } |
---|
637 | |
---|
638 | /** |
---|
639 | * Initialize the cookie for a specific index (hInternet.nb) |
---|
640 | * |
---|
641 | * @param hInternet the HINTERNET structure to know the cookie index to reset |
---|
642 | * @return 1 |
---|
643 | * @see HINTERNET |
---|
644 | */ |
---|
645 | int freeCookieList(HINTERNET hInternet){ |
---|
646 | hInternet.ihandle[hInternet.nb].cookie=zStrdup(""); |
---|
647 | #ifndef TIGER |
---|
648 | curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_COOKIELIST, "ALL"); |
---|
649 | #endif |
---|
650 | return 1; |
---|
651 | } |
---|
652 | |
---|
653 | /** |
---|
654 | * Copy a downloaded content |
---|
655 | * |
---|
656 | * @param hInternet the _HINTERNET structure |
---|
657 | * @param lpBuffer the memory space to copy the downloaded content |
---|
658 | * @param dwNumberOfBytesToRead the size of lpBuffer |
---|
659 | * @param lpdwNumberOfBytesRead number of bytes red |
---|
660 | * @return 1 on success, 0 if failure |
---|
661 | */ |
---|
662 | int InternetReadFile(_HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){ |
---|
663 | size_t dwDataSize; |
---|
664 | |
---|
665 | if(hInternet.hasCacheFile>0){ |
---|
666 | fseek (hInternet.file , 0 , SEEK_END); |
---|
667 | dwDataSize=ftell(hInternet.file); //taille du ficher |
---|
668 | //dwDataSize=hInternet.nDataLen; |
---|
669 | //rewind (hInternet.file); |
---|
670 | fseek(hInternet.file, 0, SEEK_SET); |
---|
671 | } |
---|
672 | else{ |
---|
673 | memset(lpBuffer,0,hInternet.nDataLen+1); |
---|
674 | memcpy(lpBuffer, hInternet.pabyData, hInternet.nDataLen ); |
---|
675 | dwDataSize=hInternet.nDataLen; |
---|
676 | free( hInternet.pabyData ); |
---|
677 | hInternet.pabyData=NULL; |
---|
678 | } |
---|
679 | |
---|
680 | if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize ){ |
---|
681 | return 0; |
---|
682 | } |
---|
683 | |
---|
684 | #ifdef MSG_LAF_VERBOSE |
---|
685 | fprintf(stderr,"\nfile size : %dko\n",dwDataSize/1024); |
---|
686 | #endif |
---|
687 | |
---|
688 | if(hInternet.hasCacheFile>0){ |
---|
689 | size_t freadRes = fread(lpBuffer,dwDataSize+1,1,hInternet.file); |
---|
690 | *lpdwNumberOfBytesRead = hInternet.nDataLen; |
---|
691 | } |
---|
692 | else{ |
---|
693 | *lpdwNumberOfBytesRead = hInternet.nDataLen; |
---|
694 | free( hInternet.pabyData ); |
---|
695 | hInternet.pabyData = NULL; |
---|
696 | hInternet.nDataAlloc = hInternet.nDataLen = 0; |
---|
697 | } |
---|
698 | |
---|
699 | if( *lpdwNumberOfBytesRead < dwDataSize ) |
---|
700 | return 0; |
---|
701 | else |
---|
702 | return 1; // TRUE |
---|
703 | } |
---|
704 | |
---|
705 | |
---|
706 | /** |
---|
707 | * Use basic authentication for accessing a resource |
---|
708 | * |
---|
709 | * @param hInternet the _HINTERNET structure |
---|
710 | * @param login the login to use to authenticate |
---|
711 | * @param passwd the password to use to authenticate |
---|
712 | */ |
---|
713 | int setBasicAuth(HINTERNET hInternet,char* login,char* passwd){ |
---|
714 | char *tmp; |
---|
715 | tmp=(char*)malloc((strlen(login)+strlen(passwd)+2)*sizeof(char)); |
---|
716 | sprintf(tmp,"%s:%s",login,passwd); |
---|
717 | if(curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle,CURLOPT_USERPWD,tmp)==CURLE_OUT_OF_MEMORY){ |
---|
718 | free(tmp); |
---|
719 | return -1; |
---|
720 | } |
---|
721 | curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_HTTPAUTH,CURLAUTH_ANY); |
---|
722 | free(tmp); |
---|
723 | return 0; |
---|
724 | } |
---|