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

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

Add initial doxygen comments in some C files, for future documentation generation.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 13.8 KB
Line 
1/*
2 *  ulinet.c
3 *
4 * Author : Gérald FENOY
5 *
6 * Copyright (c) 2008-2015 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
33/**
34 * Write the downloaded content to a _HINTERNET structure
35 *
36 * @param buffer the buffer to read
37 * @param size size of each member
38 * @param nmemb number of element to read
39 * @param data the _HINTERNET structure to write in
40 * @return the size red, -1 if buffer is NULL
41 */
42size_t write_data_into(void *buffer, size_t size, size_t nmemb, void *data){
43  size_t realsize = size * nmemb;
44  _HINTERNET *psInternet;
45  if(buffer==NULL){
46    buffer=NULL;
47    return -1;
48  }
49  psInternet=(_HINTERNET *)data;
50  if(psInternet->pabyData){
51    psInternet->pabyData=(unsigned char*)realloc(psInternet->pabyData,psInternet->nDataLen+realsize+1);
52    psInternet->nDataAlloc+=psInternet->nDataLen+realsize+1;
53  }
54  else{
55    psInternet->pabyData=(unsigned char*)malloc(psInternet->nDataLen+realsize+1);
56    psInternet->nDataAlloc=realsize+1;
57  }
58
59  if (psInternet->pabyData) {
60    memcpy( psInternet->pabyData + psInternet->nDataLen, buffer, realsize);
61    psInternet->nDataLen += realsize;
62    psInternet->pabyData[psInternet->nDataLen] = 0;
63  }
64
65  buffer=NULL;
66  return realsize;
67}
68
69/**
70 * In case of presence of "Set-Cookie" in the headers red, store the cookie
71 * identifier in CCookie
72 *
73 * @param buffer the buffer to read
74 * @param size size of each member
75 * @param nmemb number of element to read
76 * @param data the _HINTERNET structure to write in
77 * @return the size red, -1 if buffer is NULL
78 * @see CCookie
79 */
80size_t header_write_data(void *buffer, size_t size, size_t nmemb, void *data){
81  if(strncmp("Set-Cookie: ",(char*)buffer,12)==0){
82    int i;
83    char env[256];
84    char path[256];
85    char domain[256];
86    char* tmp;
87    for(i=0;i<12;i++)
88#ifndef WIN32
89      buffer++;
90#else
91        ;
92#endif
93    sscanf((char*)buffer,"%s; path=%s; domain=%s",env,path,domain);
94    tmp=strcat(env,CCookie[0]);
95#ifdef MSG_LAF_OUT
96    printf("\n**Cookie env : [%s] , path : [%s], domain : [%s]**\n",env,path,domain);
97    printf("buffer : %d (%s) (%s) (%s)\n",(buffer==NULL),buffer,tmp,CCookie);
98#endif
99    strcpy(CCookie[0],tmp);
100  }
101  return size * nmemb;//write_data_into(buffer,size,nmemb,data,HEADER);
102};
103
104/**
105 * Define the proxy to use for a CURL handler
106 *
107 * @param handle the CURL handler
108 * @param host the proxy host (including http://)
109 * @param port the proxy port
110 */
111void setProxy(CURL* handle,char* host,long port){
112  char* proxyDef=(char*)malloc((strlen(host)+10+2)*sizeof(char));
113  sprintf(proxyDef,"%s:%ld",host,port);
114  curl_easy_setopt(handle,CURLOPT_PROXY,proxyDef);
115  free(proxyDef);
116}
117
118/**
119 * MACOSX
120 */
121#if defined(macintosh) || (defined(__MACH__) && defined(__APPLE__))
122
123
124char* CFStringToCString(CFStringRef dest,char *buffer){
125  CFStringEncoding encoding = kCFStringEncodingUTF8;
126  Boolean bool2 = CFStringGetCString(dest,buffer,1024,encoding);
127  if(bool2){
128    printf("Loaded into local_buffer");
129    return buffer;
130  }
131  return NULL;
132}
133
134OSStatus setProxiesForProtcol(CURL* handle,const char *proto){
135  OSStatus              err;
136  CFDictionaryRef proxyDict;
137  CFArrayRef            proxies;
138 
139  CFStringRef key_enabled = NULL;
140  CFStringRef key_host = NULL;
141  CFStringRef key_port = NULL;
142 
143  bool proxy_enabled;
144  char *proxy_host;
145  long proxy_port;
146 
147  proxyDict = NULL;
148  proxies = NULL;
149
150  err = noErr;
151  proxyDict = SCDynamicStoreCopyProxies(NULL);
152
153  if(strncmp(proto,"http",4)==0){
154      key_enabled=kSCPropNetProxiesHTTPEnable;
155      key_host=kSCPropNetProxiesHTTPProxy;
156      key_port=kSCPropNetProxiesHTTPPort;
157  }
158  else
159    if(strncmp(proto,"https",5)==0){
160      key_enabled=kSCPropNetProxiesHTTPSEnable;
161      key_host=kSCPropNetProxiesHTTPSProxy;
162      key_port=kSCPropNetProxiesHTTPSPort;
163    }
164
165  CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_enabled),kCFNumberIntType,&proxy_enabled);
166  if(proxy_enabled){
167    CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_port),CFNumberGetType(CFDictionaryGetValue(proxyDict,key_port)),&proxy_port);
168    char buffer[1024];
169    CFStringToCString(CFDictionaryGetValue(proxyDict,key_host),buffer);
170    proxy_host=buffer;
171
172#ifdef MSG_LAF_VERBOSE
173    printf("\n**[PROXY SETTINGS DETECTION %s (%d) %s:%li (%s)]**\n",proto,proxy_enabled,(char*)proxy_host,proxy_port,buffer);
174#endif
175
176    if (proxyDict == NULL) {
177      err = coreFoundationUnknownErr;
178    }
179
180    setProxy(handle,proxy_host,proxy_port);
181  }
182  return err;
183}
184#else
185/**
186 * Should autodetect the proxy configuration (do nothing on linux)
187 *
188 * @param handle a CURL handle
189 * @param proto the protocol requiring the use of a proxy
190 */
191bool setProxiesForProtcol(CURL* handle,const char *proto){
192#ifdef MSG_LAF_VERBOSE
193  fprintf( stderr, "setProxiesForProtocol (do nothing) ...\n" );
194#endif
195  return true;
196}
197#endif
198
199/**
200 * Create a HINTERNET
201 *
202 * @param lpszAgent the HTPP User-Agent to use to send requests
203 * @param  dwAccessType type of access required
204 * @param  lpszProxyName the name of the proxy server(s) to use
205 * @param  lpszProxyBypass ip address or host names which should not be routed
206 *  through the proxy
207 * @param  dwFlags Options (INTERNET_FLAG_ASYNC,INTERNET_FLAG_FROM_CACHE,INTERNET_FLAG_OFFLINE)
208 * @return the created HINTERNET
209 */
210HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){
211  HINTERNET ret;
212  ret.handle=curl_multi_init();
213  ret.agent=strdup(lpszAgent);
214  ret.nb=0;
215  ret.ihandle[ret.nb].header=NULL;
216  return ret;
217}
218
219/**
220 * Close a HINTERNET connection and free allocated ressources
221 *
222 * @param handle0 the HINTERNET connection to close
223 */
224void InternetCloseHandle(HINTERNET* handle0){
225  int i=0;
226  for(i=0;i<handle0->nb;i++){
227    _HINTERNET handle=handle0->ihandle[i];
228    if(handle.hasCacheFile>0){
229      fclose(handle.file);
230      unlink(handle.filename);
231    }
232    else{
233      handle.pabyData = NULL;
234      handle.nDataAlloc = handle.nDataLen = 0;
235    }
236    if(handle0->ihandle[i].header!=NULL){
237      curl_slist_free_all(handle0->ihandle[i].header);
238      handle0->ihandle[i].header=NULL;
239    }
240    free(handle.mimeType);
241    handle.mimeType = NULL;
242  }
243  if(handle0->handle)
244    curl_multi_cleanup(handle0->handle);
245  free(handle0->agent);
246  for(i=handle0->nb-1;i>=0;i--){
247    free(handle0->waitingRequests[i]);
248  }
249}
250
251/**
252 * Create a new element in the download queue
253 *
254 * @param hInternet the HINTERNET connection to add the download link
255 * @param lpszUrl the url to download
256 * @param lpszHeaders the additional headers to be sent to the HTTP server
257 * @param dwHeadersLength the size of the additional headers
258 * @param dwFlags desired download mode (INTERNET_FLAG_NO_CACHE_WRITE for not using cache file)
259 * @param dwContext not used
260 */
261HINTERNET InternetOpenUrl(HINTERNET* hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){
262
263  char filename[255];
264  struct MemoryStruct header;
265
266  hInternet->ihandle[hInternet->nb].handle=curl_easy_init( );
267  hInternet->ihandle[hInternet->nb].hasCacheFile=0;
268  hInternet->ihandle[hInternet->nb].nDataAlloc = 0;
269  hInternet->ihandle[hInternet->nb].mimeType = NULL;
270  hInternet->ihandle[hInternet->nb].nDataLen = 0;
271  hInternet->ihandle[hInternet->nb].id = hInternet->nb;
272  hInternet->ihandle[hInternet->nb].nDataAlloc = 0;
273  hInternet->ihandle[hInternet->nb].pabyData = NULL;
274
275  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIEFILE, "ALL");
276#ifndef TIGER
277  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIELIST, "ALL");
278#endif
279  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_USERAGENT, hInternet->agent);
280 
281  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_FOLLOWLOCATION,1);
282  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_MAXREDIRS,3);
283 
284  header.memory=NULL;
285  header.size = 0;
286
287  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_HEADERFUNCTION, header_write_data);
288  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEHEADER, (void *)&header);
289
290#ifdef MSG_LAF_VERBOSE
291  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_VERBOSE, 1);
292#endif
293
294     
295  switch(dwFlags)
296    {
297    case INTERNET_FLAG_NO_CACHE_WRITE:
298      hInternet->ihandle[hInternet->nb].hasCacheFile=-1;
299      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into);
300      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]);
301      break;
302    default:
303      sprintf(hInternet->ihandle[hInternet->nb].filename,"/tmp/ZOO_Cache%d",(int)time(NULL));
304      hInternet->ihandle[hInternet->nb].filename[24]=0;
305#ifdef MSG_LAF_VERBOSE
306      fprintf(stderr,"file=%s",hInternet->ihandle[hInternet->nb].filename);
307#endif
308      hInternet->ihandle[hInternet->nb].filename=filename;
309      hInternet->ihandle[hInternet->nb].file=fopen(hInternet->ihandle[hInternet->nb].filename,"w+");
310   
311      hInternet->ihandle[hInternet->nb].hasCacheFile=1;
312      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, NULL);
313      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, hInternet->ihandle[hInternet->nb].file);
314      hInternet->ihandle[hInternet->nb].nDataLen=0;
315      break;
316    }
317#ifdef ULINET_DEBUG
318  fprintf(stderr,"URL (%s)\nBODY (%s)\n",lpszUrl,lpszHeaders);
319#endif
320  if(lpszHeaders!=NULL && strlen(lpszHeaders)>0){
321#ifdef MSG_LAF_VERBOSE
322    fprintf(stderr,"FROM ULINET !!");
323    fprintf(stderr,"HEADER : %s\n",lpszHeaders);
324#endif
325    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POST,1);
326#ifdef ULINET_DEBUG
327    fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength);
328    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_VERBOSE,1);
329#endif
330    curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDS,lpszHeaders);
331    //curl_easy_setopt(hInternet->handle,CURLOPT_POSTFIELDSIZE,dwHeadersLength+1);
332    if(hInternet->ihandle[hInternet->nb].header!=NULL)
333      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_HTTPHEADER,hInternet->ihandle[hInternet->nb].header);
334  }
335
336  curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_URL,lpszUrl);
337
338  curl_multi_add_handle(hInternet->handle,hInternet->ihandle[hInternet->nb].handle);
339 
340  ++hInternet->nb;
341  hInternet->ihandle[hInternet->nb].header=NULL;
342
343#ifdef ULINET_DEBUG
344  fprintf(stderr,"DEBUG MIMETYPE: %s\n",hInternet.mimeType);
345  fflush(stderr);
346#endif
347  return *hInternet;
348};
349
350/**
351 * Download all opened urls in the queue
352 *
353 * @param hInternet the HINTERNET structure containing the queue
354 * @return 0
355 */
356int processDownloads(HINTERNET* hInternet){
357  int still_running=0;
358  int msgs_left=0;
359  int i=0;
360  do{
361    curl_multi_perform(hInternet->handle, &still_running);
362  }while(still_running); 
363  for(i=0;i<hInternet->nb;i++){
364    char *tmp;
365    curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_CONTENT_TYPE,&tmp);
366    if(tmp!=NULL)
367      hInternet->ihandle[i].mimeType=strdup(tmp);
368    curl_multi_remove_handle(hInternet->handle, hInternet->ihandle[i].handle);
369    curl_easy_cleanup(hInternet->ihandle[i].handle);
370  }
371  return 0;
372}
373
374/**
375 * Initialize the CCookie for a specific index (hInternet.nb)
376 *
377 * @param hInternet the HINTERNET structure to know the CCookie index to reset
378 * @return 1
379 * @see HINTERNET
380 */
381int freeCookieList(HINTERNET hInternet){
382  memset(&CCookie[hInternet.nb][0],0,1024);
383#ifndef TIGER
384  curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_COOKIELIST, "ALL");
385#endif
386  return 1;
387}
388
389/**
390 * Copy a downloaded content
391 *
392 * @param hInternet the _HINTERNET structure
393 * @param lpBuffer the memory space to copy the downloaded content
394 * @param dwNumberOfBytesToRead the size of lpBuffer
395 * @param lpdwNumberOfBytesRead number of bytes red
396 * @return 1 on success, 0 if failure
397 */
398int InternetReadFile(_HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){
399  int dwDataSize;
400
401  if(hInternet.hasCacheFile>0){
402    fseek (hInternet.file , 0 , SEEK_END);
403    dwDataSize=ftell(hInternet.file); //taille du ficher
404    rewind (hInternet.file);
405  }
406  else{
407    memset(lpBuffer,0,hInternet.nDataLen+1);
408    memcpy(lpBuffer, hInternet.pabyData, hInternet.nDataLen );
409    dwDataSize=hInternet.nDataLen;
410    free( hInternet.pabyData );
411    hInternet.pabyData=NULL;
412  }
413
414  if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize )
415    return 0;
416
417#ifdef MSG_LAF_VERBOSE
418  printf("\nfile size : %dko\n",dwDataSize/1024);
419#endif
420
421  if(hInternet.hasCacheFile>0){
422    *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); 
423  }
424  else{
425    *lpdwNumberOfBytesRead = hInternet.nDataLen;
426    free( hInternet.pabyData );
427    hInternet.pabyData = NULL;
428    hInternet.nDataAlloc = hInternet.nDataLen = 0;
429  }
430
431  CCookie[hInternet.id][0]=0;
432
433  if( *lpdwNumberOfBytesRead < dwDataSize )
434      return 0;
435  else
436      return 1; // TRUE
437}
438
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