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

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

Add the capability to publish heatmap or any templated mapfile using the epecific msInclude and msLayer keys for an output. For MapServer? published output, define 4096 as the default maxsize and use pixel width or height for raster files. use the correct MapServer? imagemode depending on GDALGetRasterDataType (MS_IMAGEMODE_BYTE for GDT_Byte, MS_IMAGEMODE_INT16 for GDT_Int16 and MS_IMAGEMODE_FLOAT32 for GDT_Float32). Create a text file (.maps) listing every mapfiles created for a MapServer? published output (or inputs) using saveMapNames function. Fixes in ulinet, use uuid for naming temporary files. Add dialect input to the ogr2ogr service. Use the .maps file for removing a file from the DeleteData? service

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