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

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

Apply updates of the PHP support and fixes in ulinet provided by Knut Landmark. Fix building issue on GNU/Linux by defining semun.

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