source: trunk/zoo-project/zoo-kernel/ulinet.c @ 490

Last change on this file since 490 was 490, checked in by djay, 10 years ago

Remove memory leaks from ZOO-Kernel. Fix issue #99.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 10.8 KB
Line 
1/**
2 *  ulinet.c
3 *
4 * Author : Gérald FENOY
5 *
6 * Copyright (c) 2008-2010 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
33size_t write_data_into(void *buffer, size_t size, size_t nmemb, void *data){
34  size_t realsize = size * nmemb;
35  HINTERNET *psInternet;
36  if(buffer==NULL){
37    buffer=NULL;
38    return -1;
39  }
40  psInternet=(HINTERNET *)data;
41  if(psInternet->pabyData){
42    psInternet->pabyData=(unsigned char*)realloc(psInternet->pabyData,psInternet->nDataLen+realsize+1);
43    psInternet->nDataAlloc+=psInternet->nDataLen+realsize+1;
44  }
45  else{
46    psInternet->pabyData=(unsigned char*)malloc(psInternet->nDataLen+realsize+1);
47    psInternet->nDataAlloc=realsize+1;
48  }
49
50  if (psInternet->pabyData) {
51    memcpy( psInternet->pabyData + psInternet->nDataLen, buffer, realsize);
52    psInternet->nDataLen += realsize;
53    psInternet->pabyData[psInternet->nDataLen] = 0;
54  }
55
56  buffer=NULL;
57  return realsize;
58}
59
60size_t header_write_data(void *buffer, size_t size, size_t nmemb, void *data){
61  if(strncmp("Set-Cookie: ",buffer,12)==0){
62    int i;
63    char env[1024];
64    char path[1024];
65    char domain[1024];
66        char* tmp;
67    for(i=0;i<12;i++)
68#ifndef WIN32
69      buffer++;
70#else
71        ;
72#endif
73    sscanf(buffer,"%s; path=%s; domain=%s",env,path,domain);
74    tmp=strcat(env,CCookie);
75#ifdef MSG_LAF_OUT
76    printf("\n**Cookie env : [%s] , path : [%s], domain : [%s]**\n",env,path,domain);
77    printf("buffer : %d (%s) (%s) (%s)\n",(buffer==NULL),buffer,tmp,CCookie);
78#endif
79    strcpy(CCookie,tmp);
80  }
81  return size * nmemb;//write_data_into(buffer,size,nmemb,data,HEADER);
82};
83
84
85void setProxy(CURL* handle,char* host,long port){
86}
87
88/**
89 * MACOSX
90 */
91#if defined(macintosh) || (defined(__MACH__) && defined(__APPLE__))
92
93
94char* CFStringToCString(CFStringRef dest,char *buffer){
95  CFStringEncoding encoding = kCFStringEncodingUTF8;
96  Boolean bool2 = CFStringGetCString(dest,buffer,1024,encoding);
97  if(bool2){
98    printf("Loaded into local_buffer");
99    return buffer;
100  }
101  return NULL;
102}
103
104OSStatus setProxiesForProtcol(CURL* handle,const char *proto){
105  OSStatus              err;
106  CFDictionaryRef proxyDict;
107  CFArrayRef            proxies;
108 
109  CFStringRef key_enabled = NULL;
110  CFStringRef key_host = NULL;
111  CFStringRef key_port = NULL;
112 
113  bool proxy_enabled;
114  char *proxy_host;
115  long proxy_port;
116 
117  proxyDict = NULL;
118  proxies = NULL;
119
120  err = noErr;
121  proxyDict = SCDynamicStoreCopyProxies(NULL);
122
123  if(strncmp(proto,"http",4)==0){
124      key_enabled=kSCPropNetProxiesHTTPEnable;
125      key_host=kSCPropNetProxiesHTTPProxy;
126      key_port=kSCPropNetProxiesHTTPPort;
127  }
128  else
129    if(strncmp(proto,"https",5)==0){
130      key_enabled=kSCPropNetProxiesHTTPSEnable;
131      key_host=kSCPropNetProxiesHTTPSProxy;
132      key_port=kSCPropNetProxiesHTTPSPort;
133    }
134
135  CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_enabled),kCFNumberIntType,&proxy_enabled);
136  if(proxy_enabled){
137    CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_port),CFNumberGetType(CFDictionaryGetValue(proxyDict,key_port)),&proxy_port);
138    char buffer[1024];
139    CFStringToCString(CFDictionaryGetValue(proxyDict,key_host),buffer);
140    proxy_host=buffer;
141
142#ifdef MSG_LAF_VERBOSE
143    printf("\n**[PROXY SETTINGS DETECTION %s (%d) %s:%li (%s)]**\n",proto,proxy_enabled,(char*)proxy_host,proxy_port,buffer);
144#endif
145
146    if (proxyDict == NULL) {
147      err = coreFoundationUnknownErr;
148    }
149
150    setProxy(handle,proxy_host,proxy_port);
151  }
152  return err;
153}
154#else
155/**
156 * Linux (Gnome)
157 */
158bool setProxiesForProtcol(CURL* handle,const char *proto){
159#ifdef MSG_LAF_VERBOSE
160  fprintf( stderr, "setProxiesForProtocol (do nothing) ...\n" );
161#endif
162}
163#endif
164
165HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){ 
166  HINTERNET ret;
167  ret.handle=curl_multi_init();
168  ret.ihandle=NULL;
169  ret.hasCacheFile=0;
170  ret.nDataAlloc = 0;
171  ret.mimeType = NULL;
172  ret.agent=strdup(lpszAgent);
173  return ret;
174}
175
176void InternetCloseHandle(HINTERNET handle){
177  if(handle.hasCacheFile>0){
178    fclose(handle.file);
179    unlink(handle.filename);
180    handle.mimeType = NULL;
181  }
182  else{
183    handle.pabyData = NULL;
184    handle.mimeType = NULL;
185    handle.nDataAlloc = handle.nDataLen = 0;
186  }
187  if(handle.ihandle!=NULL)
188    curl_easy_cleanup(handle.ihandle);
189  if(handle.handle)
190    curl_multi_cleanup(handle.handle);
191  free(handle.agent);
192}
193
194HINTERNET InternetOpenUrl(HINTERNET hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){
195
196  char filename[255];
197  struct MemoryStruct header;
198
199  hInternet.nDataLen = 0;
200
201  hInternet.nDataAlloc = 0;
202  hInternet.pabyData= NULL;
203
204  if(hInternet.ihandle!=NULL)
205    curl_easy_cleanup(hInternet.ihandle);
206  hInternet.ihandle=curl_easy_init( );
207  curl_easy_setopt(hInternet.ihandle, CURLOPT_COOKIEFILE, "ALL");
208#ifndef TIGER
209  curl_easy_setopt(hInternet.ihandle, CURLOPT_COOKIELIST, "ALL");
210#endif
211  curl_easy_setopt(hInternet.ihandle, CURLOPT_USERAGENT, hInternet.agent);
212 
213  curl_easy_setopt(hInternet.ihandle,CURLOPT_FOLLOWLOCATION,1);
214  curl_easy_setopt(hInternet.ihandle,CURLOPT_MAXREDIRS,3);
215 
216  header.memory=NULL;
217  header.size = 0;
218
219  curl_easy_setopt(hInternet.ihandle, CURLOPT_HEADERFUNCTION, header_write_data);
220  curl_easy_setopt(hInternet.ihandle, CURLOPT_WRITEHEADER, (void *)&header);
221
222#ifdef MSG_LAF_VERBOSE
223  curl_easy_setopt(hInternet.ihandle, CURLOPT_VERBOSE, 1);
224#endif
225
226     
227  switch(dwFlags)
228    {
229    case INTERNET_FLAG_NO_CACHE_WRITE:   
230      hInternet.hasCacheFile=-1;
231      curl_easy_setopt(hInternet.ihandle, CURLOPT_WRITEFUNCTION, write_data_into);
232      curl_easy_setopt(hInternet.ihandle, CURLOPT_WRITEDATA, &hInternet);
233      break;
234    default:
235      sprintf(filename,"/tmp/ZOO_Cache%d",(int)time(NULL));
236      filename[24]=0;
237#ifdef MSG_LAF_VERBOSE
238      fprintf(stderr,"file=%s",filename);
239#endif
240      hInternet.filename=filename;
241      hInternet.file=fopen(hInternet.filename,"w+");
242   
243      hInternet.hasCacheFile=1;
244      curl_easy_setopt(hInternet.ihandle, CURLOPT_WRITEFUNCTION, NULL);
245      curl_easy_setopt(hInternet.ihandle, CURLOPT_WRITEDATA, hInternet.file);
246      hInternet.nDataLen=0;
247      break;
248    }
249#ifdef ULINET_DEBUG
250  fprintf(stderr,"URL (%s)\nBODY (%s)\n",lpszUrl,lpszHeaders);
251#endif
252  if(lpszHeaders!=NULL && strlen(lpszHeaders)>0){
253#ifdef MSG_LAF_VERBOSE
254    fprintf(stderr,"FROM ULINET !!");
255    fprintf(stderr,"HEADER : %s\n",lpszHeaders);
256#endif
257    //curl_easy_setopt(hInternet.handle,CURLOPT_COOKIE,lpszHeaders);
258    curl_easy_setopt(hInternet.ihandle,CURLOPT_POST,1);
259#ifdef ULINET_DEBUG
260    fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength);
261    curl_easy_setopt(hInternet.ihandle,CURLOPT_VERBOSE,1);
262#endif
263    curl_easy_setopt(hInternet.ihandle,CURLOPT_POSTFIELDS,lpszHeaders);
264    //curl_easy_setopt(hInternet.handle,CURLOPT_POSTFIELDSIZE,dwHeadersLength+1);
265    if(hInternet.header!=NULL)
266      curl_easy_setopt(hInternet.ihandle,CURLOPT_HTTPHEADER,hInternet.header);
267  }
268
269  curl_easy_setopt(hInternet.ihandle,CURLOPT_URL,lpszUrl);
270
271  curl_multi_add_handle(hInternet.handle,hInternet.ihandle);
272 
273  int still_running=0;
274  int msgs_left=0;
275  do{
276    curl_multi_perform(hInternet.handle, &still_running);
277  }while(still_running);
278
279  CURLMsg *msg=NULL;
280  while ((msg = curl_multi_info_read(hInternet.handle, &msgs_left))) {
281    if (msg->msg == CURLMSG_DONE) {
282      CURL *eh=NULL;
283      eh = msg->easy_handle;
284      curl_easy_getinfo(eh,CURLINFO_CONTENT_TYPE,&hInternet.mimeType);
285    }
286    else {
287      fprintf(stderr, "error: after curl_multi_info_read(), CURLMsg=%d\n", msg->msg);
288    }
289  }
290  curl_multi_remove_handle(hInternet.handle, hInternet.ihandle);
291
292#ifdef ULINET_DEBUG
293  fprintf(stderr,"DEBUG MIMETYPE: %s\n",hInternet.mimeType);
294  fflush(stderr);
295#endif
296  return hInternet;
297};
298
299int freeCookieList(HINTERNET hInternet){
300  memset(&CCookie[0],0,1024);
301#ifndef TIGER
302  curl_easy_setopt(hInternet.ihandle, CURLOPT_COOKIELIST, "ALL");
303#endif
304  return 1;
305}
306
307int InternetReadFile(HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){
308  int dwDataSize;
309
310  if(hInternet.hasCacheFile>0){
311    fseek (hInternet.file , 0 , SEEK_END);
312    dwDataSize=ftell(hInternet.file); //taille du ficher
313    rewind (hInternet.file);
314  }
315  else{
316    memset(lpBuffer,0,hInternet.nDataLen+1);
317    memcpy(lpBuffer, hInternet.pabyData, hInternet.nDataLen );
318    dwDataSize=hInternet.nDataLen;
319    free( hInternet.pabyData );
320    hInternet.pabyData=NULL;
321  }
322
323  if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize )
324    return 0;
325
326#ifdef MSG_LAF_VERBOSE
327  printf("\nfile size : %dko\n",dwDataSize/1024);
328#endif
329
330  if(hInternet.hasCacheFile>0){
331    *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); 
332  }
333  else{
334    *lpdwNumberOfBytesRead = hInternet.nDataLen;
335    free( hInternet.pabyData );
336    hInternet.pabyData = NULL;
337    hInternet.nDataAlloc = hInternet.nDataLen = 0;
338  }
339
340  CCookie[0]=0;
341
342  if( *lpdwNumberOfBytesRead < dwDataSize )
343      return 0;
344  else
345      return 1; // TRUE
346}
347
348bool InternetGetCookie(LPCTSTR lpszUrl,LPCTSTR lpszCookieName,LPTSTR lpszCookieData,LPDWORD lpdwSize){
349
350  bool ret=1; 
351  int count=0;
352  int hasCookie=-1;
353  char TMP[1024];
354  int j;
355  int tmpC=0;
356  lpszUrl=NULL;
357
358  for(j=0;j<strlen(CCookie);j++){
359    if(lpszCookieName[count]==CCookie[j]){
360      hasCookie=1;
361      count++;
362      if(count==strlen(lpszCookieName))
363        break;
364      continue;
365    }
366  }
367
368  if(hasCookie>0){
369    if(CCookie[count]=='='){
370      int i=0;
371      count++;
372      for(i=count;i<strlen(CCookie);i++){
373        if(CCookie[i]!=';'){
374          TMP[tmpC]=CCookie[i];
375          tmpC++;
376        }
377        else{
378          break;
379        }
380      }
381    }
382  }
383  else
384    return -1;
385
386  TMP[tmpC]=0;
387  strncpy(lpszCookieData,TMP,strlen(TMP)+1);
388  lpdwSize=(size_t*) strlen(lpszCookieData);
389
390#ifdef MSG_LAF_VERBOSE
391  printf("Cookie returned : (%s)",(char*)lpszCookieData);
392#endif
393
394  return ret;
395
396}
397
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