source: branches/prototype-v0/zoo-project/zoo-kernel/ulinet.c @ 871

Last change on this file since 871 was 871, checked in by djay, 6 years ago

Use curl_multi_wait only in case libcurl minor version number is upper or equal to 28.

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

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png