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

Last change on this file since 465 was 446, checked in by djay, 11 years ago

Detect mimetype of downloaded content.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 9.9 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=(unsigned char*)realloc(psInternet->pabyData,psInternet->nDataLen+realsize+1);
42    psInternet->nDataAlloc+=psInternet->nDataLen+realsize+1;
43  }
44  else{
45    psInternet->pabyData=(unsigned 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 = NULL;
109  CFStringRef key_host = NULL;
110  CFStringRef key_port = NULL;
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(strncmp(proto,"http",4)==0){
123      key_enabled=kSCPropNetProxiesHTTPEnable;
124      key_host=kSCPropNetProxiesHTTPProxy;
125      key_port=kSCPropNetProxiesHTTPPort;
126  }
127  else
128    if(strncmp(proto,"https",5)==0){
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  ret.mimeType = NULL;
171
172  ret.handle=curl_easy_init();
173
174  curl_easy_setopt(ret.handle, CURLOPT_COOKIEFILE, "ALL");
175#ifndef TIGER
176  curl_easy_setopt(ret.handle, CURLOPT_COOKIELIST, "ALL");
177#endif
178  curl_easy_setopt(ret.handle, CURLOPT_USERAGENT, lpszAgent);
179 
180  curl_easy_setopt(ret.handle,CURLOPT_FOLLOWLOCATION,1);
181  curl_easy_setopt(ret.handle,CURLOPT_MAXREDIRS,3);
182 
183  header.memory=NULL;
184  header.size = 0;
185
186  curl_easy_setopt(ret.handle, CURLOPT_HEADERFUNCTION, header_write_data);
187  curl_easy_setopt(ret.handle, CURLOPT_WRITEHEADER, (void *)&header);
188
189#ifdef MSG_LAF_VERBOSE
190  curl_easy_setopt(ret.handle, CURLOPT_VERBOSE, 1);
191#endif
192
193  return ret;
194}
195
196void InternetCloseHandle(HINTERNET handle){
197  if(handle.hasCacheFile>0){
198    fclose(handle.file);
199    unlink(handle.filename);
200    handle.mimeType = NULL;
201  }
202  else{
203    handle.pabyData = NULL;
204    handle.mimeType = NULL;
205    handle.nDataAlloc = handle.nDataLen = 0;
206  }
207  if(handle.handle)
208    curl_easy_cleanup(handle.handle);
209  curl_global_cleanup();
210}
211
212HINTERNET InternetOpenUrl(HINTERNET hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){
213
214  char filename[255];
215  hInternet.nDataLen = 0;
216
217  hInternet.nDataAlloc = 0;
218  hInternet.pabyData= NULL;
219     
220  switch(dwFlags)
221    {
222    case INTERNET_FLAG_NO_CACHE_WRITE:   
223      hInternet.hasCacheFile=-1;
224      curl_easy_setopt(hInternet.handle, CURLOPT_WRITEFUNCTION, write_data_into);
225      curl_easy_setopt(hInternet.handle, CURLOPT_WRITEDATA, &hInternet);
226      break;
227    default:
228      sprintf(filename,"/tmp/ZOO_Cache%d",(int)time(NULL));
229      filename[24]=0;
230      fprintf(stderr,"file=%s",filename);
231#ifdef MSG_LAF_VERBOSE
232      fprintf(stderr,"file=%s",filename);
233#endif
234      hInternet.filename=filename;
235      hInternet.file=fopen(hInternet.filename,"w+");
236   
237      hInternet.hasCacheFile=1;
238      curl_easy_setopt(hInternet.handle, CURLOPT_WRITEFUNCTION, NULL);
239      curl_easy_setopt(hInternet.handle, CURLOPT_WRITEDATA, hInternet.file);
240      hInternet.nDataLen=0;
241      break;
242    }
243#ifdef ULINET_DEBUG
244  fprintf(stderr,"URL (%s)\nBODY (%s)\n",lpszUrl,lpszHeaders);
245#endif
246  if(lpszHeaders!=NULL && strlen(lpszHeaders)>0){
247#ifdef MSG_LAF_VERBOSE
248    fprintf(stderr,"FROM ULINET !!");
249    fprintf(stderr,"HEADER : %s\n",lpszHeaders);
250#endif
251    //curl_easy_setopt(hInternet.handle,CURLOPT_COOKIE,lpszHeaders);
252    curl_easy_setopt(hInternet.handle,CURLOPT_POST,1);
253#ifdef ULINET_DEBUG
254    fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength);
255    curl_easy_setopt(hInternet.handle,CURLOPT_VERBOSE,1);
256#endif
257    curl_easy_setopt(hInternet.handle,CURLOPT_POSTFIELDS,lpszHeaders);
258    //curl_easy_setopt(hInternet.handle,CURLOPT_POSTFIELDSIZE,dwHeadersLength+1);
259    if(hInternet.header!=NULL)
260      curl_easy_setopt(hInternet.handle,CURLOPT_HTTPHEADER,hInternet.header);
261  }
262
263  curl_easy_setopt(hInternet.handle,CURLOPT_URL,lpszUrl);
264  curl_easy_perform(hInternet.handle);
265  curl_easy_getinfo(hInternet.handle,CURLINFO_CONTENT_TYPE,&hInternet.mimeType);
266#ifdef ULINET_DEBUG
267  fprintf(stderr,"DEBUG MIMETYPE: %s\n",hInternet.mimeType);
268#endif
269  return hInternet;
270};
271
272int freeCookieList(HINTERNET hInternet){
273  memset(&CCookie[0],0,1024);
274#ifndef TIGER
275  curl_easy_setopt(hInternet.handle, CURLOPT_COOKIELIST, "ALL");
276#endif
277  return 1;
278}
279
280int InternetReadFile(HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){
281  int dwDataSize;
282
283  if(hInternet.hasCacheFile>0){
284    fseek (hInternet.file , 0 , SEEK_END);
285    dwDataSize=ftell(hInternet.file); //taille du ficher
286    rewind (hInternet.file);
287  }
288  else{
289    memset(lpBuffer,0,hInternet.nDataLen+1);
290    memcpy(lpBuffer, hInternet.pabyData, hInternet.nDataLen );
291    dwDataSize=hInternet.nDataLen;
292    free( hInternet.pabyData );
293    hInternet.pabyData=NULL;
294  }
295
296  if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize )
297    return 0;
298
299#ifdef MSG_LAF_VERBOSE
300  printf("\nfile size : %dko\n",dwDataSize/1024);
301#endif
302
303  if(hInternet.hasCacheFile>0){
304    *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); 
305  }
306  else{
307    *lpdwNumberOfBytesRead = hInternet.nDataLen;
308    free( hInternet.pabyData );
309    hInternet.pabyData = NULL;
310    hInternet.nDataAlloc = hInternet.nDataLen = 0;
311  }
312
313  CCookie[0]=0;
314
315  if( *lpdwNumberOfBytesRead < dwDataSize )
316      return 0;
317  else
318      return 1; // TRUE
319}
320
321bool InternetGetCookie(LPCTSTR lpszUrl,LPCTSTR lpszCookieName,LPTSTR lpszCookieData,LPDWORD lpdwSize){
322
323  bool ret=1; 
324  int count=0;
325  int hasCookie=-1;
326  char TMP[1024];
327  int j;
328  int tmpC=0;
329  lpszUrl=NULL;
330
331  for(j=0;j<strlen(CCookie);j++){
332    if(lpszCookieName[count]==CCookie[j]){
333      hasCookie=1;
334      count++;
335      if(count==strlen(lpszCookieName))
336        break;
337      continue;
338    }
339  }
340
341  if(hasCookie>0){
342    if(CCookie[count]=='='){
343      int i=0;
344      count++;
345      for(i=count;i<strlen(CCookie);i++){
346        if(CCookie[i]!=';'){
347          TMP[tmpC]=CCookie[i];
348          tmpC++;
349        }
350        else{
351          break;
352        }
353      }
354    }
355  }
356  else
357    return -1;
358
359  TMP[tmpC]=0;
360  strncpy(lpszCookieData,TMP,strlen(TMP)+1);
361  lpdwSize=(size_t*) strlen(lpszCookieData);
362
363#ifdef MSG_LAF_VERBOSE
364  printf("Cookie returned : (%s)",(char*)lpszCookieData);
365#endif
366
367  return ret;
368
369}
370
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