source: branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/ulinet.c @ 661

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

Inputs passed by reference downloaded in parallel. Conform behavior for DescribeProcess? when the Identifier was not found.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 11.1 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.agent=strdup(lpszAgent);
169  ret.nb=0;
170  ret.ihandle[ret.nb].header=NULL;
171  return ret;
172}
173
174void InternetCloseHandle(HINTERNET* handle0){
175  int i=0;
176  for(i=0;i<handle0->nb;i++){
177    _HINTERNET handle=handle0->ihandle[i];
178    if(handle.hasCacheFile>0){
179      fclose(handle.file);
180      unlink(handle.filename);
181      handle.mimeType = NULL;
182    }
183    else{
184      handle.pabyData = NULL;
185      handle.mimeType = NULL;
186      handle.nDataAlloc = handle.nDataLen = 0;
187    }
188    if(handle0->ihandle[i].header!=NULL){
189      curl_slist_free_all(handle0->ihandle[i].header);
190      handle0->ihandle[i].header=NULL;
191    }
192    free(handle.mimeType);
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    curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_CONTENT_TYPE,&hInternet->ihandle[i].mimeType);
300    curl_multi_remove_handle(hInternet->handle, hInternet->ihandle[i].handle);
301    curl_easy_cleanup(hInternet->ihandle[i].handle);
302  }
303}
304
305int freeCookieList(HINTERNET hInternet){
306  memset(&CCookie[hInternet.nb][0],0,1024);
307#ifndef TIGER
308  curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_COOKIELIST, "ALL");
309#endif
310  return 1;
311}
312
313int InternetReadFile(_HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){
314  int dwDataSize;
315
316  if(hInternet.hasCacheFile>0){
317    fseek (hInternet.file , 0 , SEEK_END);
318    dwDataSize=ftell(hInternet.file); //taille du ficher
319    rewind (hInternet.file);
320  }
321  else{
322    memset(lpBuffer,0,hInternet.nDataLen+1);
323    memcpy(lpBuffer, hInternet.pabyData, hInternet.nDataLen );
324    dwDataSize=hInternet.nDataLen;
325    free( hInternet.pabyData );
326    hInternet.pabyData=NULL;
327  }
328
329  if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize )
330    return 0;
331
332#ifdef MSG_LAF_VERBOSE
333  printf("\nfile size : %dko\n",dwDataSize/1024);
334#endif
335
336  if(hInternet.hasCacheFile>0){
337    *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); 
338  }
339  else{
340    *lpdwNumberOfBytesRead = hInternet.nDataLen;
341    free( hInternet.pabyData );
342    hInternet.pabyData = NULL;
343    hInternet.nDataAlloc = hInternet.nDataLen = 0;
344  }
345
346  CCookie[hInternet.id][0]=0;
347
348  if( *lpdwNumberOfBytesRead < dwDataSize )
349      return 0;
350  else
351      return 1; // TRUE
352}
353
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