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

Last change on this file since 933 was 933, checked in by knut, 5 years ago

Resolve certain string-related type conflicts, e.g., char* vs. const char* or char[N], that may cause errors in some build environments. Resolve a problem with the include order of fcgi_stdio.h (note: should verify that this harmonizes with resolution of same problem in trunk).

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 22.4 KB
RevLine 
[579]1/*
[1]2 *  ulinet.c
3 *
4 * Author : Gérald FENOY
5 *
[899]6 * Copyright (c) 2008-2019 GeoLabs SARL
[1]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
[490]29#define MAX_WAIT_MSECS 180*1000 /* Wait max. 180 seconds */
[1]30#include "ulinet.h"
[862]31#include "server_internal.h"
[1]32#include <assert.h>
[817]33#include <ctype.h>
[579]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 */
[1]44size_t write_data_into(void *buffer, size_t size, size_t nmemb, void *data){
45  size_t realsize = size * nmemb;
[492]46  _HINTERNET *psInternet;
[93]47  if(buffer==NULL){
[1]48    buffer=NULL;
49    return -1;
50  }
[492]51  psInternet=(_HINTERNET *)data;
[1]52  if(psInternet->pabyData){
[446]53    psInternet->pabyData=(unsigned char*)realloc(psInternet->pabyData,psInternet->nDataLen+realsize+1);
[1]54    psInternet->nDataAlloc+=psInternet->nDataLen+realsize+1;
55  }
56  else{
[446]57    psInternet->pabyData=(unsigned char*)malloc(psInternet->nDataLen+realsize+1);
[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
[579]71/**
[863]72 * Write the downloaded content in the file pouted by the _HINTERNET structure
[862]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);
[863]91   fflush(psInternet->file);
92   psInternet->nDataLen += realsize;
93
[862]94   buffer=NULL;
95   return realsize;
96}
97
98
99/**
[579]100 * In case of presence of "Set-Cookie" in the headers red, store the cookie
[839]101 * identifier in cookie
[579]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
[839]108 * @see cookie
[579]109 */
[1]110size_t header_write_data(void *buffer, size_t size, size_t nmemb, void *data){
[509]111  if(strncmp("Set-Cookie: ",(char*)buffer,12)==0){
[1]112    int i;
[492]113    char* tmp;
[1]114    for(i=0;i<12;i++)
115#ifndef WIN32
116      buffer++;
117#else
118        ;
119#endif
[896]120    tmp=strtok((char*) buffer,";"); // knut: added cast to char*
[839]121    int cnt=0;
[797]122    _HINTERNET *psInternet=(_HINTERNET *)data;
[839]123    if(tmp!=NULL && psInternet!=NULL){
124      psInternet->cookie=(char*)malloc(sizeof(char)*(strlen(tmp)+1));
125      sprintf(psInternet->cookie,"%s",tmp);
126    }
[1]127  }
128  return size * nmemb;//write_data_into(buffer,size,nmemb,data,HEADER);
129};
130
[579]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 */
[1]138void setProxy(CURL* handle,char* host,long port){
[579]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);
[1]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 
[446]166  CFStringRef key_enabled = NULL;
167  CFStringRef key_host = NULL;
168  CFStringRef key_port = NULL;
[1]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
[446]180  if(strncmp(proto,"http",4)==0){
[1]181      key_enabled=kSCPropNetProxiesHTTPEnable;
182      key_host=kSCPropNetProxiesHTTPProxy;
183      key_port=kSCPropNetProxiesHTTPPort;
184  }
185  else
[446]186    if(strncmp(proto,"https",5)==0){
[1]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/**
[579]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
[1]217 */
[820]218int setProxiesForProtcol(CURL* handle,const char *proto){
[1]219#ifdef MSG_LAF_VERBOSE
220  fprintf( stderr, "setProxiesForProtocol (do nothing) ...\n" );
221#endif
[820]222  return 0;
[1]223}
224#endif
225
[579]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 */
[492]237HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){
[1]238  HINTERNET ret;
[490]239  ret.handle=curl_multi_init();
[890]240  ret.agent=zStrdup(lpszAgent);
[492]241  ret.nb=0;
[854]242  ret.waitingRequests[ret.nb] = NULL;
[492]243  ret.ihandle[ret.nb].header=NULL;
[854]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;
[1]254  return ret;
255}
256
[579]257/**
[854]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");
[933]271  //char *res="OTHER";
272  static char OTHER[] = "OTHER"; 
273  static char SHARED[] = "SHARED";
274  static char LOCAL[] = "LOCAL";
275  const char *paths[2]={ 
[890]276    "mapserverAddress",
277    "tmpUrl"
278  };
[854]279  if(sharedCache!=NULL){
280    char *hosts=sharedCache->value;
281    char *curs=strtok(hosts,",");
282    while(curs!=NULL){
[890]283      if(strstr(url,curs)==NULL){
[933]284        return SHARED;
[854]285      }
286      curs=strtok(NULL,",");
287    }
288  }
[890]289  for(int i=0;i<2;i++){
290    sharedCache=getMapFromMaps(conf,"main",paths[i]);
291    if(sharedCache!=NULL){
292      if(strstr(url,sharedCache->value)!=NULL){
[933]293        return LOCAL;
[890]294      }
295    }
296  }
[933]297  //return res;
298  return OTHER;
[854]299}
300
301/**
[817]302 * Add missing headers to an existing _HINTERNET
303 *
304 *
305 * @param handle the _HINTERNET pointer
306 * @param key the header parameter name
307 * @param value the header parameter value
308 * @return 0 if the operation succeeded, -1 in other case.
309 */
310int AddMissingHeaderEntry(_HINTERNET* handle,const char* key,const char* value){
311  int length=strlen(key)+strlen(value)+3;
312  char *entry=(char*)malloc((length)*sizeof(char));
313  if(entry==NULL)
314    return -1;
315  snprintf (entry, length, "%s: %s", key, value);
316  handle->header = curl_slist_append (handle->header, entry);
317  free(entry);
318  return 0;
319}
320
321/**
322 * Verify if a host is protected (appear in [security] > hosts)
323 *
324 * @param protectedHosts string containing all the protected hosts (coma separated)
325 * @param url string used to extract the host from
326 * @return 1 if the host is listed as protected, 0 in other case
327 */
[896]328int isProtectedHost(const char* protectedHosts, const char* url) {
329        char *token, *saveptr;
330        int cnt;
331        char* host;
332
333        // knut: make a copy of url since strtok family modifies first argument and cannot be used on constant strings 
334        char* urlcpy = (char*)malloc(sizeof(char)*(strlen(url) + 1));
335        urlcpy = strncpy(urlcpy, url, strlen(url) + 1); // since count > strlen(url), a null character is properly appended
336
337        //token = strtok_r (url, "//", &saveptr);
338        token = strtok_r(urlcpy, "//", &saveptr);   // knut
339        cnt = 0;
340        while (token != NULL && cnt <= 1) {
341                fprintf(stderr, "%s %d %s \n", __FILE__, __LINE__, token);
342                if (cnt == 1)
343                        fprintf(stderr, "%s %d %s \n", __FILE__, __LINE__, strstr(protectedHosts, token));
344                fflush(stderr);
345                if (cnt == 1 && strstr(protectedHosts, token) != NULL) {
346                        fprintf(stderr, "%s %d %s \n", __FILE__, __LINE__, strstr(protectedHosts, token));
347                        free(urlcpy);
348                        return 1;
349                }
350                token = strtok_r(NULL, "/", &saveptr);
351                cnt += 1;
352        }
353        free(urlcpy);
354        return 0;
[817]355}
356
357/**
358 * Add headers defined in [security] > attributes to an existing HINTERNET
359 * @see isProtectedHost, AddMissingHeaderEntry
360 *
361 * @param handle the _HINTERNET pointer
362 * @param conf the header parameter name
363 * @param value the header parameter value
364 * @return 0 if the operation succeeded, -1 in other case.
365 */
366void AddHeaderEntries(HINTERNET* handle,maps* conf){
367  map* passThrough=getMapFromMaps(conf,"security","attributes");
368  map* targetHosts=getMapFromMaps(conf,"security","hosts");
369  char* passedHeader[10];
370  int cnt=0;
371  if(passThrough!=NULL && targetHosts!=NULL){
372    char *tmp=zStrdup(passThrough->value);
[820]373    int i;
374    for(i=0;i<handle->nb;i++){
[854]375      if(strstr(targetHosts->value,"*")!=NULL || isProtectedHost(targetHosts->value,handle->ihandle[i].url)==1){
[860]376        char *token, *saveptr;
377        token = strtok_r (tmp, ",", &saveptr);
[817]378        while (token != NULL){
379          int length=strlen(token)+6;
380          char* tmp1=(char*)malloc(length*sizeof(char));
[850]381          map* tmpMap;
[817]382          snprintf(tmp1,6,"HTTP_");
[820]383          int j;
384          for(j=0;token[j]!='\0';j++){
385            if(token[j]!='-')
[846]386              tmp1[5+j]=toupper(token[j]);
[817]387            else
[820]388              tmp1[5+j]='_';
389            tmp1[5+j+1]='\0';
[817]390          }
[854]391          tmpMap = getMapFromMaps(conf,"renv",tmp1);
392          if(tmpMap!=NULL){
393            AddMissingHeaderEntry(&handle->ihandle[i],token,tmpMap->value);
[850]394          }
[817]395          if(handle->ihandle[i].header!=NULL)
396            curl_easy_setopt(handle->ihandle[i].handle,CURLOPT_HTTPHEADER,handle->ihandle[i].header);
[860]397          free(tmp1);
[817]398          cnt+=1;
399          token = strtok_r (NULL, ",", &saveptr);
400        }
401      }
402    }
403    free(tmp);
404  }
405}
406
407/**
[781]408 * Close a HINTERNET connection and free allocated resources
[579]409 *
410 * @param handle0 the HINTERNET connection to close
411 */
[492]412void InternetCloseHandle(HINTERNET* handle0){
413  int i=0;
[854]414  if(handle0!=NULL){
415    for(i=0;i<handle0->nb;i++){
416      _HINTERNET handle=handle0->ihandle[i];
417      if(handle.hasCacheFile>0){
418        fclose(handle.file);
419        unlink(handle.filename);
[862]420        free(handle.filename);
[854]421      }
422      else{
423        handle.pabyData = NULL;
424        handle.nDataAlloc = handle.nDataLen = 0;
425      }
426      if(handle.header!=NULL){
427        curl_slist_free_all(handle.header);
428        handle.header=NULL;
429      }
430      if(handle.post!=NULL){
431        free(handle.post);
432        handle.post=NULL;
433      }
434      if(handle.url!=NULL){
435        free(handle.url);
436        handle.url=NULL;
437      }
438      if(handle.mimeType!=NULL){
439        free(handle.mimeType);
440        handle.mimeType=NULL;
441      }
442      if(handle.cookie!=NULL){
443        free(handle.cookie);
444        handle.cookie=NULL;
445      }
446      if(handle0->waitingRequests[i]!=NULL){
447        free(handle0->waitingRequests[i]);
448        handle0->waitingRequests[i]=NULL;
449      }
[492]450    }
[854]451    if(handle0->handle)
452      curl_multi_cleanup(handle0->handle);
453    if(handle0->agent!=NULL){
454      free(handle0->agent);
455      handle0->agent=NULL;
[492]456    }
[1]457  }
458}
459
[579]460/**
461 * Create a new element in the download queue
462 *
463 * @param hInternet the HINTERNET connection to add the download link
464 * @param lpszUrl the url to download
465 * @param lpszHeaders the additional headers to be sent to the HTTP server
466 * @param dwHeadersLength the size of the additional headers
467 * @param dwFlags desired download mode (INTERNET_FLAG_NO_CACHE_WRITE for not using cache file)
468 * @param dwContext not used
[863]469 * @param conf the main configuration file maps pointer
[862]470 * @return the updated HINTERNET
[579]471 */
[863]472HINTERNET InternetOpenUrl(HINTERNET* hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext,const maps* conf){
[1]473
474  char filename[255];
[863]475  int ldwFlags=INTERNET_FLAG_NEED_FILE;
[490]476  struct MemoryStruct header;
[896]477  map* memUse=getMapFromMaps((maps*) conf,"main","memory"); // knut: addad cast to maps*
[490]478
[492]479  hInternet->ihandle[hInternet->nb].handle=curl_easy_init( );
480  hInternet->ihandle[hInternet->nb].hasCacheFile=0;
481  hInternet->ihandle[hInternet->nb].nDataAlloc = 0;
[817]482  hInternet->ihandle[hInternet->nb].url = NULL;
[492]483  hInternet->ihandle[hInternet->nb].mimeType = NULL;
[839]484  hInternet->ihandle[hInternet->nb].cookie = NULL;
[492]485  hInternet->ihandle[hInternet->nb].nDataLen = 0;
486  hInternet->ihandle[hInternet->nb].id = hInternet->nb;
487  hInternet->ihandle[hInternet->nb].nDataAlloc = 0;
488  hInternet->ihandle[hInternet->nb].pabyData = NULL;
[621]489  hInternet->ihandle[hInternet->nb].post = NULL;
[839]490 
[492]491  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIEFILE, "ALL");
[490]492#ifndef TIGER
[492]493  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIELIST, "ALL");
[490]494#endif
[492]495  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_USERAGENT, hInternet->agent);
[490]496 
[492]497  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_FOLLOWLOCATION,1);
498  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_MAXREDIRS,3);
[490]499 
500  header.memory=NULL;
501  header.size = 0;
502
[492]503  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_HEADERFUNCTION, header_write_data);
504  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEHEADER, (void *)&header);
[490]505
506#ifdef MSG_LAF_VERBOSE
[492]507  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_VERBOSE, 1);
[490]508#endif
[863]509
[897]510  if(memUse!=NULL && strcasecmp(memUse->value,"load")==0)
[863]511    ldwFlags=INTERNET_FLAG_NO_CACHE_WRITE;
512 
513  switch(ldwFlags)
[1]514    {
[492]515    case INTERNET_FLAG_NO_CACHE_WRITE:
516      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into);
517      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]);
[862]518      hInternet->ihandle[hInternet->nb].hasCacheFile=-1;
[1]519      break;
520    default:
[862]521      memset(filename,0,255);
522      char* tmpUuid=get_uuid();
[863]523      map* tmpPath=NULL;
524      if(conf!=NULL){
[896]525        tmpPath=getMapFromMaps((maps*) conf,"main","tmpPath"); // knut added cast to maps*
[863]526      }
527      if(tmpPath==NULL)
528        sprintf(filename,"/tmp/ZOO_Cache%s", tmpUuid);
529      else
[890]530        sprintf(filename,"%s/ZOO_Cache%s", tmpPath->value,tmpUuid);
[862]531      free(tmpUuid);
[890]532      hInternet->ihandle[hInternet->nb].filename=zStrdup(filename);
[492]533      hInternet->ihandle[hInternet->nb].file=fopen(hInternet->ihandle[hInternet->nb].filename,"w+");
534      hInternet->ihandle[hInternet->nb].hasCacheFile=1;
[862]535      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into_file);
536      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]);
[1]537      break;
538    }
539#ifdef ULINET_DEBUG
540  fprintf(stderr,"URL (%s)\nBODY (%s)\n",lpszUrl,lpszHeaders);
541#endif
542  if(lpszHeaders!=NULL && strlen(lpszHeaders)>0){
543#ifdef MSG_LAF_VERBOSE
544    fprintf(stderr,"FROM ULINET !!");
[621]545    fprintf(stderr,"HEADER : [%s] %d\n",lpszHeaders,dwHeadersLength);
[1]546#endif
[492]547    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POST,1);
[1]548#ifdef ULINET_DEBUG
549    fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength);
[492]550    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_VERBOSE,1);
[1]551#endif
[890]552    hInternet->ihandle[hInternet->nb].post=zStrdup(lpszHeaders);
[621]553    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDS,hInternet->ihandle[hInternet->nb].post);
554    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDSIZE,(long)dwHeadersLength);
[1]555  }
[607]556  if(hInternet->ihandle[hInternet->nb].header!=NULL)
557    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_HTTPHEADER,hInternet->ihandle[hInternet->nb].header);
[1]558
[492]559  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_URL,lpszUrl);
[817]560  hInternet->ihandle[hInternet->nb].url = zStrdup(lpszUrl);
[490]561
[492]562  curl_multi_add_handle(hInternet->handle,hInternet->ihandle[hInternet->nb].handle);
[490]563 
[621]564  hInternet->ihandle[hInternet->nb].header=NULL;
[492]565  ++hInternet->nb;
[854]566  hInternet->ihandle[hInternet->nb].header=NULL;
[490]567
[446]568#ifdef ULINET_DEBUG
569  fprintf(stderr,"DEBUG MIMETYPE: %s\n",hInternet.mimeType);
[478]570  fflush(stderr);
[446]571#endif
[492]572  return *hInternet;
[1]573};
574
[579]575/**
576 * Download all opened urls in the queue
577 *
578 * @param hInternet the HINTERNET structure containing the queue
579 * @return 0
580 */
[492]581int processDownloads(HINTERNET* hInternet){
[862]582  int still_running=0,numfds;
[492]583  int msgs_left=0;
584  int i=0;
585  do{
[862]586    CURLMcode mc;
587    mc = curl_multi_perform(hInternet->handle, &still_running);
588    if(mc==CURLM_OK){
[871]589#if LIBCURL_VERSION_MINOR >= 28
[862]590      mc = curl_multi_wait(hInternet->handle, NULL, 0, 1000, &numfds);
[871]591#else
592      struct timeval timeout;
593      fd_set fdread;
594      fd_set fdwrite;
595      fd_set fdexcep;
596      int maxfd = -1;
597
598      long curl_timeo = -1;
599
600      FD_ZERO(&fdread);
601      FD_ZERO(&fdwrite);
602      FD_ZERO(&fdexcep);
603
604      /* set a suitable timeout to play around with */
605      timeout.tv_sec = 1;
606      timeout.tv_usec = 0;
607
608      curl_multi_timeout(hInternet->handle, &curl_timeo);
609      if(curl_timeo >= 0) {
610        timeout.tv_sec = curl_timeo / 1000;
611        if(timeout.tv_sec > 1)
612          timeout.tv_sec = 1;
613        else
614          timeout.tv_usec = (curl_timeo % 1000) * 1000;
615      }
616
617      /* get file descriptors from the transfers */
618      mc = curl_multi_fdset(hInternet->handle, &fdread, &fdwrite, &fdexcep, &maxfd);
619#endif
[862]620    }
621    if(mc != CURLM_OK) {
622      fprintf(stderr, "curl_multi failed, code %d.n", mc);
623      break;
624    }
[492]625  }while(still_running); 
626  for(i=0;i<hInternet->nb;i++){
[534]627    char *tmp;
628    curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_CONTENT_TYPE,&tmp);
[539]629    if(tmp!=NULL)
[890]630      hInternet->ihandle[i].mimeType=zStrdup(tmp);
[607]631    curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_RESPONSE_CODE,&hInternet->ihandle[i].code);
[492]632    curl_multi_remove_handle(hInternet->handle, hInternet->ihandle[i].handle);
633    curl_easy_cleanup(hInternet->ihandle[i].handle);
634  }
[509]635  return 0;
[492]636}
637
[579]638/**
[839]639 * Initialize the cookie for a specific index (hInternet.nb)
[579]640 *
[839]641 * @param hInternet the HINTERNET structure to know the cookie index to reset
[579]642 * @return 1
643 * @see HINTERNET
644 */
[1]645int freeCookieList(HINTERNET hInternet){
[890]646  hInternet.ihandle[hInternet.nb].cookie=zStrdup("");
[1]647#ifndef TIGER
[492]648  curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_COOKIELIST, "ALL");
[1]649#endif
650  return 1;
651}
652
[579]653/**
654 * Copy a downloaded content
655 *
656 * @param hInternet the _HINTERNET structure
657 * @param lpBuffer the memory space to copy the downloaded content
658 * @param dwNumberOfBytesToRead the size of lpBuffer
659 * @param lpdwNumberOfBytesRead number of bytes red
660 * @return 1 on success, 0 if failure
661 */
[492]662int InternetReadFile(_HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){
[1]663  int dwDataSize;
664
665  if(hInternet.hasCacheFile>0){
666    fseek (hInternet.file , 0 , SEEK_END);
667    dwDataSize=ftell(hInternet.file); //taille du ficher
[862]668    //dwDataSize=hInternet.nDataLen;
669    //rewind (hInternet.file);
670    fseek(hInternet.file, 0, SEEK_SET);
[1]671  }
672  else{
673    memset(lpBuffer,0,hInternet.nDataLen+1);
[375]674    memcpy(lpBuffer, hInternet.pabyData, hInternet.nDataLen );
[1]675    dwDataSize=hInternet.nDataLen;
676    free( hInternet.pabyData );
677    hInternet.pabyData=NULL;
678  }
679
[862]680  if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize ){
[1]681    return 0;
[862]682  }
[1]683
684#ifdef MSG_LAF_VERBOSE
[862]685  fprintf(stderr,"\nfile size : %dko\n",dwDataSize/1024);
[1]686#endif
687
688  if(hInternet.hasCacheFile>0){
[862]689    int freadRes = fread(lpBuffer,dwDataSize+1,1,hInternet.file); 
690    *lpdwNumberOfBytesRead = hInternet.nDataLen;
[1]691  }
692  else{
693    *lpdwNumberOfBytesRead = hInternet.nDataLen;
694    free( hInternet.pabyData );
695    hInternet.pabyData = NULL;
696    hInternet.nDataAlloc = hInternet.nDataLen = 0;
697  }
698
699  if( *lpdwNumberOfBytesRead < dwDataSize )
700      return 0;
701  else
702      return 1; // TRUE
703}
704
[607]705
706/**
[781]707 * Use basic authentication for accessing a resource
[607]708 *
709 * @param hInternet the _HINTERNET structure
710 * @param login the login to use to authenticate
711 * @param passwd the password to use to authenticate
712 */
713int setBasicAuth(HINTERNET hInternet,char* login,char* passwd){
714  char *tmp;
715  tmp=(char*)malloc((strlen(login)+strlen(passwd)+2)*sizeof(char));
716  sprintf(tmp,"%s:%s",login,passwd);
717  if(curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle,CURLOPT_USERPWD,tmp)==CURLE_OUT_OF_MEMORY){
718    free(tmp);
719    return -1;
720  }
721  curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_HTTPAUTH,CURLAUTH_ANY);
722  free(tmp);
723  return 0;
724}
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

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