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

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

HPC support update. Add inputs for create options in Gdal_Dem.

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