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

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

Add status_code key to the lenv section to support returning a specific HTTP error code from the service code. Fix callback invocation to support inputs arrays at step 1 and 2. Fix issue with cpu usage. Fix issue with mapserver publication when an input is optional. Fix callback invocation at step 7 in case the service has failed on the HPC side.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 19.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 <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      if(strstr(url,curs)==NULL)
248        res="OTHER";
249      else{
250        res="SHARED";
251        return res;
252      }
253      curs=strtok(NULL,",");
254    }
255    return res;
256  }
257  return "OTHER";
258}
259
260/**
261 * Add missing headers to an existing _HINTERNET
262 *
263 *
264 * @param handle the _HINTERNET pointer
265 * @param key the header parameter name
266 * @param value the header parameter value
267 * @return 0 if the operation succeeded, -1 in other case.
268 */
269int AddMissingHeaderEntry(_HINTERNET* handle,const char* key,const char* value){
270  int length=strlen(key)+strlen(value)+3;
271  char *entry=(char*)malloc((length)*sizeof(char));
272  if(entry==NULL)
273    return -1;
274  snprintf (entry, length, "%s: %s", key, value);
275  handle->header = curl_slist_append (handle->header, entry);
276  free(entry);
277  return 0;
278}
279
280/**
281 * Verify if a host is protected (appear in [security] > hosts)
282 *
283 * @param protectedHosts string containing all the protected hosts (coma separated)
284 * @param url string used to extract the host from
285 * @return 1 if the host is listed as protected, 0 in other case
286 */
287int isProtectedHost(const char* protectedHosts,const char* url){
288  char *token, *saveptr;
289  token = strtok_r (url, "//", &saveptr);
290  int cnt=0;
291  char* host;
292  while(token!=NULL && cnt<=1){
293    if(cnt==1 && strstr(protectedHosts,token)!=NULL){
294      return 1;
295    }
296    token = strtok_r (NULL, "/", &saveptr);
297    cnt+=1;
298  }
299  return 0;
300}
301
302/**
303 * Add headers defined in [security] > attributes to an existing HINTERNET
304 * @see isProtectedHost, AddMissingHeaderEntry
305 *
306 * @param handle the _HINTERNET pointer
307 * @param conf the header parameter name
308 * @param value the header parameter value
309 * @return 0 if the operation succeeded, -1 in other case.
310 */
311void AddHeaderEntries(HINTERNET* handle,maps* conf){
312  map* passThrough=getMapFromMaps(conf,"security","attributes");
313  map* targetHosts=getMapFromMaps(conf,"security","hosts");
314  char* passedHeader[10];
315  int cnt=0;
316  if(passThrough!=NULL && targetHosts!=NULL){
317    char *tmp=zStrdup(passThrough->value);
318    int i;
319    for(i=0;i<handle->nb;i++){
320      if(strstr(targetHosts->value,"*")!=NULL || isProtectedHost(targetHosts->value,handle->ihandle[i].url)==1){
321        char *token, *saveptr;
322        token = strtok_r (tmp, ",", &saveptr);
323        while (token != NULL){
324          int length=strlen(token)+6;
325          char* tmp1=(char*)malloc(length*sizeof(char));
326          map* tmpMap;
327          snprintf(tmp1,6,"HTTP_");
328          int j;
329          for(j=0;token[j]!='\0';j++){
330            if(token[j]!='-')
331              tmp1[5+j]=toupper(token[j]);
332            else
333              tmp1[5+j]='_';
334            tmp1[5+j+1]='\0';
335          }
336          tmpMap = getMapFromMaps(conf,"renv",tmp1);
337          if(tmpMap!=NULL){
338            AddMissingHeaderEntry(&handle->ihandle[i],token,tmpMap->value);
339          }
340          if(handle->ihandle[i].header!=NULL)
341            curl_easy_setopt(handle->ihandle[i].handle,CURLOPT_HTTPHEADER,handle->ihandle[i].header);
342          free(tmp1);
343          cnt+=1;
344          token = strtok_r (NULL, ",", &saveptr);
345        }
346      }
347    }
348    free(tmp);
349  }
350}
351
352/**
353 * Close a HINTERNET connection and free allocated resources
354 *
355 * @param handle0 the HINTERNET connection to close
356 */
357void InternetCloseHandle(HINTERNET* handle0){
358  int i=0;
359  if(handle0!=NULL){
360    for(i=0;i<handle0->nb;i++){
361      _HINTERNET handle=handle0->ihandle[i];
362      if(handle.hasCacheFile>0){
363        fclose(handle.file);
364        unlink(handle.filename);
365      }
366      else{
367        handle.pabyData = NULL;
368        handle.nDataAlloc = handle.nDataLen = 0;
369      }
370      if(handle.header!=NULL){
371        curl_slist_free_all(handle.header);
372        handle.header=NULL;
373      }
374      if(handle.post!=NULL){
375        free(handle.post);
376        handle.post=NULL;
377      }
378      if(handle.url!=NULL){
379        free(handle.url);
380        handle.url=NULL;
381      }
382      if(handle.mimeType!=NULL){
383        free(handle.mimeType);
384        handle.mimeType=NULL;
385      }
386      if(handle.cookie!=NULL){
387        free(handle.cookie);
388        handle.cookie=NULL;
389      }
390      if(handle0->waitingRequests[i]!=NULL){
391        free(handle0->waitingRequests[i]);
392        handle0->waitingRequests[i]=NULL;
393      }
394    }
395    if(handle0->handle)
396      curl_multi_cleanup(handle0->handle);
397    if(handle0->agent!=NULL){
398      free(handle0->agent);
399      handle0->agent=NULL;
400    }
401  }
402}
403
404/**
405 * Create a new element in the download queue
406 *
407 * @param hInternet the HINTERNET connection to add the download link
408 * @param lpszUrl the url to download
409 * @param lpszHeaders the additional headers to be sent to the HTTP server
410 * @param dwHeadersLength the size of the additional headers
411 * @param dwFlags desired download mode (INTERNET_FLAG_NO_CACHE_WRITE for not using cache file)
412 * @param dwContext not used
413 */
414HINTERNET InternetOpenUrl(HINTERNET* hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){
415
416  char filename[255];
417  struct MemoryStruct header;
418
419  hInternet->ihandle[hInternet->nb].handle=curl_easy_init( );
420  hInternet->ihandle[hInternet->nb].hasCacheFile=0;
421  hInternet->ihandle[hInternet->nb].nDataAlloc = 0;
422  hInternet->ihandle[hInternet->nb].url = NULL;
423  hInternet->ihandle[hInternet->nb].mimeType = NULL;
424  hInternet->ihandle[hInternet->nb].cookie = NULL;
425  hInternet->ihandle[hInternet->nb].nDataLen = 0;
426  hInternet->ihandle[hInternet->nb].id = hInternet->nb;
427  hInternet->ihandle[hInternet->nb].nDataAlloc = 0;
428  hInternet->ihandle[hInternet->nb].pabyData = NULL;
429  hInternet->ihandle[hInternet->nb].post = NULL;
430 
431  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIEFILE, "ALL");
432#ifndef TIGER
433  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIELIST, "ALL");
434#endif
435  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_USERAGENT, hInternet->agent);
436 
437  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_FOLLOWLOCATION,1);
438  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_MAXREDIRS,3);
439 
440  header.memory=NULL;
441  header.size = 0;
442
443  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_HEADERFUNCTION, header_write_data);
444  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEHEADER, (void *)&header);
445
446#ifdef MSG_LAF_VERBOSE
447  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_VERBOSE, 1);
448#endif
449
450     
451  switch(dwFlags)
452    {
453    case INTERNET_FLAG_NO_CACHE_WRITE:
454      hInternet->ihandle[hInternet->nb].hasCacheFile=-1;
455      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into);
456      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]);
457      break;
458    default:
459      sprintf(hInternet->ihandle[hInternet->nb].filename,"/tmp/ZOO_Cache%d",(int)time(NULL));
460      hInternet->ihandle[hInternet->nb].filename[24]=0;
461#ifdef MSG_LAF_VERBOSE
462      fprintf(stderr,"file=%s",hInternet->ihandle[hInternet->nb].filename);
463#endif
464      hInternet->ihandle[hInternet->nb].filename=filename;
465      hInternet->ihandle[hInternet->nb].file=fopen(hInternet->ihandle[hInternet->nb].filename,"w+");
466   
467      hInternet->ihandle[hInternet->nb].hasCacheFile=1;
468      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, NULL);
469      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, hInternet->ihandle[hInternet->nb].file);
470      hInternet->ihandle[hInternet->nb].nDataLen=0;
471      break;
472    }
473#ifdef ULINET_DEBUG
474  fprintf(stderr,"URL (%s)\nBODY (%s)\n",lpszUrl,lpszHeaders);
475#endif
476  if(lpszHeaders!=NULL && strlen(lpszHeaders)>0){
477#ifdef MSG_LAF_VERBOSE
478    fprintf(stderr,"FROM ULINET !!");
479    fprintf(stderr,"HEADER : [%s] %d\n",lpszHeaders,dwHeadersLength);
480#endif
481    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POST,1);
482#ifdef ULINET_DEBUG
483    fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength);
484    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_VERBOSE,1);
485#endif
486    hInternet->ihandle[hInternet->nb].post=strdup(lpszHeaders);
487    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDS,hInternet->ihandle[hInternet->nb].post);
488    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDSIZE,(long)dwHeadersLength);
489  }
490  if(hInternet->ihandle[hInternet->nb].header!=NULL)
491    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_HTTPHEADER,hInternet->ihandle[hInternet->nb].header);
492
493  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_URL,lpszUrl);
494  hInternet->ihandle[hInternet->nb].url = zStrdup(lpszUrl);
495
496  curl_multi_add_handle(hInternet->handle,hInternet->ihandle[hInternet->nb].handle);
497 
498  hInternet->ihandle[hInternet->nb].header=NULL;
499  ++hInternet->nb;
500  hInternet->ihandle[hInternet->nb].header=NULL;
501
502#ifdef ULINET_DEBUG
503  fprintf(stderr,"DEBUG MIMETYPE: %s\n",hInternet.mimeType);
504  fflush(stderr);
505#endif
506  return *hInternet;
507};
508
509/**
510 * Download all opened urls in the queue
511 *
512 * @param hInternet the HINTERNET structure containing the queue
513 * @return 0
514 */
515int processDownloads(HINTERNET* hInternet){
516  int still_running=0;
517  int msgs_left=0;
518  int i=0;
519  do{
520    if(curl_multi_perform(hInternet->handle, &still_running)==CURLM_OK)
521      if(still_running){
522        zSleep(10);
523      }
524  }while(still_running); 
525  for(i=0;i<hInternet->nb;i++){
526    char *tmp;
527    curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_CONTENT_TYPE,&tmp);
528    if(tmp!=NULL)
529      hInternet->ihandle[i].mimeType=strdup(tmp);
530    curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_RESPONSE_CODE,&hInternet->ihandle[i].code);
531    curl_multi_remove_handle(hInternet->handle, hInternet->ihandle[i].handle);
532    curl_easy_cleanup(hInternet->ihandle[i].handle);
533  }
534  return 0;
535}
536
537/**
538 * Initialize the cookie for a specific index (hInternet.nb)
539 *
540 * @param hInternet the HINTERNET structure to know the cookie index to reset
541 * @return 1
542 * @see HINTERNET
543 */
544int freeCookieList(HINTERNET hInternet){
545  hInternet.ihandle[hInternet.nb].cookie=strdup("");
546#ifndef TIGER
547  curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_COOKIELIST, "ALL");
548#endif
549  return 1;
550}
551
552/**
553 * Copy a downloaded content
554 *
555 * @param hInternet the _HINTERNET structure
556 * @param lpBuffer the memory space to copy the downloaded content
557 * @param dwNumberOfBytesToRead the size of lpBuffer
558 * @param lpdwNumberOfBytesRead number of bytes red
559 * @return 1 on success, 0 if failure
560 */
561int InternetReadFile(_HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){
562  int dwDataSize;
563
564  if(hInternet.hasCacheFile>0){
565    fseek (hInternet.file , 0 , SEEK_END);
566    dwDataSize=ftell(hInternet.file); //taille du ficher
567    rewind (hInternet.file);
568  }
569  else{
570    memset(lpBuffer,0,hInternet.nDataLen+1);
571    memcpy(lpBuffer, hInternet.pabyData, hInternet.nDataLen );
572    dwDataSize=hInternet.nDataLen;
573    free( hInternet.pabyData );
574    hInternet.pabyData=NULL;
575  }
576
577  if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize )
578    return 0;
579
580#ifdef MSG_LAF_VERBOSE
581  printf("\nfile size : %dko\n",dwDataSize/1024);
582#endif
583
584  if(hInternet.hasCacheFile>0){
585    *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); 
586  }
587  else{
588    *lpdwNumberOfBytesRead = hInternet.nDataLen;
589    free( hInternet.pabyData );
590    hInternet.pabyData = NULL;
591    hInternet.nDataAlloc = hInternet.nDataLen = 0;
592  }
593
594  if( *lpdwNumberOfBytesRead < dwDataSize )
595      return 0;
596  else
597      return 1; // TRUE
598}
599
600
601/**
602 * Use basic authentication for accessing a resource
603 *
604 * @param hInternet the _HINTERNET structure
605 * @param login the login to use to authenticate
606 * @param passwd the password to use to authenticate
607 */
608int setBasicAuth(HINTERNET hInternet,char* login,char* passwd){
609  char *tmp;
610  tmp=(char*)malloc((strlen(login)+strlen(passwd)+2)*sizeof(char));
611  sprintf(tmp,"%s:%s",login,passwd);
612  if(curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle,CURLOPT_USERPWD,tmp)==CURLE_OUT_OF_MEMORY){
613    free(tmp);
614    return -1;
615  }
616  curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_HTTPAUTH,CURLAUTH_ANY);
617  free(tmp);
618  return 0;
619}
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