source: branches/branch-1.4/zoo-project/zoo-kernel/ulinet.c @ 968

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

Fix yaml service naming. Add javax javaxx optional sections in the main.cfg #105.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 11.2 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: ",(char*)buffer,12)==0){
62    int i;
63    char env[256];
64    char path[256];
65    char domain[256];
66    char* tmp;
67    for(i=0;i<12;i++)
68#ifndef WIN32
69      buffer++;
70#else
71        ;
72#endif
73    sscanf((char*)buffer,"%s; path=%s; domain=%s",env,path,domain);
74    tmp=strcat(env,CCookie[0]);
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[0],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  return true;
163}
164#endif
165
166HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){
167  HINTERNET ret;
168  ret.handle=curl_multi_init();
169  ret.agent=strdup(lpszAgent);
170  ret.nb=0;
171  ret.ihandle[ret.nb].header=NULL;
172  return ret;
173}
174
175void InternetCloseHandle(HINTERNET* handle0){
176  int i=0;
177  for(i=0;i<handle0->nb;i++){
178    _HINTERNET handle=handle0->ihandle[i];
179    if(handle.hasCacheFile>0){
180      fclose(handle.file);
181      unlink(handle.filename);
182    }
183    else{
184      handle.pabyData = NULL;
185      handle.nDataAlloc = handle.nDataLen = 0;
186    }
187    if(handle0->ihandle[i].header!=NULL){
188      curl_slist_free_all(handle0->ihandle[i].header);
189      handle0->ihandle[i].header=NULL;
190    }
191    free(handle.mimeType);
192    handle.mimeType = NULL;
193  }
194  if(handle0->handle)
195    curl_multi_cleanup(handle0->handle);
196  free(handle0->agent);
197  for(i=handle0->nb-1;i>=0;i--){
198    free(handle0->waitingRequests[i]);
199  }
200}
201
202HINTERNET InternetOpenUrl(HINTERNET* hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){
203
204  char filename[255];
205  struct MemoryStruct header;
206
207  hInternet->ihandle[hInternet->nb].handle=curl_easy_init( );
208  hInternet->ihandle[hInternet->nb].hasCacheFile=0;
209  hInternet->ihandle[hInternet->nb].nDataAlloc = 0;
210  hInternet->ihandle[hInternet->nb].mimeType = NULL;
211  hInternet->ihandle[hInternet->nb].nDataLen = 0;
212  hInternet->ihandle[hInternet->nb].id = hInternet->nb;
213  hInternet->ihandle[hInternet->nb].nDataAlloc = 0;
214  hInternet->ihandle[hInternet->nb].pabyData = NULL;
215
216  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIEFILE, "ALL");
217#ifndef TIGER
218  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIELIST, "ALL");
219#endif
220  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_USERAGENT, hInternet->agent);
221 
222  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_FOLLOWLOCATION,1);
223  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_MAXREDIRS,3);
224 
225  header.memory=NULL;
226  header.size = 0;
227
228  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_HEADERFUNCTION, header_write_data);
229  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEHEADER, (void *)&header);
230
231#ifdef MSG_LAF_VERBOSE
232  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_VERBOSE, 1);
233#endif
234
235     
236  switch(dwFlags)
237    {
238    case INTERNET_FLAG_NO_CACHE_WRITE:
239      hInternet->ihandle[hInternet->nb].hasCacheFile=-1;
240      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into);
241      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]);
242      break;
243    default:
244      sprintf(hInternet->ihandle[hInternet->nb].filename,"/tmp/ZOO_Cache%d",(int)time(NULL));
245      hInternet->ihandle[hInternet->nb].filename[24]=0;
246#ifdef MSG_LAF_VERBOSE
247      fprintf(stderr,"file=%s",hInternet->ihandle[hInternet->nb].filename);
248#endif
249      hInternet->ihandle[hInternet->nb].filename=filename;
250      hInternet->ihandle[hInternet->nb].file=fopen(hInternet->ihandle[hInternet->nb].filename,"w+");
251   
252      hInternet->ihandle[hInternet->nb].hasCacheFile=1;
253      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, NULL);
254      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, hInternet->ihandle[hInternet->nb].file);
255      hInternet->ihandle[hInternet->nb].nDataLen=0;
256      break;
257    }
258#ifdef ULINET_DEBUG
259  fprintf(stderr,"URL (%s)\nBODY (%s)\n",lpszUrl,lpszHeaders);
260#endif
261  if(lpszHeaders!=NULL && strlen(lpszHeaders)>0){
262#ifdef MSG_LAF_VERBOSE
263    fprintf(stderr,"FROM ULINET !!");
264    fprintf(stderr,"HEADER : %s\n",lpszHeaders);
265#endif
266    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POST,1);
267#ifdef ULINET_DEBUG
268    fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength);
269    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_VERBOSE,1);
270#endif
271    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDS,lpszHeaders);
272    //curl_easy_setopt(hInternet->handle,CURLOPT_POSTFIELDSIZE,dwHeadersLength+1);
273    if(hInternet->ihandle[hInternet->nb].header!=NULL)
274      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_HTTPHEADER,hInternet->ihandle[hInternet->nb].header);
275  }
276
277  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_URL,lpszUrl);
278
279  curl_multi_add_handle(hInternet->handle,hInternet->ihandle[hInternet->nb].handle);
280 
281  ++hInternet->nb;
282  hInternet->ihandle[hInternet->nb].header=NULL;
283
284#ifdef ULINET_DEBUG
285  fprintf(stderr,"DEBUG MIMETYPE: %s\n",hInternet.mimeType);
286  fflush(stderr);
287#endif
288  return *hInternet;
289};
290
291int processDownloads(HINTERNET* hInternet){
292  int still_running=0;
293  int msgs_left=0;
294  int i=0;
295  do{
296    curl_multi_perform(hInternet->handle, &still_running);
297  }while(still_running); 
298  for(i=0;i<hInternet->nb;i++){
299    char *tmp;
300    curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_CONTENT_TYPE,&tmp);
301    if(tmp!=NULL)
302      hInternet->ihandle[i].mimeType=strdup(tmp);
303    curl_multi_remove_handle(hInternet->handle, hInternet->ihandle[i].handle);
304    curl_easy_cleanup(hInternet->ihandle[i].handle);
305  }
306  return 0;
307}
308
309int freeCookieList(HINTERNET hInternet){
310  memset(&CCookie[hInternet.nb][0],0,1024);
311#ifndef TIGER
312  curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_COOKIELIST, "ALL");
313#endif
314  return 1;
315}
316
317int InternetReadFile(_HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){
318  int dwDataSize;
319
320  if(hInternet.hasCacheFile>0){
321    fseek (hInternet.file , 0 , SEEK_END);
322    dwDataSize=ftell(hInternet.file); //taille du ficher
323    rewind (hInternet.file);
324  }
325  else{
326    memset(lpBuffer,0,hInternet.nDataLen+1);
327    memcpy(lpBuffer, hInternet.pabyData, hInternet.nDataLen );
328    dwDataSize=hInternet.nDataLen;
329    free( hInternet.pabyData );
330    hInternet.pabyData=NULL;
331  }
332
333  if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize )
334    return 0;
335
336#ifdef MSG_LAF_VERBOSE
337  printf("\nfile size : %dko\n",dwDataSize/1024);
338#endif
339
340  if(hInternet.hasCacheFile>0){
341    *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); 
342  }
343  else{
344    *lpdwNumberOfBytesRead = hInternet.nDataLen;
345    free( hInternet.pabyData );
346    hInternet.pabyData = NULL;
347    hInternet.nDataAlloc = hInternet.nDataLen = 0;
348  }
349
350  CCookie[hInternet.id][0]=0;
351
352  if( *lpdwNumberOfBytesRead < dwDataSize )
353      return 0;
354  else
355      return 1; // TRUE
356}
357
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