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

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

Update to make ZOO-Kernel able to compile and run from Windows Platforms. A special thanks to Espen Messel, Knut Landmark and Benrd Härtwig for providing many patches that I can successfully apply on the SVN source tree and to Farkas for continuing requesting for ZOO-Kernel to run on Windows platforms privately and through the ZOO-Discuss mailing list.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 13.3 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#include "ulinet.h"
30#include <assert.h>
31
32size_t write_data_into(void *buffer, size_t size, size_t nmemb, void *data){
33  size_t realsize = size * nmemb;
34  HINTERNET *psInternet;
35  if(buffer==NULL){
36    buffer=NULL;
37    return -1;
38  }
39  psInternet=(HINTERNET *)data;
40  if(psInternet->pabyData){
41    psInternet->pabyData=(char*)realloc(psInternet->pabyData,psInternet->nDataLen+realsize+1);
42    psInternet->nDataAlloc+=psInternet->nDataLen+realsize+1;
43  }
44  else{
45    psInternet->pabyData=(char*)malloc(psInternet->nDataLen+realsize+1);
46    psInternet->nDataAlloc=realsize+1;
47  }
48
49  if (psInternet->pabyData) {
50    memcpy( psInternet->pabyData + psInternet->nDataLen, buffer, realsize);
51    psInternet->nDataLen += realsize;
52    psInternet->pabyData[psInternet->nDataLen] = 0;
53  }
54
55  buffer=NULL;
56  return realsize;
57}
58
59size_t header_write_data(void *buffer, size_t size, size_t nmemb, void *data){
60  if(strncmp("Set-Cookie: ",buffer,12)==0){
61    int i;
62    char env[1024];
63    char path[1024];
64    char domain[1024];
65        char* tmp;
66    for(i=0;i<12;i++)
67#ifndef WIN32
68      buffer++;
69#else
70        ;
71#endif
72    sscanf(buffer,"%s; path=%s; domain=%s",env,path,domain);
73    tmp=strcat(env,CCookie);
74#ifdef MSG_LAF_OUT
75    printf("\n**Cookie env : [%s] , path : [%s], domain : [%s]**\n",env,path,domain);
76    printf("buffer : %d (%s) (%s) (%s)\n",(buffer==NULL),buffer,tmp,CCookie);
77#endif
78    strcpy(CCookie,tmp);
79  }
80  return size * nmemb;//write_data_into(buffer,size,nmemb,data,HEADER);
81};
82
83
84void setProxy(CURL* handle,char* host,long port){
85}
86
87/**
88 * MACOSX
89 */
90#if defined(macintosh) || (defined(__MACH__) && defined(__APPLE__))
91
92
93char* CFStringToCString(CFStringRef dest,char *buffer){
94  CFStringEncoding encoding = kCFStringEncodingUTF8;
95  Boolean bool2 = CFStringGetCString(dest,buffer,1024,encoding);
96  if(bool2){
97    printf("Loaded into local_buffer");
98    return buffer;
99  }
100  return NULL;
101}
102
103OSStatus setProxiesForProtcol(CURL* handle,const char *proto){
104  OSStatus              err;
105  CFDictionaryRef proxyDict;
106  CFArrayRef            proxies;
107 
108  CFStringRef key_enabled;
109  CFStringRef key_host;
110  CFStringRef key_port;
111 
112  bool proxy_enabled;
113  char *proxy_host;
114  long proxy_port;
115 
116  proxyDict = NULL;
117  proxies = NULL;
118
119  err = noErr;
120  proxyDict = SCDynamicStoreCopyProxies(NULL);
121
122  if(proto=="http"){
123      key_enabled=kSCPropNetProxiesHTTPEnable;
124      key_host=kSCPropNetProxiesHTTPProxy;
125      key_port=kSCPropNetProxiesHTTPPort;
126  }
127  else
128    if(proto=="https"){
129      key_enabled=kSCPropNetProxiesHTTPSEnable;
130      key_host=kSCPropNetProxiesHTTPSProxy;
131      key_port=kSCPropNetProxiesHTTPSPort;
132    }
133
134  CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_enabled),kCFNumberIntType,&proxy_enabled);
135  if(proxy_enabled){
136    CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_port),CFNumberGetType(CFDictionaryGetValue(proxyDict,key_port)),&proxy_port);
137    char buffer[1024];
138    CFStringToCString(CFDictionaryGetValue(proxyDict,key_host),buffer);
139    proxy_host=buffer;
140
141#ifdef MSG_LAF_VERBOSE
142    printf("\n**[PROXY SETTINGS DETECTION %s (%d) %s:%li (%s)]**\n",proto,proxy_enabled,(char*)proxy_host,proxy_port,buffer);
143#endif
144
145    if (proxyDict == NULL) {
146      err = coreFoundationUnknownErr;
147    }
148
149    setProxy(handle,proxy_host,proxy_port);
150  }
151  return err;
152}
153#else
154/**
155 * Linux (Gnome)
156 */
157bool setProxiesForProtcol(CURL* handle,const char *proto){
158#ifdef MSG_LAF_VERBOSE
159  fprintf( stderr, "setProxiesForProtocol (do nothing) ...\n" );
160#endif
161}
162#endif
163
164HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){
165 
166  HINTERNET ret;
167  struct MemoryStruct header;
168  ret.hasCacheFile=0;
169  ret.nDataAlloc = 0;
170
171  ret.handle=curl_easy_init();
172
173  curl_easy_setopt(ret.handle, CURLOPT_COOKIEFILE, "ALL");
174#ifndef TIGER
175  curl_easy_setopt(ret.handle, CURLOPT_COOKIELIST, "ALL");
176#endif
177  curl_easy_setopt(ret.handle, CURLOPT_USERAGENT, lpszAgent);
178 
179  curl_easy_setopt(ret.handle,CURLOPT_FOLLOWLOCATION,1);
180  curl_easy_setopt(ret.handle,CURLOPT_MAXREDIRS,3);
181 
182  header.memory=NULL;
183  header.size = 0;
184
185  curl_easy_setopt(ret.handle, CURLOPT_HEADERFUNCTION, header_write_data);
186  curl_easy_setopt(ret.handle, CURLOPT_WRITEHEADER, (void *)&header);
187
188#ifdef MSG_LAF_VERBOSE
189  curl_easy_setopt(ret.handle, CURLOPT_VERBOSE, 1);
190#endif
191
192  return ret;
193}
194
195static size_t 
196CurlWriteCB(void *buffer, size_t size, size_t nmemb, void *reqInfo){
197  HINTERNET *psInternet = (HINTERNET *) reqInfo;
198
199  memcpy( psInternet->pabyData + psInternet->nDataLen, buffer,  nmemb * size );
200  psInternet->nDataLen += nmemb * size;
201  psInternet->pabyData[psInternet->nDataLen] = 0;
202
203  return nmemb *size;
204}
205
206void InternetCloseHandle(HINTERNET handle){
207  if(handle.hasCacheFile>0){
208    fclose(handle.file);
209    unlink(handle.filename);
210  }
211  else{
212    handle.pabyData = NULL;
213    handle.nDataAlloc = handle.nDataLen = 0;
214  }
215  if(handle.handle)
216    curl_easy_cleanup(handle.handle);
217  curl_global_cleanup();
218}
219
220HINTERNET InternetOpenUrl(HINTERNET hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){
221
222  char filename[255];
223  hInternet.nDataLen = 0;
224
225  hInternet.nDataAlloc = 0;
226  hInternet.pabyData= NULL;
227     
228  switch(dwFlags)
229    {
230    case INTERNET_FLAG_NO_CACHE_WRITE:   
231      hInternet.hasCacheFile=-1;
232      curl_easy_setopt(hInternet.handle, CURLOPT_WRITEFUNCTION, write_data_into);
233      curl_easy_setopt(hInternet.handle, CURLOPT_WRITEDATA, &hInternet);
234      break;
235    default:
236      sprintf(filename,"/tmp/ZOO_Cache%d",(int)time(NULL));
237      filename[24]=0;
238      fprintf(stderr,"file=%s",filename);
239#ifdef MSG_LAF_VERBOSE
240      fprintf(stderr,"file=%s",filename);
241#endif
242      hInternet.filename=filename;
243      hInternet.file=fopen(hInternet.filename,"w+");
244   
245      hInternet.hasCacheFile=1;
246      curl_easy_setopt(hInternet.handle, CURLOPT_WRITEFUNCTION, NULL);
247      curl_easy_setopt(hInternet.handle, CURLOPT_WRITEDATA, hInternet.file);
248      hInternet.nDataLen=0;
249      break;
250    }
251#ifdef ULINET_DEBUG
252  fprintf(stderr,"URL (%s)\nBODY (%s)\n",lpszUrl,lpszHeaders);
253#endif
254  if(lpszHeaders!=NULL && strlen(lpszHeaders)>0){
255#ifdef MSG_LAF_VERBOSE
256    fprintf(stderr,"FROM ULINET !!");
257    fprintf(stderr,"HEADER : %s\n",lpszHeaders);
258#endif
259    //curl_easy_setopt(hInternet.handle,CURLOPT_COOKIE,lpszHeaders);
260    curl_easy_setopt(hInternet.handle,CURLOPT_POST,1);
261#ifdef ULINET_DEBUG
262    fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength);
263    curl_easy_setopt(hInternet.handle,CURLOPT_VERBOSE,1);
264#endif
265    curl_easy_setopt(hInternet.handle,CURLOPT_POSTFIELDS,lpszHeaders);
266    //curl_easy_setopt(hInternet.handle,CURLOPT_POSTFIELDSIZE,dwHeadersLength+1);
267    if(hInternet.header!=NULL)
268      curl_easy_setopt(hInternet.handle,CURLOPT_HTTPHEADER,hInternet.header);
269  }
270
271  curl_easy_setopt(hInternet.handle,CURLOPT_URL,lpszUrl);
272  curl_easy_perform(hInternet.handle);
273
274  return hInternet;
275};
276
277int freeCookieList(HINTERNET hInternet){
278  memset(&CCookie[0],0,1024);
279#ifndef TIGER
280  curl_easy_setopt(hInternet.handle, CURLOPT_COOKIELIST, "ALL");
281#endif
282  return 1;
283}
284
285int InternetReadFile(HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){
286  int dwDataSize;
287
288  if(hInternet.hasCacheFile>0){
289    fseek (hInternet.file , 0 , SEEK_END);
290    dwDataSize=ftell(hInternet.file); //taille du ficher
291    rewind (hInternet.file);
292  }
293  else{
294    memset(lpBuffer,0,hInternet.nDataLen+1);
295    memcpy( lpBuffer, hInternet.pabyData, hInternet.nDataLen );
296    dwDataSize=hInternet.nDataLen;
297    free( hInternet.pabyData );
298    hInternet.pabyData=NULL;
299  }
300
301  if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize )
302    return 0;
303
304#ifdef MSG_LAF_VERBOSE
305  printf("\nfile size : %dko\n",dwDataSize/1024);
306#endif
307
308  if(hInternet.hasCacheFile>0){
309    *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); 
310  }
311  else{
312    *lpdwNumberOfBytesRead = hInternet.nDataLen;
313    free( hInternet.pabyData );
314    hInternet.pabyData = NULL;
315    hInternet.nDataAlloc = hInternet.nDataLen = 0;
316  }
317
318  CCookie[0]=0;
319
320  if( *lpdwNumberOfBytesRead < dwDataSize )
321      return 0;
322  else
323      return 1; // TRUE
324}
325
326bool InternetGetCookie(LPCTSTR lpszUrl,LPCTSTR lpszCookieName,LPTSTR lpszCookieData,LPDWORD lpdwSize){
327
328  bool ret=1; 
329  int count=0;
330  int hasCookie=-1;
331  char TMP[1024];
332  int j;
333  int tmpC=0;
334  lpszUrl=NULL;
335
336  for(j=0;j<strlen(CCookie);j++){
337    if(lpszCookieName[count]==CCookie[j]){
338      hasCookie=1;
339      count++;
340      if(count==strlen(lpszCookieName))
341        break;
342      continue;
343    }
344  }
345
346  if(hasCookie>0){
347    if(CCookie[count]=='='){
348      int i=0;
349      count++;
350      for(i=count;i<strlen(CCookie);i++){
351        if(CCookie[i]!=';'){
352          TMP[tmpC]=CCookie[i];
353          tmpC++;
354        }
355        else{
356          break;
357        }
358      }
359    }
360  }
361  else
362    return -1;
363
364  TMP[tmpC]=0;
365  strncpy(lpszCookieData,TMP,strlen(TMP)+1);
366  lpdwSize=(size_t*) strlen(lpszCookieData);
367
368#ifdef MSG_LAF_VERBOSE
369  printf("Cookie returned : (%s)",(char*)lpszCookieData);
370#endif
371
372  return ret;
373
374}
375
376#ifdef USE_JS
377#define XP_WIN 1
378#include "jsapi.h"
379
380char* JSValToChar(JSContext* context, jsval* arg) {
381  char *c;
382  char *tmp;
383  JSString *jsmsg;
384  size_t len;
385  int i;
386  if(!JSVAL_IS_STRING(*arg)) {
387    return NULL;
388  }
389  jsmsg = JS_ValueToString(context,*arg);
390  len = JS_GetStringLength(jsmsg);
391  tmp = JS_EncodeString(context,jsmsg);
392  c = (char*)malloc((len+1)*sizeof(char));
393  c[len] = '\0';
394#ifdef ULINET_DEBUG
395  fprintf(stderr,"%d \n",len);
396#endif
397  for(i = 0;i < len;i++) {
398    c[i] = tmp[i];
399    c[i+1] = 0;
400  }
401#ifdef ULINET_DEBUG
402  fprintf(stderr,"%s \n",c);
403#endif
404  return c;
405}
406
407HINTERNET setHeader(HINTERNET handle,JSContext *cx,JSObject *header){
408  jsuint length=0;
409  jsint i=0;
410  char *tmp1;
411#ifdef ULINET_DEBUG
412  fprintf(stderr,"setHeader\n");
413#endif
414  if(JS_IsArrayObject(cx,header)){
415#ifdef ULINET_DEBUG
416    fprintf(stderr,"header is an array\n");
417#endif
418    JS_GetArrayLength(cx,header,&length);
419#ifdef ULINET_DEBUG
420    fprintf(stderr,"header is an array of %d elements\n",length);
421#endif
422    handle.header=NULL;
423    for(i=0;i<length;i++){
424      jsval tmp;
425      JS_GetElement(cx,header,i,&tmp);
426      tmp1=JSValToChar(cx,&tmp);
427#ifdef ULINET_DEBUG
428      fprintf(stderr,"Element of array n° %d, value : %s\n",i,tmp1);
429#endif
430      handle.header=curl_slist_append(handle.header, tmp1);
431      free(tmp1);
432    }
433  }
434  else{
435    fprintf(stderr,"not an array !!!!!!!\n");
436  }
437  return handle;
438}
439
440JSBool
441JSRequest(JSContext *cx, uintN argc, jsval *argv1)
442{
443  jsval *argv = JS_ARGV(cx,argv1);
444  HINTERNET hInternet;
445  HINTERNET res;
446  HINTERNET res1;
447  JSObject *header;
448  char *url;
449  char *method;
450  char* tmpValue;
451  size_t dwRead;
452  int i=0;
453  JS_MaybeGC(cx);
454  hInternet=InternetOpen((LPCTSTR)"ZooWPSClient\0",
455                         INTERNET_OPEN_TYPE_PRECONFIG,
456                         NULL,NULL, 0);
457  if(!CHECK_INET_HANDLE(hInternet))
458    return JS_FALSE;
459  if(argc>=2){
460    method=JSValToChar(cx,&argv[0]);
461    url=JSValToChar(cx,&argv[1]);
462  }
463  else{
464    method=strdup("GET");
465    url=JSValToChar(cx,argv);
466  }
467  if(argc==4){
468    char *body;
469    body=JSValToChar(cx,&argv[2]);
470    header=JSVAL_TO_OBJECT(argv[3]);
471#ifdef ULINET_DEBUG
472    fprintf(stderr,"URL (%s) \nBODY (%s)\n",url,body);
473#endif
474    if(JS_IsArrayObject(cx,header))
475      res1=setHeader(hInternet,cx,header);
476#ifdef ULINET_DEBUG
477    fprintf(stderr,"BODY (%s)\n",body);
478#endif
479    res=InternetOpenUrl(res1,url,body,strlen(body),
480                        INTERNET_FLAG_NO_CACHE_WRITE,0);   
481    free(body);
482  }else{
483    if(argc==3){
484      char *body=JSValToChar(cx,&argv[2]);
485      res=InternetOpenUrl(hInternet,url,body,strlen(body),
486                          INTERNET_FLAG_NO_CACHE_WRITE,0);
487      free(body);
488    }
489    res=InternetOpenUrl(hInternet,url,NULL,0,
490                        INTERNET_FLAG_NO_CACHE_WRITE,0);
491  }
492  tmpValue=(char*)malloc((res.nDataLen+1)*sizeof(char));
493  InternetReadFile(res,(LPVOID)tmpValue,res.nDataLen,&dwRead);
494  fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);
495  if(dwRead==0){
496    JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,"Unable to access the file.",strlen("Unable to access the file."))));
497    return JS_TRUE;
498  }
499
500#ifdef ULINET_DEBUG
501  fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);
502#endif
503  JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpValue,strlen(tmpValue))));
504  free(url);
505  if(argc>=2)
506    free(method);
507  if(argc==4 && res.header!=NULL){
508    curl_slist_free_all(res.header);
509  }
510  InternetCloseHandle(hInternet);
511  JS_MaybeGC(cx);
512  return JS_TRUE;
513}
514#endif
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