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