1 | /* |
---|
2 | * ulinet.c |
---|
3 | * |
---|
4 | * Author : Gérald FENOY |
---|
5 | * |
---|
6 | * Copyright (c) 2008-2015 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 <assert.h> |
---|
32 | #include <ctype.h> |
---|
33 | |
---|
34 | /** |
---|
35 | * Write the downloaded content to a _HINTERNET structure |
---|
36 | * |
---|
37 | * @param buffer the buffer to read |
---|
38 | * @param size size of each member |
---|
39 | * @param nmemb number of element to read |
---|
40 | * @param data the _HINTERNET structure to write in |
---|
41 | * @return the size red, -1 if buffer is NULL |
---|
42 | */ |
---|
43 | size_t write_data_into(void *buffer, size_t size, size_t nmemb, void *data){ |
---|
44 | size_t realsize = size * nmemb; |
---|
45 | _HINTERNET *psInternet; |
---|
46 | if(buffer==NULL){ |
---|
47 | buffer=NULL; |
---|
48 | return -1; |
---|
49 | } |
---|
50 | psInternet=(_HINTERNET *)data; |
---|
51 | if(psInternet->pabyData){ |
---|
52 | psInternet->pabyData=(unsigned char*)realloc(psInternet->pabyData,psInternet->nDataLen+realsize+1); |
---|
53 | psInternet->nDataAlloc+=psInternet->nDataLen+realsize+1; |
---|
54 | } |
---|
55 | else{ |
---|
56 | psInternet->pabyData=(unsigned char*)malloc(psInternet->nDataLen+realsize+1); |
---|
57 | psInternet->nDataAlloc=realsize+1; |
---|
58 | } |
---|
59 | |
---|
60 | if (psInternet->pabyData) { |
---|
61 | memcpy( psInternet->pabyData + psInternet->nDataLen, buffer, realsize); |
---|
62 | psInternet->nDataLen += realsize; |
---|
63 | psInternet->pabyData[psInternet->nDataLen] = 0; |
---|
64 | } |
---|
65 | |
---|
66 | buffer=NULL; |
---|
67 | return realsize; |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * In case of presence of "Set-Cookie" in the headers red, store the cookie |
---|
72 | * identifier in CCookie |
---|
73 | * |
---|
74 | * @param buffer the buffer to read |
---|
75 | * @param size size of each member |
---|
76 | * @param nmemb number of element to read |
---|
77 | * @param data the _HINTERNET structure to write in |
---|
78 | * @return the size red, -1 if buffer is NULL |
---|
79 | * @see CCookie |
---|
80 | */ |
---|
81 | size_t header_write_data(void *buffer, size_t size, size_t nmemb, void *data){ |
---|
82 | if(strncmp("Set-Cookie: ",(char*)buffer,12)==0){ |
---|
83 | int i; |
---|
84 | char* tmp; |
---|
85 | for(i=0;i<12;i++) |
---|
86 | #ifndef WIN32 |
---|
87 | buffer++; |
---|
88 | #else |
---|
89 | ; |
---|
90 | #endif |
---|
91 | _HINTERNET *psInternet=(_HINTERNET *)data; |
---|
92 | tmp=strtok(buffer,";"); |
---|
93 | if(tmp!=NULL){ |
---|
94 | sprintf(CCookie[psInternet->id],"%s",tmp); |
---|
95 | } |
---|
96 | } |
---|
97 | return size * nmemb;//write_data_into(buffer,size,nmemb,data,HEADER); |
---|
98 | }; |
---|
99 | |
---|
100 | /** |
---|
101 | * Define the proxy to use for a CURL handler |
---|
102 | * |
---|
103 | * @param handle the CURL handler |
---|
104 | * @param host the proxy host (including http://) |
---|
105 | * @param port the proxy port |
---|
106 | */ |
---|
107 | void setProxy(CURL* handle,char* host,long port){ |
---|
108 | char* proxyDef=(char*)malloc((strlen(host)+10+2)*sizeof(char)); |
---|
109 | sprintf(proxyDef,"%s:%ld",host,port); |
---|
110 | curl_easy_setopt(handle,CURLOPT_PROXY,proxyDef); |
---|
111 | free(proxyDef); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * MACOSX |
---|
116 | */ |
---|
117 | #if defined(macintosh) || (defined(__MACH__) && defined(__APPLE__)) |
---|
118 | |
---|
119 | |
---|
120 | char* CFStringToCString(CFStringRef dest,char *buffer){ |
---|
121 | CFStringEncoding encoding = kCFStringEncodingUTF8; |
---|
122 | Boolean bool2 = CFStringGetCString(dest,buffer,1024,encoding); |
---|
123 | if(bool2){ |
---|
124 | printf("Loaded into local_buffer"); |
---|
125 | return buffer; |
---|
126 | } |
---|
127 | return NULL; |
---|
128 | } |
---|
129 | |
---|
130 | OSStatus setProxiesForProtcol(CURL* handle,const char *proto){ |
---|
131 | OSStatus err; |
---|
132 | CFDictionaryRef proxyDict; |
---|
133 | CFArrayRef proxies; |
---|
134 | |
---|
135 | CFStringRef key_enabled = NULL; |
---|
136 | CFStringRef key_host = NULL; |
---|
137 | CFStringRef key_port = NULL; |
---|
138 | |
---|
139 | bool proxy_enabled; |
---|
140 | char *proxy_host; |
---|
141 | long proxy_port; |
---|
142 | |
---|
143 | proxyDict = NULL; |
---|
144 | proxies = NULL; |
---|
145 | |
---|
146 | err = noErr; |
---|
147 | proxyDict = SCDynamicStoreCopyProxies(NULL); |
---|
148 | |
---|
149 | if(strncmp(proto,"http",4)==0){ |
---|
150 | key_enabled=kSCPropNetProxiesHTTPEnable; |
---|
151 | key_host=kSCPropNetProxiesHTTPProxy; |
---|
152 | key_port=kSCPropNetProxiesHTTPPort; |
---|
153 | } |
---|
154 | else |
---|
155 | if(strncmp(proto,"https",5)==0){ |
---|
156 | key_enabled=kSCPropNetProxiesHTTPSEnable; |
---|
157 | key_host=kSCPropNetProxiesHTTPSProxy; |
---|
158 | key_port=kSCPropNetProxiesHTTPSPort; |
---|
159 | } |
---|
160 | |
---|
161 | CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_enabled),kCFNumberIntType,&proxy_enabled); |
---|
162 | if(proxy_enabled){ |
---|
163 | CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_port),CFNumberGetType(CFDictionaryGetValue(proxyDict,key_port)),&proxy_port); |
---|
164 | char buffer[1024]; |
---|
165 | CFStringToCString(CFDictionaryGetValue(proxyDict,key_host),buffer); |
---|
166 | proxy_host=buffer; |
---|
167 | |
---|
168 | #ifdef MSG_LAF_VERBOSE |
---|
169 | printf("\n**[PROXY SETTINGS DETECTION %s (%d) %s:%li (%s)]**\n",proto,proxy_enabled,(char*)proxy_host,proxy_port,buffer); |
---|
170 | #endif |
---|
171 | |
---|
172 | if (proxyDict == NULL) { |
---|
173 | err = coreFoundationUnknownErr; |
---|
174 | } |
---|
175 | |
---|
176 | setProxy(handle,proxy_host,proxy_port); |
---|
177 | } |
---|
178 | return err; |
---|
179 | } |
---|
180 | #else |
---|
181 | /** |
---|
182 | * Should autodetect the proxy configuration (do nothing on linux) |
---|
183 | * |
---|
184 | * @param handle a CURL handle |
---|
185 | * @param proto the protocol requiring the use of a proxy |
---|
186 | */ |
---|
187 | int setProxiesForProtcol(CURL* handle,const char *proto){ |
---|
188 | #ifdef MSG_LAF_VERBOSE |
---|
189 | fprintf( stderr, "setProxiesForProtocol (do nothing) ...\n" ); |
---|
190 | #endif |
---|
191 | return 0; |
---|
192 | } |
---|
193 | #endif |
---|
194 | |
---|
195 | /** |
---|
196 | * Create a HINTERNET |
---|
197 | * |
---|
198 | * @param lpszAgent the HTPP User-Agent to use to send requests |
---|
199 | * @param dwAccessType type of access required |
---|
200 | * @param lpszProxyName the name of the proxy server(s) to use |
---|
201 | * @param lpszProxyBypass ip address or host names which should not be routed |
---|
202 | * through the proxy |
---|
203 | * @param dwFlags Options (INTERNET_FLAG_ASYNC,INTERNET_FLAG_FROM_CACHE,INTERNET_FLAG_OFFLINE) |
---|
204 | * @return the created HINTERNET |
---|
205 | */ |
---|
206 | HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){ |
---|
207 | HINTERNET ret; |
---|
208 | ret.handle=curl_multi_init(); |
---|
209 | ret.agent=strdup(lpszAgent); |
---|
210 | ret.nb=0; |
---|
211 | ret.ihandle[ret.nb].header=NULL; |
---|
212 | return ret; |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * Add missing headers to an existing _HINTERNET |
---|
217 | * |
---|
218 | * |
---|
219 | * @param handle the _HINTERNET pointer |
---|
220 | * @param key the header parameter name |
---|
221 | * @param value the header parameter value |
---|
222 | * @return 0 if the operation succeeded, -1 in other case. |
---|
223 | */ |
---|
224 | int AddMissingHeaderEntry(_HINTERNET* handle,const char* key,const char* value){ |
---|
225 | int length=strlen(key)+strlen(value)+3; |
---|
226 | char *entry=(char*)malloc((length)*sizeof(char)); |
---|
227 | if(entry==NULL) |
---|
228 | return -1; |
---|
229 | snprintf (entry, length, "%s: %s", key, value); |
---|
230 | handle->header = curl_slist_append (handle->header, entry); |
---|
231 | free(entry); |
---|
232 | return 0; |
---|
233 | } |
---|
234 | |
---|
235 | /** |
---|
236 | * Verify if a host is protected (appear in [security] > hosts) |
---|
237 | * |
---|
238 | * @param protectedHosts string containing all the protected hosts (coma separated) |
---|
239 | * @param url string used to extract the host from |
---|
240 | * @return 1 if the host is listed as protected, 0 in other case |
---|
241 | */ |
---|
242 | int isProtectedHost(const char* protectedHosts,const char* url){ |
---|
243 | char *token, *saveptr; |
---|
244 | token = strtok_r (url, "//", &saveptr); |
---|
245 | int cnt=0; |
---|
246 | char* host; |
---|
247 | while(token!=NULL && cnt<=1){ |
---|
248 | fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,token); |
---|
249 | if(cnt==1) |
---|
250 | fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,strstr(protectedHosts,token)); |
---|
251 | fflush(stderr); |
---|
252 | if(cnt==1 && strstr(protectedHosts,token)!=NULL){ |
---|
253 | fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,strstr(protectedHosts,token)); |
---|
254 | return 1; |
---|
255 | } |
---|
256 | token = strtok_r (NULL, "/", &saveptr); |
---|
257 | cnt+=1; |
---|
258 | } |
---|
259 | return 0; |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | * Add headers defined in [security] > attributes to an existing HINTERNET |
---|
264 | * @see isProtectedHost, AddMissingHeaderEntry |
---|
265 | * |
---|
266 | * @param handle the _HINTERNET pointer |
---|
267 | * @param conf the header parameter name |
---|
268 | * @param value the header parameter value |
---|
269 | * @return 0 if the operation succeeded, -1 in other case. |
---|
270 | */ |
---|
271 | void AddHeaderEntries(HINTERNET* handle,maps* conf){ |
---|
272 | map* passThrough=getMapFromMaps(conf,"security","attributes"); |
---|
273 | map* targetHosts=getMapFromMaps(conf,"security","hosts"); |
---|
274 | char* passedHeader[10]; |
---|
275 | int cnt=0; |
---|
276 | if(passThrough!=NULL && targetHosts!=NULL){ |
---|
277 | char *tmp=zStrdup(passThrough->value); |
---|
278 | char *token, *saveptr; |
---|
279 | token = strtok_r (tmp, ",", &saveptr); |
---|
280 | int i; |
---|
281 | for(i=0;i<handle->nb;i++){ |
---|
282 | if(targetHosts->value[0]=='*' || isProtectedHost(targetHosts->value,handle->ihandle[i].url)==1){ |
---|
283 | while (token != NULL){ |
---|
284 | int length=strlen(token)+6; |
---|
285 | char* tmp1=(char*)malloc(length*sizeof(char)); |
---|
286 | snprintf(tmp1,6,"HTTP_"); |
---|
287 | int j; |
---|
288 | for(j=0;token[j]!='\0';j++){ |
---|
289 | if(token[j]!='-') |
---|
290 | tmp1[5+j]=toupper(token[i]); |
---|
291 | else |
---|
292 | tmp1[5+j]='_'; |
---|
293 | tmp1[5+j+1]='\0'; |
---|
294 | } |
---|
295 | fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,tmp1); |
---|
296 | map* tmpMap = getMapFromMaps(conf,"renv",tmp1); |
---|
297 | if(tmpMap!=NULL) |
---|
298 | AddMissingHeaderEntry(&handle->ihandle[i],token,tmpMap->value); |
---|
299 | free(tmp1); |
---|
300 | if(handle->ihandle[i].header!=NULL) |
---|
301 | curl_easy_setopt(handle->ihandle[i].handle,CURLOPT_HTTPHEADER,handle->ihandle[i].header); |
---|
302 | cnt+=1; |
---|
303 | token = strtok_r (NULL, ",", &saveptr); |
---|
304 | } |
---|
305 | } |
---|
306 | } |
---|
307 | free(tmp); |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | * Close a HINTERNET connection and free allocated resources |
---|
313 | * |
---|
314 | * @param handle0 the HINTERNET connection to close |
---|
315 | */ |
---|
316 | void InternetCloseHandle(HINTERNET* handle0){ |
---|
317 | int i=0; |
---|
318 | for(i=0;i<handle0->nb;i++){ |
---|
319 | _HINTERNET handle=handle0->ihandle[i]; |
---|
320 | if(handle.hasCacheFile>0){ |
---|
321 | fclose(handle.file); |
---|
322 | unlink(handle.filename); |
---|
323 | } |
---|
324 | else{ |
---|
325 | handle.pabyData = NULL; |
---|
326 | handle.nDataAlloc = handle.nDataLen = 0; |
---|
327 | } |
---|
328 | if(handle0->ihandle[i].header!=NULL){ |
---|
329 | curl_slist_free_all(handle0->ihandle[i].header); |
---|
330 | handle0->ihandle[i].header=NULL; |
---|
331 | } |
---|
332 | if(handle.post!=NULL) |
---|
333 | free(handle.post); |
---|
334 | if(handle.url!=NULL) |
---|
335 | free(handle.url); |
---|
336 | free(handle.mimeType); |
---|
337 | handle.mimeType = NULL; |
---|
338 | } |
---|
339 | if(handle0->handle) |
---|
340 | curl_multi_cleanup(handle0->handle); |
---|
341 | free(handle0->agent); |
---|
342 | for(i=handle0->nb-1;i>=0;i--){ |
---|
343 | free(handle0->waitingRequests[i]); |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | * Create a new element in the download queue |
---|
349 | * |
---|
350 | * @param hInternet the HINTERNET connection to add the download link |
---|
351 | * @param lpszUrl the url to download |
---|
352 | * @param lpszHeaders the additional headers to be sent to the HTTP server |
---|
353 | * @param dwHeadersLength the size of the additional headers |
---|
354 | * @param dwFlags desired download mode (INTERNET_FLAG_NO_CACHE_WRITE for not using cache file) |
---|
355 | * @param dwContext not used |
---|
356 | */ |
---|
357 | HINTERNET InternetOpenUrl(HINTERNET* hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){ |
---|
358 | |
---|
359 | char filename[255]; |
---|
360 | struct MemoryStruct header; |
---|
361 | |
---|
362 | hInternet->ihandle[hInternet->nb].handle=curl_easy_init( ); |
---|
363 | hInternet->ihandle[hInternet->nb].hasCacheFile=0; |
---|
364 | hInternet->ihandle[hInternet->nb].nDataAlloc = 0; |
---|
365 | hInternet->ihandle[hInternet->nb].url = NULL; |
---|
366 | hInternet->ihandle[hInternet->nb].mimeType = NULL; |
---|
367 | hInternet->ihandle[hInternet->nb].nDataLen = 0; |
---|
368 | hInternet->ihandle[hInternet->nb].id = hInternet->nb; |
---|
369 | hInternet->ihandle[hInternet->nb].nDataAlloc = 0; |
---|
370 | hInternet->ihandle[hInternet->nb].pabyData = NULL; |
---|
371 | hInternet->ihandle[hInternet->nb].post = NULL; |
---|
372 | |
---|
373 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIEFILE, "ALL"); |
---|
374 | #ifndef TIGER |
---|
375 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIELIST, "ALL"); |
---|
376 | #endif |
---|
377 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_USERAGENT, hInternet->agent); |
---|
378 | |
---|
379 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_FOLLOWLOCATION,1); |
---|
380 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_MAXREDIRS,3); |
---|
381 | |
---|
382 | header.memory=NULL; |
---|
383 | header.size = 0; |
---|
384 | |
---|
385 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_HEADERFUNCTION, header_write_data); |
---|
386 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEHEADER, (void *)&header); |
---|
387 | |
---|
388 | #ifdef MSG_LAF_VERBOSE |
---|
389 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_VERBOSE, 1); |
---|
390 | #endif |
---|
391 | |
---|
392 | |
---|
393 | switch(dwFlags) |
---|
394 | { |
---|
395 | case INTERNET_FLAG_NO_CACHE_WRITE: |
---|
396 | hInternet->ihandle[hInternet->nb].hasCacheFile=-1; |
---|
397 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into); |
---|
398 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]); |
---|
399 | break; |
---|
400 | default: |
---|
401 | sprintf(hInternet->ihandle[hInternet->nb].filename,"/tmp/ZOO_Cache%d",(int)time(NULL)); |
---|
402 | hInternet->ihandle[hInternet->nb].filename[24]=0; |
---|
403 | #ifdef MSG_LAF_VERBOSE |
---|
404 | fprintf(stderr,"file=%s",hInternet->ihandle[hInternet->nb].filename); |
---|
405 | #endif |
---|
406 | hInternet->ihandle[hInternet->nb].filename=filename; |
---|
407 | hInternet->ihandle[hInternet->nb].file=fopen(hInternet->ihandle[hInternet->nb].filename,"w+"); |
---|
408 | |
---|
409 | hInternet->ihandle[hInternet->nb].hasCacheFile=1; |
---|
410 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, NULL); |
---|
411 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, hInternet->ihandle[hInternet->nb].file); |
---|
412 | hInternet->ihandle[hInternet->nb].nDataLen=0; |
---|
413 | break; |
---|
414 | } |
---|
415 | #ifdef ULINET_DEBUG |
---|
416 | fprintf(stderr,"URL (%s)\nBODY (%s)\n",lpszUrl,lpszHeaders); |
---|
417 | #endif |
---|
418 | if(lpszHeaders!=NULL && strlen(lpszHeaders)>0){ |
---|
419 | #ifdef MSG_LAF_VERBOSE |
---|
420 | fprintf(stderr,"FROM ULINET !!"); |
---|
421 | fprintf(stderr,"HEADER : [%s] %d\n",lpszHeaders,dwHeadersLength); |
---|
422 | #endif |
---|
423 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POST,1); |
---|
424 | #ifdef ULINET_DEBUG |
---|
425 | fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength); |
---|
426 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_VERBOSE,1); |
---|
427 | #endif |
---|
428 | hInternet->ihandle[hInternet->nb].post=strdup(lpszHeaders); |
---|
429 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDS,hInternet->ihandle[hInternet->nb].post); |
---|
430 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDSIZE,(long)dwHeadersLength); |
---|
431 | } |
---|
432 | if(hInternet->ihandle[hInternet->nb].header!=NULL) |
---|
433 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_HTTPHEADER,hInternet->ihandle[hInternet->nb].header); |
---|
434 | |
---|
435 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_URL,lpszUrl); |
---|
436 | hInternet->ihandle[hInternet->nb].url = zStrdup(lpszUrl); |
---|
437 | |
---|
438 | curl_multi_add_handle(hInternet->handle,hInternet->ihandle[hInternet->nb].handle); |
---|
439 | |
---|
440 | hInternet->ihandle[hInternet->nb].header=NULL; |
---|
441 | ++hInternet->nb; |
---|
442 | |
---|
443 | #ifdef ULINET_DEBUG |
---|
444 | fprintf(stderr,"DEBUG MIMETYPE: %s\n",hInternet.mimeType); |
---|
445 | fflush(stderr); |
---|
446 | #endif |
---|
447 | return *hInternet; |
---|
448 | }; |
---|
449 | |
---|
450 | /** |
---|
451 | * Download all opened urls in the queue |
---|
452 | * |
---|
453 | * @param hInternet the HINTERNET structure containing the queue |
---|
454 | * @return 0 |
---|
455 | */ |
---|
456 | int processDownloads(HINTERNET* hInternet){ |
---|
457 | int still_running=0; |
---|
458 | int msgs_left=0; |
---|
459 | int i=0; |
---|
460 | do{ |
---|
461 | curl_multi_perform(hInternet->handle, &still_running); |
---|
462 | }while(still_running); |
---|
463 | for(i=0;i<hInternet->nb;i++){ |
---|
464 | char *tmp; |
---|
465 | curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_CONTENT_TYPE,&tmp); |
---|
466 | if(tmp!=NULL) |
---|
467 | hInternet->ihandle[i].mimeType=strdup(tmp); |
---|
468 | curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_RESPONSE_CODE,&hInternet->ihandle[i].code); |
---|
469 | curl_multi_remove_handle(hInternet->handle, hInternet->ihandle[i].handle); |
---|
470 | curl_easy_cleanup(hInternet->ihandle[i].handle); |
---|
471 | } |
---|
472 | return 0; |
---|
473 | } |
---|
474 | |
---|
475 | /** |
---|
476 | * Initialize the CCookie for a specific index (hInternet.nb) |
---|
477 | * |
---|
478 | * @param hInternet the HINTERNET structure to know the CCookie index to reset |
---|
479 | * @return 1 |
---|
480 | * @see HINTERNET |
---|
481 | */ |
---|
482 | int freeCookieList(HINTERNET hInternet){ |
---|
483 | memset(&CCookie[hInternet.nb][0],0,1024); |
---|
484 | #ifndef TIGER |
---|
485 | curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_COOKIELIST, "ALL"); |
---|
486 | #endif |
---|
487 | return 1; |
---|
488 | } |
---|
489 | |
---|
490 | /** |
---|
491 | * Copy a downloaded content |
---|
492 | * |
---|
493 | * @param hInternet the _HINTERNET structure |
---|
494 | * @param lpBuffer the memory space to copy the downloaded content |
---|
495 | * @param dwNumberOfBytesToRead the size of lpBuffer |
---|
496 | * @param lpdwNumberOfBytesRead number of bytes red |
---|
497 | * @return 1 on success, 0 if failure |
---|
498 | */ |
---|
499 | int InternetReadFile(_HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){ |
---|
500 | int dwDataSize; |
---|
501 | |
---|
502 | if(hInternet.hasCacheFile>0){ |
---|
503 | fseek (hInternet.file , 0 , SEEK_END); |
---|
504 | dwDataSize=ftell(hInternet.file); //taille du ficher |
---|
505 | rewind (hInternet.file); |
---|
506 | } |
---|
507 | else{ |
---|
508 | memset(lpBuffer,0,hInternet.nDataLen+1); |
---|
509 | memcpy(lpBuffer, hInternet.pabyData, hInternet.nDataLen ); |
---|
510 | dwDataSize=hInternet.nDataLen; |
---|
511 | free( hInternet.pabyData ); |
---|
512 | hInternet.pabyData=NULL; |
---|
513 | } |
---|
514 | |
---|
515 | if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize ) |
---|
516 | return 0; |
---|
517 | |
---|
518 | #ifdef MSG_LAF_VERBOSE |
---|
519 | printf("\nfile size : %dko\n",dwDataSize/1024); |
---|
520 | #endif |
---|
521 | |
---|
522 | if(hInternet.hasCacheFile>0){ |
---|
523 | *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); |
---|
524 | } |
---|
525 | else{ |
---|
526 | *lpdwNumberOfBytesRead = hInternet.nDataLen; |
---|
527 | free( hInternet.pabyData ); |
---|
528 | hInternet.pabyData = NULL; |
---|
529 | hInternet.nDataAlloc = hInternet.nDataLen = 0; |
---|
530 | } |
---|
531 | |
---|
532 | CCookie[hInternet.id][0]=0; |
---|
533 | |
---|
534 | if( *lpdwNumberOfBytesRead < dwDataSize ) |
---|
535 | return 0; |
---|
536 | else |
---|
537 | return 1; // TRUE |
---|
538 | } |
---|
539 | |
---|
540 | |
---|
541 | /** |
---|
542 | * Use basic authentication for accessing a resource |
---|
543 | * |
---|
544 | * @param hInternet the _HINTERNET structure |
---|
545 | * @param login the login to use to authenticate |
---|
546 | * @param passwd the password to use to authenticate |
---|
547 | */ |
---|
548 | int setBasicAuth(HINTERNET hInternet,char* login,char* passwd){ |
---|
549 | char *tmp; |
---|
550 | tmp=(char*)malloc((strlen(login)+strlen(passwd)+2)*sizeof(char)); |
---|
551 | sprintf(tmp,"%s:%s",login,passwd); |
---|
552 | if(curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle,CURLOPT_USERPWD,tmp)==CURLE_OUT_OF_MEMORY){ |
---|
553 | free(tmp); |
---|
554 | return -1; |
---|
555 | } |
---|
556 | curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_HTTPAUTH,CURLAUTH_ANY); |
---|
557 | free(tmp); |
---|
558 | return 0; |
---|
559 | } |
---|