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