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

Last change on this file since 851 was 851, checked in by djay, 7 years ago

Invoke callback asynchronously. Still the ZOO-Kernel has still to wait for every requests to finish before stoping its execution.

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